Skip to content

Commit

Permalink
Apply fixes for python < 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
oddbookworm committed Feb 22, 2025
1 parent d3e0bb9 commit 01220b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
26 changes: 26 additions & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,29 @@ pg_PointList_FromArrayDouble(double const *array, int arr_length)

return sequence;
}

#if PY_MINOR_VERSION >= 13
static PyObject *__cached_exception = NULL;

#define CACHE_EXCEPTION __cached_exception = PyErr_GetRaisedException();
#define RESTORE_EXCEPTION \
{ \
PyErr_SetRaisedException(__cached_exception); \
__cached_exception = NULL; \
}

#else
static PyObject *__cached_type = NULL;
static PyObject *__cached_value = NULL;
static PyObject *__cached_traceback = NULL;

#define CACHE_EXCEPTION \
PyErr_Fetch(&__cached_type, &__cached_value, &__cached_traceback);
#define RESTORE_EXCEPTION \
{ \
PyErr_Restore(__cached_type, __cached_value, __cached_traceback); \
__cached_type = NULL; \
__cached_value = NULL; \
__cached_traceback = NULL; \
}
#endif
15 changes: 7 additions & 8 deletions src_c/pixelcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,14 @@ surface_to_array(PyObject *self, PyObject *args, PyObject *kwds)
if (view_p->ndim == 2) {
if (view_kind == VIEWKIND_RGB) {
if (_copy_mapped(view_p, surf)) {
PyObject *currentException = NULL;
if (PyErr_Occurred()) {
currentException = PyErr_GetRaisedException();
int error_occurred = (PyErr_Occurred() != NULL);
if (error_occurred) {
CACHE_EXCEPTION
}
pgBuffer_Release(&pg_view);
pgSurface_Unlock(surfobj);
if (currentException) {
PyErr_SetRaisedException(currentException);
}
if (error_occurred)
RESTORE_EXCEPTION
return 0;
}
}
Expand Down Expand Up @@ -1165,10 +1164,10 @@ map_array(PyObject *self, PyObject *args)
if (is_tar_alloc) {
pgBuffer_Release(&tar_pg_view);
}
PyObject *currentException = PyErr_GetRaisedException();
CACHE_EXCEPTION
PyErr_Clear();
pgSurface_Unlock(format_surf);
PyErr_SetRaisedException(currentException);
RESTORE_EXCEPTION
return 0;
}

Expand Down

0 comments on commit 01220b9

Please sign in to comment.