[Python-checkins] gh-91632: Fix generic_alias_iterator to be finalized at exit. (GH-91727)

corona10 webhook-mailer at python.org
Wed Apr 20 10:11:09 EDT 2022


https://github.com/python/cpython/commit/f571c26fc1805790cacb73e70cbb0291204d41e7
commit: f571c26fc1805790cacb73e70cbb0291204d41e7
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: corona10 <donghee.na92 at gmail.com>
date: 2022-04-20T23:10:41+09:00
summary:

gh-91632: Fix generic_alias_iterator to be finalized at exit. (GH-91727)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst
M Objects/genericaliasobject.c
M Objects/object.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst b/Misc/NEWS.d/next/Core and Builtins/2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst
new file mode 100644
index 0000000000000..e7837828901fc
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst	
@@ -0,0 +1 @@
+Fix a minor memory leak at exit: release the memory of the :class:`generic_alias_iterator` type. Patch by Dong-hee Na.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index dc585de4729fc..7059c4018303e 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -683,7 +683,9 @@ ga_iter_clear(PyObject *self) {
     return 0;
 }
 
-static PyTypeObject Py_GenericAliasIterType = {
+// gh-91632: _Py_GenericAliasIterType is exported  to be cleared
+// in _PyTypes_FiniTypes.
+PyTypeObject _Py_GenericAliasIterType = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     .tp_name = "generic_alias_iterator",
     .tp_basicsize = sizeof(gaiterobject),
@@ -697,7 +699,7 @@ static PyTypeObject Py_GenericAliasIterType = {
 
 static PyObject *
 ga_iter(PyObject *self) {
-    gaiterobject *gi = PyObject_GC_New(gaiterobject, &Py_GenericAliasIterType);
+    gaiterobject *gi = PyObject_GC_New(gaiterobject, &_Py_GenericAliasIterType);
     if (gi == NULL) {
         return NULL;
     }
diff --git a/Objects/object.c b/Objects/object.c
index 8adb5065c2af4..29880f6003bb1 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1834,6 +1834,7 @@ _PyTypes_InitState(PyInterpreterState *interp)
 #ifdef MS_WINDOWS
 extern PyTypeObject PyHKEY_Type;
 #endif
+extern PyTypeObject _Py_GenericAliasIterType;
 
 static PyTypeObject* static_types[] = {
     // The two most important base types: must be initialized first and
@@ -1923,6 +1924,7 @@ static PyTypeObject* static_types[] = {
     &_PyAsyncGenWrappedValue_Type,
     &_PyContextTokenMissing_Type,
     &_PyCoroWrapper_Type,
+    &_Py_GenericAliasIterType,
     &_PyHamtItems_Type,
     &_PyHamtKeys_Type,
     &_PyHamtValues_Type,



More information about the Python-checkins mailing list