From: Benjamin Peterson <benjamin@python.org> Date: Sun, 16 Feb 2014 19:17:28 +0000 (-0500) Subject: merge backout for #20621 X-Git-Tag: v3.4.1rc1~233^2~312 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ec91cd60406f5ff51a5fbb27fd32091f93b3ca92;p=python merge backout for #20621 --- ec91cd60406f5ff51a5fbb27fd32091f93b3ca92 diff --cc Lib/test/test_zipimport.py index bf058bcc56,37603b9bcd..1e351c8c8a --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@@ -54,44 -46,6 +54,23 @@@ pyc_file = importlib.util.cache_from_so pyc_ext = ('.pyc' if __debug__ else '.pyo') +class ImportHooksBaseTestCase(unittest.TestCase): + + def setUp(self): + self.path = sys.path[:] + self.meta_path = sys.meta_path[:] + self.path_hooks = sys.path_hooks[:] + sys.path_importer_cache.clear() + self.modules_before = support.modules_setup() + + def tearDown(self): + sys.path[:] = self.path + sys.meta_path[:] = self.meta_path + sys.path_hooks[:] = self.path_hooks + sys.path_importer_cache.clear() + support.modules_cleanup(*self.modules_before) + + - def _write_zip_package(zipname, files, - data_to_prepend=b"", compression=ZIP_STORED): - z = ZipFile(zipname, "w") - try: - for name, (mtime, data) in files.items(): - zinfo = ZipInfo(name, time.localtime(mtime)) - zinfo.compress_type = compression - z.writestr(zinfo, data) - finally: - z.close() - - if data_to_prepend: - # Prepend data to the start of the zipfile - with open(zipname, "rb") as f: - zip_data = f.read() - - with open(zipname, "wb") as f: - f.write(data_to_prepend) - f.write(zip_data) - - class UncompressedZipImportTestCase(ImportHooksBaseTestCase): compression = ZIP_STORED diff --cc Modules/zipimport.c index 9f662f5285,2feb2a827c..8fe919539f --- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@@ -594,8 -554,10 +560,7 @@@ zipimporter_get_data(PyObject *obj, PyO { ZipImporter *self = (ZipImporter *)obj; PyObject *path, *key; - FILE *fp; - PyObject *toc_entry, *data; -#ifdef ALTSEP - _Py_IDENTIFIER(replace); -#endif + PyObject *toc_entry; Py_ssize_t path_start, path_len, len; if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path)) @@@ -1046,8 -869,15 +873,15 @@@ read_directory(PyObject *archive const char *charset; int bootstrap; - assert(fp != NULL); - fp = _Py_fopen(archive, "rb"); ++ fp = _Py_fopen_obj(archive, "rb"); + if (fp == NULL) { + if (!PyErr_Occurred()) + PyErr_Format(ZipImportError, "can't open Zip file: %R", archive); + return NULL; + } + if (fseek(fp, -22, SEEK_END) == -1) { + fclose(fp); PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); return NULL; } @@@ -1168,7 -988,8 +1005,8 @@@ PySys_FormatStderr("# zipimport: found %ld names in %R\n", count, archive); return files; -fseek_error: +file_error: + fclose(fp); Py_XDECREF(files); Py_XDECREF(nameobj); PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); @@@ -1232,8 -1055,17 +1072,17 @@@ get_data(PyObject *archive, PyObject *t return NULL; } - fp = _Py_fopen(archive, "rb"); ++ fp = _Py_fopen_obj(archive, "rb"); + if (!fp) { + if (!PyErr_Occurred()) + PyErr_Format(PyExc_IOError, + "zipimport: can not open file %U", archive); + return NULL; + } + /* Check to make sure the local file header is correct */ if (fseek(fp, file_offset, 0) == -1) { + fclose(fp); PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); return NULL; } @@@ -1241,10 -1073,10 +1090,11 @@@ l = PyMarshal_ReadLongFromFile(fp); if (l != 0x04034B50) { /* Bad: Local File Header */ - PyErr_Format(ZipImportError, - "bad local file header in %U", - archive); + if (!PyErr_Occurred()) + PyErr_Format(ZipImportError, + "bad local file header in %U", + archive); + fclose(fp); return NULL; } if (fseek(fp, file_offset + 26, 0) == -1) { @@@ -1254,9 -1087,6 +1105,10 @@@ l = 30 + PyMarshal_ReadShortFromFile(fp) + PyMarshal_ReadShortFromFile(fp); /* local header size */ + if (PyErr_Occurred()) { ++ fclose(fp); + return NULL; + } file_offset += l; /* Start of file data */ bytes_size = compress == 0 ? data_size : data_size + 1;