From bbed8d293483fa7bd7322f5976641dfe86bf6367 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Dudfield?= <renesd@gmail.com>
Date: Sat, 7 Oct 2023 12:05:45 +0200
Subject: [PATCH 1/2] base: Clean up some error messaging

---
 src_c/base.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src_c/base.c b/src_c/base.c
index 1f12a63451..d32c17bdc5 100644
--- a/src_c/base.c
+++ b/src_c/base.c
@@ -309,11 +309,13 @@ pg_mod_autoquit(const char *modname)
 
     funcobj = PyObject_GetAttrString(module, "_internal_mod_quit");
 
+    if (PyErr_Occurred())
+        PyErr_Clear();
+
     /* If we could not load _internal_mod_quit, load quit function */
     if (!funcobj)
         funcobj = PyObject_GetAttrString(module, "quit");
 
-    /* Silence errors */
     if (PyErr_Occurred())
         PyErr_Clear();
 
@@ -322,7 +324,6 @@ pg_mod_autoquit(const char *modname)
         Py_XDECREF(temp);
     }
 
-    /* Silence errors */
     if (PyErr_Occurred())
         PyErr_Clear();
 

From d8fae59ff0f3a02fe159cc302c891177af97a41f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Dudfield?= <renesd@gmail.com>
Date: Sat, 7 Oct 2023 12:05:58 +0200
Subject: [PATCH 2/2] pixelcopy: Clean up some error messaging

---
 src_c/pixelcopy.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src_c/pixelcopy.c b/src_c/pixelcopy.c
index 923072dde8..1d1eaf3267 100644
--- a/src_c/pixelcopy.c
+++ b/src_c/pixelcopy.c
@@ -767,6 +767,9 @@ surface_to_array(PyObject *self, PyObject *args, PyObject *kwds)
     Uint8 opaque = 255;
     Uint8 clear = 0;
     SDL_Surface *surf;
+    PyObject *type = NULL;
+    PyObject *value = NULL;
+    PyObject *traceback = NULL;
     char *keywords[] = {"array", "surface", "kind", "opaque", "clear", 0};
 
     if (!PyArg_ParseTupleAndKeywords(
@@ -794,8 +797,16 @@ 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)) {
+                if (PyErr_Occurred()) {
+                    PyErr_Fetch(&type, &value, &traceback);
+                    PyErr_Clear();
+                }
+
                 pgBuffer_Release(&pg_view);
                 pgSurface_Unlock(surfobj);
+                if (type) {
+                    PyErr_Restore(type, value, traceback);
+                }
                 return 0;
             }
         }
@@ -876,6 +887,8 @@ map_array(PyObject *self, PyObject *args)
     _pc_pixel_t pixel = {0};
     int pix_bytesize;
     Py_ssize_t i;
+    PyObject *type = NULL;
+    PyObject *value, *traceback;
 
     if (!PyArg_ParseTuple(args, "OOO!", &tar_array, &src_array,
                           &pgSurface_Type, &format_surf)) {
@@ -889,6 +902,11 @@ map_array(PyObject *self, PyObject *args)
     /* Determine array shapes and check validity
      */
     if (pgObject_GetBuffer(tar_array, &tar_pg_view, PyBUF_RECORDS)) {
+        if (PyErr_Occurred()) {
+            PyErr_Fetch(&type, &value, &traceback);
+            PyErr_Clear();
+        }
+
         goto fail;
     }
     is_tar_alloc = 1;
@@ -912,6 +930,10 @@ map_array(PyObject *self, PyObject *args)
         goto fail;
     }
     if (pgObject_GetBuffer(src_array, &src_pg_view, PyBUF_RECORDS_RO)) {
+        if (PyErr_Occurred()) {
+            PyErr_Fetch(&type, &value, &traceback);
+            PyErr_Clear();
+        }
         goto fail;
     }
     is_src_alloc = 1;
@@ -1134,6 +1156,11 @@ map_array(PyObject *self, PyObject *args)
         pgBuffer_Release(&tar_pg_view);
     }
     pgSurface_Unlock(format_surf);
+
+    if (type != NULL) {
+        PyErr_Restore(type, value, traceback);
+    }
+
     return 0;
 }
 
