[Python-checkins] cpython (merge 3.2 -> default): (Merge 3.2) Issue #12124: zipimport doesn't keep a reference to

victor.stinner python-checkins at python.org
Fri May 20 00:22:56 CEST 2011


http://hg.python.org/cpython/rev/9e13869b7639
changeset:   70216:9e13869b7639
parent:      70213:44a265b95a49
parent:      70215:a043d8e168b3
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Fri May 20 00:22:39 2011 +0200
summary:
  (Merge 3.2) Issue #12124: zipimport doesn't keep a reference to
zlib.decompress() anymore to be able to unload the module.

files:
  Lib/test/test_zipimport.py |  17 ---------
  Misc/NEWS                  |   3 +
  Modules/zipimport.c        |  47 ++++++++++++-------------
  3 files changed, 26 insertions(+), 41 deletions(-)


diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -19,11 +19,6 @@
 from traceback import extract_tb, extract_stack, print_tb
 raise_src = 'def do_raise(): raise TypeError\n'
 
-# so we only run testAFakeZlib once if this test is run repeatedly
-# which happens when we look for ref leaks
-test_imported = False
-
-
 def make_pyc(co, mtime):
     data = marshal.dumps(co)
     if type(mtime) is type(0.0):
@@ -467,19 +462,7 @@
             zipimport._zip_directory_cache.clear()
 
 
-def cleanup():
-    # this is necessary if test is run repeated (like when finding leaks)
-    global test_imported
-    if test_imported:
-        zipimport._zip_directory_cache.clear()
-        if hasattr(UncompressedZipImportTestCase, 'testAFakeZlib'):
-            delattr(UncompressedZipImportTestCase, 'testAFakeZlib')
-        if hasattr(CompressedZipImportTestCase, 'testAFakeZlib'):
-            delattr(CompressedZipImportTestCase, 'testAFakeZlib')
-    test_imported = True
-
 def test_main():
-    cleanup()
     try:
         support.run_unittest(
               UncompressedZipImportTestCase,
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -153,6 +153,9 @@
 Library
 -------
 
+- Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
+  to be able to unload the module.
+
 - Issue #12120, #12119: skip a test in packaging and distutils
   if sys.dont_write_bytecode is set to True.
 
diff --git a/Modules/zipimport.c b/Modules/zipimport.c
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -867,35 +867,33 @@
 
 /* Return the zlib.decompress function object, or NULL if zlib couldn't
    be imported. The function is cached when found, so subsequent calls
-   don't import zlib again. Returns a *borrowed* reference.
-   XXX This makes zlib.decompress immortal. */
+   don't import zlib again. */
 static PyObject *
 get_decompress_func(void)
 {
-    static PyObject *decompress = NULL;
+    static int importing_zlib = 0;
+    PyObject *zlib;
+    PyObject *decompress;
 
-    if (decompress == NULL) {
-        PyObject *zlib;
-        static int importing_zlib = 0;
-
-        if (importing_zlib != 0)
-            /* Someone has a zlib.py[co] in their Zip file;
-               let's avoid a stack overflow. */
-            return NULL;
-        importing_zlib = 1;
-        zlib = PyImport_ImportModuleNoBlock("zlib");
-        importing_zlib = 0;
-        if (zlib != NULL) {
-            decompress = PyObject_GetAttrString(zlib,
-                                                "decompress");
-            Py_DECREF(zlib);
-        }
-        else
-            PyErr_Clear();
-        if (Py_VerboseFlag)
-            PySys_WriteStderr("# zipimport: zlib %s\n",
-                zlib != NULL ? "available": "UNAVAILABLE");
+    if (importing_zlib != 0)
+        /* Someone has a zlib.py[co] in their Zip file;
+           let's avoid a stack overflow. */
+        return NULL;
+    importing_zlib = 1;
+    zlib = PyImport_ImportModuleNoBlock("zlib");
+    importing_zlib = 0;
+    if (zlib != NULL) {
+        decompress = PyObject_GetAttrString(zlib,
+                                            "decompress");
+        Py_DECREF(zlib);
     }
+    else {
+        PyErr_Clear();
+        decompress = NULL;
+    }
+    if (Py_VerboseFlag)
+        PySys_WriteStderr("# zipimport: zlib %s\n",
+            zlib != NULL ? "available": "UNAVAILABLE");
     return decompress;
 }
 
@@ -986,6 +984,7 @@
         goto error;
     }
     data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
+    Py_DECREF(decompress);
 error:
     Py_DECREF(raw_data);
     return data;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list