pyc_ext = ('.pyc' if __debug__ else '.pyo')
- 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 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)
+
+
class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
compression = ZIP_STORED
{
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))
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;
}
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);
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;
}
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) {
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;