cpython: 10f09881320d (original) (raw)
--- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -42,16 +42,10 @@ struct _zipimporter { static PyObject *ZipImportError; static PyObject *zip_directory_cache = NULL; -static PyObject zip_stat_cache = NULL; -/ posix.fstat or nt.fstat function. Used due to posixmodule.c's
- superior fstat implementation over libc's on Windows. */ -static PyObject fstat_function = NULL; / posix.fstat() or nt.fstat() / / forward decls */ -static FILE *fopen_rb_and_stat(char *path, PyObject **py_stat_p); -static FILE *safely_reopen_archive(ZipImporter *self, char **archive_p); -static PyObject *read_directory(FILE *fp, char *archive); -static PyObject *get_data(FILE *fp, char *archive, PyObject *toc_entry); +static PyObject *read_directory(char *archive); +static PyObject *get_data(char *archive, PyObject *toc_entry); static PyObject *get_module_code(ZipImporter *self, char *fullname, int *p_ispackage, char **p_modpath);
@@ -132,38 +126,12 @@ zipimporter_init(ZipImporter *self, PyOb PyObject *files; files = PyDict_GetItemString(zip_directory_cache, path); if (files == NULL) {
PyObject *zip_stat = NULL;[](#l1.26)
FILE *fp = fopen_rb_and_stat(buf, &zip_stat);[](#l1.27)
if (fp == NULL) {[](#l1.28)
PyErr_Format(ZipImportError, "can't open Zip file: "[](#l1.29)
"'%.200s'", buf);[](#l1.30)
Py_XDECREF(zip_stat);[](#l1.31)
files = read_directory(buf);[](#l1.32)
if (files == NULL)[](#l1.33) return -1;[](#l1.34)
}[](#l1.35)
if (Py_VerboseFlag)[](#l1.37)
PySys_WriteStderr("# zipimport: %s not cached, "[](#l1.38)
"reading TOC.\n", path);[](#l1.39)
files = read_directory(fp, buf);[](#l1.41)
fclose(fp);[](#l1.42)
if (files == NULL) {[](#l1.43)
Py_XDECREF(zip_stat);[](#l1.44)
if (PyDict_SetItemString(zip_directory_cache, path,[](#l1.45)
files) != 0)[](#l1.46) return -1;[](#l1.47)
}[](#l1.48)
if (PyDict_SetItemString(zip_directory_cache, path,[](#l1.49)
files) != 0) {[](#l1.50)
Py_DECREF(files);[](#l1.51)
Py_XDECREF(zip_stat);[](#l1.52)
return -1;[](#l1.53)
}[](#l1.54)
if (zip_stat && PyDict_SetItemString(zip_stat_cache, path,[](#l1.55)
zip_stat) != 0) {[](#l1.56)
Py_DECREF(files);[](#l1.57)
Py_DECREF(zip_stat);[](#l1.58)
return -1;[](#l1.59)
}[](#l1.60)
Py_XDECREF(zip_stat);[](#l1.61) }[](#l1.62) else[](#l1.63) Py_INCREF(files);[](#l1.64)
@@ -451,12 +419,11 @@ static PyObject * zipimporter_get_data(PyObject *obj, PyObject *args) { ZipImporter *self = (ZipImporter *)obj;
#ifdef ALTSEP char *p, buf[MAXPATHLEN + 1]; #endif
- PyObject *toc_entry; Py_ssize_t len; if (!PyArg_ParseTuple(args, "s:zipimporter.get_data", &path)) @@ -481,19 +448,12 @@ zipimporter_get_data(PyObject *obj, PyOb path = path + len + 1; }
- toc_entry = PyDict_GetItemString(self->files, path); if (toc_entry == NULL) { PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
}fclose(fp);[](#l1.91) return NULL;[](#l1.92)
- data = get_data(fp, archive, toc_entry);
- fclose(fp);
- return data;
} static PyObject * @@ -513,8 +473,7 @@ zipimporter_get_source(PyObject *obj, Py { ZipImporter *self = (ZipImporter *)obj; PyObject *toc_entry;
- char *fullname, *subname, path[MAXPATHLEN+1]; int len; enum zi_module_info mi; @@ -542,20 +501,13 @@ zipimporter_get_source(PyObject *obj, Py else strcpy(path + len, ".py");
- toc_entry = PyDict_GetItemString(self->files, path);
- if (toc_entry != NULL) {
PyObject *data = get_data(fp, archive, toc_entry);[](#l1.121)
fclose(fp);[](#l1.122)
return data;[](#l1.123)
- }
- fclose(fp);
/* we have the module, but no source */
} PyDoc_STRVAR(doc_find_module, @@ -710,139 +662,7 @@ get_long(unsigned char buf) { } /
- fopen_rb_and_stat(path, &py_stat) -> FILE * -
- Opens path in "rb" mode and populates the Python py_stat stat_result
- with information about the opened file. *py_stat may not be changed
- if there is no fstat_function or if fstat_function fails. -
- Returns NULL and does nothing to py_stat if the open failed. -/ -static FILE * -fopen_rb_and_stat(char *path, PyObject **py_stat_p) -{
- FILE *fp;
- assert(py_stat_p != NULL);
- assert(*py_stat_p == NULL);
- if (fstat_function) {
PyObject *stat_result = PyObject_CallFunction(fstat_function,[](#l1.161)
"i", fileno(fp));[](#l1.162)
if (stat_result == NULL) {[](#l1.163)
PyErr_Clear(); /* We can function without it. */[](#l1.164)
} else {[](#l1.165)
*py_stat_p = stat_result;[](#l1.166)
}[](#l1.167)
- }
-} - -/* Return 1 if objects a and b fail a Py_EQ test for an attr. */ -static int -compare_obj_attr_strings(PyObject *obj_a, PyObject *obj_b, char *attr_name) -{
- int problem = 0;
- PyObject *attr_a = PyObject_GetAttrString(obj_a, attr_name);
- PyObject *attr_b = PyObject_GetAttrString(obj_b, attr_name);
- if (attr_a == NULL || attr_b == NULL)
problem = 1;[](#l1.181)
- else
problem = (PyObject_RichCompareBool(attr_a, attr_b, Py_EQ) != 1);[](#l1.183)
- Py_XDECREF(attr_a);
- Py_XDECREF(attr_b);
- return problem;
- *
- */ -static FILE * -safely_reopen_archive(ZipImporter *self, char **archive_p) -{
- FILE *fp;
- PyObject *stat_now = NULL;
- char *archive;
- assert(archive_p != NULL);
- *archive_p = PyString_AsString(self->archive);
- if (*archive_p == NULL)
return NULL;[](#l1.206)
- archive = *archive_p;
- fp = fopen_rb_and_stat(archive, &stat_now);
- if (!fp) {
PyErr_Format(PyExc_IOError,[](#l1.211)
"zipimport: can not open file %s", archive);[](#l1.212)
Py_XDECREF(stat_now);[](#l1.213)
return NULL;[](#l1.214)
- }
- if (stat_now != NULL) {
int problem = 0;[](#l1.218)
PyObject *files;[](#l1.219)
PyObject *prev_stat = PyDict_GetItemString(zip_stat_cache, archive);[](#l1.220)
/* Test stat_now vs the old cached stat on some key attributes. */[](#l1.221)
if (prev_stat != NULL) {[](#l1.222)
problem = compare_obj_attr_strings(prev_stat, stat_now,[](#l1.223)
"st_ino");[](#l1.224)
problem |= compare_obj_attr_strings(prev_stat, stat_now,[](#l1.225)
"st_size");[](#l1.226)
problem |= compare_obj_attr_strings(prev_stat, stat_now,[](#l1.227)
"st_mtime");[](#l1.228)
} else {[](#l1.229)
if (Py_VerboseFlag)[](#l1.230)
PySys_WriteStderr("# zipimport: no stat data for %s!\n",[](#l1.231)
archive);[](#l1.232)
problem = 1;[](#l1.233)
}[](#l1.234)
if (problem) {[](#l1.236)
if (Py_VerboseFlag)[](#l1.237)
PySys_WriteStderr("# zipimport: %s modified since last"[](#l1.238)
" import, rereading TOC.\n", archive);[](#l1.239)
files = read_directory(fp, archive);[](#l1.240)
if (files == NULL) {[](#l1.241)
Py_DECREF(stat_now);[](#l1.242)
fclose(fp);[](#l1.243)
return NULL;[](#l1.244)
}[](#l1.245)
if (PyDict_SetItem(zip_directory_cache, self->archive,[](#l1.246)
files) != 0) {[](#l1.247)
Py_DECREF(files);[](#l1.248)
Py_DECREF(stat_now);[](#l1.249)
fclose(fp);[](#l1.250)
return NULL;[](#l1.251)
}[](#l1.252)
if (stat_now && PyDict_SetItem(zip_stat_cache, self->archive,[](#l1.253)
stat_now) != 0) {[](#l1.254)
Py_DECREF(files);[](#l1.255)
Py_DECREF(stat_now);[](#l1.256)
fclose(fp);[](#l1.257)
return NULL;[](#l1.258)
}[](#l1.259)
Py_XDECREF(self->files); /* free the old value. */[](#l1.260)
self->files = files;[](#l1.261)
} else {[](#l1.262)
/* No problem, discard the new stat data. */[](#l1.263)
Py_DECREF(stat_now);[](#l1.264)
}[](#l1.265)
- } /* stat succeeded */
- read_directory(archive) -> files dict (new reference) Given a path to a Zip archive, build a dict, mapping file names (local to the archive, using SEP as a separator) to toc entries. @@ -863,9 +683,10 @@ safely_reopen_archive(ZipImporter *self, data_size and file_offset are 0. */ static PyObject * -read_directory(FILE *fp, char *archive) +read_directory(char *archive) { PyObject *files = NULL;
- FILE *fp; long compress, crc, data_size, file_size, file_offset, date, time; long header_offset, name_size, header_size, header_position; long i, l, count; @@ -875,7 +696,6 @@ read_directory(FILE *fp, char *archive) char p, endof_central_dir[22]; long arc_offset; / offset from beginning of file to start of zip-archive */
- assert(fp != NULL); if (strlen(archive) > MAXPATHLEN) { PyErr_SetString(PyExc_OverflowError, "Zip path name is too long");
@@ -883,18 +703,28 @@ read_directory(FILE *fp, char *archive) } strcpy(path, archive);
- fp = fopen(archive, "rb");
- if (fp == NULL) {
PyErr_Format(ZipImportError, "can't open Zip file: "[](#l1.303)
"'%.200s'", archive);[](#l1.304)
return NULL;[](#l1.305)
- }
+ if (fseek(fp, -22, SEEK_END) == -1) {
} header_position = ftell(fp); if (fread(endof_central_dir, 1, 22, fp) != 22) {fclose(fp);[](#l1.309) PyErr_Format(ZipImportError, "can't read Zip file: %s", archive);[](#l1.310) return NULL;[](#l1.311)
} if (get_long((unsigned char )endof_central_dir) != 0x06054B50) { / Bad: End of Central Dir signature */fclose(fp);[](#l1.315) PyErr_Format(ZipImportError, "can't read Zip file: "[](#l1.316) "'%.200s'", archive);[](#l1.317) return NULL;[](#l1.318)
fclose(fp);[](#l1.322) PyErr_Format(ZipImportError, "not a Zip file: "[](#l1.323) "'%.200s'", archive);[](#l1.324) return NULL;[](#l1.325)
@@ -963,15 +793,18 @@ read_directory(FILE *fp, char *archive) goto error; count++; }
- fclose(fp); if (Py_VerboseFlag) PySys_WriteStderr("# zipimport: found %ld names in %s\n", count, archive); return files; fseek_error:
- fclose(fp); Py_XDECREF(files); PyErr_Format(ZipImportError, "can't read Zip file: %s", archive); return NULL; error:
- fclose(fp); Py_XDECREF(files); return NULL; }
@@ -1008,13 +841,14 @@ get_decompress_func(void) return decompress; } -/* Given a FILE* to a Zip file and a toc_entry, return the (uncompressed) +/* Given a path to a Zip file and a toc_entry, return the (uncompressed) data as a new reference. */ static PyObject * -get_data(FILE *fp, char *archive, PyObject *toc_entry) +get_data(char *archive, PyObject *toc_entry) { PyObject *raw_data, *data = NULL, *decompress; char *buf;
- FILE *fp; int err; Py_ssize_t bytes_read = 0; long l; @@ -1028,8 +862,16 @@ get_data(FILE *fp, char *archive, PyObje return NULL; }
- fp = fopen(archive, "rb");
- if (!fp) {
PyErr_Format(PyExc_IOError,[](#l1.368)
"zipimport: can not open file %s", archive);[](#l1.369)
return NULL;[](#l1.370)
- }
+ /* Check to make sure the local file header is correct */ if (fseek(fp, file_offset, 0) == -1) {
} @@ -1040,9 +882,11 @@ get_data(FILE *fp, char *archive, PyObje PyErr_Format(ZipImportError, "bad local file header in %s", archive);fclose(fp);[](#l1.375) PyErr_Format(ZipImportError, "can't read Zip file: %s", archive);[](#l1.376) return NULL;[](#l1.377)
} if (fseek(fp, file_offset + 26, 0) == -1) {fclose(fp);[](#l1.383) return NULL;[](#l1.384)
} @@ -1054,6 +898,7 @@ get_data(FILE *fp, char *archive, PyObje raw_data = PyString_FromStringAndSize((char *)NULL, compress == 0 ? data_size : data_size + 1); if (raw_data == NULL) {fclose(fp);[](#l1.387) PyErr_Format(ZipImportError, "can't read Zip file: %s", archive);[](#l1.388) return NULL;[](#l1.389)
} buf = PyString_AsString(raw_data); @@ -1062,9 +907,11 @@ get_data(FILE *fp, char *archive, PyObje if (err == 0) { bytes_read = fread(buf, 1, data_size, fp); } else {fclose(fp);[](#l1.395) return NULL;[](#l1.396)
}fclose(fp);[](#l1.403) PyErr_Format(ZipImportError, "can't read Zip file: %s", archive);[](#l1.404) return NULL;[](#l1.405)
- fclose(fp); if (err || bytes_read != data_size) { PyErr_SetString(PyExc_IOError, "zipimport: can't read data");
@@ -1260,13 +1107,17 @@ get_mtime_of_source(ZipImporter self, c / Return the code object for the module named by 'fullname' from the Zip archive as a new reference. */ static PyObject * -get_code_from_data(char *archive, FILE *fp, int ispackage,
int isbytecode, time_t mtime, PyObject *toc_entry)[](#l1.416)
+get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
time_t mtime, PyObject *toc_entry)[](#l1.418)
{ PyObject *data, *code; char *modpath;
@@ -1301,19 +1152,12 @@ get_module_code(ZipImporter *self, char for (zso = zip_searchorder; *zso->suffix; zso++) { PyObject *code = NULL;
FILE *fp;[](#l1.436)
char *archive;[](#l1.437)
strcpy(path + len, zso->suffix); if (Py_VerboseFlag > 1) PySys_WriteStderr("# trying %s%c%s\n", PyString_AsString(self->archive), SEP, path); -
fp = safely_reopen_archive(self, &archive);[](#l1.445)
if (fp == NULL)[](#l1.446)
return NULL;[](#l1.447)
- toc_entry = PyDict_GetItemString(self->files, path); if (toc_entry != NULL) { time_t mtime = 0; @@ -1324,10 +1168,9 @@ get_module_code(ZipImporter *self, char mtime = get_mtime_of_source(self, path); if (p_ispackage != NULL) *p_ispackage = ispackage;
code = get_code_from_data(archive, fp, ispackage,[](#l1.456)
code = get_code_from_data(self, ispackage,[](#l1.457) isbytecode, mtime,[](#l1.458) toc_entry);[](#l1.459)
fclose(fp);[](#l1.460) if (code == Py_None) {[](#l1.461) /* bad magic number or non-matching mtime[](#l1.462) in byte code, try next */[](#l1.463)
@@ -1339,7 +1182,6 @@ get_module_code(ZipImporter *self, char PyTuple_GetItem(toc_entry, 0)); return code; }
} PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname); return NULL; @@ -1357,8 +1199,6 @@ This module exports three objects:\n[](#l1.472) subclass of ImportError, so it can be caught as ImportError, too.\n[](#l1.473)fclose(fp);[](#l1.468)
- _zip_directory_cache: a dict, mapping archive paths to zip directory\n[](#l1.474) info dicts, as used in zipimporter._files.\n[](#l1.475) -- _zip_stat_cache: a dict, mapping archive paths to stat_result\n[](#l1.476)
- info for the .zip the last time anything was imported from it.\n[](#l1.477) \n[](#l1.478) It is usually not needed to use the zipimport module explicitly; it is\n[](#l1.479) used by the builtin import mechanism for sys.path items that are paths\n[](#l1.480) @@ -1407,7 +1247,6 @@ initzipimport(void) (PyObject *)&ZipImporter_Type) < 0) return;
- Py_XDECREF(zip_directory_cache); /* Avoid embedded interpreter leaks. */ zip_directory_cache = PyDict_New(); if (zip_directory_cache == NULL) return;
@@ -1415,31 +1254,4 @@ initzipimport(void) if (PyModule_AddObject(mod, "_zip_directory_cache", zip_directory_cache) < 0) return; -
- Py_XDECREF(zip_stat_cache); /* Avoid embedded interpreter leaks. */
- zip_stat_cache = PyDict_New();
- if (zip_stat_cache == NULL)
return;[](#l1.497)
- Py_INCREF(zip_stat_cache);
- if (PyModule_AddObject(mod, "_zip_stat_cache", zip_stat_cache) < 0)
return;[](#l1.500)
- {
/* We cannot import "os" here as that is a .py/.pyc file that could[](#l1.503)
* live within a zipped up standard library. Import the posix or nt[](#l1.504)
* builtin that provides the fstat() function we want instead. */[](#l1.505)
PyObject *os_like_module;[](#l1.506)
Py_XDECREF(fstat_function); /* Avoid embedded interpreter leaks. */[](#l1.507)
os_like_module = PyImport_ImportModule("posix");[](#l1.508)
if (os_like_module == NULL) {[](#l1.509)
os_like_module = PyImport_ImportModule("nt");[](#l1.510)
}[](#l1.511)
if (os_like_module != NULL) {[](#l1.512)
fstat_function = PyObject_GetAttrString(os_like_module, "fstat");[](#l1.513)
Py_DECREF(os_like_module);[](#l1.514)
}[](#l1.515)
if (fstat_function == NULL) {[](#l1.516)
PyErr_Clear(); /* non-fatal, we'll go on without it. */[](#l1.517)
}[](#l1.518)
- }