Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14b04cd

Browse files
committed
Plug a leak in timemodule. The module dictionary is saved during
initialization. If the interpreter is shut down and reinitialized (embedded CPython), the old module dictionary was not dec-refed during the next import of the time extension module. Contributed by Torsten Marek of Google.
1 parent 28c88f4 commit 14b04cd

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,9 @@ Library
620620
Extension Modules
621621
-----------------
622622

623+
- Fix the leak of a dict in the time module when used in an embedded
624+
interpreter that is repeatedly initialized and shutdown and reinitialized.
625+
623626
- Issue #16012: Fix a regression in pyexpat. The parser's UseForeignDTD()
624627
method doesn't require an argument again.
625628

Modules/timemodule.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int floatsleep(double);
6363
static double floattime(void);
6464

6565
/* For Y2K check */
66-
static PyObject *moddict;
66+
static PyObject *moddict = NULL;
6767

6868
static PyObject *
6969
time_time(PyObject *self, PyObject *unused)
@@ -941,6 +941,11 @@ PyInit_time(void)
941941
/* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
942942
p = Py_GETENV("PYTHONY2K");
943943
PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
944+
/* If an embedded interpreter is shutdown and reinitialized the old
945+
moddict was not decrefed on shutdown and the next import of this
946+
module leads to a leak. Conditionally decref here to prevent that.
947+
*/
948+
Py_XDECREF(moddict);
944949
/* Squirrel away the module's dictionary for the y2k check */
945950
moddict = PyModule_GetDict(m);
946951
Py_INCREF(moddict);

0 commit comments

Comments
 (0)