From cdd6a5879531613d3a693a1b2e1b975b498f98e5 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 20 Apr 2022 14:22:46 +0900 Subject: [PATCH 1/5] Fix Py_GenericAliasIterType to be finalized at exit. --- Include/internal/pycore_genericaliasobject.h | 16 ++++++++++++++++ ...2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst | 1 + Objects/genericaliasobject.c | 4 ++-- Objects/object.c | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 Include/internal/pycore_genericaliasobject.h create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst diff --git a/Include/internal/pycore_genericaliasobject.h b/Include/internal/pycore_genericaliasobject.h new file mode 100644 index 00000000000000..10d4920968c19f --- /dev/null +++ b/Include/internal/pycore_genericaliasobject.h @@ -0,0 +1,16 @@ +#ifndef Py_INTERNAL_GENERICALIASOBJECT_H +#define Py_INTERNAL_GENERICALIASOBJECT_H +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef Py_BUILD_CORE +# error "this header requires Py_BUILD_CORE define" +#endif + +extern PyTypeObject _Py_GenericAliasIterType; + +#ifdef __cplusplus +} +#endif +#endif /* !Py_INTERNAL_GENERICALIASOBJECT_H */ 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 00000000000000..3001fe1c4fec9c --- /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 :class:`generic_alias_iterator` to be finalized at exit. diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index dc585de4729fc9..c68496a8d6f11d 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -683,7 +683,7 @@ ga_iter_clear(PyObject *self) { return 0; } -static PyTypeObject Py_GenericAliasIterType = { +PyTypeObject _Py_GenericAliasIterType = { PyVarObject_HEAD_INIT(&PyType_Type, 0) .tp_name = "generic_alias_iterator", .tp_basicsize = sizeof(gaiterobject), @@ -697,7 +697,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 8adb5065c2af42..1b77a13a0fcf70 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -18,6 +18,7 @@ #include "pycore_unionobject.h" // _PyUnion_Type #include "frameobject.h" // PyFrame_Type #include "pycore_interpreteridobject.h" // _PyInterpreterID_Type +#include "pycore_genericaliasobject.h" // _Py_GenericAliasIterType #ifdef Py_LIMITED_API // Prevent recursive call _Py_IncRef() <=> Py_INCREF() @@ -1923,6 +1924,7 @@ static PyTypeObject* static_types[] = { &_PyAsyncGenWrappedValue_Type, &_PyContextTokenMissing_Type, &_PyCoroWrapper_Type, + &_Py_GenericAliasIterType, &_PyHamtItems_Type, &_PyHamtKeys_Type, &_PyHamtValues_Type, From d60df1317cf4b7694b25873d997cb09bb84eb65b Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 20 Apr 2022 19:27:19 +0900 Subject: [PATCH 2/5] Address Victor's review --- Include/internal/pycore_genericaliasobject.h | 16 ---------------- Objects/object.c | 2 +- 2 files changed, 1 insertion(+), 17 deletions(-) delete mode 100644 Include/internal/pycore_genericaliasobject.h diff --git a/Include/internal/pycore_genericaliasobject.h b/Include/internal/pycore_genericaliasobject.h deleted file mode 100644 index 10d4920968c19f..00000000000000 --- a/Include/internal/pycore_genericaliasobject.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef Py_INTERNAL_GENERICALIASOBJECT_H -#define Py_INTERNAL_GENERICALIASOBJECT_H -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef Py_BUILD_CORE -# error "this header requires Py_BUILD_CORE define" -#endif - -extern PyTypeObject _Py_GenericAliasIterType; - -#ifdef __cplusplus -} -#endif -#endif /* !Py_INTERNAL_GENERICALIASOBJECT_H */ diff --git a/Objects/object.c b/Objects/object.c index 1b77a13a0fcf70..29880f6003bb1d 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -18,7 +18,6 @@ #include "pycore_unionobject.h" // _PyUnion_Type #include "frameobject.h" // PyFrame_Type #include "pycore_interpreteridobject.h" // _PyInterpreterID_Type -#include "pycore_genericaliasobject.h" // _Py_GenericAliasIterType #ifdef Py_LIMITED_API // Prevent recursive call _Py_IncRef() <=> Py_INCREF() @@ -1835,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 From 55d04c46bc566c67e1351d34630ab6c122dc47bd Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 20 Apr 2022 21:44:55 +0900 Subject: [PATCH 3/5] Update Misc/NEWS.d/next/Core and Builtins/2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst Co-authored-by: Victor Stinner --- .../2022-04-20-14-43-37.gh-issue-91632.cvUhsZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 3001fe1c4fec9c..e7837828901fcf 100644 --- 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 @@ -1 +1 @@ -Fix :class:`generic_alias_iterator` to be finalized at exit. +Fix a minor memory leak at exit: release the memory of the :class:`generic_alias_iterator` type. Patch by Dong-hee Na. From a9a938cf278abb35669150a51360788485e1a038 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 20 Apr 2022 21:51:19 +0900 Subject: [PATCH 4/5] Address code review --- Objects/genericaliasobject.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index c68496a8d6f11d..01044e4ed43143 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -683,6 +683,9 @@ ga_iter_clear(PyObject *self) { return 0; } +/* 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", From 9b3dbd0a56cff8be258fa3e4a4025a58073276df Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 20 Apr 2022 22:41:22 +0900 Subject: [PATCH 5/5] fix comment style --- Objects/genericaliasobject.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index 01044e4ed43143..7059c4018303e0 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -683,9 +683,8 @@ ga_iter_clear(PyObject *self) { return 0; } -/* gh-91632: _Py_GenericAliasIterType is exported to be cleared - in _PyTypes_FiniTypes. -*/ +// 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",