summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-05-19 22:18:58 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-05-19 22:18:58 (GMT)
commit4445ec81c17c672b5d245da7a1af0e0294debcc0 (patch)
tree76b535a06bee9683634b32452dc4f4faca400907
parentf23f786c6172da874202b134ab1dedd99e0a261a (diff)
parent4925cde1cc20fe559b9c1429a99bf9b1c17f7048 (diff)
downloadcpython-4445ec81c17c672b5d245da7a1af0e0294debcc0.zip
cpython-4445ec81c17c672b5d245da7a1af0e0294debcc0.tar.gz
cpython-4445ec81c17c672b5d245da7a1af0e0294debcc0.tar.bz2
(Merge 3.1) Issue #12124: zipimport doesn't keep a reference to
zlib.decompress() anymore to be able to unload the module.
-rw-r--r--Lib/test/test_zipimport.py17
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/zipimport.c47
3 files changed, 27 insertions, 41 deletions
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index 090f0f3..df5ff9d 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -25,11 +25,6 @@ import io
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):
@@ -473,19 +468,7 @@ class BadFileZipImportTestCase(unittest.TestCase):
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
index 30eda2b..d801082 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,10 +13,14 @@ Core and Builtins
Library
-------
+- Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
+ to be able to unload the module.
+
- Issue #12065: connect_ex() on an SSL socket now returns the original errno
when the socket's timeout expires (it used to return None).
+
What's New in Python 3.2.1 release candidate 1?
===============================================
diff --git a/Modules/zipimport.c b/Modules/zipimport.c
index 88a27c1..68c2894 100644
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -856,35 +856,33 @@ error:
/* 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;
}
@@ -975,6 +973,7 @@ get_data(PyObject *archive, PyObject *toc_entry)
goto error;
}
data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
+ Py_DECREF(decompress);
error:
Py_DECREF(raw_data);
return data;