]> granicus.if.org Git - python/commitdiff
Plug a leak in timemodule. The module dictionary is saved during
authorGregory P. Smith <greg@krypto.org>
Tue, 27 Nov 2012 18:19:29 +0000 (10:19 -0800)
committerGregory P. Smith <greg@krypto.org>
Tue, 27 Nov 2012 18:19:29 +0000 (10:19 -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 8c13755844077ec10505840e9dc7761180d9e3a4..abe5f5162fa7c3bc1fd26e4b1e3e7dab43062ba1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -507,6 +507,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 #12268: File readline, readlines and read or readall methods
   no longer lose data when an underlying read system call is interrupted
   within an io module object.  IOError is no longer raised due to a read
index 397cf8cd6f9e002e9baadd81fb8876c2799da498..13be691979f23e04d4fa5ebda8c5a5af6ae4009e 100644 (file)
@@ -96,7 +96,7 @@ static int floatsleep(double);
 static double floattime(void);
 
 /* For Y2K check */
-static PyObject *moddict;
+static PyObject *moddict = NULL;
 
 /* Exposed in timefuncs.h. */
 time_t
@@ -858,6 +858,11 @@ inittime(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);
@@ -1050,5 +1055,3 @@ floatsleep(double secs)
 
     return 0;
 }
-
-