| 1 | n/a | #include "Python.h" |
|---|
| 2 | n/a | #include "structmember.h" |
|---|
| 3 | n/a | #include "osdefs.h" |
|---|
| 4 | n/a | #include "marshal.h" |
|---|
| 5 | n/a | #include <time.h> |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | #define IS_SOURCE 0x0 |
|---|
| 9 | n/a | #define IS_BYTECODE 0x1 |
|---|
| 10 | n/a | #define IS_PACKAGE 0x2 |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | struct st_zip_searchorder { |
|---|
| 13 | n/a | char suffix[14]; |
|---|
| 14 | n/a | int type; |
|---|
| 15 | n/a | }; |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | #ifdef ALTSEP |
|---|
| 18 | n/a | _Py_IDENTIFIER(replace); |
|---|
| 19 | n/a | #endif |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | /* zip_searchorder defines how we search for a module in the Zip |
|---|
| 22 | n/a | archive: we first search for a package __init__, then for |
|---|
| 23 | n/a | non-package .pyc, and .py entries. The .pyc entries |
|---|
| 24 | n/a | are swapped by initzipimport() if we run in optimized mode. Also, |
|---|
| 25 | n/a | '/' is replaced by SEP there. */ |
|---|
| 26 | n/a | static struct st_zip_searchorder zip_searchorder[] = { |
|---|
| 27 | n/a | {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE}, |
|---|
| 28 | n/a | {"/__init__.py", IS_PACKAGE | IS_SOURCE}, |
|---|
| 29 | n/a | {".pyc", IS_BYTECODE}, |
|---|
| 30 | n/a | {".py", IS_SOURCE}, |
|---|
| 31 | n/a | {"", 0} |
|---|
| 32 | n/a | }; |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | /* zipimporter object definition and support */ |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | typedef struct _zipimporter ZipImporter; |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | struct _zipimporter { |
|---|
| 39 | n/a | PyObject_HEAD |
|---|
| 40 | n/a | PyObject *archive; /* pathname of the Zip archive, |
|---|
| 41 | n/a | decoded from the filesystem encoding */ |
|---|
| 42 | n/a | PyObject *prefix; /* file prefix: "a/sub/directory/", |
|---|
| 43 | n/a | encoded to the filesystem encoding */ |
|---|
| 44 | n/a | PyObject *files; /* dict with file info {path: toc_entry} */ |
|---|
| 45 | n/a | }; |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | static PyObject *ZipImportError; |
|---|
| 48 | n/a | /* read_directory() cache */ |
|---|
| 49 | n/a | static PyObject *zip_directory_cache = NULL; |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | /* forward decls */ |
|---|
| 52 | n/a | static PyObject *read_directory(PyObject *archive); |
|---|
| 53 | n/a | static PyObject *get_data(PyObject *archive, PyObject *toc_entry); |
|---|
| 54 | n/a | static PyObject *get_module_code(ZipImporter *self, PyObject *fullname, |
|---|
| 55 | n/a | int *p_ispackage, PyObject **p_modpath); |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | #define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type) |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | /* zipimporter.__init__ |
|---|
| 62 | n/a | Split the "subdirectory" from the Zip archive path, lookup a matching |
|---|
| 63 | n/a | entry in sys.path_importer_cache, fetch the file directory from there |
|---|
| 64 | n/a | if found, or else read it from the archive. */ |
|---|
| 65 | n/a | static int |
|---|
| 66 | n/a | zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) |
|---|
| 67 | n/a | { |
|---|
| 68 | n/a | PyObject *path, *files, *tmp; |
|---|
| 69 | n/a | PyObject *filename = NULL; |
|---|
| 70 | n/a | Py_ssize_t len, flen; |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | if (!_PyArg_NoKeywords("zipimporter()", kwds)) |
|---|
| 73 | n/a | return -1; |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | if (!PyArg_ParseTuple(args, "O&:zipimporter", |
|---|
| 76 | n/a | PyUnicode_FSDecoder, &path)) |
|---|
| 77 | n/a | return -1; |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | if (PyUnicode_READY(path) == -1) |
|---|
| 80 | n/a | return -1; |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | len = PyUnicode_GET_LENGTH(path); |
|---|
| 83 | n/a | if (len == 0) { |
|---|
| 84 | n/a | PyErr_SetString(ZipImportError, "archive path is empty"); |
|---|
| 85 | n/a | goto error; |
|---|
| 86 | n/a | } |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | #ifdef ALTSEP |
|---|
| 89 | n/a | tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); |
|---|
| 90 | n/a | if (!tmp) |
|---|
| 91 | n/a | goto error; |
|---|
| 92 | n/a | Py_DECREF(path); |
|---|
| 93 | n/a | path = tmp; |
|---|
| 94 | n/a | #endif |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | filename = path; |
|---|
| 97 | n/a | Py_INCREF(filename); |
|---|
| 98 | n/a | flen = len; |
|---|
| 99 | n/a | for (;;) { |
|---|
| 100 | n/a | struct stat statbuf; |
|---|
| 101 | n/a | int rv; |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | rv = _Py_stat(filename, &statbuf); |
|---|
| 104 | n/a | if (rv == -2) |
|---|
| 105 | n/a | goto error; |
|---|
| 106 | n/a | if (rv == 0) { |
|---|
| 107 | n/a | /* it exists */ |
|---|
| 108 | n/a | if (!S_ISREG(statbuf.st_mode)) |
|---|
| 109 | n/a | /* it's a not file */ |
|---|
| 110 | n/a | Py_CLEAR(filename); |
|---|
| 111 | n/a | break; |
|---|
| 112 | n/a | } |
|---|
| 113 | n/a | Py_CLEAR(filename); |
|---|
| 114 | n/a | /* back up one path element */ |
|---|
| 115 | n/a | flen = PyUnicode_FindChar(path, SEP, 0, flen, -1); |
|---|
| 116 | n/a | if (flen == -1) |
|---|
| 117 | n/a | break; |
|---|
| 118 | n/a | filename = PyUnicode_Substring(path, 0, flen); |
|---|
| 119 | n/a | if (filename == NULL) |
|---|
| 120 | n/a | goto error; |
|---|
| 121 | n/a | } |
|---|
| 122 | n/a | if (filename == NULL) { |
|---|
| 123 | n/a | PyErr_SetString(ZipImportError, "not a Zip file"); |
|---|
| 124 | n/a | goto error; |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | if (PyUnicode_READY(filename) < 0) |
|---|
| 128 | n/a | goto error; |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | files = PyDict_GetItem(zip_directory_cache, filename); |
|---|
| 131 | n/a | if (files == NULL) { |
|---|
| 132 | n/a | files = read_directory(filename); |
|---|
| 133 | n/a | if (files == NULL) |
|---|
| 134 | n/a | goto error; |
|---|
| 135 | n/a | if (PyDict_SetItem(zip_directory_cache, filename, files) != 0) |
|---|
| 136 | n/a | goto error; |
|---|
| 137 | n/a | } |
|---|
| 138 | n/a | else |
|---|
| 139 | n/a | Py_INCREF(files); |
|---|
| 140 | n/a | self->files = files; |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | /* Transfer reference */ |
|---|
| 143 | n/a | self->archive = filename; |
|---|
| 144 | n/a | filename = NULL; |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | /* Check if there is a prefix directory following the filename. */ |
|---|
| 147 | n/a | if (flen != len) { |
|---|
| 148 | n/a | tmp = PyUnicode_Substring(path, flen+1, |
|---|
| 149 | n/a | PyUnicode_GET_LENGTH(path)); |
|---|
| 150 | n/a | if (tmp == NULL) |
|---|
| 151 | n/a | goto error; |
|---|
| 152 | n/a | self->prefix = tmp; |
|---|
| 153 | n/a | if (PyUnicode_READ_CHAR(path, len-1) != SEP) { |
|---|
| 154 | n/a | /* add trailing SEP */ |
|---|
| 155 | n/a | tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP); |
|---|
| 156 | n/a | if (tmp == NULL) |
|---|
| 157 | n/a | goto error; |
|---|
| 158 | n/a | Py_SETREF(self->prefix, tmp); |
|---|
| 159 | n/a | } |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | else |
|---|
| 162 | n/a | self->prefix = PyUnicode_New(0, 0); |
|---|
| 163 | n/a | Py_DECREF(path); |
|---|
| 164 | n/a | return 0; |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | error: |
|---|
| 167 | n/a | Py_DECREF(path); |
|---|
| 168 | n/a | Py_XDECREF(filename); |
|---|
| 169 | n/a | return -1; |
|---|
| 170 | n/a | } |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | /* GC support. */ |
|---|
| 173 | n/a | static int |
|---|
| 174 | n/a | zipimporter_traverse(PyObject *obj, visitproc visit, void *arg) |
|---|
| 175 | n/a | { |
|---|
| 176 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 177 | n/a | Py_VISIT(self->files); |
|---|
| 178 | n/a | return 0; |
|---|
| 179 | n/a | } |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | static void |
|---|
| 182 | n/a | zipimporter_dealloc(ZipImporter *self) |
|---|
| 183 | n/a | { |
|---|
| 184 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 185 | n/a | Py_XDECREF(self->archive); |
|---|
| 186 | n/a | Py_XDECREF(self->prefix); |
|---|
| 187 | n/a | Py_XDECREF(self->files); |
|---|
| 188 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
|---|
| 189 | n/a | } |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | static PyObject * |
|---|
| 192 | n/a | zipimporter_repr(ZipImporter *self) |
|---|
| 193 | n/a | { |
|---|
| 194 | n/a | if (self->archive == NULL) |
|---|
| 195 | n/a | return PyUnicode_FromString("<zipimporter object \"???\">"); |
|---|
| 196 | n/a | else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0) |
|---|
| 197 | n/a | return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">", |
|---|
| 198 | n/a | self->archive, SEP, self->prefix); |
|---|
| 199 | n/a | else |
|---|
| 200 | n/a | return PyUnicode_FromFormat("<zipimporter object \"%U\">", |
|---|
| 201 | n/a | self->archive); |
|---|
| 202 | n/a | } |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | /* return fullname.split(".")[-1] */ |
|---|
| 205 | n/a | static PyObject * |
|---|
| 206 | n/a | get_subname(PyObject *fullname) |
|---|
| 207 | n/a | { |
|---|
| 208 | n/a | Py_ssize_t len, dot; |
|---|
| 209 | n/a | if (PyUnicode_READY(fullname) < 0) |
|---|
| 210 | n/a | return NULL; |
|---|
| 211 | n/a | len = PyUnicode_GET_LENGTH(fullname); |
|---|
| 212 | n/a | dot = PyUnicode_FindChar(fullname, '.', 0, len, -1); |
|---|
| 213 | n/a | if (dot == -1) { |
|---|
| 214 | n/a | Py_INCREF(fullname); |
|---|
| 215 | n/a | return fullname; |
|---|
| 216 | n/a | } else |
|---|
| 217 | n/a | return PyUnicode_Substring(fullname, dot+1, len); |
|---|
| 218 | n/a | } |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | /* Given a (sub)modulename, write the potential file path in the |
|---|
| 221 | n/a | archive (without extension) to the path buffer. Return the |
|---|
| 222 | n/a | length of the resulting string. |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | return self.prefix + name.replace('.', os.sep) */ |
|---|
| 225 | n/a | static PyObject* |
|---|
| 226 | n/a | make_filename(PyObject *prefix, PyObject *name) |
|---|
| 227 | n/a | { |
|---|
| 228 | n/a | PyObject *pathobj; |
|---|
| 229 | n/a | Py_UCS4 *p, *buf; |
|---|
| 230 | n/a | Py_ssize_t len; |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1; |
|---|
| 233 | n/a | p = buf = PyMem_New(Py_UCS4, len); |
|---|
| 234 | n/a | if (buf == NULL) { |
|---|
| 235 | n/a | PyErr_NoMemory(); |
|---|
| 236 | n/a | return NULL; |
|---|
| 237 | n/a | } |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | if (!PyUnicode_AsUCS4(prefix, p, len, 0)) { |
|---|
| 240 | n/a | PyMem_Free(buf); |
|---|
| 241 | n/a | return NULL; |
|---|
| 242 | n/a | } |
|---|
| 243 | n/a | p += PyUnicode_GET_LENGTH(prefix); |
|---|
| 244 | n/a | len -= PyUnicode_GET_LENGTH(prefix); |
|---|
| 245 | n/a | if (!PyUnicode_AsUCS4(name, p, len, 1)) { |
|---|
| 246 | n/a | PyMem_Free(buf); |
|---|
| 247 | n/a | return NULL; |
|---|
| 248 | n/a | } |
|---|
| 249 | n/a | for (; *p; p++) { |
|---|
| 250 | n/a | if (*p == '.') |
|---|
| 251 | n/a | *p = SEP; |
|---|
| 252 | n/a | } |
|---|
| 253 | n/a | pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, |
|---|
| 254 | n/a | buf, p-buf); |
|---|
| 255 | n/a | PyMem_Free(buf); |
|---|
| 256 | n/a | return pathobj; |
|---|
| 257 | n/a | } |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | enum zi_module_info { |
|---|
| 260 | n/a | MI_ERROR, |
|---|
| 261 | n/a | MI_NOT_FOUND, |
|---|
| 262 | n/a | MI_MODULE, |
|---|
| 263 | n/a | MI_PACKAGE |
|---|
| 264 | n/a | }; |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | /* Does this path represent a directory? |
|---|
| 267 | n/a | on error, return < 0 |
|---|
| 268 | n/a | if not a dir, return 0 |
|---|
| 269 | n/a | if a dir, return 1 |
|---|
| 270 | n/a | */ |
|---|
| 271 | n/a | static int |
|---|
| 272 | n/a | check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path) |
|---|
| 273 | n/a | { |
|---|
| 274 | n/a | PyObject *dirpath; |
|---|
| 275 | n/a | int res; |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | /* See if this is a "directory". If so, it's eligible to be part |
|---|
| 278 | n/a | of a namespace package. We test by seeing if the name, with an |
|---|
| 279 | n/a | appended path separator, exists. */ |
|---|
| 280 | n/a | dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP); |
|---|
| 281 | n/a | if (dirpath == NULL) |
|---|
| 282 | n/a | return -1; |
|---|
| 283 | n/a | /* If dirpath is present in self->files, we have a directory. */ |
|---|
| 284 | n/a | res = PyDict_Contains(self->files, dirpath); |
|---|
| 285 | n/a | Py_DECREF(dirpath); |
|---|
| 286 | n/a | return res; |
|---|
| 287 | n/a | } |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | /* Return some information about a module. */ |
|---|
| 290 | n/a | static enum zi_module_info |
|---|
| 291 | n/a | get_module_info(ZipImporter *self, PyObject *fullname) |
|---|
| 292 | n/a | { |
|---|
| 293 | n/a | PyObject *subname; |
|---|
| 294 | n/a | PyObject *path, *fullpath, *item; |
|---|
| 295 | n/a | struct st_zip_searchorder *zso; |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | subname = get_subname(fullname); |
|---|
| 298 | n/a | if (subname == NULL) |
|---|
| 299 | n/a | return MI_ERROR; |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | path = make_filename(self->prefix, subname); |
|---|
| 302 | n/a | Py_DECREF(subname); |
|---|
| 303 | n/a | if (path == NULL) |
|---|
| 304 | n/a | return MI_ERROR; |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | for (zso = zip_searchorder; *zso->suffix; zso++) { |
|---|
| 307 | n/a | fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
|---|
| 308 | n/a | if (fullpath == NULL) { |
|---|
| 309 | n/a | Py_DECREF(path); |
|---|
| 310 | n/a | return MI_ERROR; |
|---|
| 311 | n/a | } |
|---|
| 312 | n/a | item = PyDict_GetItem(self->files, fullpath); |
|---|
| 313 | n/a | Py_DECREF(fullpath); |
|---|
| 314 | n/a | if (item != NULL) { |
|---|
| 315 | n/a | Py_DECREF(path); |
|---|
| 316 | n/a | if (zso->type & IS_PACKAGE) |
|---|
| 317 | n/a | return MI_PACKAGE; |
|---|
| 318 | n/a | else |
|---|
| 319 | n/a | return MI_MODULE; |
|---|
| 320 | n/a | } |
|---|
| 321 | n/a | } |
|---|
| 322 | n/a | Py_DECREF(path); |
|---|
| 323 | n/a | return MI_NOT_FOUND; |
|---|
| 324 | n/a | } |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | typedef enum { |
|---|
| 327 | n/a | FL_ERROR = -1, /* error */ |
|---|
| 328 | n/a | FL_NOT_FOUND, /* no loader or namespace portions found */ |
|---|
| 329 | n/a | FL_MODULE_FOUND, /* module/package found */ |
|---|
| 330 | n/a | FL_NS_FOUND /* namespace portion found: */ |
|---|
| 331 | n/a | /* *namespace_portion will point to the name */ |
|---|
| 332 | n/a | } find_loader_result; |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | /* The guts of "find_loader" and "find_module". |
|---|
| 335 | n/a | */ |
|---|
| 336 | n/a | static find_loader_result |
|---|
| 337 | n/a | find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion) |
|---|
| 338 | n/a | { |
|---|
| 339 | n/a | enum zi_module_info mi; |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | *namespace_portion = NULL; |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | mi = get_module_info(self, fullname); |
|---|
| 344 | n/a | if (mi == MI_ERROR) |
|---|
| 345 | n/a | return FL_ERROR; |
|---|
| 346 | n/a | if (mi == MI_NOT_FOUND) { |
|---|
| 347 | n/a | /* Not a module or regular package. See if this is a directory, and |
|---|
| 348 | n/a | therefore possibly a portion of a namespace package. */ |
|---|
| 349 | n/a | find_loader_result result = FL_NOT_FOUND; |
|---|
| 350 | n/a | PyObject *subname; |
|---|
| 351 | n/a | int is_dir; |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | /* We're only interested in the last path component of fullname; |
|---|
| 354 | n/a | earlier components are recorded in self->prefix. */ |
|---|
| 355 | n/a | subname = get_subname(fullname); |
|---|
| 356 | n/a | if (subname == NULL) { |
|---|
| 357 | n/a | return FL_ERROR; |
|---|
| 358 | n/a | } |
|---|
| 359 | n/a | |
|---|
| 360 | n/a | is_dir = check_is_directory(self, self->prefix, subname); |
|---|
| 361 | n/a | if (is_dir < 0) |
|---|
| 362 | n/a | result = FL_ERROR; |
|---|
| 363 | n/a | else if (is_dir) { |
|---|
| 364 | n/a | /* This is possibly a portion of a namespace |
|---|
| 365 | n/a | package. Return the string representing its path, |
|---|
| 366 | n/a | without a trailing separator. */ |
|---|
| 367 | n/a | *namespace_portion = PyUnicode_FromFormat("%U%c%U%U", |
|---|
| 368 | n/a | self->archive, SEP, |
|---|
| 369 | n/a | self->prefix, subname); |
|---|
| 370 | n/a | if (*namespace_portion == NULL) |
|---|
| 371 | n/a | result = FL_ERROR; |
|---|
| 372 | n/a | else |
|---|
| 373 | n/a | result = FL_NS_FOUND; |
|---|
| 374 | n/a | } |
|---|
| 375 | n/a | Py_DECREF(subname); |
|---|
| 376 | n/a | return result; |
|---|
| 377 | n/a | } |
|---|
| 378 | n/a | /* This is a module or package. */ |
|---|
| 379 | n/a | return FL_MODULE_FOUND; |
|---|
| 380 | n/a | } |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | /* Check whether we can satisfy the import of the module named by |
|---|
| 384 | n/a | 'fullname'. Return self if we can, None if we can't. */ |
|---|
| 385 | n/a | static PyObject * |
|---|
| 386 | n/a | zipimporter_find_module(PyObject *obj, PyObject *args) |
|---|
| 387 | n/a | { |
|---|
| 388 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 389 | n/a | PyObject *path = NULL; |
|---|
| 390 | n/a | PyObject *fullname; |
|---|
| 391 | n/a | PyObject *namespace_portion = NULL; |
|---|
| 392 | n/a | PyObject *result = NULL; |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) |
|---|
| 395 | n/a | return NULL; |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | switch (find_loader(self, fullname, &namespace_portion)) { |
|---|
| 398 | n/a | case FL_ERROR: |
|---|
| 399 | n/a | return NULL; |
|---|
| 400 | n/a | case FL_NS_FOUND: |
|---|
| 401 | n/a | /* A namespace portion is not allowed via find_module, so return None. */ |
|---|
| 402 | n/a | Py_DECREF(namespace_portion); |
|---|
| 403 | n/a | /* FALL THROUGH */ |
|---|
| 404 | n/a | case FL_NOT_FOUND: |
|---|
| 405 | n/a | result = Py_None; |
|---|
| 406 | n/a | break; |
|---|
| 407 | n/a | case FL_MODULE_FOUND: |
|---|
| 408 | n/a | result = (PyObject *)self; |
|---|
| 409 | n/a | break; |
|---|
| 410 | n/a | default: |
|---|
| 411 | n/a | PyErr_BadInternalCall(); |
|---|
| 412 | n/a | return NULL; |
|---|
| 413 | n/a | } |
|---|
| 414 | n/a | Py_INCREF(result); |
|---|
| 415 | n/a | return result; |
|---|
| 416 | n/a | } |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | /* Check whether we can satisfy the import of the module named by |
|---|
| 420 | n/a | 'fullname', or whether it could be a portion of a namespace |
|---|
| 421 | n/a | package. Return self if we can load it, a string containing the |
|---|
| 422 | n/a | full path if it's a possible namespace portion, None if we |
|---|
| 423 | n/a | can't load it. */ |
|---|
| 424 | n/a | static PyObject * |
|---|
| 425 | n/a | zipimporter_find_loader(PyObject *obj, PyObject *args) |
|---|
| 426 | n/a | { |
|---|
| 427 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 428 | n/a | PyObject *path = NULL; |
|---|
| 429 | n/a | PyObject *fullname; |
|---|
| 430 | n/a | PyObject *result = NULL; |
|---|
| 431 | n/a | PyObject *namespace_portion = NULL; |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) |
|---|
| 434 | n/a | return NULL; |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | switch (find_loader(self, fullname, &namespace_portion)) { |
|---|
| 437 | n/a | case FL_ERROR: |
|---|
| 438 | n/a | return NULL; |
|---|
| 439 | n/a | case FL_NOT_FOUND: /* Not found, return (None, []) */ |
|---|
| 440 | n/a | result = Py_BuildValue("O[]", Py_None); |
|---|
| 441 | n/a | break; |
|---|
| 442 | n/a | case FL_MODULE_FOUND: /* Return (self, []) */ |
|---|
| 443 | n/a | result = Py_BuildValue("O[]", self); |
|---|
| 444 | n/a | break; |
|---|
| 445 | n/a | case FL_NS_FOUND: /* Return (None, [namespace_portion]) */ |
|---|
| 446 | n/a | result = Py_BuildValue("O[O]", Py_None, namespace_portion); |
|---|
| 447 | n/a | Py_DECREF(namespace_portion); |
|---|
| 448 | n/a | return result; |
|---|
| 449 | n/a | default: |
|---|
| 450 | n/a | PyErr_BadInternalCall(); |
|---|
| 451 | n/a | return NULL; |
|---|
| 452 | n/a | } |
|---|
| 453 | n/a | return result; |
|---|
| 454 | n/a | } |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | /* Load and return the module named by 'fullname'. */ |
|---|
| 457 | n/a | static PyObject * |
|---|
| 458 | n/a | zipimporter_load_module(PyObject *obj, PyObject *args) |
|---|
| 459 | n/a | { |
|---|
| 460 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 461 | n/a | PyObject *code = NULL, *mod, *dict; |
|---|
| 462 | n/a | PyObject *fullname; |
|---|
| 463 | n/a | PyObject *modpath = NULL; |
|---|
| 464 | n/a | int ispackage; |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | if (!PyArg_ParseTuple(args, "U:zipimporter.load_module", |
|---|
| 467 | n/a | &fullname)) |
|---|
| 468 | n/a | return NULL; |
|---|
| 469 | n/a | if (PyUnicode_READY(fullname) == -1) |
|---|
| 470 | n/a | return NULL; |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | code = get_module_code(self, fullname, &ispackage, &modpath); |
|---|
| 473 | n/a | if (code == NULL) |
|---|
| 474 | n/a | goto error; |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | mod = PyImport_AddModuleObject(fullname); |
|---|
| 477 | n/a | if (mod == NULL) |
|---|
| 478 | n/a | goto error; |
|---|
| 479 | n/a | dict = PyModule_GetDict(mod); |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | /* mod.__loader__ = self */ |
|---|
| 482 | n/a | if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) |
|---|
| 483 | n/a | goto error; |
|---|
| 484 | n/a | |
|---|
| 485 | n/a | if (ispackage) { |
|---|
| 486 | n/a | /* add __path__ to the module *before* the code gets |
|---|
| 487 | n/a | executed */ |
|---|
| 488 | n/a | PyObject *pkgpath, *fullpath, *subname; |
|---|
| 489 | n/a | int err; |
|---|
| 490 | n/a | |
|---|
| 491 | n/a | subname = get_subname(fullname); |
|---|
| 492 | n/a | if (subname == NULL) |
|---|
| 493 | n/a | goto error; |
|---|
| 494 | n/a | |
|---|
| 495 | n/a | fullpath = PyUnicode_FromFormat("%U%c%U%U", |
|---|
| 496 | n/a | self->archive, SEP, |
|---|
| 497 | n/a | self->prefix, subname); |
|---|
| 498 | n/a | Py_DECREF(subname); |
|---|
| 499 | n/a | if (fullpath == NULL) |
|---|
| 500 | n/a | goto error; |
|---|
| 501 | n/a | |
|---|
| 502 | n/a | pkgpath = Py_BuildValue("[N]", fullpath); |
|---|
| 503 | n/a | if (pkgpath == NULL) |
|---|
| 504 | n/a | goto error; |
|---|
| 505 | n/a | err = PyDict_SetItemString(dict, "__path__", pkgpath); |
|---|
| 506 | n/a | Py_DECREF(pkgpath); |
|---|
| 507 | n/a | if (err != 0) |
|---|
| 508 | n/a | goto error; |
|---|
| 509 | n/a | } |
|---|
| 510 | n/a | mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL); |
|---|
| 511 | n/a | Py_CLEAR(code); |
|---|
| 512 | n/a | if (mod == NULL) |
|---|
| 513 | n/a | goto error; |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | if (Py_VerboseFlag) |
|---|
| 516 | n/a | PySys_FormatStderr("import %U # loaded from Zip %U\n", |
|---|
| 517 | n/a | fullname, modpath); |
|---|
| 518 | n/a | Py_DECREF(modpath); |
|---|
| 519 | n/a | return mod; |
|---|
| 520 | n/a | error: |
|---|
| 521 | n/a | Py_XDECREF(code); |
|---|
| 522 | n/a | Py_XDECREF(modpath); |
|---|
| 523 | n/a | return NULL; |
|---|
| 524 | n/a | } |
|---|
| 525 | n/a | |
|---|
| 526 | n/a | /* Return a string matching __file__ for the named module */ |
|---|
| 527 | n/a | static PyObject * |
|---|
| 528 | n/a | zipimporter_get_filename(PyObject *obj, PyObject *args) |
|---|
| 529 | n/a | { |
|---|
| 530 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 531 | n/a | PyObject *fullname, *code, *modpath; |
|---|
| 532 | n/a | int ispackage; |
|---|
| 533 | n/a | |
|---|
| 534 | n/a | if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename", |
|---|
| 535 | n/a | &fullname)) |
|---|
| 536 | n/a | return NULL; |
|---|
| 537 | n/a | |
|---|
| 538 | n/a | /* Deciding the filename requires working out where the code |
|---|
| 539 | n/a | would come from if the module was actually loaded */ |
|---|
| 540 | n/a | code = get_module_code(self, fullname, &ispackage, &modpath); |
|---|
| 541 | n/a | if (code == NULL) |
|---|
| 542 | n/a | return NULL; |
|---|
| 543 | n/a | Py_DECREF(code); /* Only need the path info */ |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | return modpath; |
|---|
| 546 | n/a | } |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | /* Return a bool signifying whether the module is a package or not. */ |
|---|
| 549 | n/a | static PyObject * |
|---|
| 550 | n/a | zipimporter_is_package(PyObject *obj, PyObject *args) |
|---|
| 551 | n/a | { |
|---|
| 552 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 553 | n/a | PyObject *fullname; |
|---|
| 554 | n/a | enum zi_module_info mi; |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | if (!PyArg_ParseTuple(args, "U:zipimporter.is_package", |
|---|
| 557 | n/a | &fullname)) |
|---|
| 558 | n/a | return NULL; |
|---|
| 559 | n/a | |
|---|
| 560 | n/a | mi = get_module_info(self, fullname); |
|---|
| 561 | n/a | if (mi == MI_ERROR) |
|---|
| 562 | n/a | return NULL; |
|---|
| 563 | n/a | if (mi == MI_NOT_FOUND) { |
|---|
| 564 | n/a | PyErr_Format(ZipImportError, "can't find module %R", fullname); |
|---|
| 565 | n/a | return NULL; |
|---|
| 566 | n/a | } |
|---|
| 567 | n/a | return PyBool_FromLong(mi == MI_PACKAGE); |
|---|
| 568 | n/a | } |
|---|
| 569 | n/a | |
|---|
| 570 | n/a | |
|---|
| 571 | n/a | static PyObject * |
|---|
| 572 | n/a | zipimporter_get_data(PyObject *obj, PyObject *args) |
|---|
| 573 | n/a | { |
|---|
| 574 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 575 | n/a | PyObject *path, *key; |
|---|
| 576 | n/a | PyObject *toc_entry; |
|---|
| 577 | n/a | Py_ssize_t path_start, path_len, len; |
|---|
| 578 | n/a | |
|---|
| 579 | n/a | if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path)) |
|---|
| 580 | n/a | return NULL; |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | #ifdef ALTSEP |
|---|
| 583 | n/a | path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); |
|---|
| 584 | n/a | if (!path) |
|---|
| 585 | n/a | return NULL; |
|---|
| 586 | n/a | #else |
|---|
| 587 | n/a | Py_INCREF(path); |
|---|
| 588 | n/a | #endif |
|---|
| 589 | n/a | if (PyUnicode_READY(path) == -1) |
|---|
| 590 | n/a | goto error; |
|---|
| 591 | n/a | |
|---|
| 592 | n/a | path_len = PyUnicode_GET_LENGTH(path); |
|---|
| 593 | n/a | |
|---|
| 594 | n/a | len = PyUnicode_GET_LENGTH(self->archive); |
|---|
| 595 | n/a | path_start = 0; |
|---|
| 596 | n/a | if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1) |
|---|
| 597 | n/a | && PyUnicode_READ_CHAR(path, len) == SEP) { |
|---|
| 598 | n/a | path_start = len + 1; |
|---|
| 599 | n/a | } |
|---|
| 600 | n/a | |
|---|
| 601 | n/a | key = PyUnicode_Substring(path, path_start, path_len); |
|---|
| 602 | n/a | if (key == NULL) |
|---|
| 603 | n/a | goto error; |
|---|
| 604 | n/a | toc_entry = PyDict_GetItem(self->files, key); |
|---|
| 605 | n/a | if (toc_entry == NULL) { |
|---|
| 606 | n/a | PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key); |
|---|
| 607 | n/a | Py_DECREF(key); |
|---|
| 608 | n/a | goto error; |
|---|
| 609 | n/a | } |
|---|
| 610 | n/a | Py_DECREF(key); |
|---|
| 611 | n/a | Py_DECREF(path); |
|---|
| 612 | n/a | return get_data(self->archive, toc_entry); |
|---|
| 613 | n/a | error: |
|---|
| 614 | n/a | Py_DECREF(path); |
|---|
| 615 | n/a | return NULL; |
|---|
| 616 | n/a | } |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | static PyObject * |
|---|
| 619 | n/a | zipimporter_get_code(PyObject *obj, PyObject *args) |
|---|
| 620 | n/a | { |
|---|
| 621 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 622 | n/a | PyObject *fullname; |
|---|
| 623 | n/a | |
|---|
| 624 | n/a | if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname)) |
|---|
| 625 | n/a | return NULL; |
|---|
| 626 | n/a | |
|---|
| 627 | n/a | return get_module_code(self, fullname, NULL, NULL); |
|---|
| 628 | n/a | } |
|---|
| 629 | n/a | |
|---|
| 630 | n/a | static PyObject * |
|---|
| 631 | n/a | zipimporter_get_source(PyObject *obj, PyObject *args) |
|---|
| 632 | n/a | { |
|---|
| 633 | n/a | ZipImporter *self = (ZipImporter *)obj; |
|---|
| 634 | n/a | PyObject *toc_entry; |
|---|
| 635 | n/a | PyObject *fullname, *subname, *path, *fullpath; |
|---|
| 636 | n/a | enum zi_module_info mi; |
|---|
| 637 | n/a | |
|---|
| 638 | n/a | if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname)) |
|---|
| 639 | n/a | return NULL; |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | mi = get_module_info(self, fullname); |
|---|
| 642 | n/a | if (mi == MI_ERROR) |
|---|
| 643 | n/a | return NULL; |
|---|
| 644 | n/a | if (mi == MI_NOT_FOUND) { |
|---|
| 645 | n/a | PyErr_Format(ZipImportError, "can't find module %R", fullname); |
|---|
| 646 | n/a | return NULL; |
|---|
| 647 | n/a | } |
|---|
| 648 | n/a | |
|---|
| 649 | n/a | subname = get_subname(fullname); |
|---|
| 650 | n/a | if (subname == NULL) |
|---|
| 651 | n/a | return NULL; |
|---|
| 652 | n/a | |
|---|
| 653 | n/a | path = make_filename(self->prefix, subname); |
|---|
| 654 | n/a | Py_DECREF(subname); |
|---|
| 655 | n/a | if (path == NULL) |
|---|
| 656 | n/a | return NULL; |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | if (mi == MI_PACKAGE) |
|---|
| 659 | n/a | fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP); |
|---|
| 660 | n/a | else |
|---|
| 661 | n/a | fullpath = PyUnicode_FromFormat("%U.py", path); |
|---|
| 662 | n/a | Py_DECREF(path); |
|---|
| 663 | n/a | if (fullpath == NULL) |
|---|
| 664 | n/a | return NULL; |
|---|
| 665 | n/a | |
|---|
| 666 | n/a | toc_entry = PyDict_GetItem(self->files, fullpath); |
|---|
| 667 | n/a | Py_DECREF(fullpath); |
|---|
| 668 | n/a | if (toc_entry != NULL) { |
|---|
| 669 | n/a | PyObject *res, *bytes; |
|---|
| 670 | n/a | bytes = get_data(self->archive, toc_entry); |
|---|
| 671 | n/a | if (bytes == NULL) |
|---|
| 672 | n/a | return NULL; |
|---|
| 673 | n/a | res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes), |
|---|
| 674 | n/a | PyBytes_GET_SIZE(bytes)); |
|---|
| 675 | n/a | Py_DECREF(bytes); |
|---|
| 676 | n/a | return res; |
|---|
| 677 | n/a | } |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | /* we have the module, but no source */ |
|---|
| 680 | n/a | Py_RETURN_NONE; |
|---|
| 681 | n/a | } |
|---|
| 682 | n/a | |
|---|
| 683 | n/a | PyDoc_STRVAR(doc_find_module, |
|---|
| 684 | n/a | "find_module(fullname, path=None) -> self or None.\n\ |
|---|
| 685 | n/a | \n\ |
|---|
| 686 | n/a | Search for a module specified by 'fullname'. 'fullname' must be the\n\ |
|---|
| 687 | n/a | fully qualified (dotted) module name. It returns the zipimporter\n\ |
|---|
| 688 | n/a | instance itself if the module was found, or None if it wasn't.\n\ |
|---|
| 689 | n/a | The optional 'path' argument is ignored -- it's there for compatibility\n\ |
|---|
| 690 | n/a | with the importer protocol."); |
|---|
| 691 | n/a | |
|---|
| 692 | n/a | PyDoc_STRVAR(doc_find_loader, |
|---|
| 693 | n/a | "find_loader(fullname, path=None) -> self, str or None.\n\ |
|---|
| 694 | n/a | \n\ |
|---|
| 695 | n/a | Search for a module specified by 'fullname'. 'fullname' must be the\n\ |
|---|
| 696 | n/a | fully qualified (dotted) module name. It returns the zipimporter\n\ |
|---|
| 697 | n/a | instance itself if the module was found, a string containing the\n\ |
|---|
| 698 | n/a | full path name if it's possibly a portion of a namespace package,\n\ |
|---|
| 699 | n/a | or None otherwise. The optional 'path' argument is ignored -- it's\n\ |
|---|
| 700 | n/a | there for compatibility with the importer protocol."); |
|---|
| 701 | n/a | |
|---|
| 702 | n/a | PyDoc_STRVAR(doc_load_module, |
|---|
| 703 | n/a | "load_module(fullname) -> module.\n\ |
|---|
| 704 | n/a | \n\ |
|---|
| 705 | n/a | Load the module specified by 'fullname'. 'fullname' must be the\n\ |
|---|
| 706 | n/a | fully qualified (dotted) module name. It returns the imported\n\ |
|---|
| 707 | n/a | module, or raises ZipImportError if it wasn't found."); |
|---|
| 708 | n/a | |
|---|
| 709 | n/a | PyDoc_STRVAR(doc_get_data, |
|---|
| 710 | n/a | "get_data(pathname) -> string with file data.\n\ |
|---|
| 711 | n/a | \n\ |
|---|
| 712 | n/a | Return the data associated with 'pathname'. Raise IOError if\n\ |
|---|
| 713 | n/a | the file wasn't found."); |
|---|
| 714 | n/a | |
|---|
| 715 | n/a | PyDoc_STRVAR(doc_is_package, |
|---|
| 716 | n/a | "is_package(fullname) -> bool.\n\ |
|---|
| 717 | n/a | \n\ |
|---|
| 718 | n/a | Return True if the module specified by fullname is a package.\n\ |
|---|
| 719 | n/a | Raise ZipImportError if the module couldn't be found."); |
|---|
| 720 | n/a | |
|---|
| 721 | n/a | PyDoc_STRVAR(doc_get_code, |
|---|
| 722 | n/a | "get_code(fullname) -> code object.\n\ |
|---|
| 723 | n/a | \n\ |
|---|
| 724 | n/a | Return the code object for the specified module. Raise ZipImportError\n\ |
|---|
| 725 | n/a | if the module couldn't be found."); |
|---|
| 726 | n/a | |
|---|
| 727 | n/a | PyDoc_STRVAR(doc_get_source, |
|---|
| 728 | n/a | "get_source(fullname) -> source string.\n\ |
|---|
| 729 | n/a | \n\ |
|---|
| 730 | n/a | Return the source code for the specified module. Raise ZipImportError\n\ |
|---|
| 731 | n/a | if the module couldn't be found, return None if the archive does\n\ |
|---|
| 732 | n/a | contain the module, but has no source for it."); |
|---|
| 733 | n/a | |
|---|
| 734 | n/a | |
|---|
| 735 | n/a | PyDoc_STRVAR(doc_get_filename, |
|---|
| 736 | n/a | "get_filename(fullname) -> filename string.\n\ |
|---|
| 737 | n/a | \n\ |
|---|
| 738 | n/a | Return the filename for the specified module."); |
|---|
| 739 | n/a | |
|---|
| 740 | n/a | static PyMethodDef zipimporter_methods[] = { |
|---|
| 741 | n/a | {"find_module", zipimporter_find_module, METH_VARARGS, |
|---|
| 742 | n/a | doc_find_module}, |
|---|
| 743 | n/a | {"find_loader", zipimporter_find_loader, METH_VARARGS, |
|---|
| 744 | n/a | doc_find_loader}, |
|---|
| 745 | n/a | {"load_module", zipimporter_load_module, METH_VARARGS, |
|---|
| 746 | n/a | doc_load_module}, |
|---|
| 747 | n/a | {"get_data", zipimporter_get_data, METH_VARARGS, |
|---|
| 748 | n/a | doc_get_data}, |
|---|
| 749 | n/a | {"get_code", zipimporter_get_code, METH_VARARGS, |
|---|
| 750 | n/a | doc_get_code}, |
|---|
| 751 | n/a | {"get_source", zipimporter_get_source, METH_VARARGS, |
|---|
| 752 | n/a | doc_get_source}, |
|---|
| 753 | n/a | {"get_filename", zipimporter_get_filename, METH_VARARGS, |
|---|
| 754 | n/a | doc_get_filename}, |
|---|
| 755 | n/a | {"is_package", zipimporter_is_package, METH_VARARGS, |
|---|
| 756 | n/a | doc_is_package}, |
|---|
| 757 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 758 | n/a | }; |
|---|
| 759 | n/a | |
|---|
| 760 | n/a | static PyMemberDef zipimporter_members[] = { |
|---|
| 761 | n/a | {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY}, |
|---|
| 762 | n/a | {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY}, |
|---|
| 763 | n/a | {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY}, |
|---|
| 764 | n/a | {NULL} |
|---|
| 765 | n/a | }; |
|---|
| 766 | n/a | |
|---|
| 767 | n/a | PyDoc_STRVAR(zipimporter_doc, |
|---|
| 768 | n/a | "zipimporter(archivepath) -> zipimporter object\n\ |
|---|
| 769 | n/a | \n\ |
|---|
| 770 | n/a | Create a new zipimporter instance. 'archivepath' must be a path to\n\ |
|---|
| 771 | n/a | a zipfile, or to a specific path inside a zipfile. For example, it can be\n\ |
|---|
| 772 | n/a | '/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\ |
|---|
| 773 | n/a | valid directory inside the archive.\n\ |
|---|
| 774 | n/a | \n\ |
|---|
| 775 | n/a | 'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\ |
|---|
| 776 | n/a | archive.\n\ |
|---|
| 777 | n/a | \n\ |
|---|
| 778 | n/a | The 'archive' attribute of zipimporter objects contains the name of the\n\ |
|---|
| 779 | n/a | zipfile targeted."); |
|---|
| 780 | n/a | |
|---|
| 781 | n/a | #define DEFERRED_ADDRESS(ADDR) 0 |
|---|
| 782 | n/a | |
|---|
| 783 | n/a | static PyTypeObject ZipImporter_Type = { |
|---|
| 784 | n/a | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
|---|
| 785 | n/a | "zipimport.zipimporter", |
|---|
| 786 | n/a | sizeof(ZipImporter), |
|---|
| 787 | n/a | 0, /* tp_itemsize */ |
|---|
| 788 | n/a | (destructor)zipimporter_dealloc, /* tp_dealloc */ |
|---|
| 789 | n/a | 0, /* tp_print */ |
|---|
| 790 | n/a | 0, /* tp_getattr */ |
|---|
| 791 | n/a | 0, /* tp_setattr */ |
|---|
| 792 | n/a | 0, /* tp_reserved */ |
|---|
| 793 | n/a | (reprfunc)zipimporter_repr, /* tp_repr */ |
|---|
| 794 | n/a | 0, /* tp_as_number */ |
|---|
| 795 | n/a | 0, /* tp_as_sequence */ |
|---|
| 796 | n/a | 0, /* tp_as_mapping */ |
|---|
| 797 | n/a | 0, /* tp_hash */ |
|---|
| 798 | n/a | 0, /* tp_call */ |
|---|
| 799 | n/a | 0, /* tp_str */ |
|---|
| 800 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 801 | n/a | 0, /* tp_setattro */ |
|---|
| 802 | n/a | 0, /* tp_as_buffer */ |
|---|
| 803 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
|---|
| 804 | n/a | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 805 | n/a | zipimporter_doc, /* tp_doc */ |
|---|
| 806 | n/a | zipimporter_traverse, /* tp_traverse */ |
|---|
| 807 | n/a | 0, /* tp_clear */ |
|---|
| 808 | n/a | 0, /* tp_richcompare */ |
|---|
| 809 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 810 | n/a | 0, /* tp_iter */ |
|---|
| 811 | n/a | 0, /* tp_iternext */ |
|---|
| 812 | n/a | zipimporter_methods, /* tp_methods */ |
|---|
| 813 | n/a | zipimporter_members, /* tp_members */ |
|---|
| 814 | n/a | 0, /* tp_getset */ |
|---|
| 815 | n/a | 0, /* tp_base */ |
|---|
| 816 | n/a | 0, /* tp_dict */ |
|---|
| 817 | n/a | 0, /* tp_descr_get */ |
|---|
| 818 | n/a | 0, /* tp_descr_set */ |
|---|
| 819 | n/a | 0, /* tp_dictoffset */ |
|---|
| 820 | n/a | (initproc)zipimporter_init, /* tp_init */ |
|---|
| 821 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
|---|
| 822 | n/a | PyType_GenericNew, /* tp_new */ |
|---|
| 823 | n/a | PyObject_GC_Del, /* tp_free */ |
|---|
| 824 | n/a | }; |
|---|
| 825 | n/a | |
|---|
| 826 | n/a | |
|---|
| 827 | n/a | /* implementation */ |
|---|
| 828 | n/a | |
|---|
| 829 | n/a | /* Given a buffer, return the unsigned int that is represented by the first |
|---|
| 830 | n/a | 4 bytes, encoded as little endian. This partially reimplements |
|---|
| 831 | n/a | marshal.c:r_long() */ |
|---|
| 832 | n/a | static unsigned int |
|---|
| 833 | n/a | get_uint32(const unsigned char *buf) |
|---|
| 834 | n/a | { |
|---|
| 835 | n/a | unsigned int x; |
|---|
| 836 | n/a | x = buf[0]; |
|---|
| 837 | n/a | x |= (unsigned int)buf[1] << 8; |
|---|
| 838 | n/a | x |= (unsigned int)buf[2] << 16; |
|---|
| 839 | n/a | x |= (unsigned int)buf[3] << 24; |
|---|
| 840 | n/a | return x; |
|---|
| 841 | n/a | } |
|---|
| 842 | n/a | |
|---|
| 843 | n/a | /* Given a buffer, return the unsigned int that is represented by the first |
|---|
| 844 | n/a | 2 bytes, encoded as little endian. This partially reimplements |
|---|
| 845 | n/a | marshal.c:r_short() */ |
|---|
| 846 | n/a | static unsigned short |
|---|
| 847 | n/a | get_uint16(const unsigned char *buf) |
|---|
| 848 | n/a | { |
|---|
| 849 | n/a | unsigned short x; |
|---|
| 850 | n/a | x = buf[0]; |
|---|
| 851 | n/a | x |= (unsigned short)buf[1] << 8; |
|---|
| 852 | n/a | return x; |
|---|
| 853 | n/a | } |
|---|
| 854 | n/a | |
|---|
| 855 | n/a | static void |
|---|
| 856 | n/a | set_file_error(PyObject *archive, int eof) |
|---|
| 857 | n/a | { |
|---|
| 858 | n/a | if (eof) { |
|---|
| 859 | n/a | PyErr_SetString(PyExc_EOFError, "EOF read where not expected"); |
|---|
| 860 | n/a | } |
|---|
| 861 | n/a | else { |
|---|
| 862 | n/a | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, archive); |
|---|
| 863 | n/a | } |
|---|
| 864 | n/a | } |
|---|
| 865 | n/a | |
|---|
| 866 | n/a | /* |
|---|
| 867 | n/a | read_directory(archive) -> files dict (new reference) |
|---|
| 868 | n/a | |
|---|
| 869 | n/a | Given a path to a Zip archive, build a dict, mapping file names |
|---|
| 870 | n/a | (local to the archive, using SEP as a separator) to toc entries. |
|---|
| 871 | n/a | |
|---|
| 872 | n/a | A toc_entry is a tuple: |
|---|
| 873 | n/a | |
|---|
| 874 | n/a | (__file__, # value to use for __file__, available for all files, |
|---|
| 875 | n/a | # encoded to the filesystem encoding |
|---|
| 876 | n/a | compress, # compression kind; 0 for uncompressed |
|---|
| 877 | n/a | data_size, # size of compressed data on disk |
|---|
| 878 | n/a | file_size, # size of decompressed data |
|---|
| 879 | n/a | file_offset, # offset of file header from start of archive |
|---|
| 880 | n/a | time, # mod time of file (in dos format) |
|---|
| 881 | n/a | date, # mod data of file (in dos format) |
|---|
| 882 | n/a | crc, # crc checksum of the data |
|---|
| 883 | n/a | ) |
|---|
| 884 | n/a | |
|---|
| 885 | n/a | Directories can be recognized by the trailing SEP in the name, |
|---|
| 886 | n/a | data_size and file_offset are 0. |
|---|
| 887 | n/a | */ |
|---|
| 888 | n/a | static PyObject * |
|---|
| 889 | n/a | read_directory(PyObject *archive) |
|---|
| 890 | n/a | { |
|---|
| 891 | n/a | PyObject *files = NULL; |
|---|
| 892 | n/a | FILE *fp; |
|---|
| 893 | n/a | unsigned short flags, compress, time, date, name_size; |
|---|
| 894 | n/a | unsigned int crc, data_size, file_size, header_size, header_offset; |
|---|
| 895 | n/a | unsigned long file_offset, header_position; |
|---|
| 896 | n/a | unsigned long arc_offset; /* Absolute offset to start of the zip-archive. */ |
|---|
| 897 | n/a | unsigned int count, i; |
|---|
| 898 | n/a | unsigned char buffer[46]; |
|---|
| 899 | n/a | char name[MAXPATHLEN + 5]; |
|---|
| 900 | n/a | PyObject *nameobj = NULL; |
|---|
| 901 | n/a | PyObject *path; |
|---|
| 902 | n/a | const char *charset; |
|---|
| 903 | n/a | int bootstrap; |
|---|
| 904 | n/a | const char *errmsg = NULL; |
|---|
| 905 | n/a | |
|---|
| 906 | n/a | fp = _Py_fopen_obj(archive, "rb"); |
|---|
| 907 | n/a | if (fp == NULL) { |
|---|
| 908 | n/a | if (PyErr_ExceptionMatches(PyExc_OSError)) { |
|---|
| 909 | n/a | _PyErr_FormatFromCause(ZipImportError, |
|---|
| 910 | n/a | "can't open Zip file: %R", archive); |
|---|
| 911 | n/a | } |
|---|
| 912 | n/a | return NULL; |
|---|
| 913 | n/a | } |
|---|
| 914 | n/a | |
|---|
| 915 | n/a | if (fseek(fp, -22, SEEK_END) == -1) { |
|---|
| 916 | n/a | goto file_error; |
|---|
| 917 | n/a | } |
|---|
| 918 | n/a | header_position = (unsigned long)ftell(fp); |
|---|
| 919 | n/a | if (header_position == (unsigned long)-1) { |
|---|
| 920 | n/a | goto file_error; |
|---|
| 921 | n/a | } |
|---|
| 922 | n/a | assert(header_position <= (unsigned long)LONG_MAX); |
|---|
| 923 | n/a | if (fread(buffer, 1, 22, fp) != 22) { |
|---|
| 924 | n/a | goto file_error; |
|---|
| 925 | n/a | } |
|---|
| 926 | n/a | if (get_uint32(buffer) != 0x06054B50u) { |
|---|
| 927 | n/a | /* Bad: End of Central Dir signature */ |
|---|
| 928 | n/a | errmsg = "not a Zip file"; |
|---|
| 929 | n/a | goto invalid_header; |
|---|
| 930 | n/a | } |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | header_size = get_uint32(buffer + 12); |
|---|
| 933 | n/a | header_offset = get_uint32(buffer + 16); |
|---|
| 934 | n/a | if (header_position < header_size) { |
|---|
| 935 | n/a | errmsg = "bad central directory size"; |
|---|
| 936 | n/a | goto invalid_header; |
|---|
| 937 | n/a | } |
|---|
| 938 | n/a | if (header_position < header_offset) { |
|---|
| 939 | n/a | errmsg = "bad central directory offset"; |
|---|
| 940 | n/a | goto invalid_header; |
|---|
| 941 | n/a | } |
|---|
| 942 | n/a | if (header_position - header_size < header_offset) { |
|---|
| 943 | n/a | errmsg = "bad central directory size or offset"; |
|---|
| 944 | n/a | goto invalid_header; |
|---|
| 945 | n/a | } |
|---|
| 946 | n/a | header_position -= header_size; |
|---|
| 947 | n/a | arc_offset = header_position - header_offset; |
|---|
| 948 | n/a | |
|---|
| 949 | n/a | files = PyDict_New(); |
|---|
| 950 | n/a | if (files == NULL) { |
|---|
| 951 | n/a | goto error; |
|---|
| 952 | n/a | } |
|---|
| 953 | n/a | /* Start of Central Directory */ |
|---|
| 954 | n/a | count = 0; |
|---|
| 955 | n/a | if (fseek(fp, (long)header_position, 0) == -1) { |
|---|
| 956 | n/a | goto file_error; |
|---|
| 957 | n/a | } |
|---|
| 958 | n/a | for (;;) { |
|---|
| 959 | n/a | PyObject *t; |
|---|
| 960 | n/a | size_t n; |
|---|
| 961 | n/a | int err; |
|---|
| 962 | n/a | |
|---|
| 963 | n/a | n = fread(buffer, 1, 46, fp); |
|---|
| 964 | n/a | if (n < 4) { |
|---|
| 965 | n/a | goto eof_error; |
|---|
| 966 | n/a | } |
|---|
| 967 | n/a | /* Start of file header */ |
|---|
| 968 | n/a | if (get_uint32(buffer) != 0x02014B50u) { |
|---|
| 969 | n/a | break; /* Bad: Central Dir File Header */ |
|---|
| 970 | n/a | } |
|---|
| 971 | n/a | if (n != 46) { |
|---|
| 972 | n/a | goto eof_error; |
|---|
| 973 | n/a | } |
|---|
| 974 | n/a | flags = get_uint16(buffer + 8); |
|---|
| 975 | n/a | compress = get_uint16(buffer + 10); |
|---|
| 976 | n/a | time = get_uint16(buffer + 12); |
|---|
| 977 | n/a | date = get_uint16(buffer + 14); |
|---|
| 978 | n/a | crc = get_uint32(buffer + 16); |
|---|
| 979 | n/a | data_size = get_uint32(buffer + 20); |
|---|
| 980 | n/a | file_size = get_uint32(buffer + 24); |
|---|
| 981 | n/a | name_size = get_uint16(buffer + 28); |
|---|
| 982 | n/a | header_size = (unsigned int)name_size + |
|---|
| 983 | n/a | get_uint16(buffer + 30) /* extra field */ + |
|---|
| 984 | n/a | get_uint16(buffer + 32) /* comment */; |
|---|
| 985 | n/a | |
|---|
| 986 | n/a | file_offset = get_uint32(buffer + 42); |
|---|
| 987 | n/a | if (file_offset > header_offset) { |
|---|
| 988 | n/a | errmsg = "bad local header offset"; |
|---|
| 989 | n/a | goto invalid_header; |
|---|
| 990 | n/a | } |
|---|
| 991 | n/a | file_offset += arc_offset; |
|---|
| 992 | n/a | |
|---|
| 993 | n/a | if (name_size > MAXPATHLEN) { |
|---|
| 994 | n/a | name_size = MAXPATHLEN; |
|---|
| 995 | n/a | } |
|---|
| 996 | n/a | if (fread(name, 1, name_size, fp) != name_size) { |
|---|
| 997 | n/a | goto file_error; |
|---|
| 998 | n/a | } |
|---|
| 999 | n/a | name[name_size] = '\0'; /* Add terminating null byte */ |
|---|
| 1000 | n/a | #if SEP != '/' |
|---|
| 1001 | n/a | for (i = 0; i < name_size; i++) { |
|---|
| 1002 | n/a | if (name[i] == '/') { |
|---|
| 1003 | n/a | name[i] = SEP; |
|---|
| 1004 | n/a | } |
|---|
| 1005 | n/a | } |
|---|
| 1006 | n/a | #endif |
|---|
| 1007 | n/a | /* Skip the rest of the header. |
|---|
| 1008 | n/a | * On Windows, calling fseek to skip over the fields we don't use is |
|---|
| 1009 | n/a | * slower than reading the data because fseek flushes stdio's |
|---|
| 1010 | n/a | * internal buffers. See issue #8745. */ |
|---|
| 1011 | n/a | assert(header_size <= 3*0xFFFFu); |
|---|
| 1012 | n/a | for (i = name_size; i < header_size; i++) { |
|---|
| 1013 | n/a | if (getc(fp) == EOF) { |
|---|
| 1014 | n/a | goto file_error; |
|---|
| 1015 | n/a | } |
|---|
| 1016 | n/a | } |
|---|
| 1017 | n/a | |
|---|
| 1018 | n/a | bootstrap = 0; |
|---|
| 1019 | n/a | if (flags & 0x0800) { |
|---|
| 1020 | n/a | charset = "utf-8"; |
|---|
| 1021 | n/a | } |
|---|
| 1022 | n/a | else if (!PyThreadState_GET()->interp->codecs_initialized) { |
|---|
| 1023 | n/a | /* During bootstrap, we may need to load the encodings |
|---|
| 1024 | n/a | package from a ZIP file. But the cp437 encoding is implemented |
|---|
| 1025 | n/a | in Python in the encodings package. |
|---|
| 1026 | n/a | |
|---|
| 1027 | n/a | Break out of this dependency by assuming that the path to |
|---|
| 1028 | n/a | the encodings module is ASCII-only. */ |
|---|
| 1029 | n/a | charset = "ascii"; |
|---|
| 1030 | n/a | bootstrap = 1; |
|---|
| 1031 | n/a | } |
|---|
| 1032 | n/a | else { |
|---|
| 1033 | n/a | charset = "cp437"; |
|---|
| 1034 | n/a | } |
|---|
| 1035 | n/a | nameobj = PyUnicode_Decode(name, name_size, charset, NULL); |
|---|
| 1036 | n/a | if (nameobj == NULL) { |
|---|
| 1037 | n/a | if (bootstrap) { |
|---|
| 1038 | n/a | PyErr_Format(PyExc_NotImplementedError, |
|---|
| 1039 | n/a | "bootstrap issue: python%i%i.zip contains non-ASCII " |
|---|
| 1040 | n/a | "filenames without the unicode flag", |
|---|
| 1041 | n/a | PY_MAJOR_VERSION, PY_MINOR_VERSION); |
|---|
| 1042 | n/a | } |
|---|
| 1043 | n/a | goto error; |
|---|
| 1044 | n/a | } |
|---|
| 1045 | n/a | if (PyUnicode_READY(nameobj) == -1) { |
|---|
| 1046 | n/a | goto error; |
|---|
| 1047 | n/a | } |
|---|
| 1048 | n/a | path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj); |
|---|
| 1049 | n/a | if (path == NULL) { |
|---|
| 1050 | n/a | goto error; |
|---|
| 1051 | n/a | } |
|---|
| 1052 | n/a | t = Py_BuildValue("NHIIkHHI", path, compress, data_size, |
|---|
| 1053 | n/a | file_size, file_offset, time, date, crc); |
|---|
| 1054 | n/a | if (t == NULL) { |
|---|
| 1055 | n/a | goto error; |
|---|
| 1056 | n/a | } |
|---|
| 1057 | n/a | err = PyDict_SetItem(files, nameobj, t); |
|---|
| 1058 | n/a | Py_CLEAR(nameobj); |
|---|
| 1059 | n/a | Py_DECREF(t); |
|---|
| 1060 | n/a | if (err != 0) { |
|---|
| 1061 | n/a | goto error; |
|---|
| 1062 | n/a | } |
|---|
| 1063 | n/a | count++; |
|---|
| 1064 | n/a | } |
|---|
| 1065 | n/a | fclose(fp); |
|---|
| 1066 | n/a | if (Py_VerboseFlag) { |
|---|
| 1067 | n/a | PySys_FormatStderr("# zipimport: found %u names in %R\n", |
|---|
| 1068 | n/a | count, archive); |
|---|
| 1069 | n/a | } |
|---|
| 1070 | n/a | return files; |
|---|
| 1071 | n/a | |
|---|
| 1072 | n/a | eof_error: |
|---|
| 1073 | n/a | set_file_error(archive, !ferror(fp)); |
|---|
| 1074 | n/a | goto error; |
|---|
| 1075 | n/a | |
|---|
| 1076 | n/a | file_error: |
|---|
| 1077 | n/a | PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|---|
| 1078 | n/a | goto error; |
|---|
| 1079 | n/a | |
|---|
| 1080 | n/a | invalid_header: |
|---|
| 1081 | n/a | assert(errmsg != NULL); |
|---|
| 1082 | n/a | PyErr_Format(ZipImportError, "%s: %R", errmsg, archive); |
|---|
| 1083 | n/a | goto error; |
|---|
| 1084 | n/a | |
|---|
| 1085 | n/a | error: |
|---|
| 1086 | n/a | fclose(fp); |
|---|
| 1087 | n/a | Py_XDECREF(files); |
|---|
| 1088 | n/a | Py_XDECREF(nameobj); |
|---|
| 1089 | n/a | return NULL; |
|---|
| 1090 | n/a | } |
|---|
| 1091 | n/a | |
|---|
| 1092 | n/a | /* Return the zlib.decompress function object, or NULL if zlib couldn't |
|---|
| 1093 | n/a | be imported. The function is cached when found, so subsequent calls |
|---|
| 1094 | n/a | don't import zlib again. */ |
|---|
| 1095 | n/a | static PyObject * |
|---|
| 1096 | n/a | get_decompress_func(void) |
|---|
| 1097 | n/a | { |
|---|
| 1098 | n/a | static int importing_zlib = 0; |
|---|
| 1099 | n/a | PyObject *zlib; |
|---|
| 1100 | n/a | PyObject *decompress; |
|---|
| 1101 | n/a | _Py_IDENTIFIER(decompress); |
|---|
| 1102 | n/a | |
|---|
| 1103 | n/a | if (importing_zlib != 0) |
|---|
| 1104 | n/a | /* Someone has a zlib.py[co] in their Zip file; |
|---|
| 1105 | n/a | let's avoid a stack overflow. */ |
|---|
| 1106 | n/a | return NULL; |
|---|
| 1107 | n/a | importing_zlib = 1; |
|---|
| 1108 | n/a | zlib = PyImport_ImportModuleNoBlock("zlib"); |
|---|
| 1109 | n/a | importing_zlib = 0; |
|---|
| 1110 | n/a | if (zlib != NULL) { |
|---|
| 1111 | n/a | decompress = _PyObject_GetAttrId(zlib, |
|---|
| 1112 | n/a | &PyId_decompress); |
|---|
| 1113 | n/a | Py_DECREF(zlib); |
|---|
| 1114 | n/a | } |
|---|
| 1115 | n/a | else { |
|---|
| 1116 | n/a | PyErr_Clear(); |
|---|
| 1117 | n/a | decompress = NULL; |
|---|
| 1118 | n/a | } |
|---|
| 1119 | n/a | if (Py_VerboseFlag) |
|---|
| 1120 | n/a | PySys_WriteStderr("# zipimport: zlib %s\n", |
|---|
| 1121 | n/a | zlib != NULL ? "available": "UNAVAILABLE"); |
|---|
| 1122 | n/a | return decompress; |
|---|
| 1123 | n/a | } |
|---|
| 1124 | n/a | |
|---|
| 1125 | n/a | /* Given a path to a Zip file and a toc_entry, return the (uncompressed) |
|---|
| 1126 | n/a | data as a new reference. */ |
|---|
| 1127 | n/a | static PyObject * |
|---|
| 1128 | n/a | get_data(PyObject *archive, PyObject *toc_entry) |
|---|
| 1129 | n/a | { |
|---|
| 1130 | n/a | PyObject *raw_data = NULL, *data, *decompress; |
|---|
| 1131 | n/a | char *buf; |
|---|
| 1132 | n/a | FILE *fp; |
|---|
| 1133 | n/a | PyObject *datapath; |
|---|
| 1134 | n/a | unsigned short compress, time, date; |
|---|
| 1135 | n/a | unsigned int crc; |
|---|
| 1136 | n/a | Py_ssize_t data_size, file_size, bytes_size; |
|---|
| 1137 | n/a | long file_offset, header_size; |
|---|
| 1138 | n/a | unsigned char buffer[30]; |
|---|
| 1139 | n/a | const char *errmsg = NULL; |
|---|
| 1140 | n/a | |
|---|
| 1141 | n/a | if (!PyArg_ParseTuple(toc_entry, "OHnnlHHI", &datapath, &compress, |
|---|
| 1142 | n/a | &data_size, &file_size, &file_offset, &time, |
|---|
| 1143 | n/a | &date, &crc)) { |
|---|
| 1144 | n/a | return NULL; |
|---|
| 1145 | n/a | } |
|---|
| 1146 | n/a | if (data_size < 0) { |
|---|
| 1147 | n/a | PyErr_Format(ZipImportError, "negative data size"); |
|---|
| 1148 | n/a | return NULL; |
|---|
| 1149 | n/a | } |
|---|
| 1150 | n/a | |
|---|
| 1151 | n/a | fp = _Py_fopen_obj(archive, "rb"); |
|---|
| 1152 | n/a | if (!fp) { |
|---|
| 1153 | n/a | return NULL; |
|---|
| 1154 | n/a | } |
|---|
| 1155 | n/a | /* Check to make sure the local file header is correct */ |
|---|
| 1156 | n/a | if (fseek(fp, file_offset, 0) == -1) { |
|---|
| 1157 | n/a | goto file_error; |
|---|
| 1158 | n/a | } |
|---|
| 1159 | n/a | if (fread(buffer, 1, 30, fp) != 30) { |
|---|
| 1160 | n/a | goto eof_error; |
|---|
| 1161 | n/a | } |
|---|
| 1162 | n/a | if (get_uint32(buffer) != 0x04034B50u) { |
|---|
| 1163 | n/a | /* Bad: Local File Header */ |
|---|
| 1164 | n/a | errmsg = "bad local file header"; |
|---|
| 1165 | n/a | goto invalid_header; |
|---|
| 1166 | n/a | } |
|---|
| 1167 | n/a | |
|---|
| 1168 | n/a | header_size = (unsigned int)30 + |
|---|
| 1169 | n/a | get_uint16(buffer + 26) /* file name */ + |
|---|
| 1170 | n/a | get_uint16(buffer + 28) /* extra field */; |
|---|
| 1171 | n/a | if (file_offset > LONG_MAX - header_size) { |
|---|
| 1172 | n/a | errmsg = "bad local file header size"; |
|---|
| 1173 | n/a | goto invalid_header; |
|---|
| 1174 | n/a | } |
|---|
| 1175 | n/a | file_offset += header_size; /* Start of file data */ |
|---|
| 1176 | n/a | |
|---|
| 1177 | n/a | if (data_size > LONG_MAX - 1) { |
|---|
| 1178 | n/a | fclose(fp); |
|---|
| 1179 | n/a | PyErr_NoMemory(); |
|---|
| 1180 | n/a | return NULL; |
|---|
| 1181 | n/a | } |
|---|
| 1182 | n/a | bytes_size = compress == 0 ? data_size : data_size + 1; |
|---|
| 1183 | n/a | if (bytes_size == 0) { |
|---|
| 1184 | n/a | bytes_size++; |
|---|
| 1185 | n/a | } |
|---|
| 1186 | n/a | raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size); |
|---|
| 1187 | n/a | if (raw_data == NULL) { |
|---|
| 1188 | n/a | goto error; |
|---|
| 1189 | n/a | } |
|---|
| 1190 | n/a | buf = PyBytes_AsString(raw_data); |
|---|
| 1191 | n/a | |
|---|
| 1192 | n/a | if (fseek(fp, file_offset, 0) == -1) { |
|---|
| 1193 | n/a | goto file_error; |
|---|
| 1194 | n/a | } |
|---|
| 1195 | n/a | if (fread(buf, 1, data_size, fp) != (size_t)data_size) { |
|---|
| 1196 | n/a | PyErr_SetString(PyExc_IOError, |
|---|
| 1197 | n/a | "zipimport: can't read data"); |
|---|
| 1198 | n/a | goto error; |
|---|
| 1199 | n/a | } |
|---|
| 1200 | n/a | |
|---|
| 1201 | n/a | fclose(fp); |
|---|
| 1202 | n/a | fp = NULL; |
|---|
| 1203 | n/a | |
|---|
| 1204 | n/a | if (compress != 0) { |
|---|
| 1205 | n/a | buf[data_size] = 'Z'; /* saw this in zipfile.py */ |
|---|
| 1206 | n/a | data_size++; |
|---|
| 1207 | n/a | } |
|---|
| 1208 | n/a | buf[data_size] = '\0'; |
|---|
| 1209 | n/a | |
|---|
| 1210 | n/a | if (compress == 0) { /* data is not compressed */ |
|---|
| 1211 | n/a | data = PyBytes_FromStringAndSize(buf, data_size); |
|---|
| 1212 | n/a | Py_DECREF(raw_data); |
|---|
| 1213 | n/a | return data; |
|---|
| 1214 | n/a | } |
|---|
| 1215 | n/a | |
|---|
| 1216 | n/a | /* Decompress with zlib */ |
|---|
| 1217 | n/a | decompress = get_decompress_func(); |
|---|
| 1218 | n/a | if (decompress == NULL) { |
|---|
| 1219 | n/a | PyErr_SetString(ZipImportError, |
|---|
| 1220 | n/a | "can't decompress data; " |
|---|
| 1221 | n/a | "zlib not available"); |
|---|
| 1222 | n/a | goto error; |
|---|
| 1223 | n/a | } |
|---|
| 1224 | n/a | data = PyObject_CallFunction(decompress, "Oi", raw_data, -15); |
|---|
| 1225 | n/a | Py_DECREF(decompress); |
|---|
| 1226 | n/a | Py_DECREF(raw_data); |
|---|
| 1227 | n/a | return data; |
|---|
| 1228 | n/a | |
|---|
| 1229 | n/a | eof_error: |
|---|
| 1230 | n/a | set_file_error(archive, !ferror(fp)); |
|---|
| 1231 | n/a | goto error; |
|---|
| 1232 | n/a | |
|---|
| 1233 | n/a | file_error: |
|---|
| 1234 | n/a | PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|---|
| 1235 | n/a | goto error; |
|---|
| 1236 | n/a | |
|---|
| 1237 | n/a | invalid_header: |
|---|
| 1238 | n/a | assert(errmsg != NULL); |
|---|
| 1239 | n/a | PyErr_Format(ZipImportError, "%s: %R", errmsg, archive); |
|---|
| 1240 | n/a | goto error; |
|---|
| 1241 | n/a | |
|---|
| 1242 | n/a | error: |
|---|
| 1243 | n/a | if (fp != NULL) { |
|---|
| 1244 | n/a | fclose(fp); |
|---|
| 1245 | n/a | } |
|---|
| 1246 | n/a | Py_XDECREF(raw_data); |
|---|
| 1247 | n/a | return NULL; |
|---|
| 1248 | n/a | } |
|---|
| 1249 | n/a | |
|---|
| 1250 | n/a | /* Lenient date/time comparison function. The precision of the mtime |
|---|
| 1251 | n/a | in the archive is lower than the mtime stored in a .pyc: we |
|---|
| 1252 | n/a | must allow a difference of at most one second. */ |
|---|
| 1253 | n/a | static int |
|---|
| 1254 | n/a | eq_mtime(time_t t1, time_t t2) |
|---|
| 1255 | n/a | { |
|---|
| 1256 | n/a | time_t d = t1 - t2; |
|---|
| 1257 | n/a | if (d < 0) |
|---|
| 1258 | n/a | d = -d; |
|---|
| 1259 | n/a | /* dostime only stores even seconds, so be lenient */ |
|---|
| 1260 | n/a | return d <= 1; |
|---|
| 1261 | n/a | } |
|---|
| 1262 | n/a | |
|---|
| 1263 | n/a | /* Given the contents of a .py[co] file in a buffer, unmarshal the data |
|---|
| 1264 | n/a | and return the code object. Return None if it the magic word doesn't |
|---|
| 1265 | n/a | match (we do this instead of raising an exception as we fall back |
|---|
| 1266 | n/a | to .py if available and we don't want to mask other errors). |
|---|
| 1267 | n/a | Returns a new reference. */ |
|---|
| 1268 | n/a | static PyObject * |
|---|
| 1269 | n/a | unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime) |
|---|
| 1270 | n/a | { |
|---|
| 1271 | n/a | PyObject *code; |
|---|
| 1272 | n/a | unsigned char *buf = (unsigned char *)PyBytes_AsString(data); |
|---|
| 1273 | n/a | Py_ssize_t size = PyBytes_Size(data); |
|---|
| 1274 | n/a | |
|---|
| 1275 | n/a | if (size < 12) { |
|---|
| 1276 | n/a | PyErr_SetString(ZipImportError, |
|---|
| 1277 | n/a | "bad pyc data"); |
|---|
| 1278 | n/a | return NULL; |
|---|
| 1279 | n/a | } |
|---|
| 1280 | n/a | |
|---|
| 1281 | n/a | if (get_uint32(buf) != (unsigned int)PyImport_GetMagicNumber()) { |
|---|
| 1282 | n/a | if (Py_VerboseFlag) { |
|---|
| 1283 | n/a | PySys_FormatStderr("# %R has bad magic\n", |
|---|
| 1284 | n/a | pathname); |
|---|
| 1285 | n/a | } |
|---|
| 1286 | n/a | Py_RETURN_NONE; /* signal caller to try alternative */ |
|---|
| 1287 | n/a | } |
|---|
| 1288 | n/a | |
|---|
| 1289 | n/a | if (mtime != 0 && !eq_mtime(get_uint32(buf + 4), mtime)) { |
|---|
| 1290 | n/a | if (Py_VerboseFlag) { |
|---|
| 1291 | n/a | PySys_FormatStderr("# %R has bad mtime\n", |
|---|
| 1292 | n/a | pathname); |
|---|
| 1293 | n/a | } |
|---|
| 1294 | n/a | Py_RETURN_NONE; /* signal caller to try alternative */ |
|---|
| 1295 | n/a | } |
|---|
| 1296 | n/a | |
|---|
| 1297 | n/a | /* XXX the pyc's size field is ignored; timestamp collisions are probably |
|---|
| 1298 | n/a | unimportant with zip files. */ |
|---|
| 1299 | n/a | code = PyMarshal_ReadObjectFromString((char *)buf + 12, size - 12); |
|---|
| 1300 | n/a | if (code == NULL) { |
|---|
| 1301 | n/a | return NULL; |
|---|
| 1302 | n/a | } |
|---|
| 1303 | n/a | if (!PyCode_Check(code)) { |
|---|
| 1304 | n/a | Py_DECREF(code); |
|---|
| 1305 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1306 | n/a | "compiled module %R is not a code object", |
|---|
| 1307 | n/a | pathname); |
|---|
| 1308 | n/a | return NULL; |
|---|
| 1309 | n/a | } |
|---|
| 1310 | n/a | return code; |
|---|
| 1311 | n/a | } |
|---|
| 1312 | n/a | |
|---|
| 1313 | n/a | /* Replace any occurrences of "\r\n?" in the input string with "\n". |
|---|
| 1314 | n/a | This converts DOS and Mac line endings to Unix line endings. |
|---|
| 1315 | n/a | Also append a trailing "\n" to be compatible with |
|---|
| 1316 | n/a | PyParser_SimpleParseFile(). Returns a new reference. */ |
|---|
| 1317 | n/a | static PyObject * |
|---|
| 1318 | n/a | normalize_line_endings(PyObject *source) |
|---|
| 1319 | n/a | { |
|---|
| 1320 | n/a | char *buf, *q, *p; |
|---|
| 1321 | n/a | PyObject *fixed_source; |
|---|
| 1322 | n/a | int len = 0; |
|---|
| 1323 | n/a | |
|---|
| 1324 | n/a | p = PyBytes_AsString(source); |
|---|
| 1325 | n/a | if (p == NULL) { |
|---|
| 1326 | n/a | return PyBytes_FromStringAndSize("\n\0", 2); |
|---|
| 1327 | n/a | } |
|---|
| 1328 | n/a | |
|---|
| 1329 | n/a | /* one char extra for trailing \n and one for terminating \0 */ |
|---|
| 1330 | n/a | buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2); |
|---|
| 1331 | n/a | if (buf == NULL) { |
|---|
| 1332 | n/a | PyErr_SetString(PyExc_MemoryError, |
|---|
| 1333 | n/a | "zipimport: no memory to allocate " |
|---|
| 1334 | n/a | "source buffer"); |
|---|
| 1335 | n/a | return NULL; |
|---|
| 1336 | n/a | } |
|---|
| 1337 | n/a | /* replace "\r\n?" by "\n" */ |
|---|
| 1338 | n/a | for (q = buf; *p != '\0'; p++) { |
|---|
| 1339 | n/a | if (*p == '\r') { |
|---|
| 1340 | n/a | *q++ = '\n'; |
|---|
| 1341 | n/a | if (*(p + 1) == '\n') |
|---|
| 1342 | n/a | p++; |
|---|
| 1343 | n/a | } |
|---|
| 1344 | n/a | else |
|---|
| 1345 | n/a | *q++ = *p; |
|---|
| 1346 | n/a | len++; |
|---|
| 1347 | n/a | } |
|---|
| 1348 | n/a | *q++ = '\n'; /* add trailing \n */ |
|---|
| 1349 | n/a | *q = '\0'; |
|---|
| 1350 | n/a | fixed_source = PyBytes_FromStringAndSize(buf, len + 2); |
|---|
| 1351 | n/a | PyMem_Free(buf); |
|---|
| 1352 | n/a | return fixed_source; |
|---|
| 1353 | n/a | } |
|---|
| 1354 | n/a | |
|---|
| 1355 | n/a | /* Given a string buffer containing Python source code, compile it |
|---|
| 1356 | n/a | and return a code object as a new reference. */ |
|---|
| 1357 | n/a | static PyObject * |
|---|
| 1358 | n/a | compile_source(PyObject *pathname, PyObject *source) |
|---|
| 1359 | n/a | { |
|---|
| 1360 | n/a | PyObject *code, *fixed_source; |
|---|
| 1361 | n/a | |
|---|
| 1362 | n/a | fixed_source = normalize_line_endings(source); |
|---|
| 1363 | n/a | if (fixed_source == NULL) { |
|---|
| 1364 | n/a | return NULL; |
|---|
| 1365 | n/a | } |
|---|
| 1366 | n/a | |
|---|
| 1367 | n/a | code = Py_CompileStringObject(PyBytes_AsString(fixed_source), |
|---|
| 1368 | n/a | pathname, Py_file_input, NULL, -1); |
|---|
| 1369 | n/a | |
|---|
| 1370 | n/a | Py_DECREF(fixed_source); |
|---|
| 1371 | n/a | return code; |
|---|
| 1372 | n/a | } |
|---|
| 1373 | n/a | |
|---|
| 1374 | n/a | /* Convert the date/time values found in the Zip archive to a value |
|---|
| 1375 | n/a | that's compatible with the time stamp stored in .pyc files. */ |
|---|
| 1376 | n/a | static time_t |
|---|
| 1377 | n/a | parse_dostime(int dostime, int dosdate) |
|---|
| 1378 | n/a | { |
|---|
| 1379 | n/a | struct tm stm; |
|---|
| 1380 | n/a | |
|---|
| 1381 | n/a | memset((void *) &stm, '\0', sizeof(stm)); |
|---|
| 1382 | n/a | |
|---|
| 1383 | n/a | stm.tm_sec = (dostime & 0x1f) * 2; |
|---|
| 1384 | n/a | stm.tm_min = (dostime >> 5) & 0x3f; |
|---|
| 1385 | n/a | stm.tm_hour = (dostime >> 11) & 0x1f; |
|---|
| 1386 | n/a | stm.tm_mday = dosdate & 0x1f; |
|---|
| 1387 | n/a | stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1; |
|---|
| 1388 | n/a | stm.tm_year = ((dosdate >> 9) & 0x7f) + 80; |
|---|
| 1389 | n/a | stm.tm_isdst = -1; /* wday/yday is ignored */ |
|---|
| 1390 | n/a | |
|---|
| 1391 | n/a | return mktime(&stm); |
|---|
| 1392 | n/a | } |
|---|
| 1393 | n/a | |
|---|
| 1394 | n/a | /* Given a path to a .pyc file in the archive, return the |
|---|
| 1395 | n/a | modification time of the matching .py file, or 0 if no source |
|---|
| 1396 | n/a | is available. */ |
|---|
| 1397 | n/a | static time_t |
|---|
| 1398 | n/a | get_mtime_of_source(ZipImporter *self, PyObject *path) |
|---|
| 1399 | n/a | { |
|---|
| 1400 | n/a | PyObject *toc_entry, *stripped; |
|---|
| 1401 | n/a | time_t mtime; |
|---|
| 1402 | n/a | |
|---|
| 1403 | n/a | /* strip 'c' or 'o' from *.py[co] */ |
|---|
| 1404 | n/a | if (PyUnicode_READY(path) == -1) |
|---|
| 1405 | n/a | return (time_t)-1; |
|---|
| 1406 | n/a | stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path), |
|---|
| 1407 | n/a | PyUnicode_DATA(path), |
|---|
| 1408 | n/a | PyUnicode_GET_LENGTH(path) - 1); |
|---|
| 1409 | n/a | if (stripped == NULL) |
|---|
| 1410 | n/a | return (time_t)-1; |
|---|
| 1411 | n/a | |
|---|
| 1412 | n/a | toc_entry = PyDict_GetItem(self->files, stripped); |
|---|
| 1413 | n/a | Py_DECREF(stripped); |
|---|
| 1414 | n/a | if (toc_entry != NULL && PyTuple_Check(toc_entry) && |
|---|
| 1415 | n/a | PyTuple_Size(toc_entry) == 8) { |
|---|
| 1416 | n/a | /* fetch the time stamp of the .py file for comparison |
|---|
| 1417 | n/a | with an embedded pyc time stamp */ |
|---|
| 1418 | n/a | int time, date; |
|---|
| 1419 | n/a | time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5)); |
|---|
| 1420 | n/a | date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6)); |
|---|
| 1421 | n/a | mtime = parse_dostime(time, date); |
|---|
| 1422 | n/a | } else |
|---|
| 1423 | n/a | mtime = 0; |
|---|
| 1424 | n/a | return mtime; |
|---|
| 1425 | n/a | } |
|---|
| 1426 | n/a | |
|---|
| 1427 | n/a | /* Return the code object for the module named by 'fullname' from the |
|---|
| 1428 | n/a | Zip archive as a new reference. */ |
|---|
| 1429 | n/a | static PyObject * |
|---|
| 1430 | n/a | get_code_from_data(ZipImporter *self, int ispackage, int isbytecode, |
|---|
| 1431 | n/a | time_t mtime, PyObject *toc_entry) |
|---|
| 1432 | n/a | { |
|---|
| 1433 | n/a | PyObject *data, *modpath, *code; |
|---|
| 1434 | n/a | |
|---|
| 1435 | n/a | data = get_data(self->archive, toc_entry); |
|---|
| 1436 | n/a | if (data == NULL) |
|---|
| 1437 | n/a | return NULL; |
|---|
| 1438 | n/a | |
|---|
| 1439 | n/a | modpath = PyTuple_GetItem(toc_entry, 0); |
|---|
| 1440 | n/a | if (isbytecode) |
|---|
| 1441 | n/a | code = unmarshal_code(modpath, data, mtime); |
|---|
| 1442 | n/a | else |
|---|
| 1443 | n/a | code = compile_source(modpath, data); |
|---|
| 1444 | n/a | Py_DECREF(data); |
|---|
| 1445 | n/a | return code; |
|---|
| 1446 | n/a | } |
|---|
| 1447 | n/a | |
|---|
| 1448 | n/a | /* Get the code object associated with the module specified by |
|---|
| 1449 | n/a | 'fullname'. */ |
|---|
| 1450 | n/a | static PyObject * |
|---|
| 1451 | n/a | get_module_code(ZipImporter *self, PyObject *fullname, |
|---|
| 1452 | n/a | int *p_ispackage, PyObject **p_modpath) |
|---|
| 1453 | n/a | { |
|---|
| 1454 | n/a | PyObject *code = NULL, *toc_entry, *subname; |
|---|
| 1455 | n/a | PyObject *path, *fullpath = NULL; |
|---|
| 1456 | n/a | struct st_zip_searchorder *zso; |
|---|
| 1457 | n/a | |
|---|
| 1458 | n/a | subname = get_subname(fullname); |
|---|
| 1459 | n/a | if (subname == NULL) |
|---|
| 1460 | n/a | return NULL; |
|---|
| 1461 | n/a | |
|---|
| 1462 | n/a | path = make_filename(self->prefix, subname); |
|---|
| 1463 | n/a | Py_DECREF(subname); |
|---|
| 1464 | n/a | if (path == NULL) |
|---|
| 1465 | n/a | return NULL; |
|---|
| 1466 | n/a | |
|---|
| 1467 | n/a | for (zso = zip_searchorder; *zso->suffix; zso++) { |
|---|
| 1468 | n/a | code = NULL; |
|---|
| 1469 | n/a | |
|---|
| 1470 | n/a | fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
|---|
| 1471 | n/a | if (fullpath == NULL) |
|---|
| 1472 | n/a | goto exit; |
|---|
| 1473 | n/a | |
|---|
| 1474 | n/a | if (Py_VerboseFlag > 1) |
|---|
| 1475 | n/a | PySys_FormatStderr("# trying %U%c%U\n", |
|---|
| 1476 | n/a | self->archive, (int)SEP, fullpath); |
|---|
| 1477 | n/a | toc_entry = PyDict_GetItem(self->files, fullpath); |
|---|
| 1478 | n/a | if (toc_entry != NULL) { |
|---|
| 1479 | n/a | time_t mtime = 0; |
|---|
| 1480 | n/a | int ispackage = zso->type & IS_PACKAGE; |
|---|
| 1481 | n/a | int isbytecode = zso->type & IS_BYTECODE; |
|---|
| 1482 | n/a | |
|---|
| 1483 | n/a | if (isbytecode) { |
|---|
| 1484 | n/a | mtime = get_mtime_of_source(self, fullpath); |
|---|
| 1485 | n/a | if (mtime == (time_t)-1 && PyErr_Occurred()) { |
|---|
| 1486 | n/a | goto exit; |
|---|
| 1487 | n/a | } |
|---|
| 1488 | n/a | } |
|---|
| 1489 | n/a | Py_CLEAR(fullpath); |
|---|
| 1490 | n/a | if (p_ispackage != NULL) |
|---|
| 1491 | n/a | *p_ispackage = ispackage; |
|---|
| 1492 | n/a | code = get_code_from_data(self, ispackage, |
|---|
| 1493 | n/a | isbytecode, mtime, |
|---|
| 1494 | n/a | toc_entry); |
|---|
| 1495 | n/a | if (code == Py_None) { |
|---|
| 1496 | n/a | /* bad magic number or non-matching mtime |
|---|
| 1497 | n/a | in byte code, try next */ |
|---|
| 1498 | n/a | Py_DECREF(code); |
|---|
| 1499 | n/a | continue; |
|---|
| 1500 | n/a | } |
|---|
| 1501 | n/a | if (code != NULL && p_modpath != NULL) { |
|---|
| 1502 | n/a | *p_modpath = PyTuple_GetItem(toc_entry, 0); |
|---|
| 1503 | n/a | Py_INCREF(*p_modpath); |
|---|
| 1504 | n/a | } |
|---|
| 1505 | n/a | goto exit; |
|---|
| 1506 | n/a | } |
|---|
| 1507 | n/a | else |
|---|
| 1508 | n/a | Py_CLEAR(fullpath); |
|---|
| 1509 | n/a | } |
|---|
| 1510 | n/a | PyErr_Format(ZipImportError, "can't find module %R", fullname); |
|---|
| 1511 | n/a | exit: |
|---|
| 1512 | n/a | Py_DECREF(path); |
|---|
| 1513 | n/a | Py_XDECREF(fullpath); |
|---|
| 1514 | n/a | return code; |
|---|
| 1515 | n/a | } |
|---|
| 1516 | n/a | |
|---|
| 1517 | n/a | |
|---|
| 1518 | n/a | /* Module init */ |
|---|
| 1519 | n/a | |
|---|
| 1520 | n/a | PyDoc_STRVAR(zipimport_doc, |
|---|
| 1521 | n/a | "zipimport provides support for importing Python modules from Zip archives.\n\ |
|---|
| 1522 | n/a | \n\ |
|---|
| 1523 | n/a | This module exports three objects:\n\ |
|---|
| 1524 | n/a | - zipimporter: a class; its constructor takes a path to a Zip archive.\n\ |
|---|
| 1525 | n/a | - ZipImportError: exception raised by zipimporter objects. It's a\n\ |
|---|
| 1526 | n/a | subclass of ImportError, so it can be caught as ImportError, too.\n\ |
|---|
| 1527 | n/a | - _zip_directory_cache: a dict, mapping archive paths to zip directory\n\ |
|---|
| 1528 | n/a | info dicts, as used in zipimporter._files.\n\ |
|---|
| 1529 | n/a | \n\ |
|---|
| 1530 | n/a | It is usually not needed to use the zipimport module explicitly; it is\n\ |
|---|
| 1531 | n/a | used by the builtin import mechanism for sys.path items that are paths\n\ |
|---|
| 1532 | n/a | to Zip archives."); |
|---|
| 1533 | n/a | |
|---|
| 1534 | n/a | static struct PyModuleDef zipimportmodule = { |
|---|
| 1535 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 1536 | n/a | "zipimport", |
|---|
| 1537 | n/a | zipimport_doc, |
|---|
| 1538 | n/a | -1, |
|---|
| 1539 | n/a | NULL, |
|---|
| 1540 | n/a | NULL, |
|---|
| 1541 | n/a | NULL, |
|---|
| 1542 | n/a | NULL, |
|---|
| 1543 | n/a | NULL |
|---|
| 1544 | n/a | }; |
|---|
| 1545 | n/a | |
|---|
| 1546 | n/a | PyMODINIT_FUNC |
|---|
| 1547 | n/a | PyInit_zipimport(void) |
|---|
| 1548 | n/a | { |
|---|
| 1549 | n/a | PyObject *mod; |
|---|
| 1550 | n/a | |
|---|
| 1551 | n/a | if (PyType_Ready(&ZipImporter_Type) < 0) |
|---|
| 1552 | n/a | return NULL; |
|---|
| 1553 | n/a | |
|---|
| 1554 | n/a | /* Correct directory separator */ |
|---|
| 1555 | n/a | zip_searchorder[0].suffix[0] = SEP; |
|---|
| 1556 | n/a | zip_searchorder[1].suffix[0] = SEP; |
|---|
| 1557 | n/a | |
|---|
| 1558 | n/a | mod = PyModule_Create(&zipimportmodule); |
|---|
| 1559 | n/a | if (mod == NULL) |
|---|
| 1560 | n/a | return NULL; |
|---|
| 1561 | n/a | |
|---|
| 1562 | n/a | ZipImportError = PyErr_NewException("zipimport.ZipImportError", |
|---|
| 1563 | n/a | PyExc_ImportError, NULL); |
|---|
| 1564 | n/a | if (ZipImportError == NULL) |
|---|
| 1565 | n/a | return NULL; |
|---|
| 1566 | n/a | |
|---|
| 1567 | n/a | Py_INCREF(ZipImportError); |
|---|
| 1568 | n/a | if (PyModule_AddObject(mod, "ZipImportError", |
|---|
| 1569 | n/a | ZipImportError) < 0) |
|---|
| 1570 | n/a | return NULL; |
|---|
| 1571 | n/a | |
|---|
| 1572 | n/a | Py_INCREF(&ZipImporter_Type); |
|---|
| 1573 | n/a | if (PyModule_AddObject(mod, "zipimporter", |
|---|
| 1574 | n/a | (PyObject *)&ZipImporter_Type) < 0) |
|---|
| 1575 | n/a | return NULL; |
|---|
| 1576 | n/a | |
|---|
| 1577 | n/a | zip_directory_cache = PyDict_New(); |
|---|
| 1578 | n/a | if (zip_directory_cache == NULL) |
|---|
| 1579 | n/a | return NULL; |
|---|
| 1580 | n/a | Py_INCREF(zip_directory_cache); |
|---|
| 1581 | n/a | if (PyModule_AddObject(mod, "_zip_directory_cache", |
|---|
| 1582 | n/a | zip_directory_cache) < 0) |
|---|
| 1583 | n/a | return NULL; |
|---|
| 1584 | n/a | return mod; |
|---|
| 1585 | n/a | } |
|---|