Skip to content

Commit f2b0160

Browse files
authored
Merge pull request #2606 from pygame-community/ankith26-rwops-free
Fix file-like `close` not called in freetype (Removed `pgRWops_ReleaseObject` and replaced usage with `SDL_RWclose`)
2 parents 4258630 + 272ee38 commit f2b0160

File tree

6 files changed

+2
-57
lines changed

6 files changed

+2
-57
lines changed

docs/reST/c_api/rwobject.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ Header file: src_c/include/pygame.h
4141
Return true if *rw* is a Python file-like object wrapper returned by :c:func:`pgRWops_FromObject`
4242
or :c:func:`pgRWops_FromFileObject`.
4343
44-
.. c:function:: int pgRWops_ReleaseObject(SDL_RWops *context)
45-
46-
Free a SDL_RWops struct. If it is attached to a Python file-like object, decrement its
47-
refcount. Otherwise, close the file handle.
48-
Return 0 on success. On error, raise a Python exception and return a negative value.
49-
5044
.. c:function:: PyObject* pg_EncodeFilePath(PyObject *obj, PyObject *eclass)
5145
5246
Return the file path *obj* as a byte string properly encoded for the OS.

src_c/_freetype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ _ftfont_dealloc(pgFontObject *self)
678678
SDL_RWops *src = _PGFT_GetRWops(self);
679679
_PGFT_UnloadFont(self->freetype, self);
680680
if (src) {
681-
pgRWops_ReleaseObject(src);
681+
SDL_RWclose(src);
682682
}
683683
_PGFT_Quit(self->freetype);
684684

src_c/_pygame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ typedef enum {
481481
#define PYGAMEAPI_DISPLAY_NUMSLOTS 2
482482
#define PYGAMEAPI_SURFACE_NUMSLOTS 4
483483
#define PYGAMEAPI_SURFLOCK_NUMSLOTS 8
484-
#define PYGAMEAPI_RWOBJECT_NUMSLOTS 6
484+
#define PYGAMEAPI_RWOBJECT_NUMSLOTS 5
485485
#define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2
486486
#define PYGAMEAPI_COLOR_NUMSLOTS 5
487487
#define PYGAMEAPI_MATH_NUMSLOTS 2

src_c/include/_pygame.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,6 @@ typedef struct pgEventObject pgEventObject;
423423
#define pgRWops_FromFileObject \
424424
(*(SDL_RWops * (*)(PyObject *)) PYGAMEAPI_GET_SLOT(rwobject, 4))
425425

426-
#define pgRWops_ReleaseObject \
427-
(*(int (*)(SDL_RWops *))PYGAMEAPI_GET_SLOT(rwobject, 5))
428-
429426
#define import_pygame_rwobject() IMPORT_PYGAME_MODULE(rwobject)
430427

431428
#endif

src_c/rwobject.c

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -505,50 +505,6 @@ pgRWops_FromFileObject(PyObject *obj)
505505
return rw;
506506
}
507507

508-
static int
509-
pgRWops_ReleaseObject(SDL_RWops *context)
510-
{
511-
int ret = 0;
512-
if (pgRWops_IsFileObject(context)) {
513-
#ifdef WITH_THREAD
514-
PyGILState_STATE state = PyGILState_Ensure();
515-
#endif /* WITH_THREAD */
516-
517-
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
518-
PyObject *fileobj = helper->file;
519-
/* 5 helper functions */
520-
Py_ssize_t filerefcnt = Py_REFCNT(fileobj) - 1 - 5;
521-
522-
if (filerefcnt) {
523-
Py_XDECREF(helper->seek);
524-
Py_XDECREF(helper->tell);
525-
Py_XDECREF(helper->write);
526-
Py_XDECREF(helper->read);
527-
Py_XDECREF(helper->close);
528-
Py_DECREF(fileobj);
529-
PyMem_Free(helper);
530-
SDL_FreeRW(context);
531-
}
532-
else {
533-
ret = SDL_RWclose(context);
534-
if (ret < 0) {
535-
PyErr_SetString(PyExc_IOError, SDL_GetError());
536-
Py_DECREF(fileobj);
537-
}
538-
}
539-
540-
#ifdef WITH_THREAD
541-
PyGILState_Release(state);
542-
#endif /* WITH_THREAD */
543-
}
544-
else {
545-
ret = SDL_RWclose(context);
546-
if (ret < 0)
547-
PyErr_SetString(PyExc_IOError, SDL_GetError());
548-
}
549-
return ret;
550-
}
551-
552508
static Sint64
553509
_pg_rw_seek(SDL_RWops *context, Sint64 offset, int whence)
554510
{
@@ -894,7 +850,6 @@ MODINIT_DEFINE(rwobject)
894850
c_api[2] = pg_EncodeFilePath;
895851
c_api[3] = pg_EncodeString;
896852
c_api[4] = pgRWops_FromFileObject;
897-
c_api[5] = pgRWops_ReleaseObject;
898853
apiobj = encapsulate_api(c_api, "rwobject");
899854
if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
900855
Py_XDECREF(apiobj);

src_c/static.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ PyInit_pygame_static()
366366
#undef pgRWops_IsFileObject
367367
#undef pgRWops_GetFileExtension
368368
#undef pgRWops_FromFileObject
369-
#undef pgRWops_ReleaseObject
370369
#undef pgRWops_FromObject
371370

372371
#include "rwobject.c"

0 commit comments

Comments
 (0)