From 8dbad0945a2b92731b02e427ceee6ba08aed96ca Mon Sep 17 00:00:00 2001 From: Eclips4 Date: Sat, 13 May 2023 22:54:53 +0300 Subject: [PATCH 1/5] gh-104456: Fix ref leak in test_ctypes --- Modules/_ctypes/_ctypes.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index f6cda45eaeac279..b876233de61dc1c 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5487,6 +5487,14 @@ static void comerror_dealloc(PyObject *self) { PyTypeObject *tp = Py_TYPE(self); + PyBaseExceptionObject *obj = (PyBaseExceptionObject *) self; + + Py_CLEAR(obj->dict); + Py_CLEAR(obj->args); + Py_CLEAR(obj->notes); + Py_CLEAR(obj->traceback); + Py_CLEAR(obj->cause); + Py_CLEAR(obj->context); PyObject_GC_UnTrack(self); tp->tp_free(self); Py_DECREF(tp); From 28ac7ed44275d48fad021e55ff10d44906de4531 Mon Sep 17 00:00:00 2001 From: Eclips4 Date: Sun, 14 May 2023 00:09:43 +0300 Subject: [PATCH 2/5] Add suggestion --- Modules/_ctypes/_ctypes.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index b876233de61dc1c..61df0663dabb791 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5476,10 +5476,24 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds) return 0; } +static int +comerror_clear(PyObject *self) { + PyBaseExceptionObject *obj = (PyBaseExceptionObject *) self; + Py_CLEAR(obj->dict); + Py_CLEAR(obj->args); + Py_CLEAR(obj->notes); + Py_CLEAR(obj->traceback); + Py_CLEAR(obj->cause); + Py_CLEAR(obj->context); + return 0; +} + static int comerror_traverse(PyObject *self, visitproc visit, void *arg) { - Py_VISIT(Py_TYPE(self)); + PyTypeObject *tp = Py_TYPE(self); + Py_VISIT(tp); + tp->tp_clear(self); return 0; } @@ -5487,15 +5501,8 @@ static void comerror_dealloc(PyObject *self) { PyTypeObject *tp = Py_TYPE(self); - PyBaseExceptionObject *obj = (PyBaseExceptionObject *) self; - - Py_CLEAR(obj->dict); - Py_CLEAR(obj->args); - Py_CLEAR(obj->notes); - Py_CLEAR(obj->traceback); - Py_CLEAR(obj->cause); - Py_CLEAR(obj->context); PyObject_GC_UnTrack(self); + tp->tp_clear(self); tp->tp_free(self); Py_DECREF(tp); } @@ -5505,6 +5512,7 @@ static PyType_Slot comerror_slots[] = { {Py_tp_init, comerror_init}, {Py_tp_traverse, comerror_traverse}, {Py_tp_dealloc, comerror_dealloc}, + {Py_tp_clear, comerror_clear}, {0, NULL}, }; From 91e8893c4065cfd47c942685fd82a4bb05122284 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sun, 14 May 2023 01:53:44 +0300 Subject: [PATCH 3/5] Update Modules/_ctypes/_ctypes.c Co-authored-by: Erlend E. Aasland --- Modules/_ctypes/_ctypes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 61df0663dabb791..a501838e34e150d 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5502,7 +5502,7 @@ comerror_dealloc(PyObject *self) { PyTypeObject *tp = Py_TYPE(self); PyObject_GC_UnTrack(self); - tp->tp_clear(self); + (void)comerror_clear(self); tp->tp_free(self); Py_DECREF(tp); } From e3174500a42be7d516a9a4c978550accbf3bb64a Mon Sep 17 00:00:00 2001 From: Eclips4 Date: Sun, 14 May 2023 10:26:25 +0300 Subject: [PATCH 4/5] add suggestions from sunmy2019 --- Modules/_ctypes/_ctypes.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index a501838e34e150d..757696f423e5ffe 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5478,23 +5478,14 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds) static int comerror_clear(PyObject *self) { - PyBaseExceptionObject *obj = (PyBaseExceptionObject *) self; - Py_CLEAR(obj->dict); - Py_CLEAR(obj->args); - Py_CLEAR(obj->notes); - Py_CLEAR(obj->traceback); - Py_CLEAR(obj->cause); - Py_CLEAR(obj->context); - return 0; + return ((PyTypeObject *)PyExc_BaseException)->tp_clear(self); } static int comerror_traverse(PyObject *self, visitproc visit, void *arg) { - PyTypeObject *tp = Py_TYPE(self); - Py_VISIT(tp); - tp->tp_clear(self); - return 0; + Py_VISIT(Py_TYPE(self)); + return ((PyTypeObject *)PyExc_BaseException)->tp_traverse(self, visit, arg); } static void From 019234eb26a8382f0d4ce1b57ca6b517fcdb7538 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 14 May 2023 22:40:59 +0200 Subject: [PATCH 5/5] Update Modules/_ctypes/_ctypes.c --- Modules/_ctypes/_ctypes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 757696f423e5ffe..534ef8c1d6cf8f4 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5477,7 +5477,8 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds) } static int -comerror_clear(PyObject *self) { +comerror_clear(PyObject *self) +{ return ((PyTypeObject *)PyExc_BaseException)->tp_clear(self); }