blob: cc1dffb6f2482539dfa4229ab4c88d0d002f3fd7 [file] [log] [blame]
Just van Rossum52e14d62002-12-30 22:08:05 +00001#include "Python.h"
2#include "structmember.h"
3#include "osdefs.h"
4#include "marshal.h"
Just van Rossum52e14d62002-12-30 22:08:05 +00005#include <time.h>
6
7
8#define IS_SOURCE 0x0
9#define IS_BYTECODE 0x1
10#define IS_PACKAGE 0x2
11
12struct st_zip_searchorder {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 char suffix[14];
14 int type;
Just van Rossum52e14d62002-12-30 22:08:05 +000015};
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 Norwitz29fd2ba2003-03-23 13:21:03 +000022static struct st_zip_searchorder zip_searchorder[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 {"/__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 Rossum52e14d62002-12-30 22:08:05 +000030};
31
32/* zipimporter object definition and support */
33
34typedef struct _zipimporter ZipImporter;
35
36struct _zipimporter {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 PyObject_HEAD
Victor Stinner9e40fad2010-10-18 22:34:46 +000038 PyObject *archive; /* pathname of the Zip archive,
39 decoded from the filesystem encoding */
Victor Stinner72f767e2010-10-18 11:44:21 +000040 PyObject *prefix; /* file prefix: "a/sub/directory/",
41 encoded to the filesystem encoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 PyObject *files; /* dict with file info {path: toc_entry} */
Just van Rossum52e14d62002-12-30 22:08:05 +000043};
44
Just van Rossum52e14d62002-12-30 22:08:05 +000045static PyObject *ZipImportError;
Victor Stinnerc342fca2010-10-18 11:39:05 +000046/* read_directory() cache */
Just van Rossum52e14d62002-12-30 22:08:05 +000047static PyObject *zip_directory_cache = NULL;
48
49/* forward decls */
Victor Stinner2460a432010-08-16 17:54:28 +000050static PyObject *read_directory(PyObject *archive);
Victor Stinner60fe8d92010-08-16 23:48:11 +000051static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
Victor Stinnerf6b563a2011-03-14 20:46:50 -040052static PyObject *get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +000053 int *p_ispackage, PyObject **p_modpath);
Just van Rossum52e14d62002-12-30 22:08:05 +000054
55
56#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
57
58
59/* zipimporter.__init__
60 Split the "subdirectory" from the Zip archive path, lookup a matching
61 entry in sys.path_importer_cache, fetch the file directory from there
62 if found, or else read it from the archive. */
63static int
64zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
65{
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010066 PyObject *path, *files, *tmp;
67 PyObject *filename = NULL;
68 Py_ssize_t len, flen;
69#ifdef ALTSEP
70 _Py_IDENTIFIER(replace);
71#endif
Just van Rossum52e14d62002-12-30 22:08:05 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (!_PyArg_NoKeywords("zipimporter()", kwds))
74 return -1;
Georg Brandl02c42872005-08-26 06:42:30 +000075
Victor Stinner2b8dab72010-08-14 14:54:10 +000076 if (!PyArg_ParseTuple(args, "O&:zipimporter",
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010077 PyUnicode_FSDecoder, &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +000079
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010080 if (PyUnicode_READY(path) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020081 return -1;
82
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010083 len = PyUnicode_GET_LENGTH(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (len == 0) {
85 PyErr_SetString(ZipImportError, "archive path is empty");
Victor Stinner2b8dab72010-08-14 14:54:10 +000086 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 }
Just van Rossum52e14d62002-12-30 22:08:05 +000088
89#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +010090 tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010091 if (!tmp)
92 goto error;
93 Py_DECREF(path);
94 path = tmp;
Just van Rossum52e14d62002-12-30 22:08:05 +000095#endif
96
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010097 filename = path;
98 Py_INCREF(filename);
99 flen = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 for (;;) {
101 struct stat statbuf;
102 int rv;
Just van Rossum52e14d62002-12-30 22:08:05 +0000103
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100104 rv = _Py_stat(filename, &statbuf);
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100105 if (rv == -2)
106 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (rv == 0) {
108 /* it exists */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100109 if (!S_ISREG(statbuf.st_mode))
110 /* it's a not file */
111 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 break;
113 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100114 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 /* back up one path element */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100116 flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);
117 if (flen == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 break;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100119 filename = PyUnicode_Substring(path, 0, flen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100121 if (filename == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 PyErr_SetString(ZipImportError, "not a Zip file");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000123 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000125
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100126 if (PyUnicode_READY(filename) < 0)
127 goto error;
128
129 files = PyDict_GetItem(zip_directory_cache, filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000130 if (files == NULL) {
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100131 files = read_directory(filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000132 if (files == NULL)
133 goto error;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100134 if (PyDict_SetItem(zip_directory_cache, filename, files) != 0)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000135 goto error;
136 }
137 else
138 Py_INCREF(files);
139 self->files = files;
140
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100141 /* Transfer reference */
142 self->archive = filename;
143 filename = NULL;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000144
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100145 /* Check if there is a prefix directory following the filename. */
146 if (flen != len) {
147 tmp = PyUnicode_Substring(path, flen+1,
148 PyUnicode_GET_LENGTH(path));
149 if (tmp == NULL)
150 goto error;
151 self->prefix = tmp;
152 if (PyUnicode_READ_CHAR(path, len-1) != SEP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 /* add trailing SEP */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100154 tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
155 if (tmp == NULL)
156 goto error;
157 Py_DECREF(self->prefix);
158 self->prefix = tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 }
160 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000161 else
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100162 self->prefix = PyUnicode_New(0, 0);
163 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return 0;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000165
166error:
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100167 Py_DECREF(path);
168 Py_XDECREF(filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000169 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000170}
171
172/* GC support. */
173static int
174zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 ZipImporter *self = (ZipImporter *)obj;
177 Py_VISIT(self->files);
178 return 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000179}
180
181static void
182zipimporter_dealloc(ZipImporter *self)
183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyObject_GC_UnTrack(self);
185 Py_XDECREF(self->archive);
186 Py_XDECREF(self->prefix);
187 Py_XDECREF(self->files);
188 Py_TYPE(self)->tp_free((PyObject *)self);
Just van Rossum52e14d62002-12-30 22:08:05 +0000189}
190
191static PyObject *
192zipimporter_repr(ZipImporter *self)
193{
Victor Stinner028dd972010-08-17 00:04:48 +0000194 if (self->archive == NULL)
195 return PyUnicode_FromString("<zipimporter object \"???\">");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200196 else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0)
Victor Stinner07298a12010-10-18 22:45:54 +0000197 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000198 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 else
Victor Stinner07298a12010-10-18 22:45:54 +0000200 return PyUnicode_FromFormat("<zipimporter object \"%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000201 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000202}
203
204/* return fullname.split(".")[-1] */
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400205static PyObject *
206get_subname(PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000207{
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100208 Py_ssize_t len, dot;
209 if (PyUnicode_READY(fullname) < 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200210 return NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100211 len = PyUnicode_GET_LENGTH(fullname);
212 dot = PyUnicode_FindChar(fullname, '.', 0, len, -1);
213 if (dot == -1) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400214 Py_INCREF(fullname);
215 return fullname;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100216 } else
217 return PyUnicode_Substring(fullname, dot+1, len);
Just van Rossum52e14d62002-12-30 22:08:05 +0000218}
219
220/* Given a (sub)modulename, write the potential file path in the
221 archive (without extension) to the path buffer. Return the
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400222 length of the resulting string.
223
224 return self.prefix + name.replace('.', os.sep) */
225static PyObject*
226make_filename(PyObject *prefix, PyObject *name)
Just van Rossum52e14d62002-12-30 22:08:05 +0000227{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400228 PyObject *pathobj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200229 Py_UCS4 *p, *buf;
230 Py_ssize_t len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000231
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200232 len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1;
233 p = buf = PyMem_Malloc(sizeof(Py_UCS4) * len);
234 if (buf == NULL) {
235 PyErr_NoMemory();
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400236 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200237 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000238
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200239 if (!PyUnicode_AsUCS4(prefix, p, len, 0))
240 return NULL;
241 p += PyUnicode_GET_LENGTH(prefix);
242 len -= PyUnicode_GET_LENGTH(prefix);
243 if (!PyUnicode_AsUCS4(name, p, len, 1))
244 return NULL;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400245 for (; *p; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (*p == '.')
247 *p = SEP;
248 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200249 pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
250 buf, p-buf);
251 PyMem_Free(buf);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400252 return pathobj;
Just van Rossum52e14d62002-12-30 22:08:05 +0000253}
254
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000255enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 MI_ERROR,
257 MI_NOT_FOUND,
258 MI_MODULE,
259 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000260};
261
Eric V. Smith984b11f2012-05-24 20:21:04 -0400262/* Does this path represent a directory?
263 on error, return < 0
264 if not a dir, return 0
265 if a dir, return 1
266*/
267static int
268check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path)
269{
270 PyObject *dirpath;
271 PyObject *item;
272
273 /* See if this is a "directory". If so, it's eligible to be part
274 of a namespace package. We test by seeing if the name, with an
275 appended path separator, exists. */
276 dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
277 if (dirpath == NULL)
278 return -1;
279 /* If dirpath is present in self->files, we have a directory. */
280 item = PyDict_GetItem(self->files, dirpath);
281 Py_DECREF(dirpath);
282 return item != NULL;
283}
284
Just van Rossum52e14d62002-12-30 22:08:05 +0000285/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000286static enum zi_module_info
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400287get_module_info(ZipImporter *self, PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000288{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400289 PyObject *subname;
290 PyObject *path, *fullpath, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000292
Victor Stinner965a8a12010-10-18 21:44:33 +0000293 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400294 if (subname == NULL)
295 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000296
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400297 path = make_filename(self->prefix, subname);
298 Py_DECREF(subname);
299 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400303 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
304 if (fullpath == NULL) {
305 Py_DECREF(path);
306 return MI_ERROR;
307 }
308 item = PyDict_GetItem(self->files, fullpath);
309 Py_DECREF(fullpath);
310 if (item != NULL) {
311 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (zso->type & IS_PACKAGE)
313 return MI_PACKAGE;
314 else
315 return MI_MODULE;
316 }
317 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400318 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000320}
321
Eric V. Smith984b11f2012-05-24 20:21:04 -0400322/* The guts of "find_loader" and "find_module". Return values:
323 -1: error
324 0: no loader or namespace portions found
325 1: module/package found
326 2: namespace portion found: *namespace_portion will point to the name
327*/
328static int
329find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
330{
331 enum zi_module_info mi;
332
333 *namespace_portion = NULL;
334
335 mi = get_module_info(self, fullname);
336 if (mi == MI_ERROR)
337 return -1;
338 if (mi == MI_NOT_FOUND) {
339 /* Not a module or regular package. See if this is a directory, and
340 therefore possibly a portion of a namespace package. */
341 int is_dir = check_is_directory(self, self->prefix, fullname);
342 if (is_dir < 0)
343 return -1;
344 if (is_dir) {
345 /* This is possibly a portion of a namespace
346 package. Return the string representing its path,
347 without a trailing separator. */
348 *namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
349 self->archive, SEP,
350 self->prefix, fullname);
351 if (*namespace_portion == NULL)
352 return -1;
353 return 2;
354 }
355 return 0;
356 }
357 /* This is a module or package. */
358 return 1;
359}
360
361
Just van Rossum52e14d62002-12-30 22:08:05 +0000362/* Check whether we can satisfy the import of the module named by
363 'fullname'. Return self if we can, None if we can't. */
364static PyObject *
365zipimporter_find_module(PyObject *obj, PyObject *args)
366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 ZipImporter *self = (ZipImporter *)obj;
368 PyObject *path = NULL;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400369 PyObject *fullname;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400370 PyObject* namespace_portion = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000371
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400372 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module",
Victor Stinner965a8a12010-10-18 21:44:33 +0000373 &fullname, &path))
Eric V. Smith984b11f2012-05-24 20:21:04 -0400374 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000375
Eric V. Smith984b11f2012-05-24 20:21:04 -0400376 switch (find_loader(self, fullname, &namespace_portion)) {
377 case -1: /* Error */
378 goto error;
379 case 0: /* Not found, return None */
380 Py_INCREF(Py_None);
381 return Py_None;
382 case 1: /* Return self */
383 Py_INCREF(self);
384 return (PyObject *)self;
385 case 2: /* A namespace portion, but not allowed via
386 find_module, so return None */
387 Py_DECREF(namespace_portion);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 Py_INCREF(Py_None);
389 return Py_None;
390 }
Eric V. Smith984b11f2012-05-24 20:21:04 -0400391 /* Can't get here. */
392 assert(0);
393 return NULL;
394error:
395 Py_XDECREF(namespace_portion);
396 return NULL;
397}
398
399
400/* Check whether we can satisfy the import of the module named by
401 'fullname', or whether it could be a portion of a namespace
402 package. Return self if we can load it, a string containing the
403 full path if it's a possible namespace portion, None if we
404 can't load it. */
405static PyObject *
406zipimporter_find_loader(PyObject *obj, PyObject *args)
407{
408 ZipImporter *self = (ZipImporter *)obj;
409 PyObject *path = NULL;
410 PyObject *fullname;
411 PyObject *result = NULL;
412 PyObject *namespace_portion = NULL;
413
414 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module",
415 &fullname, &path))
416 goto error;
417
418 switch (find_loader(self, fullname, &namespace_portion)) {
419 case -1: /* Error */
420 goto error;
421 case 0: /* Not found, return (None, []) */
422 if (!(result = Py_BuildValue("O[]", Py_None)))
423 goto error;
424 return result;
425 case 1: /* Return (self, []) */
426 if (!(result = Py_BuildValue("O[]", self)))
427 goto error;
428 return result;
429 case 2: /* Return (None, [namespace_portion]) */
430 if (!(result = Py_BuildValue("O[O]", Py_None, namespace_portion)))
431 goto error;
Benjamin Peterson209e04c2012-05-24 22:35:39 -0700432 Py_DECREF(namespace_portion);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400433 return result;
434 }
435 /* Can't get here. */
436 assert(0);
437 return NULL;
438
439error:
440 Py_XDECREF(namespace_portion);
441 Py_XDECREF(result);
442 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000443}
444
445/* Load and return the module named by 'fullname'. */
446static PyObject *
447zipimporter_load_module(PyObject *obj, PyObject *args)
448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner26fabe12010-10-18 12:03:25 +0000450 PyObject *code = NULL, *mod, *dict;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400451 PyObject *fullname;
452 PyObject *modpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000454
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400455 if (!PyArg_ParseTuple(args, "U:zipimporter.load_module",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 &fullname))
457 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200458 if (PyUnicode_READY(fullname) == -1)
459 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 code = get_module_code(self, fullname, &ispackage, &modpath);
462 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000463 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000464
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400465 mod = PyImport_AddModuleObject(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000466 if (mod == NULL)
467 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 /* mod.__loader__ = self */
471 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
472 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (ispackage) {
475 /* add __path__ to the module *before* the code gets
476 executed */
477 PyObject *pkgpath, *fullpath;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400478 PyObject *subname = get_subname(fullname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000480
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400481 fullpath = PyUnicode_FromFormat("%U%c%U%U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 self->archive, SEP,
483 self->prefix, subname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400484 Py_DECREF(subname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (fullpath == NULL)
486 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000487
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400488 pkgpath = Py_BuildValue("[N]", fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 if (pkgpath == NULL)
490 goto error;
491 err = PyDict_SetItemString(dict, "__path__", pkgpath);
492 Py_DECREF(pkgpath);
493 if (err != 0)
494 goto error;
495 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400496 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
Victor Stinner26fabe12010-10-18 12:03:25 +0000497 Py_CLEAR(code);
498 if (mod == NULL)
499 goto error;
500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400502 PySys_FormatStderr("import %U # loaded from Zip %U\n",
Victor Stinner08654e12010-10-18 12:09:02 +0000503 fullname, modpath);
504 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000506error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000507 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000508 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000510}
511
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000512/* Return a string matching __file__ for the named module */
513static PyObject *
514zipimporter_get_filename(PyObject *obj, PyObject *args)
515{
516 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400517 PyObject *fullname, *code, *modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000518 int ispackage;
519
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400520 if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename",
Victor Stinner9e40fad2010-10-18 22:34:46 +0000521 &fullname))
Victor Stinnerc342fca2010-10-18 11:39:05 +0000522 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000523
524 /* Deciding the filename requires working out where the code
525 would come from if the module was actually loaded */
526 code = get_module_code(self, fullname, &ispackage, &modpath);
527 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000528 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000529 Py_DECREF(code); /* Only need the path info */
530
Victor Stinner08654e12010-10-18 12:09:02 +0000531 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000532}
533
Just van Rossum52e14d62002-12-30 22:08:05 +0000534/* Return a bool signifying whether the module is a package or not. */
535static PyObject *
536zipimporter_is_package(PyObject *obj, PyObject *args)
537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400539 PyObject *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000541
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400542 if (!PyArg_ParseTuple(args, "U:zipimporter.is_package",
Victor Stinner965a8a12010-10-18 21:44:33 +0000543 &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 mi = get_module_info(self, fullname);
547 if (mi == MI_ERROR)
Victor Stinner965a8a12010-10-18 21:44:33 +0000548 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400550 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000551 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 }
553 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000554}
555
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200556
Just van Rossum52e14d62002-12-30 22:08:05 +0000557static PyObject *
558zipimporter_get_data(PyObject *obj, PyObject *args)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 ZipImporter *self = (ZipImporter *)obj;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100561 PyObject *path, *key;
Just van Rossum52e14d62002-12-30 22:08:05 +0000562#ifdef ALTSEP
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100563 _Py_IDENTIFIER(replace);
Just van Rossum52e14d62002-12-30 22:08:05 +0000564#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 PyObject *toc_entry;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100566 Py_ssize_t path_start, path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000567
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100568 if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000570
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200571#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +0100572 path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100573 if (!path)
574 return NULL;
575#else
576 Py_INCREF(path);
Just van Rossum52e14d62002-12-30 22:08:05 +0000577#endif
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100578 if (PyUnicode_READY(path) == -1)
579 goto error;
580
581 path_len = PyUnicode_GET_LENGTH(path);
582
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200583 len = PyUnicode_GET_LENGTH(self->archive);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100584 path_start = 0;
585 if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
586 && PyUnicode_READ_CHAR(path, len) == SEP) {
587 path_start = len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000589
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100590 key = PyUnicode_Substring(path, path_start, path_len);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000591 if (key == NULL)
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100592 goto error;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000593 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 if (toc_entry == NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000595 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
596 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100597 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000599 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100600 Py_DECREF(path);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000601 return get_data(self->archive, toc_entry);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100602 error:
603 Py_DECREF(path);
604 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000605}
606
607static PyObject *
608zipimporter_get_code(PyObject *obj, PyObject *args)
609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400611 PyObject *fullname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000612
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400613 if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000617}
618
619static PyObject *
620zipimporter_get_source(PyObject *obj, PyObject *args)
621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 ZipImporter *self = (ZipImporter *)obj;
623 PyObject *toc_entry;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400624 PyObject *fullname, *subname, *path, *fullpath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000626
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400627 if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000631 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000633 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400634 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000635 return NULL;
636 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400637
Victor Stinner965a8a12010-10-18 21:44:33 +0000638 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400639 if (subname == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000641
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400642 path = make_filename(self->prefix, subname);
643 Py_DECREF(subname);
644 if (path == NULL)
645 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000646
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400647 if (mi == MI_PACKAGE)
648 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
649 else
650 fullpath = PyUnicode_FromFormat("%U.py", path);
651 Py_DECREF(path);
652 if (fullpath == NULL)
653 return NULL;
654
655 toc_entry = PyDict_GetItem(self->files, fullpath);
656 Py_DECREF(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000658 PyObject *res, *bytes;
659 bytes = get_data(self->archive, toc_entry);
660 if (bytes == NULL)
661 return NULL;
662 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
663 PyBytes_GET_SIZE(bytes));
664 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 return res;
666 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 /* we have the module, but no source */
669 Py_INCREF(Py_None);
670 return Py_None;
Just van Rossum52e14d62002-12-30 22:08:05 +0000671}
672
673PyDoc_STRVAR(doc_find_module,
674"find_module(fullname, path=None) -> self or None.\n\
675\n\
676Search for a module specified by 'fullname'. 'fullname' must be the\n\
677fully qualified (dotted) module name. It returns the zipimporter\n\
678instance itself if the module was found, or None if it wasn't.\n\
679The optional 'path' argument is ignored -- it's there for compatibility\n\
680with the importer protocol.");
681
Eric V. Smith984b11f2012-05-24 20:21:04 -0400682PyDoc_STRVAR(doc_find_loader,
683"find_loader(fullname, path=None) -> self, str or None.\n\
684\n\
685Search for a module specified by 'fullname'. 'fullname' must be the\n\
686fully qualified (dotted) module name. It returns the zipimporter\n\
687instance itself if the module was found, a string containing the\n\
688full path name if it's possibly a portion of a namespace package,\n\
689or None otherwise. The optional 'path' argument is ignored -- it's\n\
690 there for compatibility with the importer protocol.");
691
Just van Rossum52e14d62002-12-30 22:08:05 +0000692PyDoc_STRVAR(doc_load_module,
693"load_module(fullname) -> module.\n\
694\n\
695Load the module specified by 'fullname'. 'fullname' must be the\n\
696fully qualified (dotted) module name. It returns the imported\n\
697module, or raises ZipImportError if it wasn't found.");
698
699PyDoc_STRVAR(doc_get_data,
700"get_data(pathname) -> string with file data.\n\
701\n\
702Return the data associated with 'pathname'. Raise IOError if\n\
703the file wasn't found.");
704
705PyDoc_STRVAR(doc_is_package,
706"is_package(fullname) -> bool.\n\
707\n\
708Return True if the module specified by fullname is a package.\n\
Brian Curtin32839732010-07-21 01:44:19 +0000709Raise ZipImportError if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000710
711PyDoc_STRVAR(doc_get_code,
712"get_code(fullname) -> code object.\n\
713\n\
714Return the code object for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000715if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000716
717PyDoc_STRVAR(doc_get_source,
718"get_source(fullname) -> source string.\n\
719\n\
720Return the source code for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000721if the module couldn't be found, return None if the archive does\n\
Just van Rossum52e14d62002-12-30 22:08:05 +0000722contain the module, but has no source for it.");
723
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000724
725PyDoc_STRVAR(doc_get_filename,
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000726"get_filename(fullname) -> filename string.\n\
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000727\n\
728Return the filename for the specified module.");
729
Just van Rossum52e14d62002-12-30 22:08:05 +0000730static PyMethodDef zipimporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 {"find_module", zipimporter_find_module, METH_VARARGS,
732 doc_find_module},
Eric V. Smith984b11f2012-05-24 20:21:04 -0400733 {"find_loader", zipimporter_find_loader, METH_VARARGS,
734 doc_find_loader},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 {"load_module", zipimporter_load_module, METH_VARARGS,
736 doc_load_module},
737 {"get_data", zipimporter_get_data, METH_VARARGS,
738 doc_get_data},
739 {"get_code", zipimporter_get_code, METH_VARARGS,
740 doc_get_code},
741 {"get_source", zipimporter_get_source, METH_VARARGS,
742 doc_get_source},
743 {"get_filename", zipimporter_get_filename, METH_VARARGS,
744 doc_get_filename},
745 {"is_package", zipimporter_is_package, METH_VARARGS,
746 doc_is_package},
747 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000748};
749
750static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
752 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
753 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
754 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000755};
756
757PyDoc_STRVAR(zipimporter_doc,
758"zipimporter(archivepath) -> zipimporter object\n\
759\n\
760Create a new zipimporter instance. 'archivepath' must be a path to\n\
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000761a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
762'/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
763valid directory inside the archive.\n\
764\n\
765'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
766archive.\n\
767\n\
768The 'archive' attribute of zipimporter objects contains the name of the\n\
769zipfile targeted.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000770
771#define DEFERRED_ADDRESS(ADDR) 0
772
773static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
775 "zipimport.zipimporter",
776 sizeof(ZipImporter),
777 0, /* tp_itemsize */
778 (destructor)zipimporter_dealloc, /* tp_dealloc */
779 0, /* tp_print */
780 0, /* tp_getattr */
781 0, /* tp_setattr */
782 0, /* tp_reserved */
783 (reprfunc)zipimporter_repr, /* tp_repr */
784 0, /* tp_as_number */
785 0, /* tp_as_sequence */
786 0, /* tp_as_mapping */
787 0, /* tp_hash */
788 0, /* tp_call */
789 0, /* tp_str */
790 PyObject_GenericGetAttr, /* tp_getattro */
791 0, /* tp_setattro */
792 0, /* tp_as_buffer */
793 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
794 Py_TPFLAGS_HAVE_GC, /* tp_flags */
795 zipimporter_doc, /* tp_doc */
796 zipimporter_traverse, /* tp_traverse */
797 0, /* tp_clear */
798 0, /* tp_richcompare */
799 0, /* tp_weaklistoffset */
800 0, /* tp_iter */
801 0, /* tp_iternext */
802 zipimporter_methods, /* tp_methods */
803 zipimporter_members, /* tp_members */
804 0, /* tp_getset */
805 0, /* tp_base */
806 0, /* tp_dict */
807 0, /* tp_descr_get */
808 0, /* tp_descr_set */
809 0, /* tp_dictoffset */
810 (initproc)zipimporter_init, /* tp_init */
811 PyType_GenericAlloc, /* tp_alloc */
812 PyType_GenericNew, /* tp_new */
813 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000814};
815
816
817/* implementation */
818
Just van Rossum52e14d62002-12-30 22:08:05 +0000819/* Given a buffer, return the long that is represented by the first
820 4 bytes, encoded as little endian. This partially reimplements
821 marshal.c:r_long() */
822static long
823get_long(unsigned char *buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 long x;
825 x = buf[0];
826 x |= (long)buf[1] << 8;
827 x |= (long)buf[2] << 16;
828 x |= (long)buf[3] << 24;
Just van Rossum52e14d62002-12-30 22:08:05 +0000829#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 /* Sign extension for 64-bit machines */
831 x |= -(x & 0x80000000L);
Just van Rossum52e14d62002-12-30 22:08:05 +0000832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000834}
835
836/*
837 read_directory(archive) -> files dict (new reference)
838
839 Given a path to a Zip archive, build a dict, mapping file names
840 (local to the archive, using SEP as a separator) to toc entries.
841
842 A toc_entry is a tuple:
843
Victor Stinner08654e12010-10-18 12:09:02 +0000844 (__file__, # value to use for __file__, available for all files,
845 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 compress, # compression kind; 0 for uncompressed
847 data_size, # size of compressed data on disk
848 file_size, # size of decompressed data
849 file_offset, # offset of file header from start of archive
850 time, # mod time of file (in dos format)
851 date, # mod data of file (in dos format)
852 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000853 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000854
855 Directories can be recognized by the trailing SEP in the name,
856 data_size and file_offset are 0.
857*/
858static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400859read_directory(PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +0000860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 PyObject *files = NULL;
862 FILE *fp;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000863 unsigned short flags;
Gregory P. Smithab320662012-01-30 15:17:33 -0800864 short compress, time, date, name_size;
865 long crc, data_size, file_size, header_size;
866 Py_ssize_t file_offset, header_position, header_offset;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200867 long l, count;
868 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000870 PyObject *nameobj = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 char *p, endof_central_dir[22];
Gregory P. Smithab320662012-01-30 15:17:33 -0800872 Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100873 PyObject *path;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000874 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000875 int bootstrap;
Just van Rossum52e14d62002-12-30 22:08:05 +0000876
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400877 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (fp == NULL) {
Victor Stinnerbd206e22011-12-18 21:04:17 +0100879 if (!PyErr_Occurred())
Victor Stinner35734762011-12-18 21:05:22 +0100880 PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 return NULL;
882 }
883 fseek(fp, -22, SEEK_END);
884 header_position = ftell(fp);
885 if (fread(endof_central_dir, 1, 22, fp) != 22) {
886 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400887 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 return NULL;
889 }
890 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
891 /* Bad: End of Central Dir signature */
892 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400893 PyErr_Format(ZipImportError, "not a Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 return NULL;
895 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 header_size = get_long((unsigned char *)endof_central_dir + 12);
898 header_offset = get_long((unsigned char *)endof_central_dir + 16);
899 arc_offset = header_position - header_offset - header_size;
900 header_offset += arc_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 files = PyDict_New();
903 if (files == NULL)
904 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* Start of Central Directory */
907 count = 0;
908 for (;;) {
909 PyObject *t;
910 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 fseek(fp, header_offset, 0); /* Start of file header */
913 l = PyMarshal_ReadLongFromFile(fp);
914 if (l != 0x02014B50)
915 break; /* Bad: Central Dir File Header */
Victor Stinnerd36c8212010-10-18 12:13:46 +0000916 fseek(fp, header_offset + 8, 0);
917 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 compress = PyMarshal_ReadShortFromFile(fp);
919 time = PyMarshal_ReadShortFromFile(fp);
920 date = PyMarshal_ReadShortFromFile(fp);
921 crc = PyMarshal_ReadLongFromFile(fp);
922 data_size = PyMarshal_ReadLongFromFile(fp);
923 file_size = PyMarshal_ReadLongFromFile(fp);
924 name_size = PyMarshal_ReadShortFromFile(fp);
925 header_size = 46 + name_size +
926 PyMarshal_ReadShortFromFile(fp) +
927 PyMarshal_ReadShortFromFile(fp);
928 fseek(fp, header_offset + 42, 0);
929 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
930 if (name_size > MAXPATHLEN)
931 name_size = MAXPATHLEN;
Just van Rossum52e14d62002-12-30 22:08:05 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 p = name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200934 for (i = 0; i < (Py_ssize_t)name_size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 *p = (char)getc(fp);
936 if (*p == '/')
937 *p = SEP;
938 p++;
939 }
940 *p = 0; /* Add terminating null byte */
941 header_offset += header_size;
Just van Rossum52e14d62002-12-30 22:08:05 +0000942
Victor Stinner4ee65a92011-01-22 10:30:29 +0000943 bootstrap = 0;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000944 if (flags & 0x0800)
945 charset = "utf-8";
Victor Stinner4ee65a92011-01-22 10:30:29 +0000946 else if (!PyThreadState_GET()->interp->codecs_initialized) {
947 /* During bootstrap, we may need to load the encodings
948 package from a ZIP file. But the cp437 encoding is implemented
949 in Python in the encodings package.
950
951 Break out of this dependency by assuming that the path to
952 the encodings module is ASCII-only. */
953 charset = "ascii";
954 bootstrap = 1;
955 }
Victor Stinnerd36c8212010-10-18 12:13:46 +0000956 else
957 charset = "cp437";
958 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200959 if (PyUnicode_READY(nameobj) == -1)
960 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000961 if (nameobj == NULL) {
962 if (bootstrap)
963 PyErr_Format(PyExc_NotImplementedError,
964 "bootstrap issue: python%i%i.zip contains non-ASCII "
965 "filenames without the unicode flag",
966 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Victor Stinner2460a432010-08-16 17:54:28 +0000967 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000968 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100969 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
970 if (path == NULL)
Victor Stinner2460a432010-08-16 17:54:28 +0000971 goto error;
Gregory P. Smithcc6abd52012-01-30 15:55:29 -0800972 t = Py_BuildValue("Nhllnhhl", path, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 file_size, file_offset, time, date, crc);
974 if (t == NULL)
975 goto error;
Victor Stinner2460a432010-08-16 17:54:28 +0000976 err = PyDict_SetItem(files, nameobj, t);
977 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 Py_DECREF(t);
979 if (err != 0)
980 goto error;
981 count++;
982 }
983 fclose(fp);
984 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400985 PySys_FormatStderr("# zipimport: found %ld names in %R\n",
986 count, archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 return files;
Just van Rossum52e14d62002-12-30 22:08:05 +0000988error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 fclose(fp);
990 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +0000991 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000993}
994
995/* Return the zlib.decompress function object, or NULL if zlib couldn't
996 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +0200997 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +0000998static PyObject *
999get_decompress_func(void)
1000{
Victor Stinner4925cde2011-05-20 00:16:09 +02001001 static int importing_zlib = 0;
1002 PyObject *zlib;
1003 PyObject *decompress;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001004 _Py_IDENTIFIER(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001005
Victor Stinner4925cde2011-05-20 00:16:09 +02001006 if (importing_zlib != 0)
1007 /* Someone has a zlib.py[co] in their Zip file;
1008 let's avoid a stack overflow. */
1009 return NULL;
1010 importing_zlib = 1;
1011 zlib = PyImport_ImportModuleNoBlock("zlib");
1012 importing_zlib = 0;
1013 if (zlib != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001014 decompress = _PyObject_GetAttrId(zlib,
1015 &PyId_decompress);
Victor Stinner4925cde2011-05-20 00:16:09 +02001016 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 }
Victor Stinner4925cde2011-05-20 00:16:09 +02001018 else {
1019 PyErr_Clear();
1020 decompress = NULL;
1021 }
1022 if (Py_VerboseFlag)
1023 PySys_WriteStderr("# zipimport: zlib %s\n",
1024 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +00001026}
1027
1028/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
1029 data as a new reference. */
1030static PyObject *
Victor Stinner60fe8d92010-08-16 23:48:11 +00001031get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PyObject *raw_data, *data = NULL, *decompress;
1034 char *buf;
1035 FILE *fp;
1036 int err;
1037 Py_ssize_t bytes_read = 0;
1038 long l;
Victor Stinner60fe8d92010-08-16 23:48:11 +00001039 PyObject *datapath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 long compress, data_size, file_size, file_offset, bytes_size;
1041 long time, date, crc;
Just van Rossum52e14d62002-12-30 22:08:05 +00001042
Victor Stinner60fe8d92010-08-16 23:48:11 +00001043 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 &data_size, &file_size, &file_offset, &time,
1045 &date, &crc)) {
1046 return NULL;
1047 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001048
Victor Stinner60fe8d92010-08-16 23:48:11 +00001049 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (!fp) {
Victor Stinnerbd206e22011-12-18 21:04:17 +01001051 if (!PyErr_Occurred())
1052 PyErr_Format(PyExc_IOError,
1053 "zipimport: can not open file %U", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 return NULL;
1055 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 /* Check to make sure the local file header is correct */
1058 fseek(fp, file_offset, 0);
1059 l = PyMarshal_ReadLongFromFile(fp);
1060 if (l != 0x04034B50) {
1061 /* Bad: Local File Header */
1062 PyErr_Format(ZipImportError,
Victor Stinner60fe8d92010-08-16 23:48:11 +00001063 "bad local file header in %U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 archive);
1065 fclose(fp);
1066 return NULL;
1067 }
1068 fseek(fp, file_offset + 26, 0);
1069 l = 30 + PyMarshal_ReadShortFromFile(fp) +
1070 PyMarshal_ReadShortFromFile(fp); /* local header size */
1071 file_offset += l; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 bytes_size = compress == 0 ? data_size : data_size + 1;
1074 if (bytes_size == 0)
1075 bytes_size++;
1076 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Just van Rossum52e14d62002-12-30 22:08:05 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (raw_data == NULL) {
1079 fclose(fp);
1080 return NULL;
1081 }
1082 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 err = fseek(fp, file_offset, 0);
1085 if (err == 0)
1086 bytes_read = fread(buf, 1, data_size, fp);
1087 fclose(fp);
1088 if (err || bytes_read != data_size) {
1089 PyErr_SetString(PyExc_IOError,
1090 "zipimport: can't read data");
1091 Py_DECREF(raw_data);
1092 return NULL;
1093 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 if (compress != 0) {
1096 buf[data_size] = 'Z'; /* saw this in zipfile.py */
1097 data_size++;
1098 }
1099 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 if (compress == 0) { /* data is not compressed */
1102 data = PyBytes_FromStringAndSize(buf, data_size);
1103 Py_DECREF(raw_data);
1104 return data;
1105 }
1106
1107 /* Decompress with zlib */
1108 decompress = get_decompress_func();
1109 if (decompress == NULL) {
1110 PyErr_SetString(ZipImportError,
1111 "can't decompress data; "
1112 "zlib not available");
1113 goto error;
1114 }
1115 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +02001116 Py_DECREF(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001117error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 Py_DECREF(raw_data);
1119 return data;
Just van Rossum52e14d62002-12-30 22:08:05 +00001120}
1121
1122/* Lenient date/time comparison function. The precision of the mtime
1123 in the archive is lower than the mtime stored in a .pyc: we
1124 must allow a difference of at most one second. */
1125static int
1126eq_mtime(time_t t1, time_t t2)
1127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 time_t d = t1 - t2;
1129 if (d < 0)
1130 d = -d;
1131 /* dostime only stores even seconds, so be lenient */
1132 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001133}
1134
1135/* Given the contents of a .py[co] file in a buffer, unmarshal the data
1136 and return the code object. Return None if it the magic word doesn't
1137 match (we do this instead of raising an exception as we fall back
1138 to .py if available and we don't want to mask other errors).
1139 Returns a new reference. */
1140static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001141unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 PyObject *code;
1144 char *buf = PyBytes_AsString(data);
1145 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (size <= 9) {
1148 PyErr_SetString(ZipImportError,
1149 "bad pyc data");
1150 return NULL;
1151 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
1154 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001155 PySys_FormatStderr("# %R has bad magic\n",
1156 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 Py_INCREF(Py_None);
1158 return Py_None; /* signal caller to try alternative */
1159 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
1162 mtime)) {
1163 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001164 PySys_FormatStderr("# %R has bad mtime\n",
1165 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 Py_INCREF(Py_None);
1167 return Py_None; /* signal caller to try alternative */
1168 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001169
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001170 /* XXX the pyc's size field is ignored; timestamp collisions are probably
1171 unimportant with zip files. */
1172 code = PyMarshal_ReadObjectFromString(buf + 12, size - 12);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 if (code == NULL)
1174 return NULL;
1175 if (!PyCode_Check(code)) {
1176 Py_DECREF(code);
1177 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001178 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 pathname);
1180 return NULL;
1181 }
1182 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001183}
1184
1185/* Replace any occurances of "\r\n?" in the input string with "\n".
1186 This converts DOS and Mac line endings to Unix line endings.
1187 Also append a trailing "\n" to be compatible with
1188 PyParser_SimpleParseFile(). Returns a new reference. */
1189static PyObject *
1190normalize_line_endings(PyObject *source)
1191{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001192 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 PyObject *fixed_source;
1194 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001195
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001196 p = PyBytes_AsString(source);
1197 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 return PyBytes_FromStringAndSize("\n\0", 2);
1199 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 /* one char extra for trailing \n and one for terminating \0 */
1202 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1203 if (buf == NULL) {
1204 PyErr_SetString(PyExc_MemoryError,
1205 "zipimport: no memory to allocate "
1206 "source buffer");
1207 return NULL;
1208 }
1209 /* replace "\r\n?" by "\n" */
1210 for (q = buf; *p != '\0'; p++) {
1211 if (*p == '\r') {
1212 *q++ = '\n';
1213 if (*(p + 1) == '\n')
1214 p++;
1215 }
1216 else
1217 *q++ = *p;
1218 len++;
1219 }
1220 *q++ = '\n'; /* add trailing \n */
1221 *q = '\0';
1222 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1223 PyMem_Free(buf);
1224 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001225}
1226
1227/* Given a string buffer containing Python source code, compile it
1228 return and return a code object as a new reference. */
1229static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001230compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001231{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001232 PyObject *code, *fixed_source, *pathbytes;
Just van Rossum52e14d62002-12-30 22:08:05 +00001233
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001234 pathbytes = PyUnicode_EncodeFSDefault(pathname);
1235 if (pathbytes == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001237
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001238 fixed_source = normalize_line_endings(source);
1239 if (fixed_source == NULL) {
1240 Py_DECREF(pathbytes);
1241 return NULL;
1242 }
1243
1244 code = Py_CompileString(PyBytes_AsString(fixed_source),
1245 PyBytes_AsString(pathbytes),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 Py_file_input);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001247 Py_DECREF(pathbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Py_DECREF(fixed_source);
1249 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001250}
1251
1252/* Convert the date/time values found in the Zip archive to a value
1253 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001254static time_t
1255parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 stm.tm_sec = (dostime & 0x1f) * 2;
1262 stm.tm_min = (dostime >> 5) & 0x3f;
1263 stm.tm_hour = (dostime >> 11) & 0x1f;
1264 stm.tm_mday = dosdate & 0x1f;
1265 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1266 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1267 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001270}
1271
1272/* Given a path to a .pyc or .pyo file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001273 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001274 is available. */
1275static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001276get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001277{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001278 PyObject *toc_entry, *stripped;
1279 time_t mtime;
1280
1281 /* strip 'c' or 'o' from *.py[co] */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001282 if (PyUnicode_READY(path) == -1)
1283 return (time_t)-1;
1284 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
1285 PyUnicode_DATA(path),
1286 PyUnicode_GET_LENGTH(path) - 1);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001287 if (stripped == NULL)
1288 return (time_t)-1;
1289
1290 toc_entry = PyDict_GetItem(self->files, stripped);
1291 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1293 PyTuple_Size(toc_entry) == 8) {
1294 /* fetch the time stamp of the .py file for comparison
1295 with an embedded pyc time stamp */
1296 int time, date;
1297 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1298 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1299 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001300 } else
1301 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001303}
1304
1305/* Return the code object for the module named by 'fullname' from the
1306 Zip archive as a new reference. */
1307static PyObject *
1308get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001310{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001311 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001312
Victor Stinner60fe8d92010-08-16 23:48:11 +00001313 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (data == NULL)
1315 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001316
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001317 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001318 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001319 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001320 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001321 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 Py_DECREF(data);
1323 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001324}
1325
Ezio Melotti42da6632011-03-15 05:18:48 +02001326/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001327 'fullname'. */
1328static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001329get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001330 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001331{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001332 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001333 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001337 if (subname == NULL)
1338 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001339
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001340 path = make_filename(self->prefix, subname);
1341 Py_DECREF(subname);
1342 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001346 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001347
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001348 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1349 if (fullpath == NULL)
1350 goto exit;
1351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001353 PySys_FormatStderr("# trying %U%c%U\n",
1354 self->archive, (int)SEP, fullpath);
1355 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 if (toc_entry != NULL) {
1357 time_t mtime = 0;
1358 int ispackage = zso->type & IS_PACKAGE;
1359 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001360
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001361 if (isbytecode) {
1362 mtime = get_mtime_of_source(self, fullpath);
1363 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1364 goto exit;
1365 }
1366 }
1367 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if (p_ispackage != NULL)
1369 *p_ispackage = ispackage;
1370 code = get_code_from_data(self, ispackage,
1371 isbytecode, mtime,
1372 toc_entry);
1373 if (code == Py_None) {
1374 /* bad magic number or non-matching mtime
1375 in byte code, try next */
1376 Py_DECREF(code);
1377 continue;
1378 }
Victor Stinner08654e12010-10-18 12:09:02 +00001379 if (code != NULL && p_modpath != NULL) {
1380 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1381 Py_INCREF(*p_modpath);
1382 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001383 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001385 else
1386 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001388 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1389exit:
1390 Py_DECREF(path);
1391 Py_XDECREF(fullpath);
1392 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001393}
1394
1395
1396/* Module init */
1397
1398PyDoc_STRVAR(zipimport_doc,
1399"zipimport provides support for importing Python modules from Zip archives.\n\
1400\n\
1401This module exports three objects:\n\
1402- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001403- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001404 subclass of ImportError, so it can be caught as ImportError, too.\n\
1405- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1406 info dicts, as used in zipimporter._files.\n\
1407\n\
1408It is usually not needed to use the zipimport module explicitly; it is\n\
1409used by the builtin import mechanism for sys.path items that are paths\n\
1410to Zip archives.");
1411
Martin v. Löwis1a214512008-06-11 05:26:20 +00001412static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 PyModuleDef_HEAD_INIT,
1414 "zipimport",
1415 zipimport_doc,
1416 -1,
1417 NULL,
1418 NULL,
1419 NULL,
1420 NULL,
1421 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001422};
1423
Just van Rossum52e14d62002-12-30 22:08:05 +00001424PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001425PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 if (PyType_Ready(&ZipImporter_Type) < 0)
1430 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 /* Correct directory separator */
1433 zip_searchorder[0].suffix[0] = SEP;
1434 zip_searchorder[1].suffix[0] = SEP;
1435 zip_searchorder[2].suffix[0] = SEP;
1436 if (Py_OptimizeFlag) {
1437 /* Reverse *.pyc and *.pyo */
1438 struct st_zip_searchorder tmp;
1439 tmp = zip_searchorder[0];
1440 zip_searchorder[0] = zip_searchorder[1];
1441 zip_searchorder[1] = tmp;
1442 tmp = zip_searchorder[3];
1443 zip_searchorder[3] = zip_searchorder[4];
1444 zip_searchorder[4] = tmp;
1445 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 mod = PyModule_Create(&zipimportmodule);
1448 if (mod == NULL)
1449 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1452 PyExc_ImportError, NULL);
1453 if (ZipImportError == NULL)
1454 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 Py_INCREF(ZipImportError);
1457 if (PyModule_AddObject(mod, "ZipImportError",
1458 ZipImportError) < 0)
1459 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 Py_INCREF(&ZipImporter_Type);
1462 if (PyModule_AddObject(mod, "zipimporter",
1463 (PyObject *)&ZipImporter_Type) < 0)
1464 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 zip_directory_cache = PyDict_New();
1467 if (zip_directory_cache == NULL)
1468 return NULL;
1469 Py_INCREF(zip_directory_cache);
1470 if (PyModule_AddObject(mod, "_zip_directory_cache",
1471 zip_directory_cache) < 0)
1472 return NULL;
1473 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001474}