Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore

to be able to unload the module.
This commit is contained in:
Victor Stinner 2011-05-21 02:13:22 +02:00
parent ce77defd84
commit f58f1c33c1
3 changed files with 27 additions and 42 deletions

View File

@ -25,11 +25,6 @@ import StringIO
from traceback import extract_tb, extract_stack, print_tb from traceback import extract_tb, extract_stack, print_tb
raise_src = 'def do_raise(): raise TypeError\n' 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): def make_pyc(co, mtime):
data = marshal.dumps(co) data = marshal.dumps(co)
if type(mtime) is type(0.0): if type(mtime) is type(0.0):
@ -463,19 +458,7 @@ class BadFileZipImportTestCase(unittest.TestCase):
zipimport._zip_directory_cache.clear() 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(): def test_main():
cleanup()
try: try:
test_support.run_unittest( test_support.run_unittest(
UncompressedZipImportTestCase, UncompressedZipImportTestCase,

View File

@ -80,13 +80,16 @@ Core and Builtins
Library Library
------- -------
- Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
to be able to unload the module.
- Issue #11088: don't crash when using F5 to run a script in IDLE on MacOSX - Issue #11088: don't crash when using F5 to run a script in IDLE on MacOSX
with Tk 8.5. with Tk 8.5.
- Issue #10154, #10090: change the normalization of UTF-8 to "UTF-8" instead - Issue #10154, #10090: change the normalization of UTF-8 to "UTF-8" instead
of "UTF8" in the locale module as the latter is not supported MacOSX and OpenBSD. of "UTF8" in the locale module as the latter is not supported MacOSX and OpenBSD.
- Issue #9516: avoid errors in sysconfig when MACOSX_DEPLOYMENT_TARGET is - Issue #9516: avoid errors in sysconfig when MACOSX_DEPLOYMENT_TARGET is
set in shell. set in shell.
- Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail - Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail

View File

@ -798,35 +798,33 @@ error:
/* Return the zlib.decompress function object, or NULL if zlib couldn't /* Return the zlib.decompress function object, or NULL if zlib couldn't
be imported. The function is cached when found, so subsequent calls be imported. The function is cached when found, so subsequent calls
don't import zlib again. Returns a *borrowed* reference. don't import zlib again. */
XXX This makes zlib.decompress immortal. */
static PyObject * static PyObject *
get_decompress_func(void) get_decompress_func(void)
{ {
static PyObject *decompress = NULL; static int importing_zlib = 0;
PyObject *zlib;
PyObject *decompress;
if (decompress == NULL) { if (importing_zlib != 0)
PyObject *zlib; /* Someone has a zlib.py[co] in their Zip file;
static int importing_zlib = 0; let's avoid a stack overflow. */
return NULL;
if (importing_zlib != 0) importing_zlib = 1;
/* Someone has a zlib.py[co] in their Zip file; zlib = PyImport_ImportModuleNoBlock("zlib");
let's avoid a stack overflow. */ importing_zlib = 0;
return NULL; if (zlib != NULL) {
importing_zlib = 1; decompress = PyObject_GetAttrString(zlib,
zlib = PyImport_ImportModuleNoBlock("zlib"); "decompress");
importing_zlib = 0; Py_DECREF(zlib);
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");
} }
else {
PyErr_Clear();
decompress = NULL;
}
if (Py_VerboseFlag)
PySys_WriteStderr("# zipimport: zlib %s\n",
zlib != NULL ? "available": "UNAVAILABLE");
return decompress; return decompress;
} }
@ -911,6 +909,7 @@ get_data(char *archive, PyObject *toc_entry)
goto error; goto error;
} }
data = PyObject_CallFunction(decompress, "Oi", raw_data, -15); data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Py_DECREF(decompress);
error: error:
Py_DECREF(raw_data); Py_DECREF(raw_data);
return data; return data;