Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "structmember.h" |
| 3 | #include "osdefs.h" |
| 4 | #include "marshal.h" |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 5 | #include <time.h> |
| 6 | |
| 7 | |
| 8 | #define IS_SOURCE 0x0 |
| 9 | #define IS_BYTECODE 0x1 |
| 10 | #define IS_PACKAGE 0x2 |
| 11 | |
| 12 | struct st_zip_searchorder { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 13 | char suffix[14]; |
| 14 | int type; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | /* zip_searchorder defines how we search for a module in the Zip |
| 18 | archive: we first search for a package __init__, then for |
| 19 | non-package .pyc, .pyo and .py entries. The .pyc and .pyo entries |
| 20 | are swapped by initzipimport() if we run in optimized mode. Also, |
| 21 | '/' is replaced by SEP there. */ |
Neal Norwitz | 29fd2ba | 2003-03-23 13:21:03 +0000 | [diff] [blame] | 22 | static struct st_zip_searchorder zip_searchorder[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 23 | {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE}, |
| 24 | {"/__init__.pyo", IS_PACKAGE | IS_BYTECODE}, |
| 25 | {"/__init__.py", IS_PACKAGE | IS_SOURCE}, |
| 26 | {".pyc", IS_BYTECODE}, |
| 27 | {".pyo", IS_BYTECODE}, |
| 28 | {".py", IS_SOURCE}, |
| 29 | {"", 0} |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /* zipimporter object definition and support */ |
| 33 | |
| 34 | typedef struct _zipimporter ZipImporter; |
| 35 | |
| 36 | struct _zipimporter { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 37 | PyObject_HEAD |
| 38 | PyObject *archive; /* pathname of the Zip archive */ |
| 39 | PyObject *prefix; /* file prefix: "a/sub/directory/" */ |
| 40 | PyObject *files; /* dict with file info {path: toc_entry} */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 43 | static PyObject *ZipImportError; |
| 44 | static PyObject *zip_directory_cache = NULL; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 45 | static PyObject *zip_stat_cache = NULL; |
| 46 | /* posix.fstat or nt.fstat function. Used due to posixmodule.c's |
| 47 | * superior fstat implementation over libc's on Windows. */ |
| 48 | static PyObject *fstat_function = NULL; /* posix.fstat() or nt.fstat() */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 49 | |
| 50 | /* forward decls */ |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 51 | static FILE *fopen_rb_and_stat(char *path, PyObject **py_stat_p); |
| 52 | static FILE *safely_reopen_archive(ZipImporter *self, char **archive_p); |
| 53 | static PyObject *read_directory(FILE *fp, char *archive); |
| 54 | static PyObject *get_data(FILE *fp, char *archive, PyObject *toc_entry); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 55 | static PyObject *get_module_code(ZipImporter *self, char *fullname, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 56 | int *p_ispackage, char **p_modpath); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | #define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type) |
| 60 | |
| 61 | |
| 62 | /* zipimporter.__init__ |
| 63 | Split the "subdirectory" from the Zip archive path, lookup a matching |
| 64 | entry in sys.path_importer_cache, fetch the file directory from there |
| 65 | if found, or else read it from the archive. */ |
| 66 | static int |
| 67 | zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) |
| 68 | { |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 69 | char *path, *p, *prefix, buf[MAXPATHLEN+2]; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 70 | size_t len; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 72 | if (!_PyArg_NoKeywords("zipimporter()", kwds)) |
| 73 | return -1; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 74 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 75 | if (!PyArg_ParseTuple(args, "s:zipimporter", |
| 76 | &path)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 77 | return -1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 78 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 79 | len = strlen(path); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 80 | if (len == 0) { |
| 81 | PyErr_SetString(ZipImportError, "archive path is empty"); |
| 82 | return -1; |
| 83 | } |
| 84 | if (len >= MAXPATHLEN) { |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 85 | PyErr_SetString(ZipImportError, |
| 86 | "archive path too long"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 87 | return -1; |
| 88 | } |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 89 | strcpy(buf, path); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef ALTSEP |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 92 | for (p = buf; *p; p++) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 93 | if (*p == ALTSEP) |
| 94 | *p = SEP; |
| 95 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 98 | path = NULL; |
| 99 | prefix = NULL; |
| 100 | for (;;) { |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 101 | #ifndef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 102 | struct stat statbuf; |
| 103 | int rv; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 104 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 105 | rv = stat(buf, &statbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 106 | if (rv == 0) { |
| 107 | /* it exists */ |
| 108 | if (S_ISREG(statbuf.st_mode)) |
| 109 | /* it's a file */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 110 | path = buf; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 113 | #else |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 114 | if (object_exists(buf)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 115 | /* it exists */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 116 | if (isfile(buf)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 117 | /* it's a file */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 118 | path = buf; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 119 | break; |
| 120 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 121 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 122 | /* back up one path element */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 123 | p = strrchr(buf, SEP); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 124 | if (prefix != NULL) |
| 125 | *prefix = SEP; |
| 126 | if (p == NULL) |
| 127 | break; |
| 128 | *p = '\0'; |
| 129 | prefix = p; |
| 130 | } |
| 131 | if (path != NULL) { |
| 132 | PyObject *files; |
| 133 | files = PyDict_GetItemString(zip_directory_cache, path); |
| 134 | if (files == NULL) { |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 135 | PyObject *zip_stat = NULL; |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 136 | FILE *fp = fopen_rb_and_stat(buf, &zip_stat); |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 137 | if (fp == NULL) { |
| 138 | PyErr_Format(ZipImportError, "can't open Zip file: " |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 139 | "'%.200s'", buf); |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 140 | Py_XDECREF(zip_stat); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 141 | return -1; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (Py_VerboseFlag) |
| 145 | PySys_WriteStderr("# zipimport: %s not cached, " |
| 146 | "reading TOC.\n", path); |
| 147 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 148 | files = read_directory(fp, buf); |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 149 | fclose(fp); |
| 150 | if (files == NULL) { |
| 151 | Py_XDECREF(zip_stat); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 152 | return -1; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 153 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 154 | if (PyDict_SetItemString(zip_directory_cache, path, |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 155 | files) != 0) { |
| 156 | Py_DECREF(files); |
| 157 | Py_XDECREF(zip_stat); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 158 | return -1; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 159 | } |
| 160 | if (zip_stat && PyDict_SetItemString(zip_stat_cache, path, |
| 161 | zip_stat) != 0) { |
| 162 | Py_DECREF(files); |
| 163 | Py_DECREF(zip_stat); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 164 | return -1; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 165 | } |
| 166 | Py_XDECREF(zip_stat); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 167 | } |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 168 | else |
| 169 | Py_INCREF(files); |
| 170 | self->files = files; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 171 | } |
| 172 | else { |
| 173 | PyErr_SetString(ZipImportError, "not a Zip file"); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 174 | return -1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 175 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 177 | if (prefix == NULL) |
| 178 | prefix = ""; |
| 179 | else { |
| 180 | prefix++; |
| 181 | len = strlen(prefix); |
| 182 | if (prefix[len-1] != SEP) { |
| 183 | /* add trailing SEP */ |
| 184 | prefix[len] = SEP; |
| 185 | prefix[len + 1] = '\0'; |
| 186 | } |
| 187 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 188 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 189 | self->archive = PyString_FromString(buf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 190 | if (self->archive == NULL) |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 191 | return -1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 193 | self->prefix = PyString_FromString(prefix); |
| 194 | if (self->prefix == NULL) |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 195 | return -1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 197 | return 0; |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 198 | } |
| 199 | |
| 200 | /* GC support. */ |
| 201 | static int |
| 202 | zipimporter_traverse(PyObject *obj, visitproc visit, void *arg) |
| 203 | { |
| 204 | ZipImporter *self = (ZipImporter *)obj; |
| 205 | Py_VISIT(self->files); |
| 206 | return 0; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void |
| 210 | zipimporter_dealloc(ZipImporter *self) |
| 211 | { |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 212 | PyObject_GC_UnTrack(self); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 213 | Py_XDECREF(self->archive); |
| 214 | Py_XDECREF(self->prefix); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 215 | Py_XDECREF(self->files); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 216 | Py_TYPE(self)->tp_free((PyObject *)self); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static PyObject * |
| 220 | zipimporter_repr(ZipImporter *self) |
| 221 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 222 | char buf[500]; |
| 223 | char *archive = "???"; |
| 224 | char *prefix = ""; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 226 | if (self->archive != NULL && PyString_Check(self->archive)) |
| 227 | archive = PyString_AsString(self->archive); |
| 228 | if (self->prefix != NULL && PyString_Check(self->prefix)) |
| 229 | prefix = PyString_AsString(self->prefix); |
| 230 | if (prefix != NULL && *prefix) |
| 231 | PyOS_snprintf(buf, sizeof(buf), |
| 232 | "<zipimporter object \"%.300s%c%.150s\">", |
| 233 | archive, SEP, prefix); |
| 234 | else |
| 235 | PyOS_snprintf(buf, sizeof(buf), |
| 236 | "<zipimporter object \"%.300s\">", |
| 237 | archive); |
| 238 | return PyString_FromString(buf); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* return fullname.split(".")[-1] */ |
| 242 | static char * |
| 243 | get_subname(char *fullname) |
| 244 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 245 | char *subname = strrchr(fullname, '.'); |
| 246 | if (subname == NULL) |
| 247 | subname = fullname; |
| 248 | else |
| 249 | subname++; |
| 250 | return subname; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* Given a (sub)modulename, write the potential file path in the |
| 254 | archive (without extension) to the path buffer. Return the |
| 255 | length of the resulting string. */ |
| 256 | static int |
| 257 | make_filename(char *prefix, char *name, char *path) |
| 258 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 259 | size_t len; |
| 260 | char *p; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 262 | len = strlen(prefix); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 264 | /* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */ |
| 265 | if (len + strlen(name) + 13 >= MAXPATHLEN) { |
| 266 | PyErr_SetString(ZipImportError, "path too long"); |
| 267 | return -1; |
| 268 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 270 | strcpy(path, prefix); |
| 271 | strcpy(path + len, name); |
| 272 | for (p = path + len; *p; p++) { |
| 273 | if (*p == '.') |
| 274 | *p = SEP; |
| 275 | } |
| 276 | len += strlen(name); |
| 277 | assert(len < INT_MAX); |
| 278 | return (int)len; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Raymond Hettinger | 2c45c9a | 2004-11-10 13:08:35 +0000 | [diff] [blame] | 281 | enum zi_module_info { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 282 | MI_ERROR, |
| 283 | MI_NOT_FOUND, |
| 284 | MI_MODULE, |
| 285 | MI_PACKAGE |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | /* Return some information about a module. */ |
Raymond Hettinger | 2c45c9a | 2004-11-10 13:08:35 +0000 | [diff] [blame] | 289 | static enum zi_module_info |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 290 | get_module_info(ZipImporter *self, char *fullname) |
| 291 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 292 | char *subname, path[MAXPATHLEN + 1]; |
| 293 | int len; |
| 294 | struct st_zip_searchorder *zso; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 296 | subname = get_subname(fullname); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | len = make_filename(PyString_AsString(self->prefix), subname, path); |
| 299 | if (len < 0) |
| 300 | return MI_ERROR; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 302 | for (zso = zip_searchorder; *zso->suffix; zso++) { |
| 303 | strcpy(path + len, zso->suffix); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 304 | if (PyDict_GetItemString(self->files, path) != NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 305 | if (zso->type & IS_PACKAGE) |
| 306 | return MI_PACKAGE; |
| 307 | else |
| 308 | return MI_MODULE; |
| 309 | } |
| 310 | } |
| 311 | return MI_NOT_FOUND; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* Check whether we can satisfy the import of the module named by |
| 315 | 'fullname'. Return self if we can, None if we can't. */ |
| 316 | static PyObject * |
| 317 | zipimporter_find_module(PyObject *obj, PyObject *args) |
| 318 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 319 | ZipImporter *self = (ZipImporter *)obj; |
| 320 | PyObject *path = NULL; |
| 321 | char *fullname; |
| 322 | enum zi_module_info mi; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 323 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 324 | if (!PyArg_ParseTuple(args, "s|O:zipimporter.find_module", |
| 325 | &fullname, &path)) |
| 326 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 328 | mi = get_module_info(self, fullname); |
| 329 | if (mi == MI_ERROR) |
| 330 | return NULL; |
| 331 | if (mi == MI_NOT_FOUND) { |
| 332 | Py_INCREF(Py_None); |
| 333 | return Py_None; |
| 334 | } |
| 335 | Py_INCREF(self); |
| 336 | return (PyObject *)self; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Load and return the module named by 'fullname'. */ |
| 340 | static PyObject * |
| 341 | zipimporter_load_module(PyObject *obj, PyObject *args) |
| 342 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 343 | ZipImporter *self = (ZipImporter *)obj; |
| 344 | PyObject *code, *mod, *dict; |
| 345 | char *fullname, *modpath; |
| 346 | int ispackage; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 347 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 348 | if (!PyArg_ParseTuple(args, "s:zipimporter.load_module", |
| 349 | &fullname)) |
| 350 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 352 | code = get_module_code(self, fullname, &ispackage, &modpath); |
| 353 | if (code == NULL) |
| 354 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 356 | mod = PyImport_AddModule(fullname); |
| 357 | if (mod == NULL) { |
| 358 | Py_DECREF(code); |
| 359 | return NULL; |
| 360 | } |
| 361 | dict = PyModule_GetDict(mod); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 362 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 363 | /* mod.__loader__ = self */ |
| 364 | if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) |
| 365 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 367 | if (ispackage) { |
| 368 | /* add __path__ to the module *before* the code gets |
| 369 | executed */ |
| 370 | PyObject *pkgpath, *fullpath; |
| 371 | char *prefix = PyString_AsString(self->prefix); |
| 372 | char *subname = get_subname(fullname); |
| 373 | int err; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 374 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 375 | fullpath = PyString_FromFormat("%s%c%s%s", |
| 376 | PyString_AsString(self->archive), |
| 377 | SEP, |
| 378 | *prefix ? prefix : "", |
| 379 | subname); |
| 380 | if (fullpath == NULL) |
| 381 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 382 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 383 | pkgpath = Py_BuildValue("[O]", fullpath); |
| 384 | Py_DECREF(fullpath); |
| 385 | if (pkgpath == NULL) |
| 386 | goto error; |
| 387 | err = PyDict_SetItemString(dict, "__path__", pkgpath); |
| 388 | Py_DECREF(pkgpath); |
| 389 | if (err != 0) |
| 390 | goto error; |
| 391 | } |
| 392 | mod = PyImport_ExecCodeModuleEx(fullname, code, modpath); |
| 393 | Py_DECREF(code); |
| 394 | if (Py_VerboseFlag) |
| 395 | PySys_WriteStderr("import %s # loaded from Zip %s\n", |
| 396 | fullname, modpath); |
| 397 | return mod; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 398 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 399 | Py_DECREF(code); |
| 400 | Py_DECREF(mod); |
| 401 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Nick Coghlan | a205347 | 2008-12-14 10:54:50 +0000 | [diff] [blame] | 404 | /* Return a string matching __file__ for the named module */ |
| 405 | static PyObject * |
| 406 | zipimporter_get_filename(PyObject *obj, PyObject *args) |
| 407 | { |
| 408 | ZipImporter *self = (ZipImporter *)obj; |
| 409 | PyObject *code; |
| 410 | char *fullname, *modpath; |
| 411 | int ispackage; |
| 412 | |
Nick Coghlan | 0194f5b | 2009-02-08 03:17:00 +0000 | [diff] [blame] | 413 | if (!PyArg_ParseTuple(args, "s:zipimporter.get_filename", |
Nick Coghlan | a205347 | 2008-12-14 10:54:50 +0000 | [diff] [blame] | 414 | &fullname)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 415 | return NULL; |
Nick Coghlan | a205347 | 2008-12-14 10:54:50 +0000 | [diff] [blame] | 416 | |
| 417 | /* Deciding the filename requires working out where the code |
| 418 | would come from if the module was actually loaded */ |
| 419 | code = get_module_code(self, fullname, &ispackage, &modpath); |
| 420 | if (code == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 421 | return NULL; |
Nick Coghlan | a205347 | 2008-12-14 10:54:50 +0000 | [diff] [blame] | 422 | Py_DECREF(code); /* Only need the path info */ |
| 423 | |
| 424 | return PyString_FromString(modpath); |
| 425 | } |
| 426 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 427 | /* Return a bool signifying whether the module is a package or not. */ |
| 428 | static PyObject * |
| 429 | zipimporter_is_package(PyObject *obj, PyObject *args) |
| 430 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 431 | ZipImporter *self = (ZipImporter *)obj; |
| 432 | char *fullname; |
| 433 | enum zi_module_info mi; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 435 | if (!PyArg_ParseTuple(args, "s:zipimporter.is_package", |
| 436 | &fullname)) |
| 437 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 438 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 439 | mi = get_module_info(self, fullname); |
| 440 | if (mi == MI_ERROR) |
| 441 | return NULL; |
| 442 | if (mi == MI_NOT_FOUND) { |
| 443 | PyErr_Format(ZipImportError, "can't find module '%.200s'", |
| 444 | fullname); |
| 445 | return NULL; |
| 446 | } |
| 447 | return PyBool_FromLong(mi == MI_PACKAGE); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static PyObject * |
| 451 | zipimporter_get_data(PyObject *obj, PyObject *args) |
| 452 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 453 | ZipImporter *self = (ZipImporter *)obj; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 454 | char *path, *archive; |
| 455 | FILE *fp; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 456 | #ifdef ALTSEP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 457 | char *p, buf[MAXPATHLEN + 1]; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 458 | #endif |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 459 | PyObject *toc_entry, *data; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 460 | Py_ssize_t len; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 462 | if (!PyArg_ParseTuple(args, "s:zipimporter.get_data", &path)) |
| 463 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 464 | |
| 465 | #ifdef ALTSEP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 466 | if (strlen(path) >= MAXPATHLEN) { |
| 467 | PyErr_SetString(ZipImportError, "path too long"); |
| 468 | return NULL; |
| 469 | } |
| 470 | strcpy(buf, path); |
| 471 | for (p = buf; *p; p++) { |
| 472 | if (*p == ALTSEP) |
| 473 | *p = SEP; |
| 474 | } |
| 475 | path = buf; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 476 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 477 | len = PyString_Size(self->archive); |
| 478 | if ((size_t)len < strlen(path) && |
| 479 | strncmp(path, PyString_AsString(self->archive), len) == 0 && |
| 480 | path[len] == SEP) { |
| 481 | path = path + len + 1; |
| 482 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 483 | |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 484 | fp = safely_reopen_archive(self, &archive); |
| 485 | if (fp == NULL) |
| 486 | return NULL; |
| 487 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 488 | toc_entry = PyDict_GetItemString(self->files, path); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 489 | if (toc_entry == NULL) { |
| 490 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, path); |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 491 | fclose(fp); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 494 | data = get_data(fp, archive, toc_entry); |
| 495 | fclose(fp); |
| 496 | return data; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static PyObject * |
| 500 | zipimporter_get_code(PyObject *obj, PyObject *args) |
| 501 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 502 | ZipImporter *self = (ZipImporter *)obj; |
| 503 | char *fullname; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 505 | if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname)) |
| 506 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 508 | return get_module_code(self, fullname, NULL, NULL); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static PyObject * |
| 512 | zipimporter_get_source(PyObject *obj, PyObject *args) |
| 513 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 514 | ZipImporter *self = (ZipImporter *)obj; |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 515 | PyObject *toc_entry; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 516 | FILE *fp; |
| 517 | char *fullname, *subname, path[MAXPATHLEN+1], *archive; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 518 | int len; |
| 519 | enum zi_module_info mi; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 521 | if (!PyArg_ParseTuple(args, "s:zipimporter.get_source", &fullname)) |
| 522 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 523 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 524 | mi = get_module_info(self, fullname); |
| 525 | if (mi == MI_ERROR) |
| 526 | return NULL; |
| 527 | if (mi == MI_NOT_FOUND) { |
| 528 | PyErr_Format(ZipImportError, "can't find module '%.200s'", |
| 529 | fullname); |
| 530 | return NULL; |
| 531 | } |
| 532 | subname = get_subname(fullname); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 534 | len = make_filename(PyString_AsString(self->prefix), subname, path); |
| 535 | if (len < 0) |
| 536 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 538 | if (mi == MI_PACKAGE) { |
| 539 | path[len] = SEP; |
| 540 | strcpy(path + len + 1, "__init__.py"); |
| 541 | } |
| 542 | else |
| 543 | strcpy(path + len, ".py"); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 544 | |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 545 | fp = safely_reopen_archive(self, &archive); |
| 546 | if (fp == NULL) |
| 547 | return NULL; |
| 548 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 549 | toc_entry = PyDict_GetItemString(self->files, path); |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 550 | if (toc_entry != NULL) { |
| 551 | PyObject *data = get_data(fp, archive, toc_entry); |
| 552 | fclose(fp); |
| 553 | return data; |
| 554 | } |
| 555 | fclose(fp); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 556 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 557 | /* we have the module, but no source */ |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 558 | Py_RETURN_NONE; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | PyDoc_STRVAR(doc_find_module, |
| 562 | "find_module(fullname, path=None) -> self or None.\n\ |
| 563 | \n\ |
| 564 | Search for a module specified by 'fullname'. 'fullname' must be the\n\ |
| 565 | fully qualified (dotted) module name. It returns the zipimporter\n\ |
| 566 | instance itself if the module was found, or None if it wasn't.\n\ |
| 567 | The optional 'path' argument is ignored -- it's there for compatibility\n\ |
| 568 | with the importer protocol."); |
| 569 | |
| 570 | PyDoc_STRVAR(doc_load_module, |
| 571 | "load_module(fullname) -> module.\n\ |
| 572 | \n\ |
| 573 | Load the module specified by 'fullname'. 'fullname' must be the\n\ |
| 574 | fully qualified (dotted) module name. It returns the imported\n\ |
| 575 | module, or raises ZipImportError if it wasn't found."); |
| 576 | |
| 577 | PyDoc_STRVAR(doc_get_data, |
| 578 | "get_data(pathname) -> string with file data.\n\ |
| 579 | \n\ |
| 580 | Return the data associated with 'pathname'. Raise IOError if\n\ |
| 581 | the file wasn't found."); |
| 582 | |
| 583 | PyDoc_STRVAR(doc_is_package, |
| 584 | "is_package(fullname) -> bool.\n\ |
| 585 | \n\ |
| 586 | Return True if the module specified by fullname is a package.\n\ |
Brian Curtin | 13b43e7 | 2010-07-21 01:35:46 +0000 | [diff] [blame] | 587 | Raise ZipImportError if the module couldn't be found."); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 588 | |
| 589 | PyDoc_STRVAR(doc_get_code, |
| 590 | "get_code(fullname) -> code object.\n\ |
| 591 | \n\ |
| 592 | Return the code object for the specified module. Raise ZipImportError\n\ |
Brian Curtin | 13b43e7 | 2010-07-21 01:35:46 +0000 | [diff] [blame] | 593 | if the module couldn't be found."); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 594 | |
| 595 | PyDoc_STRVAR(doc_get_source, |
| 596 | "get_source(fullname) -> source string.\n\ |
| 597 | \n\ |
| 598 | Return the source code for the specified module. Raise ZipImportError\n\ |
Brian Curtin | 13b43e7 | 2010-07-21 01:35:46 +0000 | [diff] [blame] | 599 | if the module couldn't be found, return None if the archive does\n\ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 600 | contain the module, but has no source for it."); |
| 601 | |
Nick Coghlan | a205347 | 2008-12-14 10:54:50 +0000 | [diff] [blame] | 602 | |
| 603 | PyDoc_STRVAR(doc_get_filename, |
Nick Coghlan | 0194f5b | 2009-02-08 03:17:00 +0000 | [diff] [blame] | 604 | "get_filename(fullname) -> filename string.\n\ |
Nick Coghlan | a205347 | 2008-12-14 10:54:50 +0000 | [diff] [blame] | 605 | \n\ |
| 606 | Return the filename for the specified module."); |
| 607 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 608 | static PyMethodDef zipimporter_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 609 | {"find_module", zipimporter_find_module, METH_VARARGS, |
| 610 | doc_find_module}, |
| 611 | {"load_module", zipimporter_load_module, METH_VARARGS, |
| 612 | doc_load_module}, |
| 613 | {"get_data", zipimporter_get_data, METH_VARARGS, |
| 614 | doc_get_data}, |
| 615 | {"get_code", zipimporter_get_code, METH_VARARGS, |
| 616 | doc_get_code}, |
| 617 | {"get_source", zipimporter_get_source, METH_VARARGS, |
| 618 | doc_get_source}, |
| 619 | {"get_filename", zipimporter_get_filename, METH_VARARGS, |
| 620 | doc_get_filename}, |
| 621 | {"is_package", zipimporter_is_package, METH_VARARGS, |
| 622 | doc_is_package}, |
| 623 | {NULL, NULL} /* sentinel */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | static PyMemberDef zipimporter_members[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 627 | {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY}, |
| 628 | {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY}, |
| 629 | {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY}, |
| 630 | {NULL} |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | PyDoc_STRVAR(zipimporter_doc, |
| 634 | "zipimporter(archivepath) -> zipimporter object\n\ |
| 635 | \n\ |
| 636 | Create a new zipimporter instance. 'archivepath' must be a path to\n\ |
Georg Brandl | 6a57c08 | 2008-05-11 15:05:13 +0000 | [diff] [blame] | 637 | a zipfile, or to a specific path inside a zipfile. For example, it can be\n\ |
| 638 | '/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\ |
| 639 | valid directory inside the archive.\n\ |
| 640 | \n\ |
| 641 | 'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\ |
| 642 | archive.\n\ |
| 643 | \n\ |
| 644 | The 'archive' attribute of zipimporter objects contains the name of the\n\ |
| 645 | zipfile targeted."); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 646 | |
| 647 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 648 | |
| 649 | static PyTypeObject ZipImporter_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 650 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 651 | "zipimport.zipimporter", |
| 652 | sizeof(ZipImporter), |
| 653 | 0, /* tp_itemsize */ |
| 654 | (destructor)zipimporter_dealloc, /* tp_dealloc */ |
| 655 | 0, /* tp_print */ |
| 656 | 0, /* tp_getattr */ |
| 657 | 0, /* tp_setattr */ |
| 658 | 0, /* tp_compare */ |
| 659 | (reprfunc)zipimporter_repr, /* tp_repr */ |
| 660 | 0, /* tp_as_number */ |
| 661 | 0, /* tp_as_sequence */ |
| 662 | 0, /* tp_as_mapping */ |
| 663 | 0, /* tp_hash */ |
| 664 | 0, /* tp_call */ |
| 665 | 0, /* tp_str */ |
| 666 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 667 | 0, /* tp_setattro */ |
| 668 | 0, /* tp_as_buffer */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 669 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 670 | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 671 | zipimporter_doc, /* tp_doc */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 672 | zipimporter_traverse, /* tp_traverse */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 673 | 0, /* tp_clear */ |
| 674 | 0, /* tp_richcompare */ |
| 675 | 0, /* tp_weaklistoffset */ |
| 676 | 0, /* tp_iter */ |
| 677 | 0, /* tp_iternext */ |
| 678 | zipimporter_methods, /* tp_methods */ |
| 679 | zipimporter_members, /* tp_members */ |
| 680 | 0, /* tp_getset */ |
| 681 | 0, /* tp_base */ |
| 682 | 0, /* tp_dict */ |
| 683 | 0, /* tp_descr_get */ |
| 684 | 0, /* tp_descr_set */ |
| 685 | 0, /* tp_dictoffset */ |
| 686 | (initproc)zipimporter_init, /* tp_init */ |
| 687 | PyType_GenericAlloc, /* tp_alloc */ |
| 688 | PyType_GenericNew, /* tp_new */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 689 | PyObject_GC_Del, /* tp_free */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 690 | }; |
| 691 | |
| 692 | |
| 693 | /* implementation */ |
| 694 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 695 | /* Given a buffer, return the long that is represented by the first |
| 696 | 4 bytes, encoded as little endian. This partially reimplements |
| 697 | marshal.c:r_long() */ |
| 698 | static long |
| 699 | get_long(unsigned char *buf) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 700 | long x; |
| 701 | x = buf[0]; |
| 702 | x |= (long)buf[1] << 8; |
| 703 | x |= (long)buf[2] << 16; |
| 704 | x |= (long)buf[3] << 24; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 705 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 706 | /* Sign extension for 64-bit machines */ |
| 707 | x |= -(x & 0x80000000L); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 708 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 709 | return x; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 713 | fopen_rb_and_stat(path, &py_stat) -> FILE * |
| 714 | |
| 715 | Opens path in "rb" mode and populates the Python py_stat stat_result |
| 716 | with information about the opened file. *py_stat may not be changed |
| 717 | if there is no fstat_function or if fstat_function fails. |
| 718 | |
| 719 | Returns NULL and does nothing to *py_stat if the open failed. |
| 720 | */ |
| 721 | static FILE * |
| 722 | fopen_rb_and_stat(char *path, PyObject **py_stat_p) |
| 723 | { |
| 724 | FILE *fp; |
| 725 | assert(py_stat_p != NULL); |
| 726 | assert(*py_stat_p == NULL); |
| 727 | |
| 728 | fp = fopen(path, "rb"); |
| 729 | if (fp == NULL) { |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | if (fstat_function) { |
| 734 | PyObject *stat_result = PyObject_CallFunction(fstat_function, |
| 735 | "i", fileno(fp)); |
| 736 | if (stat_result == NULL) { |
| 737 | PyErr_Clear(); /* We can function without it. */ |
| 738 | } else { |
| 739 | *py_stat_p = stat_result; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | return fp; |
| 744 | } |
| 745 | |
| 746 | /* Return 1 if objects a and b fail a Py_EQ test for an attr. */ |
| 747 | static int |
| 748 | compare_obj_attr_strings(PyObject *obj_a, PyObject *obj_b, char *attr_name) |
| 749 | { |
| 750 | int problem = 0; |
| 751 | PyObject *attr_a = PyObject_GetAttrString(obj_a, attr_name); |
| 752 | PyObject *attr_b = PyObject_GetAttrString(obj_b, attr_name); |
| 753 | if (attr_a == NULL || attr_b == NULL) |
| 754 | problem = 1; |
| 755 | else |
| 756 | problem = (PyObject_RichCompareBool(attr_a, attr_b, Py_EQ) != 1); |
| 757 | Py_XDECREF(attr_a); |
| 758 | Py_XDECREF(attr_b); |
| 759 | return problem; |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Returns an open FILE * on success and sets *archive_p to point to |
| 764 | * a read only C string representation of the archive name (as a |
| 765 | * convenience for use in error messages). |
| 766 | * |
| 767 | * Returns NULL on error with the Python error context set. |
| 768 | */ |
| 769 | static FILE * |
| 770 | safely_reopen_archive(ZipImporter *self, char **archive_p) |
| 771 | { |
| 772 | FILE *fp; |
| 773 | PyObject *stat_now = NULL; |
| 774 | char *archive; |
| 775 | |
| 776 | assert(archive_p != NULL); |
| 777 | *archive_p = PyString_AsString(self->archive); |
| 778 | if (*archive_p == NULL) |
| 779 | return NULL; |
| 780 | archive = *archive_p; |
| 781 | |
| 782 | fp = fopen_rb_and_stat(archive, &stat_now); |
| 783 | if (!fp) { |
| 784 | PyErr_Format(PyExc_IOError, |
| 785 | "zipimport: can not open file %s", archive); |
| 786 | Py_XDECREF(stat_now); |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | if (stat_now != NULL) { |
| 791 | int problem = 0; |
| 792 | PyObject *files; |
| 793 | PyObject *prev_stat = PyDict_GetItemString(zip_stat_cache, archive); |
| 794 | /* Test stat_now vs the old cached stat on some key attributes. */ |
| 795 | if (prev_stat != NULL) { |
| 796 | problem = compare_obj_attr_strings(prev_stat, stat_now, |
| 797 | "st_ino"); |
| 798 | problem |= compare_obj_attr_strings(prev_stat, stat_now, |
| 799 | "st_size"); |
| 800 | problem |= compare_obj_attr_strings(prev_stat, stat_now, |
| 801 | "st_mtime"); |
| 802 | } else { |
| 803 | if (Py_VerboseFlag) |
| 804 | PySys_WriteStderr("# zipimport: no stat data for %s!\n", |
| 805 | archive); |
| 806 | problem = 1; |
| 807 | } |
| 808 | |
| 809 | if (problem) { |
| 810 | if (Py_VerboseFlag) |
| 811 | PySys_WriteStderr("# zipimport: %s modified since last" |
| 812 | " import, rereading TOC.\n", archive); |
| 813 | files = read_directory(fp, archive); |
| 814 | if (files == NULL) { |
| 815 | Py_DECREF(stat_now); |
| 816 | fclose(fp); |
| 817 | return NULL; |
| 818 | } |
| 819 | if (PyDict_SetItem(zip_directory_cache, self->archive, |
| 820 | files) != 0) { |
| 821 | Py_DECREF(files); |
| 822 | Py_DECREF(stat_now); |
| 823 | fclose(fp); |
| 824 | return NULL; |
| 825 | } |
| 826 | if (stat_now && PyDict_SetItem(zip_stat_cache, self->archive, |
| 827 | stat_now) != 0) { |
| 828 | Py_DECREF(files); |
| 829 | Py_DECREF(stat_now); |
| 830 | fclose(fp); |
| 831 | return NULL; |
| 832 | } |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 833 | Py_XDECREF(self->files); /* free the old value. */ |
| 834 | self->files = files; |
| 835 | } else { |
| 836 | /* No problem, discard the new stat data. */ |
| 837 | Py_DECREF(stat_now); |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 838 | } |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 839 | } /* stat succeeded */ |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 840 | |
| 841 | return fp; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | read_directory(fp, archive) -> files dict (new reference) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 846 | |
| 847 | Given a path to a Zip archive, build a dict, mapping file names |
| 848 | (local to the archive, using SEP as a separator) to toc entries. |
| 849 | |
| 850 | A toc_entry is a tuple: |
| 851 | |
Fred Drake | f5b7fd2 | 2005-11-11 19:34:56 +0000 | [diff] [blame] | 852 | (__file__, # value to use for __file__, available for all files |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 853 | compress, # compression kind; 0 for uncompressed |
| 854 | data_size, # size of compressed data on disk |
| 855 | file_size, # size of decompressed data |
| 856 | file_offset, # offset of file header from start of archive |
| 857 | time, # mod time of file (in dos format) |
| 858 | date, # mod data of file (in dos format) |
| 859 | crc, # crc checksum of the data |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 860 | ) |
| 861 | |
| 862 | Directories can be recognized by the trailing SEP in the name, |
| 863 | data_size and file_offset are 0. |
| 864 | */ |
| 865 | static PyObject * |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 866 | read_directory(FILE *fp, char *archive) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 867 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 868 | PyObject *files = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 869 | long compress, crc, data_size, file_size, file_offset, date, time; |
| 870 | long header_offset, name_size, header_size, header_position; |
| 871 | long i, l, count; |
| 872 | size_t length; |
| 873 | char path[MAXPATHLEN + 5]; |
| 874 | char name[MAXPATHLEN + 5]; |
| 875 | char *p, endof_central_dir[22]; |
| 876 | long arc_offset; /* offset from beginning of file to start of zip-archive */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 877 | |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 878 | assert(fp != NULL); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 879 | if (strlen(archive) > MAXPATHLEN) { |
| 880 | PyErr_SetString(PyExc_OverflowError, |
| 881 | "Zip path name is too long"); |
| 882 | return NULL; |
| 883 | } |
| 884 | strcpy(path, archive); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 885 | |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 886 | if (fseek(fp, -22, SEEK_END) == -1) { |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 887 | PyErr_Format(ZipImportError, "can't read Zip file: %s", archive); |
| 888 | return NULL; |
| 889 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 890 | header_position = ftell(fp); |
| 891 | if (fread(endof_central_dir, 1, 22, fp) != 22) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 892 | PyErr_Format(ZipImportError, "can't read Zip file: " |
| 893 | "'%.200s'", archive); |
| 894 | return NULL; |
| 895 | } |
| 896 | if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) { |
| 897 | /* Bad: End of Central Dir signature */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 898 | PyErr_Format(ZipImportError, "not a Zip file: " |
| 899 | "'%.200s'", archive); |
| 900 | return NULL; |
| 901 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 903 | header_size = get_long((unsigned char *)endof_central_dir + 12); |
| 904 | header_offset = get_long((unsigned char *)endof_central_dir + 16); |
| 905 | arc_offset = header_position - header_offset - header_size; |
| 906 | header_offset += arc_offset; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 908 | files = PyDict_New(); |
| 909 | if (files == NULL) |
| 910 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 912 | length = (long)strlen(path); |
| 913 | path[length] = SEP; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 915 | /* Start of Central Directory */ |
| 916 | count = 0; |
| 917 | for (;;) { |
| 918 | PyObject *t; |
| 919 | int err; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 920 | |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 921 | if (fseek(fp, header_offset, 0) == -1) /* Start of file header */ |
| 922 | goto fseek_error; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 923 | l = PyMarshal_ReadLongFromFile(fp); |
| 924 | if (l != 0x02014B50) |
| 925 | break; /* Bad: Central Dir File Header */ |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 926 | if (fseek(fp, header_offset + 10, 0) == -1) |
| 927 | goto fseek_error; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 928 | compress = PyMarshal_ReadShortFromFile(fp); |
| 929 | time = PyMarshal_ReadShortFromFile(fp); |
| 930 | date = PyMarshal_ReadShortFromFile(fp); |
| 931 | crc = PyMarshal_ReadLongFromFile(fp); |
| 932 | data_size = PyMarshal_ReadLongFromFile(fp); |
| 933 | file_size = PyMarshal_ReadLongFromFile(fp); |
| 934 | name_size = PyMarshal_ReadShortFromFile(fp); |
| 935 | header_size = 46 + name_size + |
| 936 | PyMarshal_ReadShortFromFile(fp) + |
| 937 | PyMarshal_ReadShortFromFile(fp); |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 938 | if (fseek(fp, header_offset + 42, 0) == -1) |
| 939 | goto fseek_error; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 940 | file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset; |
| 941 | if (name_size > MAXPATHLEN) |
| 942 | name_size = MAXPATHLEN; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 943 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 944 | p = name; |
| 945 | for (i = 0; i < name_size; i++) { |
| 946 | *p = (char)getc(fp); |
| 947 | if (*p == '/') |
| 948 | *p = SEP; |
| 949 | p++; |
| 950 | } |
| 951 | *p = 0; /* Add terminating null byte */ |
| 952 | header_offset += header_size; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 953 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 954 | strncpy(path + length + 1, name, MAXPATHLEN - length - 1); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 956 | t = Py_BuildValue("siiiiiii", path, compress, data_size, |
| 957 | file_size, file_offset, time, date, crc); |
| 958 | if (t == NULL) |
| 959 | goto error; |
| 960 | err = PyDict_SetItemString(files, name, t); |
| 961 | Py_DECREF(t); |
| 962 | if (err != 0) |
| 963 | goto error; |
| 964 | count++; |
| 965 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 966 | if (Py_VerboseFlag) |
| 967 | PySys_WriteStderr("# zipimport: found %ld names in %s\n", |
| 968 | count, archive); |
| 969 | return files; |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 970 | fseek_error: |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 971 | Py_XDECREF(files); |
| 972 | PyErr_Format(ZipImportError, "can't read Zip file: %s", archive); |
| 973 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 974 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 975 | Py_XDECREF(files); |
| 976 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* Return the zlib.decompress function object, or NULL if zlib couldn't |
| 980 | be imported. The function is cached when found, so subsequent calls |
Victor Stinner | f58f1c3 | 2011-05-21 02:13:22 +0200 | [diff] [blame] | 981 | don't import zlib again. */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 982 | static PyObject * |
| 983 | get_decompress_func(void) |
| 984 | { |
Victor Stinner | f58f1c3 | 2011-05-21 02:13:22 +0200 | [diff] [blame] | 985 | static int importing_zlib = 0; |
| 986 | PyObject *zlib; |
| 987 | PyObject *decompress; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 988 | |
Victor Stinner | f58f1c3 | 2011-05-21 02:13:22 +0200 | [diff] [blame] | 989 | if (importing_zlib != 0) |
| 990 | /* Someone has a zlib.py[co] in their Zip file; |
| 991 | let's avoid a stack overflow. */ |
| 992 | return NULL; |
| 993 | importing_zlib = 1; |
| 994 | zlib = PyImport_ImportModuleNoBlock("zlib"); |
| 995 | importing_zlib = 0; |
| 996 | if (zlib != NULL) { |
| 997 | decompress = PyObject_GetAttrString(zlib, |
| 998 | "decompress"); |
| 999 | Py_DECREF(zlib); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1000 | } |
Victor Stinner | f58f1c3 | 2011-05-21 02:13:22 +0200 | [diff] [blame] | 1001 | else { |
| 1002 | PyErr_Clear(); |
| 1003 | decompress = NULL; |
| 1004 | } |
| 1005 | if (Py_VerboseFlag) |
| 1006 | PySys_WriteStderr("# zipimport: zlib %s\n", |
| 1007 | zlib != NULL ? "available": "UNAVAILABLE"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1008 | return decompress; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1011 | /* Given a FILE* to a Zip file and a toc_entry, return the (uncompressed) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1012 | data as a new reference. */ |
| 1013 | static PyObject * |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1014 | get_data(FILE *fp, char *archive, PyObject *toc_entry) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1015 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1016 | PyObject *raw_data, *data = NULL, *decompress; |
| 1017 | char *buf; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1018 | int err; |
| 1019 | Py_ssize_t bytes_read = 0; |
| 1020 | long l; |
| 1021 | char *datapath; |
| 1022 | long compress, data_size, file_size, file_offset; |
| 1023 | long time, date, crc; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1025 | if (!PyArg_ParseTuple(toc_entry, "slllllll", &datapath, &compress, |
| 1026 | &data_size, &file_size, &file_offset, &time, |
| 1027 | &date, &crc)) { |
| 1028 | return NULL; |
| 1029 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1031 | /* Check to make sure the local file header is correct */ |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 1032 | if (fseek(fp, file_offset, 0) == -1) { |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 1033 | PyErr_Format(ZipImportError, "can't read Zip file: %s", archive); |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1037 | l = PyMarshal_ReadLongFromFile(fp); |
| 1038 | if (l != 0x04034B50) { |
| 1039 | /* Bad: Local File Header */ |
| 1040 | PyErr_Format(ZipImportError, |
| 1041 | "bad local file header in %s", |
| 1042 | archive); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1043 | return NULL; |
| 1044 | } |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 1045 | if (fseek(fp, file_offset + 26, 0) == -1) { |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 1046 | PyErr_Format(ZipImportError, "can't read Zip file: %s", archive); |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1050 | l = 30 + PyMarshal_ReadShortFromFile(fp) + |
| 1051 | PyMarshal_ReadShortFromFile(fp); /* local header size */ |
| 1052 | file_offset += l; /* Start of file data */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1053 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1054 | raw_data = PyString_FromStringAndSize((char *)NULL, compress == 0 ? |
| 1055 | data_size : data_size + 1); |
| 1056 | if (raw_data == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1057 | return NULL; |
| 1058 | } |
| 1059 | buf = PyString_AsString(raw_data); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1060 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1061 | err = fseek(fp, file_offset, 0); |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 1062 | if (err == 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1063 | bytes_read = fread(buf, 1, data_size, fp); |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 1064 | } else { |
Jesus Cea | e884be6 | 2012-10-03 02:13:05 +0200 | [diff] [blame] | 1065 | PyErr_Format(ZipImportError, "can't read Zip file: %s", archive); |
| 1066 | return NULL; |
| 1067 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1068 | if (err || bytes_read != data_size) { |
| 1069 | PyErr_SetString(PyExc_IOError, |
| 1070 | "zipimport: can't read data"); |
| 1071 | Py_DECREF(raw_data); |
| 1072 | return NULL; |
| 1073 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1075 | if (compress != 0) { |
| 1076 | buf[data_size] = 'Z'; /* saw this in zipfile.py */ |
| 1077 | data_size++; |
| 1078 | } |
| 1079 | buf[data_size] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1080 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1081 | if (compress == 0) /* data is not compressed */ |
| 1082 | return raw_data; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1084 | /* Decompress with zlib */ |
| 1085 | decompress = get_decompress_func(); |
| 1086 | if (decompress == NULL) { |
| 1087 | PyErr_SetString(ZipImportError, |
| 1088 | "can't decompress data; " |
| 1089 | "zlib not available"); |
| 1090 | goto error; |
| 1091 | } |
| 1092 | data = PyObject_CallFunction(decompress, "Oi", raw_data, -15); |
Victor Stinner | f58f1c3 | 2011-05-21 02:13:22 +0200 | [diff] [blame] | 1093 | Py_DECREF(decompress); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1094 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1095 | Py_DECREF(raw_data); |
| 1096 | return data; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* Lenient date/time comparison function. The precision of the mtime |
| 1100 | in the archive is lower than the mtime stored in a .pyc: we |
| 1101 | must allow a difference of at most one second. */ |
| 1102 | static int |
| 1103 | eq_mtime(time_t t1, time_t t2) |
| 1104 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1105 | time_t d = t1 - t2; |
| 1106 | if (d < 0) |
| 1107 | d = -d; |
| 1108 | /* dostime only stores even seconds, so be lenient */ |
| 1109 | return d <= 1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* Given the contents of a .py[co] file in a buffer, unmarshal the data |
| 1113 | and return the code object. Return None if it the magic word doesn't |
| 1114 | match (we do this instead of raising an exception as we fall back |
| 1115 | to .py if available and we don't want to mask other errors). |
| 1116 | Returns a new reference. */ |
| 1117 | static PyObject * |
| 1118 | unmarshal_code(char *pathname, PyObject *data, time_t mtime) |
| 1119 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1120 | PyObject *code; |
| 1121 | char *buf = PyString_AsString(data); |
| 1122 | Py_ssize_t size = PyString_Size(data); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1123 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1124 | if (size <= 9) { |
| 1125 | PyErr_SetString(ZipImportError, |
| 1126 | "bad pyc data"); |
| 1127 | return NULL; |
| 1128 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1130 | if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) { |
| 1131 | if (Py_VerboseFlag) |
| 1132 | PySys_WriteStderr("# %s has bad magic\n", |
| 1133 | pathname); |
| 1134 | Py_INCREF(Py_None); |
| 1135 | return Py_None; /* signal caller to try alternative */ |
| 1136 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1138 | if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4), |
| 1139 | mtime)) { |
| 1140 | if (Py_VerboseFlag) |
| 1141 | PySys_WriteStderr("# %s has bad mtime\n", |
| 1142 | pathname); |
| 1143 | Py_INCREF(Py_None); |
| 1144 | return Py_None; /* signal caller to try alternative */ |
| 1145 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1147 | code = PyMarshal_ReadObjectFromString(buf + 8, size - 8); |
| 1148 | if (code == NULL) |
| 1149 | return NULL; |
| 1150 | if (!PyCode_Check(code)) { |
| 1151 | Py_DECREF(code); |
| 1152 | PyErr_Format(PyExc_TypeError, |
| 1153 | "compiled module %.200s is not a code object", |
| 1154 | pathname); |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | return code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | /* Replace any occurances of "\r\n?" in the input string with "\n". |
| 1161 | This converts DOS and Mac line endings to Unix line endings. |
| 1162 | Also append a trailing "\n" to be compatible with |
| 1163 | PyParser_SimpleParseFile(). Returns a new reference. */ |
| 1164 | static PyObject * |
| 1165 | normalize_line_endings(PyObject *source) |
| 1166 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1167 | char *buf, *q, *p = PyString_AsString(source); |
| 1168 | PyObject *fixed_source; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1170 | if (!p) |
| 1171 | return NULL; |
Neal Norwitz | ee7c8f9 | 2006-08-13 18:12:03 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1173 | /* one char extra for trailing \n and one for terminating \0 */ |
| 1174 | buf = (char *)PyMem_Malloc(PyString_Size(source) + 2); |
| 1175 | if (buf == NULL) { |
| 1176 | PyErr_SetString(PyExc_MemoryError, |
| 1177 | "zipimport: no memory to allocate " |
| 1178 | "source buffer"); |
| 1179 | return NULL; |
| 1180 | } |
| 1181 | /* replace "\r\n?" by "\n" */ |
| 1182 | for (q = buf; *p != '\0'; p++) { |
| 1183 | if (*p == '\r') { |
| 1184 | *q++ = '\n'; |
| 1185 | if (*(p + 1) == '\n') |
| 1186 | p++; |
| 1187 | } |
| 1188 | else |
| 1189 | *q++ = *p; |
| 1190 | } |
| 1191 | *q++ = '\n'; /* add trailing \n */ |
| 1192 | *q = '\0'; |
| 1193 | fixed_source = PyString_FromString(buf); |
| 1194 | PyMem_Free(buf); |
| 1195 | return fixed_source; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* Given a string buffer containing Python source code, compile it |
| 1199 | return and return a code object as a new reference. */ |
| 1200 | static PyObject * |
| 1201 | compile_source(char *pathname, PyObject *source) |
| 1202 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1203 | PyObject *code, *fixed_source; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1205 | fixed_source = normalize_line_endings(source); |
| 1206 | if (fixed_source == NULL) |
| 1207 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1209 | code = Py_CompileString(PyString_AsString(fixed_source), pathname, |
| 1210 | Py_file_input); |
| 1211 | Py_DECREF(fixed_source); |
| 1212 | return code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | /* Convert the date/time values found in the Zip archive to a value |
| 1216 | that's compatible with the time stamp stored in .pyc files. */ |
Neal Norwitz | 29fd2ba | 2003-03-23 13:21:03 +0000 | [diff] [blame] | 1217 | static time_t |
| 1218 | parse_dostime(int dostime, int dosdate) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1219 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1220 | struct tm stm; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1222 | memset((void *) &stm, '\0', sizeof(stm)); |
Christian Heimes | 62a8e95 | 2008-01-18 07:30:20 +0000 | [diff] [blame] | 1223 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1224 | stm.tm_sec = (dostime & 0x1f) * 2; |
| 1225 | stm.tm_min = (dostime >> 5) & 0x3f; |
| 1226 | stm.tm_hour = (dostime >> 11) & 0x1f; |
| 1227 | stm.tm_mday = dosdate & 0x1f; |
| 1228 | stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1; |
| 1229 | stm.tm_year = ((dosdate >> 9) & 0x7f) + 80; |
| 1230 | stm.tm_isdst = -1; /* wday/yday is ignored */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1231 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1232 | return mktime(&stm); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /* Given a path to a .pyc or .pyo file in the archive, return the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 1236 | modification time of the matching .py file, or 0 if no source |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1237 | is available. */ |
| 1238 | static time_t |
| 1239 | get_mtime_of_source(ZipImporter *self, char *path) |
| 1240 | { |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1241 | PyObject *toc_entry; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1242 | time_t mtime = 0; |
| 1243 | Py_ssize_t lastchar = strlen(path) - 1; |
| 1244 | char savechar = path[lastchar]; |
| 1245 | path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */ |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1246 | toc_entry = PyDict_GetItemString(self->files, path); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1247 | if (toc_entry != NULL && PyTuple_Check(toc_entry) && |
| 1248 | PyTuple_Size(toc_entry) == 8) { |
| 1249 | /* fetch the time stamp of the .py file for comparison |
| 1250 | with an embedded pyc time stamp */ |
| 1251 | int time, date; |
| 1252 | time = PyInt_AsLong(PyTuple_GetItem(toc_entry, 5)); |
| 1253 | date = PyInt_AsLong(PyTuple_GetItem(toc_entry, 6)); |
| 1254 | mtime = parse_dostime(time, date); |
| 1255 | } |
| 1256 | path[lastchar] = savechar; |
| 1257 | return mtime; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /* Return the code object for the module named by 'fullname' from the |
| 1261 | Zip archive as a new reference. */ |
| 1262 | static PyObject * |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1263 | get_code_from_data(char *archive, FILE *fp, int ispackage, |
| 1264 | int isbytecode, time_t mtime, PyObject *toc_entry) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1265 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1266 | PyObject *data, *code; |
| 1267 | char *modpath; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1268 | |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1269 | data = get_data(fp, archive, toc_entry); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1270 | if (data == NULL) |
| 1271 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1272 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1273 | modpath = PyString_AsString(PyTuple_GetItem(toc_entry, 0)); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1275 | if (isbytecode) { |
| 1276 | code = unmarshal_code(modpath, data, mtime); |
| 1277 | } |
| 1278 | else { |
| 1279 | code = compile_source(modpath, data); |
| 1280 | } |
| 1281 | Py_DECREF(data); |
| 1282 | return code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Ezio Melotti | 24b07bc | 2011-03-15 18:55:01 +0200 | [diff] [blame] | 1285 | /* Get the code object associated with the module specified by |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1286 | 'fullname'. */ |
| 1287 | static PyObject * |
| 1288 | get_module_code(ZipImporter *self, char *fullname, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1289 | int *p_ispackage, char **p_modpath) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1290 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1291 | PyObject *toc_entry; |
| 1292 | char *subname, path[MAXPATHLEN + 1]; |
| 1293 | int len; |
| 1294 | struct st_zip_searchorder *zso; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1296 | subname = get_subname(fullname); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1298 | len = make_filename(PyString_AsString(self->prefix), subname, path); |
| 1299 | if (len < 0) |
| 1300 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1302 | for (zso = zip_searchorder; *zso->suffix; zso++) { |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1303 | PyObject *code = NULL; |
| 1304 | FILE *fp; |
| 1305 | char *archive; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1306 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1307 | strcpy(path + len, zso->suffix); |
| 1308 | if (Py_VerboseFlag > 1) |
| 1309 | PySys_WriteStderr("# trying %s%c%s\n", |
| 1310 | PyString_AsString(self->archive), |
| 1311 | SEP, path); |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1312 | |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1313 | fp = safely_reopen_archive(self, &archive); |
| 1314 | if (fp == NULL) |
Gregory P. Smith | 027ab39 | 2014-01-27 00:15:10 -0800 | [diff] [blame] | 1315 | return NULL; |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1316 | |
| 1317 | toc_entry = PyDict_GetItemString(self->files, path); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1318 | if (toc_entry != NULL) { |
| 1319 | time_t mtime = 0; |
| 1320 | int ispackage = zso->type & IS_PACKAGE; |
| 1321 | int isbytecode = zso->type & IS_BYTECODE; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1322 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1323 | if (isbytecode) |
| 1324 | mtime = get_mtime_of_source(self, path); |
| 1325 | if (p_ispackage != NULL) |
| 1326 | *p_ispackage = ispackage; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1327 | code = get_code_from_data(archive, fp, ispackage, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1328 | isbytecode, mtime, |
| 1329 | toc_entry); |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1330 | fclose(fp); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1331 | if (code == Py_None) { |
| 1332 | /* bad magic number or non-matching mtime |
| 1333 | in byte code, try next */ |
| 1334 | Py_DECREF(code); |
| 1335 | continue; |
| 1336 | } |
| 1337 | if (code != NULL && p_modpath != NULL) |
| 1338 | *p_modpath = PyString_AsString( |
| 1339 | PyTuple_GetItem(toc_entry, 0)); |
| 1340 | return code; |
| 1341 | } |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1342 | fclose(fp); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1343 | } |
| 1344 | PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname); |
| 1345 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | /* Module init */ |
| 1350 | |
| 1351 | PyDoc_STRVAR(zipimport_doc, |
| 1352 | "zipimport provides support for importing Python modules from Zip archives.\n\ |
| 1353 | \n\ |
| 1354 | This module exports three objects:\n\ |
| 1355 | - zipimporter: a class; its constructor takes a path to a Zip archive.\n\ |
Fredrik Lundh | b84b35f | 2006-01-15 15:00:40 +0000 | [diff] [blame] | 1356 | - ZipImportError: exception raised by zipimporter objects. It's a\n\ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1357 | subclass of ImportError, so it can be caught as ImportError, too.\n\ |
| 1358 | - _zip_directory_cache: a dict, mapping archive paths to zip directory\n\ |
| 1359 | info dicts, as used in zipimporter._files.\n\ |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1360 | - _zip_stat_cache: a dict, mapping archive paths to stat_result\n\ |
| 1361 | info for the .zip the last time anything was imported from it.\n\ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1362 | \n\ |
| 1363 | It is usually not needed to use the zipimport module explicitly; it is\n\ |
| 1364 | used by the builtin import mechanism for sys.path items that are paths\n\ |
| 1365 | to Zip archives."); |
| 1366 | |
| 1367 | PyMODINIT_FUNC |
| 1368 | initzipimport(void) |
| 1369 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1370 | PyObject *mod; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1372 | if (PyType_Ready(&ZipImporter_Type) < 0) |
| 1373 | return; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1374 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1375 | /* Correct directory separator */ |
| 1376 | zip_searchorder[0].suffix[0] = SEP; |
| 1377 | zip_searchorder[1].suffix[0] = SEP; |
| 1378 | zip_searchorder[2].suffix[0] = SEP; |
| 1379 | if (Py_OptimizeFlag) { |
| 1380 | /* Reverse *.pyc and *.pyo */ |
| 1381 | struct st_zip_searchorder tmp; |
| 1382 | tmp = zip_searchorder[0]; |
| 1383 | zip_searchorder[0] = zip_searchorder[1]; |
| 1384 | zip_searchorder[1] = tmp; |
| 1385 | tmp = zip_searchorder[3]; |
| 1386 | zip_searchorder[3] = zip_searchorder[4]; |
| 1387 | zip_searchorder[4] = tmp; |
| 1388 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1390 | mod = Py_InitModule4("zipimport", NULL, zipimport_doc, |
| 1391 | NULL, PYTHON_API_VERSION); |
| 1392 | if (mod == NULL) |
| 1393 | return; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1394 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1395 | ZipImportError = PyErr_NewException("zipimport.ZipImportError", |
| 1396 | PyExc_ImportError, NULL); |
| 1397 | if (ZipImportError == NULL) |
| 1398 | return; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1400 | Py_INCREF(ZipImportError); |
| 1401 | if (PyModule_AddObject(mod, "ZipImportError", |
| 1402 | ZipImportError) < 0) |
| 1403 | return; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1405 | Py_INCREF(&ZipImporter_Type); |
| 1406 | if (PyModule_AddObject(mod, "zipimporter", |
| 1407 | (PyObject *)&ZipImporter_Type) < 0) |
| 1408 | return; |
Just van Rossum | f8b6de1 | 2002-12-31 09:51:59 +0000 | [diff] [blame] | 1409 | |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1410 | Py_XDECREF(zip_directory_cache); /* Avoid embedded interpreter leaks. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1411 | zip_directory_cache = PyDict_New(); |
| 1412 | if (zip_directory_cache == NULL) |
| 1413 | return; |
| 1414 | Py_INCREF(zip_directory_cache); |
| 1415 | if (PyModule_AddObject(mod, "_zip_directory_cache", |
| 1416 | zip_directory_cache) < 0) |
| 1417 | return; |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1418 | |
| 1419 | Py_XDECREF(zip_stat_cache); /* Avoid embedded interpreter leaks. */ |
| 1420 | zip_stat_cache = PyDict_New(); |
| 1421 | if (zip_stat_cache == NULL) |
| 1422 | return; |
| 1423 | Py_INCREF(zip_stat_cache); |
| 1424 | if (PyModule_AddObject(mod, "_zip_stat_cache", zip_stat_cache) < 0) |
| 1425 | return; |
| 1426 | |
| 1427 | { |
| 1428 | /* We cannot import "os" here as that is a .py/.pyc file that could |
| 1429 | * live within a zipped up standard library. Import the posix or nt |
| 1430 | * builtin that provides the fstat() function we want instead. */ |
| 1431 | PyObject *os_like_module; |
Benjamin Peterson | e9aab0f | 2014-02-16 14:20:14 -0500 | [diff] [blame^] | 1432 | Py_XDECREF(fstat_function); /* Avoid embedded interpreter leaks. */ |
Gregory P. Smith | b48c5d5 | 2014-01-06 09:46:46 -0800 | [diff] [blame] | 1433 | os_like_module = PyImport_ImportModule("posix"); |
| 1434 | if (os_like_module == NULL) { |
| 1435 | os_like_module = PyImport_ImportModule("nt"); |
| 1436 | } |
| 1437 | if (os_like_module != NULL) { |
| 1438 | fstat_function = PyObject_GetAttrString(os_like_module, "fstat"); |
| 1439 | Py_DECREF(os_like_module); |
| 1440 | } |
| 1441 | if (fstat_function == NULL) { |
| 1442 | PyErr_Clear(); /* non-fatal, we'll go on without it. */ |
| 1443 | } |
| 1444 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1445 | } |