Skip to content

Commit 873d419

Browse files
committed
Apply fixes for python < 3.12
1 parent 7b6488c commit 873d419

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src_c/include/_pygame.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,3 +738,29 @@ pg_PointList_FromArrayDouble(double const *array, int arr_length)
738738

739739
return sequence;
740740
}
741+
742+
#if PY_MINOR_VERSION >= 13
743+
static PyObject *__cached_exception = NULL;
744+
745+
#define CACHE_EXCEPTION __cached_exception = PyErr_GetRaisedException();
746+
#define RESTORE_EXCEPTION \
747+
{ \
748+
PyErr_SetRaisedException(__cached_exception); \
749+
__cached_exception = NULL; \
750+
}
751+
752+
#else
753+
static PyObject *__cached_type = NULL;
754+
static PyObject *__cached_value = NULL;
755+
static PyObject *__cached_traceback = NULL;
756+
757+
#define CACHE_EXCEPTION \
758+
PyErr_Fetch(&__cached_type, &__cached_value, &__cached_traceback);
759+
#define RESTORE_EXCEPTION \
760+
{ \
761+
PyErr_Restore(__cached_type, __cached_value, __cached_traceback); \
762+
__cached_type = NULL; \
763+
__cached_value = NULL; \
764+
__cached_traceback = NULL; \
765+
}
766+
#endif

src_c/pixelcopy.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -815,15 +815,14 @@ surface_to_array(PyObject *self, PyObject *args, PyObject *kwds)
815815
if (view_p->ndim == 2) {
816816
if (view_kind == VIEWKIND_RGB) {
817817
if (_copy_mapped(view_p, surf)) {
818-
PyObject *currentException = NULL;
819-
if (PyErr_Occurred()) {
820-
currentException = PyErr_GetRaisedException();
818+
int error_occurred = (PyErr_Occurred() != NULL);
819+
if (error_occurred) {
820+
CACHE_EXCEPTION
821821
}
822822
pgBuffer_Release(&pg_view);
823823
pgSurface_Unlock(surfobj);
824-
if (currentException) {
825-
PyErr_SetRaisedException(currentException);
826-
}
824+
if (error_occurred)
825+
RESTORE_EXCEPTION
827826
return 0;
828827
}
829828
}
@@ -1165,10 +1164,10 @@ map_array(PyObject *self, PyObject *args)
11651164
if (is_tar_alloc) {
11661165
pgBuffer_Release(&tar_pg_view);
11671166
}
1168-
PyObject *currentException = PyErr_GetRaisedException();
1167+
CACHE_EXCEPTION
11691168
PyErr_Clear();
11701169
pgSurface_Unlock(format_surf);
1171-
PyErr_SetRaisedException(currentException);
1170+
RESTORE_EXCEPTION
11721171
return 0;
11731172
}
11741173

0 commit comments

Comments
 (0)