blob: 4e74edf9e81d5b29c21d99565aeb6b2fbf2763c0 [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
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700322typedef enum {
323 fl_error,
324 fl_not_found,
325 fl_module_found,
326 fl_ns_found
327} find_loader_result;
328
Eric V. Smith984b11f2012-05-24 20:21:04 -0400329/* The guts of "find_loader" and "find_module". Return values:
330 -1: error
331 0: no loader or namespace portions found
332 1: module/package found
333 2: namespace portion found: *namespace_portion will point to the name
334*/
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700335static find_loader_result
Eric V. Smith984b11f2012-05-24 20:21:04 -0400336find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
337{
338 enum zi_module_info mi;
339
340 *namespace_portion = NULL;
341
342 mi = get_module_info(self, fullname);
343 if (mi == MI_ERROR)
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700344 return fl_error;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400345 if (mi == MI_NOT_FOUND) {
346 /* Not a module or regular package. See if this is a directory, and
347 therefore possibly a portion of a namespace package. */
348 int is_dir = check_is_directory(self, self->prefix, fullname);
349 if (is_dir < 0)
350 return -1;
351 if (is_dir) {
352 /* This is possibly a portion of a namespace
353 package. Return the string representing its path,
354 without a trailing separator. */
355 *namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
356 self->archive, SEP,
357 self->prefix, fullname);
358 if (*namespace_portion == NULL)
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700359 return fl_error;
360 return fl_ns_found;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400361 }
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700362 return fl_not_found;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400363 }
364 /* This is a module or package. */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700365 return fl_module_found;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400366}
367
368
Just van Rossum52e14d62002-12-30 22:08:05 +0000369/* Check whether we can satisfy the import of the module named by
370 'fullname'. Return self if we can, None if we can't. */
371static PyObject *
372zipimporter_find_module(PyObject *obj, PyObject *args)
373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 ZipImporter *self = (ZipImporter *)obj;
375 PyObject *path = NULL;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400376 PyObject *fullname;
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700377 PyObject *namespace_portion = NULL;
378 PyObject *result = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000379
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700380 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
381 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000382
Eric V. Smith984b11f2012-05-24 20:21:04 -0400383 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700384 case fl_ns_found:
385 /* A namespace portion is not allowed via find_module, so return None. */
Eric V. Smith984b11f2012-05-24 20:21:04 -0400386 Py_DECREF(namespace_portion);
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700387 /* FALL THROUGH */
388 case fl_error:
389 case fl_not_found:
390 result = Py_None;
391 break;
392 case fl_module_found:
393 result = (PyObject *)self;
394 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 }
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700396 Py_XINCREF(result);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400397 return NULL;
398}
399
400
401/* Check whether we can satisfy the import of the module named by
402 'fullname', or whether it could be a portion of a namespace
403 package. Return self if we can load it, a string containing the
404 full path if it's a possible namespace portion, None if we
405 can't load it. */
406static PyObject *
407zipimporter_find_loader(PyObject *obj, PyObject *args)
408{
409 ZipImporter *self = (ZipImporter *)obj;
410 PyObject *path = NULL;
411 PyObject *fullname;
412 PyObject *result = NULL;
413 PyObject *namespace_portion = NULL;
414
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700415 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
416 return NULL;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400417
418 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700419 case fl_error:
420 return NULL;
421 case fl_not_found: /* Not found, return (None, []) */
422 result = Py_BuildValue("O[]", Py_None);
423 break;
424 case fl_module_found: /* Return (self, []) */
425 result = Py_BuildValue("O[]", self);
426 break;
427 case fl_ns_found: /* Return (None, [namespace_portion]) */
428 result = Py_BuildValue("O[O]", Py_None, namespace_portion);
Benjamin Peterson209e04c2012-05-24 22:35:39 -0700429 Py_DECREF(namespace_portion);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400430 return result;
431 }
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700432 return result;
Just van Rossum52e14d62002-12-30 22:08:05 +0000433}
434
435/* Load and return the module named by 'fullname'. */
436static PyObject *
437zipimporter_load_module(PyObject *obj, PyObject *args)
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner26fabe12010-10-18 12:03:25 +0000440 PyObject *code = NULL, *mod, *dict;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400441 PyObject *fullname;
442 PyObject *modpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000444
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400445 if (!PyArg_ParseTuple(args, "U:zipimporter.load_module",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 &fullname))
447 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200448 if (PyUnicode_READY(fullname) == -1)
449 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 code = get_module_code(self, fullname, &ispackage, &modpath);
452 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000453 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000454
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400455 mod = PyImport_AddModuleObject(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000456 if (mod == NULL)
457 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* mod.__loader__ = self */
461 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
462 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (ispackage) {
465 /* add __path__ to the module *before* the code gets
466 executed */
467 PyObject *pkgpath, *fullpath;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400468 PyObject *subname = get_subname(fullname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000470
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400471 fullpath = PyUnicode_FromFormat("%U%c%U%U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 self->archive, SEP,
473 self->prefix, subname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400474 Py_DECREF(subname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (fullpath == NULL)
476 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000477
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400478 pkgpath = Py_BuildValue("[N]", fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (pkgpath == NULL)
480 goto error;
481 err = PyDict_SetItemString(dict, "__path__", pkgpath);
482 Py_DECREF(pkgpath);
483 if (err != 0)
484 goto error;
485 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400486 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
Victor Stinner26fabe12010-10-18 12:03:25 +0000487 Py_CLEAR(code);
488 if (mod == NULL)
489 goto error;
490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400492 PySys_FormatStderr("import %U # loaded from Zip %U\n",
Victor Stinner08654e12010-10-18 12:09:02 +0000493 fullname, modpath);
494 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000496error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000497 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000498 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000500}
501
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000502/* Return a string matching __file__ for the named module */
503static PyObject *
504zipimporter_get_filename(PyObject *obj, PyObject *args)
505{
506 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400507 PyObject *fullname, *code, *modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000508 int ispackage;
509
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400510 if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename",
Victor Stinner9e40fad2010-10-18 22:34:46 +0000511 &fullname))
Victor Stinnerc342fca2010-10-18 11:39:05 +0000512 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000513
514 /* Deciding the filename requires working out where the code
515 would come from if the module was actually loaded */
516 code = get_module_code(self, fullname, &ispackage, &modpath);
517 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000518 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000519 Py_DECREF(code); /* Only need the path info */
520
Victor Stinner08654e12010-10-18 12:09:02 +0000521 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000522}
523
Just van Rossum52e14d62002-12-30 22:08:05 +0000524/* Return a bool signifying whether the module is a package or not. */
525static PyObject *
526zipimporter_is_package(PyObject *obj, PyObject *args)
527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400529 PyObject *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000531
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400532 if (!PyArg_ParseTuple(args, "U:zipimporter.is_package",
Victor Stinner965a8a12010-10-18 21:44:33 +0000533 &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 mi = get_module_info(self, fullname);
537 if (mi == MI_ERROR)
Victor Stinner965a8a12010-10-18 21:44:33 +0000538 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400540 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000541 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 }
543 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000544}
545
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200546
Just van Rossum52e14d62002-12-30 22:08:05 +0000547static PyObject *
548zipimporter_get_data(PyObject *obj, PyObject *args)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 ZipImporter *self = (ZipImporter *)obj;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100551 PyObject *path, *key;
Just van Rossum52e14d62002-12-30 22:08:05 +0000552#ifdef ALTSEP
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100553 _Py_IDENTIFIER(replace);
Just van Rossum52e14d62002-12-30 22:08:05 +0000554#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 PyObject *toc_entry;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100556 Py_ssize_t path_start, path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000557
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100558 if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000560
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200561#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +0100562 path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100563 if (!path)
564 return NULL;
565#else
566 Py_INCREF(path);
Just van Rossum52e14d62002-12-30 22:08:05 +0000567#endif
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100568 if (PyUnicode_READY(path) == -1)
569 goto error;
570
571 path_len = PyUnicode_GET_LENGTH(path);
572
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200573 len = PyUnicode_GET_LENGTH(self->archive);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100574 path_start = 0;
575 if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
576 && PyUnicode_READ_CHAR(path, len) == SEP) {
577 path_start = len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000579
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100580 key = PyUnicode_Substring(path, path_start, path_len);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000581 if (key == NULL)
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100582 goto error;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000583 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (toc_entry == NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000585 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
586 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100587 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000589 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100590 Py_DECREF(path);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000591 return get_data(self->archive, toc_entry);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100592 error:
593 Py_DECREF(path);
594 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000595}
596
597static PyObject *
598zipimporter_get_code(PyObject *obj, PyObject *args)
599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400601 PyObject *fullname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000602
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400603 if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000607}
608
609static PyObject *
610zipimporter_get_source(PyObject *obj, PyObject *args)
611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 ZipImporter *self = (ZipImporter *)obj;
613 PyObject *toc_entry;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400614 PyObject *fullname, *subname, *path, *fullpath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000616
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400617 if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000621 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000623 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400624 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000625 return NULL;
626 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400627
Victor Stinner965a8a12010-10-18 21:44:33 +0000628 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400629 if (subname == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000631
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400632 path = make_filename(self->prefix, subname);
633 Py_DECREF(subname);
634 if (path == NULL)
635 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000636
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400637 if (mi == MI_PACKAGE)
638 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
639 else
640 fullpath = PyUnicode_FromFormat("%U.py", path);
641 Py_DECREF(path);
642 if (fullpath == NULL)
643 return NULL;
644
645 toc_entry = PyDict_GetItem(self->files, fullpath);
646 Py_DECREF(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000648 PyObject *res, *bytes;
649 bytes = get_data(self->archive, toc_entry);
650 if (bytes == NULL)
651 return NULL;
652 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
653 PyBytes_GET_SIZE(bytes));
654 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return res;
656 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 /* we have the module, but no source */
659 Py_INCREF(Py_None);
660 return Py_None;
Just van Rossum52e14d62002-12-30 22:08:05 +0000661}
662
663PyDoc_STRVAR(doc_find_module,
664"find_module(fullname, path=None) -> self or None.\n\
665\n\
666Search for a module specified by 'fullname'. 'fullname' must be the\n\
667fully qualified (dotted) module name. It returns the zipimporter\n\
668instance itself if the module was found, or None if it wasn't.\n\
669The optional 'path' argument is ignored -- it's there for compatibility\n\
670with the importer protocol.");
671
Eric V. Smith984b11f2012-05-24 20:21:04 -0400672PyDoc_STRVAR(doc_find_loader,
673"find_loader(fullname, path=None) -> self, str or None.\n\
674\n\
675Search for a module specified by 'fullname'. 'fullname' must be the\n\
676fully qualified (dotted) module name. It returns the zipimporter\n\
677instance itself if the module was found, a string containing the\n\
678full path name if it's possibly a portion of a namespace package,\n\
679or None otherwise. The optional 'path' argument is ignored -- it's\n\
680 there for compatibility with the importer protocol.");
681
Just van Rossum52e14d62002-12-30 22:08:05 +0000682PyDoc_STRVAR(doc_load_module,
683"load_module(fullname) -> module.\n\
684\n\
685Load the module specified by 'fullname'. 'fullname' must be the\n\
686fully qualified (dotted) module name. It returns the imported\n\
687module, or raises ZipImportError if it wasn't found.");
688
689PyDoc_STRVAR(doc_get_data,
690"get_data(pathname) -> string with file data.\n\
691\n\
692Return the data associated with 'pathname'. Raise IOError if\n\
693the file wasn't found.");
694
695PyDoc_STRVAR(doc_is_package,
696"is_package(fullname) -> bool.\n\
697\n\
698Return True if the module specified by fullname is a package.\n\
Brian Curtin32839732010-07-21 01:44:19 +0000699Raise ZipImportError if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000700
701PyDoc_STRVAR(doc_get_code,
702"get_code(fullname) -> code object.\n\
703\n\
704Return the code object for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000705if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000706
707PyDoc_STRVAR(doc_get_source,
708"get_source(fullname) -> source string.\n\
709\n\
710Return the source code for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000711if the module couldn't be found, return None if the archive does\n\
Just van Rossum52e14d62002-12-30 22:08:05 +0000712contain the module, but has no source for it.");
713
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000714
715PyDoc_STRVAR(doc_get_filename,
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000716"get_filename(fullname) -> filename string.\n\
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000717\n\
718Return the filename for the specified module.");
719
Just van Rossum52e14d62002-12-30 22:08:05 +0000720static PyMethodDef zipimporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 {"find_module", zipimporter_find_module, METH_VARARGS,
722 doc_find_module},
Eric V. Smith984b11f2012-05-24 20:21:04 -0400723 {"find_loader", zipimporter_find_loader, METH_VARARGS,
724 doc_find_loader},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 {"load_module", zipimporter_load_module, METH_VARARGS,
726 doc_load_module},
727 {"get_data", zipimporter_get_data, METH_VARARGS,
728 doc_get_data},
729 {"get_code", zipimporter_get_code, METH_VARARGS,
730 doc_get_code},
731 {"get_source", zipimporter_get_source, METH_VARARGS,
732 doc_get_source},
733 {"get_filename", zipimporter_get_filename, METH_VARARGS,
734 doc_get_filename},
735 {"is_package", zipimporter_is_package, METH_VARARGS,
736 doc_is_package},
737 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000738};
739
740static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
742 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
743 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
744 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000745};
746
747PyDoc_STRVAR(zipimporter_doc,
748"zipimporter(archivepath) -> zipimporter object\n\
749\n\
750Create a new zipimporter instance. 'archivepath' must be a path to\n\
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000751a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
752'/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
753valid directory inside the archive.\n\
754\n\
755'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
756archive.\n\
757\n\
758The 'archive' attribute of zipimporter objects contains the name of the\n\
759zipfile targeted.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000760
761#define DEFERRED_ADDRESS(ADDR) 0
762
763static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
765 "zipimport.zipimporter",
766 sizeof(ZipImporter),
767 0, /* tp_itemsize */
768 (destructor)zipimporter_dealloc, /* tp_dealloc */
769 0, /* tp_print */
770 0, /* tp_getattr */
771 0, /* tp_setattr */
772 0, /* tp_reserved */
773 (reprfunc)zipimporter_repr, /* tp_repr */
774 0, /* tp_as_number */
775 0, /* tp_as_sequence */
776 0, /* tp_as_mapping */
777 0, /* tp_hash */
778 0, /* tp_call */
779 0, /* tp_str */
780 PyObject_GenericGetAttr, /* tp_getattro */
781 0, /* tp_setattro */
782 0, /* tp_as_buffer */
783 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
784 Py_TPFLAGS_HAVE_GC, /* tp_flags */
785 zipimporter_doc, /* tp_doc */
786 zipimporter_traverse, /* tp_traverse */
787 0, /* tp_clear */
788 0, /* tp_richcompare */
789 0, /* tp_weaklistoffset */
790 0, /* tp_iter */
791 0, /* tp_iternext */
792 zipimporter_methods, /* tp_methods */
793 zipimporter_members, /* tp_members */
794 0, /* tp_getset */
795 0, /* tp_base */
796 0, /* tp_dict */
797 0, /* tp_descr_get */
798 0, /* tp_descr_set */
799 0, /* tp_dictoffset */
800 (initproc)zipimporter_init, /* tp_init */
801 PyType_GenericAlloc, /* tp_alloc */
802 PyType_GenericNew, /* tp_new */
803 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000804};
805
806
807/* implementation */
808
Just van Rossum52e14d62002-12-30 22:08:05 +0000809/* Given a buffer, return the long that is represented by the first
810 4 bytes, encoded as little endian. This partially reimplements
811 marshal.c:r_long() */
812static long
813get_long(unsigned char *buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 long x;
815 x = buf[0];
816 x |= (long)buf[1] << 8;
817 x |= (long)buf[2] << 16;
818 x |= (long)buf[3] << 24;
Just van Rossum52e14d62002-12-30 22:08:05 +0000819#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* Sign extension for 64-bit machines */
821 x |= -(x & 0x80000000L);
Just van Rossum52e14d62002-12-30 22:08:05 +0000822#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000824}
825
826/*
827 read_directory(archive) -> files dict (new reference)
828
829 Given a path to a Zip archive, build a dict, mapping file names
830 (local to the archive, using SEP as a separator) to toc entries.
831
832 A toc_entry is a tuple:
833
Victor Stinner08654e12010-10-18 12:09:02 +0000834 (__file__, # value to use for __file__, available for all files,
835 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 compress, # compression kind; 0 for uncompressed
837 data_size, # size of compressed data on disk
838 file_size, # size of decompressed data
839 file_offset, # offset of file header from start of archive
840 time, # mod time of file (in dos format)
841 date, # mod data of file (in dos format)
842 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000843 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000844
845 Directories can be recognized by the trailing SEP in the name,
846 data_size and file_offset are 0.
847*/
848static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400849read_directory(PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +0000850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyObject *files = NULL;
852 FILE *fp;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000853 unsigned short flags;
Gregory P. Smithab320662012-01-30 15:17:33 -0800854 short compress, time, date, name_size;
855 long crc, data_size, file_size, header_size;
856 Py_ssize_t file_offset, header_position, header_offset;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200857 long l, count;
858 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000860 PyObject *nameobj = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 char *p, endof_central_dir[22];
Gregory P. Smithab320662012-01-30 15:17:33 -0800862 Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100863 PyObject *path;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000864 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000865 int bootstrap;
Just van Rossum52e14d62002-12-30 22:08:05 +0000866
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400867 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (fp == NULL) {
Victor Stinnerbd206e22011-12-18 21:04:17 +0100869 if (!PyErr_Occurred())
Victor Stinner35734762011-12-18 21:05:22 +0100870 PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 return NULL;
872 }
873 fseek(fp, -22, SEEK_END);
874 header_position = ftell(fp);
875 if (fread(endof_central_dir, 1, 22, fp) != 22) {
876 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400877 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 return NULL;
879 }
880 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
881 /* Bad: End of Central Dir signature */
882 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400883 PyErr_Format(ZipImportError, "not a Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 return NULL;
885 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 header_size = get_long((unsigned char *)endof_central_dir + 12);
888 header_offset = get_long((unsigned char *)endof_central_dir + 16);
889 arc_offset = header_position - header_offset - header_size;
890 header_offset += arc_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 files = PyDict_New();
893 if (files == NULL)
894 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 /* Start of Central Directory */
897 count = 0;
898 for (;;) {
899 PyObject *t;
900 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 fseek(fp, header_offset, 0); /* Start of file header */
903 l = PyMarshal_ReadLongFromFile(fp);
904 if (l != 0x02014B50)
905 break; /* Bad: Central Dir File Header */
Victor Stinnerd36c8212010-10-18 12:13:46 +0000906 fseek(fp, header_offset + 8, 0);
907 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 compress = PyMarshal_ReadShortFromFile(fp);
909 time = PyMarshal_ReadShortFromFile(fp);
910 date = PyMarshal_ReadShortFromFile(fp);
911 crc = PyMarshal_ReadLongFromFile(fp);
912 data_size = PyMarshal_ReadLongFromFile(fp);
913 file_size = PyMarshal_ReadLongFromFile(fp);
914 name_size = PyMarshal_ReadShortFromFile(fp);
915 header_size = 46 + name_size +
916 PyMarshal_ReadShortFromFile(fp) +
917 PyMarshal_ReadShortFromFile(fp);
918 fseek(fp, header_offset + 42, 0);
919 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
920 if (name_size > MAXPATHLEN)
921 name_size = MAXPATHLEN;
Just van Rossum52e14d62002-12-30 22:08:05 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 p = name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200924 for (i = 0; i < (Py_ssize_t)name_size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 *p = (char)getc(fp);
926 if (*p == '/')
927 *p = SEP;
928 p++;
929 }
930 *p = 0; /* Add terminating null byte */
931 header_offset += header_size;
Just van Rossum52e14d62002-12-30 22:08:05 +0000932
Victor Stinner4ee65a92011-01-22 10:30:29 +0000933 bootstrap = 0;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000934 if (flags & 0x0800)
935 charset = "utf-8";
Victor Stinner4ee65a92011-01-22 10:30:29 +0000936 else if (!PyThreadState_GET()->interp->codecs_initialized) {
937 /* During bootstrap, we may need to load the encodings
938 package from a ZIP file. But the cp437 encoding is implemented
939 in Python in the encodings package.
940
941 Break out of this dependency by assuming that the path to
942 the encodings module is ASCII-only. */
943 charset = "ascii";
944 bootstrap = 1;
945 }
Victor Stinnerd36c8212010-10-18 12:13:46 +0000946 else
947 charset = "cp437";
948 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200949 if (PyUnicode_READY(nameobj) == -1)
950 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000951 if (nameobj == NULL) {
952 if (bootstrap)
953 PyErr_Format(PyExc_NotImplementedError,
954 "bootstrap issue: python%i%i.zip contains non-ASCII "
955 "filenames without the unicode flag",
956 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Victor Stinner2460a432010-08-16 17:54:28 +0000957 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000958 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100959 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
960 if (path == NULL)
Victor Stinner2460a432010-08-16 17:54:28 +0000961 goto error;
Gregory P. Smithcc6abd52012-01-30 15:55:29 -0800962 t = Py_BuildValue("Nhllnhhl", path, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 file_size, file_offset, time, date, crc);
964 if (t == NULL)
965 goto error;
Victor Stinner2460a432010-08-16 17:54:28 +0000966 err = PyDict_SetItem(files, nameobj, t);
967 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 Py_DECREF(t);
969 if (err != 0)
970 goto error;
971 count++;
972 }
973 fclose(fp);
974 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400975 PySys_FormatStderr("# zipimport: found %ld names in %R\n",
976 count, archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 return files;
Just van Rossum52e14d62002-12-30 22:08:05 +0000978error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 fclose(fp);
980 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +0000981 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000983}
984
985/* Return the zlib.decompress function object, or NULL if zlib couldn't
986 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +0200987 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +0000988static PyObject *
989get_decompress_func(void)
990{
Victor Stinner4925cde2011-05-20 00:16:09 +0200991 static int importing_zlib = 0;
992 PyObject *zlib;
993 PyObject *decompress;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200994 _Py_IDENTIFIER(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +0000995
Victor Stinner4925cde2011-05-20 00:16:09 +0200996 if (importing_zlib != 0)
997 /* Someone has a zlib.py[co] in their Zip file;
998 let's avoid a stack overflow. */
999 return NULL;
1000 importing_zlib = 1;
1001 zlib = PyImport_ImportModuleNoBlock("zlib");
1002 importing_zlib = 0;
1003 if (zlib != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001004 decompress = _PyObject_GetAttrId(zlib,
1005 &PyId_decompress);
Victor Stinner4925cde2011-05-20 00:16:09 +02001006 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 }
Victor Stinner4925cde2011-05-20 00:16:09 +02001008 else {
1009 PyErr_Clear();
1010 decompress = NULL;
1011 }
1012 if (Py_VerboseFlag)
1013 PySys_WriteStderr("# zipimport: zlib %s\n",
1014 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +00001016}
1017
1018/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
1019 data as a new reference. */
1020static PyObject *
Victor Stinner60fe8d92010-08-16 23:48:11 +00001021get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyObject *raw_data, *data = NULL, *decompress;
1024 char *buf;
1025 FILE *fp;
1026 int err;
1027 Py_ssize_t bytes_read = 0;
1028 long l;
Victor Stinner60fe8d92010-08-16 23:48:11 +00001029 PyObject *datapath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 long compress, data_size, file_size, file_offset, bytes_size;
1031 long time, date, crc;
Just van Rossum52e14d62002-12-30 22:08:05 +00001032
Victor Stinner60fe8d92010-08-16 23:48:11 +00001033 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 &data_size, &file_size, &file_offset, &time,
1035 &date, &crc)) {
1036 return NULL;
1037 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001038
Victor Stinner60fe8d92010-08-16 23:48:11 +00001039 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (!fp) {
Victor Stinnerbd206e22011-12-18 21:04:17 +01001041 if (!PyErr_Occurred())
1042 PyErr_Format(PyExc_IOError,
1043 "zipimport: can not open file %U", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 return NULL;
1045 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 /* Check to make sure the local file header is correct */
1048 fseek(fp, file_offset, 0);
1049 l = PyMarshal_ReadLongFromFile(fp);
1050 if (l != 0x04034B50) {
1051 /* Bad: Local File Header */
1052 PyErr_Format(ZipImportError,
Victor Stinner60fe8d92010-08-16 23:48:11 +00001053 "bad local file header in %U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 archive);
1055 fclose(fp);
1056 return NULL;
1057 }
1058 fseek(fp, file_offset + 26, 0);
1059 l = 30 + PyMarshal_ReadShortFromFile(fp) +
1060 PyMarshal_ReadShortFromFile(fp); /* local header size */
1061 file_offset += l; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 bytes_size = compress == 0 ? data_size : data_size + 1;
1064 if (bytes_size == 0)
1065 bytes_size++;
1066 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Just van Rossum52e14d62002-12-30 22:08:05 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (raw_data == NULL) {
1069 fclose(fp);
1070 return NULL;
1071 }
1072 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 err = fseek(fp, file_offset, 0);
1075 if (err == 0)
1076 bytes_read = fread(buf, 1, data_size, fp);
1077 fclose(fp);
1078 if (err || bytes_read != data_size) {
1079 PyErr_SetString(PyExc_IOError,
1080 "zipimport: can't read data");
1081 Py_DECREF(raw_data);
1082 return NULL;
1083 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (compress != 0) {
1086 buf[data_size] = 'Z'; /* saw this in zipfile.py */
1087 data_size++;
1088 }
1089 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (compress == 0) { /* data is not compressed */
1092 data = PyBytes_FromStringAndSize(buf, data_size);
1093 Py_DECREF(raw_data);
1094 return data;
1095 }
1096
1097 /* Decompress with zlib */
1098 decompress = get_decompress_func();
1099 if (decompress == NULL) {
1100 PyErr_SetString(ZipImportError,
1101 "can't decompress data; "
1102 "zlib not available");
1103 goto error;
1104 }
1105 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +02001106 Py_DECREF(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001107error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 Py_DECREF(raw_data);
1109 return data;
Just van Rossum52e14d62002-12-30 22:08:05 +00001110}
1111
1112/* Lenient date/time comparison function. The precision of the mtime
1113 in the archive is lower than the mtime stored in a .pyc: we
1114 must allow a difference of at most one second. */
1115static int
1116eq_mtime(time_t t1, time_t t2)
1117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 time_t d = t1 - t2;
1119 if (d < 0)
1120 d = -d;
1121 /* dostime only stores even seconds, so be lenient */
1122 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001123}
1124
1125/* Given the contents of a .py[co] file in a buffer, unmarshal the data
1126 and return the code object. Return None if it the magic word doesn't
1127 match (we do this instead of raising an exception as we fall back
1128 to .py if available and we don't want to mask other errors).
1129 Returns a new reference. */
1130static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001131unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 PyObject *code;
1134 char *buf = PyBytes_AsString(data);
1135 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 if (size <= 9) {
1138 PyErr_SetString(ZipImportError,
1139 "bad pyc data");
1140 return NULL;
1141 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
1144 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001145 PySys_FormatStderr("# %R has bad magic\n",
1146 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 Py_INCREF(Py_None);
1148 return Py_None; /* signal caller to try alternative */
1149 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
1152 mtime)) {
1153 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001154 PySys_FormatStderr("# %R has bad mtime\n",
1155 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 Py_INCREF(Py_None);
1157 return Py_None; /* signal caller to try alternative */
1158 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001159
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001160 /* XXX the pyc's size field is ignored; timestamp collisions are probably
1161 unimportant with zip files. */
1162 code = PyMarshal_ReadObjectFromString(buf + 12, size - 12);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (code == NULL)
1164 return NULL;
1165 if (!PyCode_Check(code)) {
1166 Py_DECREF(code);
1167 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001168 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 pathname);
1170 return NULL;
1171 }
1172 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001173}
1174
1175/* Replace any occurances of "\r\n?" in the input string with "\n".
1176 This converts DOS and Mac line endings to Unix line endings.
1177 Also append a trailing "\n" to be compatible with
1178 PyParser_SimpleParseFile(). Returns a new reference. */
1179static PyObject *
1180normalize_line_endings(PyObject *source)
1181{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001182 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 PyObject *fixed_source;
1184 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001185
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001186 p = PyBytes_AsString(source);
1187 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 return PyBytes_FromStringAndSize("\n\0", 2);
1189 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 /* one char extra for trailing \n and one for terminating \0 */
1192 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1193 if (buf == NULL) {
1194 PyErr_SetString(PyExc_MemoryError,
1195 "zipimport: no memory to allocate "
1196 "source buffer");
1197 return NULL;
1198 }
1199 /* replace "\r\n?" by "\n" */
1200 for (q = buf; *p != '\0'; p++) {
1201 if (*p == '\r') {
1202 *q++ = '\n';
1203 if (*(p + 1) == '\n')
1204 p++;
1205 }
1206 else
1207 *q++ = *p;
1208 len++;
1209 }
1210 *q++ = '\n'; /* add trailing \n */
1211 *q = '\0';
1212 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1213 PyMem_Free(buf);
1214 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001215}
1216
1217/* Given a string buffer containing Python source code, compile it
1218 return and return a code object as a new reference. */
1219static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001220compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001221{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001222 PyObject *code, *fixed_source, *pathbytes;
Just van Rossum52e14d62002-12-30 22:08:05 +00001223
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001224 pathbytes = PyUnicode_EncodeFSDefault(pathname);
1225 if (pathbytes == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001227
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001228 fixed_source = normalize_line_endings(source);
1229 if (fixed_source == NULL) {
1230 Py_DECREF(pathbytes);
1231 return NULL;
1232 }
1233
1234 code = Py_CompileString(PyBytes_AsString(fixed_source),
1235 PyBytes_AsString(pathbytes),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 Py_file_input);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001237 Py_DECREF(pathbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 Py_DECREF(fixed_source);
1239 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001240}
1241
1242/* Convert the date/time values found in the Zip archive to a value
1243 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001244static time_t
1245parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 stm.tm_sec = (dostime & 0x1f) * 2;
1252 stm.tm_min = (dostime >> 5) & 0x3f;
1253 stm.tm_hour = (dostime >> 11) & 0x1f;
1254 stm.tm_mday = dosdate & 0x1f;
1255 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1256 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1257 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001260}
1261
1262/* Given a path to a .pyc or .pyo file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001263 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001264 is available. */
1265static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001266get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001267{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001268 PyObject *toc_entry, *stripped;
1269 time_t mtime;
1270
1271 /* strip 'c' or 'o' from *.py[co] */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001272 if (PyUnicode_READY(path) == -1)
1273 return (time_t)-1;
1274 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
1275 PyUnicode_DATA(path),
1276 PyUnicode_GET_LENGTH(path) - 1);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001277 if (stripped == NULL)
1278 return (time_t)-1;
1279
1280 toc_entry = PyDict_GetItem(self->files, stripped);
1281 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1283 PyTuple_Size(toc_entry) == 8) {
1284 /* fetch the time stamp of the .py file for comparison
1285 with an embedded pyc time stamp */
1286 int time, date;
1287 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1288 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1289 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001290 } else
1291 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001293}
1294
1295/* Return the code object for the module named by 'fullname' from the
1296 Zip archive as a new reference. */
1297static PyObject *
1298get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001300{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001301 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001302
Victor Stinner60fe8d92010-08-16 23:48:11 +00001303 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 if (data == NULL)
1305 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001306
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001307 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001308 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001309 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001310 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001311 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 Py_DECREF(data);
1313 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001314}
1315
Ezio Melotti42da6632011-03-15 05:18:48 +02001316/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001317 'fullname'. */
1318static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001319get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001320 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001321{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001322 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001323 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001327 if (subname == NULL)
1328 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001329
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001330 path = make_filename(self->prefix, subname);
1331 Py_DECREF(subname);
1332 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001336 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001337
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001338 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1339 if (fullpath == NULL)
1340 goto exit;
1341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001343 PySys_FormatStderr("# trying %U%c%U\n",
1344 self->archive, (int)SEP, fullpath);
1345 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (toc_entry != NULL) {
1347 time_t mtime = 0;
1348 int ispackage = zso->type & IS_PACKAGE;
1349 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001350
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001351 if (isbytecode) {
1352 mtime = get_mtime_of_source(self, fullpath);
1353 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1354 goto exit;
1355 }
1356 }
1357 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 if (p_ispackage != NULL)
1359 *p_ispackage = ispackage;
1360 code = get_code_from_data(self, ispackage,
1361 isbytecode, mtime,
1362 toc_entry);
1363 if (code == Py_None) {
1364 /* bad magic number or non-matching mtime
1365 in byte code, try next */
1366 Py_DECREF(code);
1367 continue;
1368 }
Victor Stinner08654e12010-10-18 12:09:02 +00001369 if (code != NULL && p_modpath != NULL) {
1370 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1371 Py_INCREF(*p_modpath);
1372 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001373 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001375 else
1376 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001378 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1379exit:
1380 Py_DECREF(path);
1381 Py_XDECREF(fullpath);
1382 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001383}
1384
1385
1386/* Module init */
1387
1388PyDoc_STRVAR(zipimport_doc,
1389"zipimport provides support for importing Python modules from Zip archives.\n\
1390\n\
1391This module exports three objects:\n\
1392- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001393- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001394 subclass of ImportError, so it can be caught as ImportError, too.\n\
1395- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1396 info dicts, as used in zipimporter._files.\n\
1397\n\
1398It is usually not needed to use the zipimport module explicitly; it is\n\
1399used by the builtin import mechanism for sys.path items that are paths\n\
1400to Zip archives.");
1401
Martin v. Löwis1a214512008-06-11 05:26:20 +00001402static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 PyModuleDef_HEAD_INIT,
1404 "zipimport",
1405 zipimport_doc,
1406 -1,
1407 NULL,
1408 NULL,
1409 NULL,
1410 NULL,
1411 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001412};
1413
Just van Rossum52e14d62002-12-30 22:08:05 +00001414PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001415PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (PyType_Ready(&ZipImporter_Type) < 0)
1420 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 /* Correct directory separator */
1423 zip_searchorder[0].suffix[0] = SEP;
1424 zip_searchorder[1].suffix[0] = SEP;
1425 zip_searchorder[2].suffix[0] = SEP;
1426 if (Py_OptimizeFlag) {
1427 /* Reverse *.pyc and *.pyo */
1428 struct st_zip_searchorder tmp;
1429 tmp = zip_searchorder[0];
1430 zip_searchorder[0] = zip_searchorder[1];
1431 zip_searchorder[1] = tmp;
1432 tmp = zip_searchorder[3];
1433 zip_searchorder[3] = zip_searchorder[4];
1434 zip_searchorder[4] = tmp;
1435 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 mod = PyModule_Create(&zipimportmodule);
1438 if (mod == NULL)
1439 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1442 PyExc_ImportError, NULL);
1443 if (ZipImportError == NULL)
1444 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 Py_INCREF(ZipImportError);
1447 if (PyModule_AddObject(mod, "ZipImportError",
1448 ZipImportError) < 0)
1449 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 Py_INCREF(&ZipImporter_Type);
1452 if (PyModule_AddObject(mod, "zipimporter",
1453 (PyObject *)&ZipImporter_Type) < 0)
1454 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 zip_directory_cache = PyDict_New();
1457 if (zip_directory_cache == NULL)
1458 return NULL;
1459 Py_INCREF(zip_directory_cache);
1460 if (PyModule_AddObject(mod, "_zip_directory_cache",
1461 zip_directory_cache) < 0)
1462 return NULL;
1463 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001464}