]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Plug a leak in timemodule. The module dictionary is saved during
authorGregory P. Smith <greg@krypto.org>
Tue, 27 Nov 2012 18:16:55 +0000 (10:16 -0800)
committerGregory P. Smith <greg@krypto.org>
Tue, 27 Nov 2012 18:16:55 +0000 (10:16 -0800)
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.

Misc/NEWS
Modules/timemodule.c

index b04986b404fc039f94773bdd778929bb2ecf7993..0b6e917a00b44a237c2e425b7c37cead60321bc7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -620,6 +620,9 @@ Library
 Extension Modules
 -----------------
 
+- Fix the leak of a dict in the time module when used in an embedded
+  interpreter that is repeatedly initialized and shutdown and reinitialized.
+
 - Issue #16012: Fix a regression in pyexpat. The parser's UseForeignDTD()
   method doesn't require an argument again.
 
index 626db3e251f68d4afa7cb4ae616af1430c3732e5..35162ff1331f4dbb6601e25b5a722dfac7cf4618 100644 (file)
@@ -63,7 +63,7 @@ static int floatsleep(double);
 static double floattime(void);
 
 /* For Y2K check */
-static PyObject *moddict;
+static PyObject *moddict = NULL;
 
 static PyObject *
 time_time(PyObject *self, PyObject *unused)
@@ -941,6 +941,11 @@ PyInit_time(void)
     /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
     p = Py_GETENV("PYTHONY2K");
     PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
+    /* If an embedded interpreter is shutdown and reinitialized the old
+       moddict was not decrefed on shutdown and the next import of this
+       module leads to a leak.  Conditionally decref here to prevent that.
+    */
+    Py_XDECREF(moddict);
     /* Squirrel away the module's dictionary for the y2k check */
     moddict = PyModule_GetDict(m);
     Py_INCREF(moddict);