Skip to content

Commit

Permalink
Merge pull request #2717 from Starbuck5/rwobject-code
Browse files Browse the repository at this point in the history
Cleanup rwobject.c file object methods
  • Loading branch information
MyreMylar authored Mar 4, 2024
2 parents f45b277 + b7e797d commit ec62643
Showing 1 changed file with 7 additions and 109 deletions.
116 changes: 7 additions & 109 deletions src_c/rwobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,12 @@

#include "doc/pygame_doc.h"

#if defined(_WIN32)
#define PG_LSEEK _lseeki64
#elif defined(__APPLE__)
/* Mac does not implement lseek64 */
#define PG_LSEEK lseek
#elif defined(__EMSCRIPTEN__)
/* emsdk mvp 1.0 does not implement lseek64 */
#define PG_LSEEK lseek
#elif defined(_LARGEFILE64_SOURCE)
/* for glibc system that support LFS */
#define PG_LSEEK lseek64
#else
#define PG_LSEEK lseek
#endif

typedef struct {
PyObject *read;
PyObject *write;
PyObject *seek;
PyObject *tell;
PyObject *close;
PyObject *file;
int fileno;
} pgRWHelper;

/*static const char pg_default_encoding[] = "unicode_escape";*/
Expand Down Expand Up @@ -319,15 +302,11 @@ _pg_rw_size(SDL_RWops *context)
PyObject *tmp = NULL;
Sint64 size;
Sint64 retval = -1;
#ifdef WITH_THREAD
PyGILState_STATE state;
#endif /* WITH_THREAD */

if (!helper->seek || !helper->tell)
return retval;
#ifdef WITH_THREAD
state = PyGILState_Ensure();
#endif /* WITH_THREAD */

PyGILState_STATE state = PyGILState_Ensure();

/* Current file position; need to restore it later.
*/
Expand Down Expand Up @@ -378,38 +357,21 @@ _pg_rw_size(SDL_RWops *context)
*/
Py_XDECREF(pos);
Py_XDECREF(tmp);
#ifdef WITH_THREAD
PyGILState_Release(state);
#endif
return retval;
}

static size_t
_pg_rw_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
{
#ifndef WITH_THREAD
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
PyObject *result;

if (!helper->write)
return -1;

result = PyObject_CallFunction(helper->write, "y#", (const char *)ptr,
(Py_ssize_t)size * num);
if (!result)
return -1;

Py_DECREF(result);
return num;
#else /* WITH_THREAD */
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
PyObject *result;
size_t retval;

PyGILState_STATE state;
if (!helper->write)
return -1;
state = PyGILState_Ensure();

PyGILState_STATE state = PyGILState_Ensure();

result = PyObject_CallFunction(helper->write, "y#", (const char *)ptr,
(Py_ssize_t)size * num);
Expand All @@ -425,7 +387,6 @@ _pg_rw_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
end:
PyGILState_Release(state);
return retval;
#endif /* WITH_THREAD */
}

static int
Expand All @@ -434,10 +395,7 @@ _pg_rw_close(SDL_RWops *context)
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
PyObject *result;
int retval = 0;
#ifdef WITH_THREAD
PyGILState_STATE state;
state = PyGILState_Ensure();
#endif /* WITH_THREAD */
PyGILState_STATE state = PyGILState_Ensure();

if (helper->close) {
result = PyObject_CallFunction(helper->close, NULL);
Expand All @@ -453,12 +411,9 @@ _pg_rw_close(SDL_RWops *context)
Py_XDECREF(helper->write);
Py_XDECREF(helper->read);
Py_XDECREF(helper->close);
Py_XDECREF(helper->file);

PyMem_Free(helper);
#ifdef WITH_THREAD
PyGILState_Release(state);
#endif /* WITH_THREAD */
SDL_FreeRW(context);
return retval;
}
Expand All @@ -477,9 +432,6 @@ pgRWops_FromFileObject(PyObject *obj)
if (helper == NULL) {
return (SDL_RWops *)PyErr_NoMemory();
}
helper->fileno = PyObject_AsFileDescriptor(obj);
if (helper->fileno == -1)
PyErr_Clear();
if (fetch_object_methods(helper, obj)) {
PyMem_Free(helper);
return NULL;
Expand All @@ -491,9 +443,6 @@ pgRWops_FromFileObject(PyObject *obj)
return (SDL_RWops *)PyErr_NoMemory();
}

helper->file = obj;
Py_INCREF(obj);

/* Adding a helper to the hidden data to support file-like object RWops */
rw->hidden.unknown.data1 = (void *)helper;
rw->size = _pg_rw_size;
Expand All @@ -511,17 +460,11 @@ _pg_rw_seek(SDL_RWops *context, Sint64 offset, int whence)
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
PyObject *result;
Sint64 retval;
#ifdef WITH_THREAD
PyGILState_STATE state;

if (helper->fileno != -1) {
return PG_LSEEK(helper->fileno, offset, whence);
}

if (!helper->seek || !helper->tell)
return -1;

state = PyGILState_Ensure();
PyGILState_STATE state = PyGILState_Ensure();

if (!(offset == 0 &&
whence == SEEK_CUR)) /* being seek'd, not just tell'd */
Expand Down Expand Up @@ -553,35 +496,6 @@ _pg_rw_seek(SDL_RWops *context, Sint64 offset, int whence)
PyGILState_Release(state);

return retval;
#else /* ~WITH_THREAD */
if (helper->fileno != -1) {
return PG_LSEEK(helper->fileno, offset, whence);
}

if (!helper->seek || !helper->tell)
return -1;

if (!(offset == 0 && whence == SEEK_CUR)) /*being called only for 'tell'*/
{
result = PyObject_CallFunction(helper->seek, "Li", (long long)offset,
whence);
if (!result)
return -1;
Py_DECREF(result);
}

result = PyObject_CallFunction(helper->tell, NULL);
if (!result)
return -1;

retval = PyLong_AsLongLong(result);
if (retval == -1 && PyErr_Occurred())
PyErr_Clear();

Py_DECREF(result);

return retval;
#endif /* ~WITH_THREAD*/
}

static size_t
Expand All @@ -590,25 +504,11 @@ _pg_rw_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
PyObject *result;
Py_ssize_t retval;
#ifdef WITH_THREAD
PyGILState_STATE state;
#endif /* WITH_THREAD */

if (helper->fileno != -1) {
retval = read(helper->fileno, ptr, (unsigned int)(size * maxnum));
if (retval == -1) {
return -1;
}
retval /= size;
return retval;
}

if (!helper->read)
return -1;

#ifdef WITH_THREAD
state = PyGILState_Ensure();
#endif /* WITH_THREAD */
PyGILState_STATE state = PyGILState_Ensure();
result = PyObject_CallFunction(helper->read, "K",
(unsigned long long)size * maxnum);
if (!result) {
Expand All @@ -633,9 +533,7 @@ _pg_rw_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
Py_DECREF(result);

end:
#ifdef WITH_THREAD
PyGILState_Release(state);
#endif /* WITH_THREAD */

return retval;
}
Expand Down

0 comments on commit ec62643

Please sign in to comment.