blob: 9f662f528589e1a70b46612615962b7a80f722ee [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
Victor Stinner651f9f72013-11-12 21:44:18 +010017#ifdef ALTSEP
18_Py_IDENTIFIER(replace);
19#endif
20
Just van Rossum52e14d62002-12-30 22:08:05 +000021/* zip_searchorder defines how we search for a module in the Zip
22 archive: we first search for a package __init__, then for
23 non-package .pyc, .pyo and .py entries. The .pyc and .pyo entries
24 are swapped by initzipimport() if we run in optimized mode. Also,
25 '/' is replaced by SEP there. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +000026static struct st_zip_searchorder zip_searchorder[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
28 {"/__init__.pyo", IS_PACKAGE | IS_BYTECODE},
29 {"/__init__.py", IS_PACKAGE | IS_SOURCE},
30 {".pyc", IS_BYTECODE},
31 {".pyo", IS_BYTECODE},
32 {".py", IS_SOURCE},
33 {"", 0}
Just van Rossum52e14d62002-12-30 22:08:05 +000034};
35
36/* zipimporter object definition and support */
37
38typedef struct _zipimporter ZipImporter;
39
40struct _zipimporter {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 PyObject_HEAD
Victor Stinner9e40fad2010-10-18 22:34:46 +000042 PyObject *archive; /* pathname of the Zip archive,
43 decoded from the filesystem encoding */
Victor Stinner72f767e2010-10-18 11:44:21 +000044 PyObject *prefix; /* file prefix: "a/sub/directory/",
45 encoded to the filesystem encoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 PyObject *files; /* dict with file info {path: toc_entry} */
Just van Rossum52e14d62002-12-30 22:08:05 +000047};
48
Just van Rossum52e14d62002-12-30 22:08:05 +000049static PyObject *ZipImportError;
Victor Stinnerc342fca2010-10-18 11:39:05 +000050/* read_directory() cache */
Just van Rossum52e14d62002-12-30 22:08:05 +000051static PyObject *zip_directory_cache = NULL;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -080052static PyObject *zip_stat_cache = NULL;
53/* posix.fstat or nt.fstat function. Used due to posixmodule.c's
54 * superior fstat implementation over libc's on Windows. */
55static PyObject *fstat_function = NULL; /* posix.fstat() or nt.fstat() */
Just van Rossum52e14d62002-12-30 22:08:05 +000056
57/* forward decls */
Gregory P. Smith2bcbc142014-01-07 18:30:07 -080058static FILE *fopen_rb_and_stat(PyObject *path, PyObject **py_stat_p);
59static FILE *safely_reopen_archive(ZipImporter *self);
60static PyObject *read_directory(FILE *fp, PyObject *archive);
61static PyObject *get_data(FILE *fp, PyObject *archive, PyObject *toc_entry);
Victor Stinnerf6b563a2011-03-14 20:46:50 -040062static PyObject *get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +000063 int *p_ispackage, PyObject **p_modpath);
Just van Rossum52e14d62002-12-30 22:08:05 +000064
65
66#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
67
68
69/* zipimporter.__init__
70 Split the "subdirectory" from the Zip archive path, lookup a matching
71 entry in sys.path_importer_cache, fetch the file directory from there
72 if found, or else read it from the archive. */
73static int
74zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
75{
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010076 PyObject *path, *files, *tmp;
77 PyObject *filename = NULL;
78 Py_ssize_t len, flen;
Just van Rossum52e14d62002-12-30 22:08:05 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 if (!_PyArg_NoKeywords("zipimporter()", kwds))
81 return -1;
Georg Brandl02c42872005-08-26 06:42:30 +000082
Victor Stinner2b8dab72010-08-14 14:54:10 +000083 if (!PyArg_ParseTuple(args, "O&:zipimporter",
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010084 PyUnicode_FSDecoder, &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +000086
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010087 if (PyUnicode_READY(path) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020088 return -1;
89
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010090 len = PyUnicode_GET_LENGTH(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (len == 0) {
92 PyErr_SetString(ZipImportError, "archive path is empty");
Victor Stinner2b8dab72010-08-14 14:54:10 +000093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 }
Just van Rossum52e14d62002-12-30 22:08:05 +000095
96#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +010097 tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +010098 if (!tmp)
99 goto error;
100 Py_DECREF(path);
101 path = tmp;
Just van Rossum52e14d62002-12-30 22:08:05 +0000102#endif
103
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100104 filename = path;
105 Py_INCREF(filename);
106 flen = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 for (;;) {
108 struct stat statbuf;
109 int rv;
Just van Rossum52e14d62002-12-30 22:08:05 +0000110
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100111 rv = _Py_stat(filename, &statbuf);
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100112 if (rv == -2)
113 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (rv == 0) {
115 /* it exists */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100116 if (!S_ISREG(statbuf.st_mode))
117 /* it's a not file */
118 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 break;
120 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100121 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 /* back up one path element */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100123 flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);
124 if (flen == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 break;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100126 filename = PyUnicode_Substring(path, 0, flen);
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100127 if (filename == NULL)
128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100130 if (filename == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyErr_SetString(ZipImportError, "not a Zip file");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000134
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100135 if (PyUnicode_READY(filename) < 0)
136 goto error;
137
138 files = PyDict_GetItem(zip_directory_cache, filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000139 if (files == NULL) {
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800140 PyObject *zip_stat = NULL;
141 FILE *fp = fopen_rb_and_stat(filename, &zip_stat);
142 if (fp == NULL) {
143 if (!PyErr_Occurred())
144 PyErr_Format(ZipImportError, "can't open Zip file: %R",
145 filename);
146
147 Py_XDECREF(zip_stat);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000148 goto error;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800149 }
150
151 if (Py_VerboseFlag)
152 PySys_FormatStderr("# zipimport: %U not cached, "
153 "reading TOC.\n", filename);
154
155 files = read_directory(fp, filename);
156 fclose(fp);
157 if (files == NULL) {
158 Py_XDECREF(zip_stat);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000159 goto error;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800160 }
161 if (PyDict_SetItem(zip_directory_cache, filename, files) != 0) {
162 Py_DECREF(files);
163 Py_XDECREF(zip_stat);
164 goto error;
165 }
166 if (zip_stat && PyDict_SetItem(zip_stat_cache, filename,
167 zip_stat) != 0) {
168 Py_DECREF(files);
169 Py_DECREF(zip_stat);
170 goto error;
171 }
172 Py_XDECREF(zip_stat);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000173 }
174 else
175 Py_INCREF(files);
176 self->files = files;
177
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100178 /* Transfer reference */
179 self->archive = filename;
180 filename = NULL;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000181
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100182 /* Check if there is a prefix directory following the filename. */
183 if (flen != len) {
184 tmp = PyUnicode_Substring(path, flen+1,
185 PyUnicode_GET_LENGTH(path));
186 if (tmp == NULL)
187 goto error;
188 self->prefix = tmp;
189 if (PyUnicode_READ_CHAR(path, len-1) != SEP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 /* add trailing SEP */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100191 tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
192 if (tmp == NULL)
193 goto error;
194 Py_DECREF(self->prefix);
195 self->prefix = tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 }
197 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000198 else
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100199 self->prefix = PyUnicode_New(0, 0);
200 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return 0;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000202
203error:
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100204 Py_DECREF(path);
205 Py_XDECREF(filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000206 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000207}
208
209/* GC support. */
210static int
211zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 ZipImporter *self = (ZipImporter *)obj;
214 Py_VISIT(self->files);
215 return 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000216}
217
218static void
219zipimporter_dealloc(ZipImporter *self)
220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PyObject_GC_UnTrack(self);
222 Py_XDECREF(self->archive);
223 Py_XDECREF(self->prefix);
224 Py_XDECREF(self->files);
225 Py_TYPE(self)->tp_free((PyObject *)self);
Just van Rossum52e14d62002-12-30 22:08:05 +0000226}
227
228static PyObject *
229zipimporter_repr(ZipImporter *self)
230{
Victor Stinner028dd972010-08-17 00:04:48 +0000231 if (self->archive == NULL)
232 return PyUnicode_FromString("<zipimporter object \"???\">");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200233 else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0)
Victor Stinner07298a12010-10-18 22:45:54 +0000234 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000235 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 else
Victor Stinner07298a12010-10-18 22:45:54 +0000237 return PyUnicode_FromFormat("<zipimporter object \"%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000238 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000239}
240
241/* return fullname.split(".")[-1] */
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400242static PyObject *
243get_subname(PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000244{
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100245 Py_ssize_t len, dot;
246 if (PyUnicode_READY(fullname) < 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200247 return NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100248 len = PyUnicode_GET_LENGTH(fullname);
249 dot = PyUnicode_FindChar(fullname, '.', 0, len, -1);
250 if (dot == -1) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400251 Py_INCREF(fullname);
252 return fullname;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100253 } else
254 return PyUnicode_Substring(fullname, dot+1, len);
Just van Rossum52e14d62002-12-30 22:08:05 +0000255}
256
257/* Given a (sub)modulename, write the potential file path in the
258 archive (without extension) to the path buffer. Return the
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400259 length of the resulting string.
260
261 return self.prefix + name.replace('.', os.sep) */
262static PyObject*
263make_filename(PyObject *prefix, PyObject *name)
Just van Rossum52e14d62002-12-30 22:08:05 +0000264{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400265 PyObject *pathobj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200266 Py_UCS4 *p, *buf;
267 Py_ssize_t len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000268
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200269 len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1;
270 p = buf = PyMem_Malloc(sizeof(Py_UCS4) * len);
271 if (buf == NULL) {
272 PyErr_NoMemory();
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400273 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200274 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000275
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200276 if (!PyUnicode_AsUCS4(prefix, p, len, 0)) {
277 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200278 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200279 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200280 p += PyUnicode_GET_LENGTH(prefix);
281 len -= PyUnicode_GET_LENGTH(prefix);
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200282 if (!PyUnicode_AsUCS4(name, p, len, 1)) {
283 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200284 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200285 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400286 for (; *p; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 if (*p == '.')
288 *p = SEP;
289 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200290 pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
291 buf, p-buf);
292 PyMem_Free(buf);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400293 return pathobj;
Just van Rossum52e14d62002-12-30 22:08:05 +0000294}
295
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000296enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 MI_ERROR,
298 MI_NOT_FOUND,
299 MI_MODULE,
300 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000301};
302
Eric V. Smith984b11f2012-05-24 20:21:04 -0400303/* Does this path represent a directory?
304 on error, return < 0
305 if not a dir, return 0
306 if a dir, return 1
307*/
308static int
309check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path)
310{
311 PyObject *dirpath;
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700312 int res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400313
314 /* See if this is a "directory". If so, it's eligible to be part
315 of a namespace package. We test by seeing if the name, with an
316 appended path separator, exists. */
317 dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
318 if (dirpath == NULL)
319 return -1;
320 /* If dirpath is present in self->files, we have a directory. */
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700321 res = PyDict_Contains(self->files, dirpath);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400322 Py_DECREF(dirpath);
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700323 return res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400324}
325
Just van Rossum52e14d62002-12-30 22:08:05 +0000326/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000327static enum zi_module_info
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400328get_module_info(ZipImporter *self, PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000329{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400330 PyObject *subname;
331 PyObject *path, *fullpath, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000333
Victor Stinner965a8a12010-10-18 21:44:33 +0000334 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400335 if (subname == NULL)
336 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000337
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400338 path = make_filename(self->prefix, subname);
339 Py_DECREF(subname);
340 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400344 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
345 if (fullpath == NULL) {
346 Py_DECREF(path);
347 return MI_ERROR;
348 }
349 item = PyDict_GetItem(self->files, fullpath);
350 Py_DECREF(fullpath);
351 if (item != NULL) {
352 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (zso->type & IS_PACKAGE)
354 return MI_PACKAGE;
355 else
356 return MI_MODULE;
357 }
358 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400359 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000361}
362
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700363typedef enum {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700364 FL_ERROR,
365 FL_NOT_FOUND,
366 FL_MODULE_FOUND,
367 FL_NS_FOUND
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700368} find_loader_result;
369
Eric V. Smith984b11f2012-05-24 20:21:04 -0400370/* The guts of "find_loader" and "find_module". Return values:
371 -1: error
372 0: no loader or namespace portions found
373 1: module/package found
374 2: namespace portion found: *namespace_portion will point to the name
375*/
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700376static find_loader_result
Eric V. Smith984b11f2012-05-24 20:21:04 -0400377find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
378{
379 enum zi_module_info mi;
380
381 *namespace_portion = NULL;
382
383 mi = get_module_info(self, fullname);
384 if (mi == MI_ERROR)
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700385 return FL_ERROR;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400386 if (mi == MI_NOT_FOUND) {
387 /* Not a module or regular package. See if this is a directory, and
388 therefore possibly a portion of a namespace package. */
389 int is_dir = check_is_directory(self, self->prefix, fullname);
390 if (is_dir < 0)
391 return -1;
392 if (is_dir) {
393 /* This is possibly a portion of a namespace
394 package. Return the string representing its path,
395 without a trailing separator. */
396 *namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
397 self->archive, SEP,
398 self->prefix, fullname);
399 if (*namespace_portion == NULL)
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700400 return FL_ERROR;
401 return FL_NS_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400402 }
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700403 return FL_NOT_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400404 }
405 /* This is a module or package. */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700406 return FL_MODULE_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400407}
408
409
Just van Rossum52e14d62002-12-30 22:08:05 +0000410/* Check whether we can satisfy the import of the module named by
411 'fullname'. Return self if we can, None if we can't. */
412static PyObject *
413zipimporter_find_module(PyObject *obj, PyObject *args)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 ZipImporter *self = (ZipImporter *)obj;
416 PyObject *path = NULL;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400417 PyObject *fullname;
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700418 PyObject *namespace_portion = NULL;
419 PyObject *result = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000420
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700421 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
422 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000423
Eric V. Smith984b11f2012-05-24 20:21:04 -0400424 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700425 case FL_ERROR:
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700426 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700427 case FL_NS_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700428 /* A namespace portion is not allowed via find_module, so return None. */
Eric V. Smith984b11f2012-05-24 20:21:04 -0400429 Py_DECREF(namespace_portion);
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700430 /* FALL THROUGH */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700431 case FL_NOT_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700432 result = Py_None;
433 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700434 case FL_MODULE_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700435 result = (PyObject *)self;
436 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 }
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700438 Py_INCREF(result);
Benjamin Peterson2d12e142012-05-25 00:19:40 -0700439 return result;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400440}
441
442
443/* Check whether we can satisfy the import of the module named by
444 'fullname', or whether it could be a portion of a namespace
445 package. Return self if we can load it, a string containing the
446 full path if it's a possible namespace portion, None if we
447 can't load it. */
448static PyObject *
449zipimporter_find_loader(PyObject *obj, PyObject *args)
450{
451 ZipImporter *self = (ZipImporter *)obj;
452 PyObject *path = NULL;
453 PyObject *fullname;
454 PyObject *result = NULL;
455 PyObject *namespace_portion = NULL;
456
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700457 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
458 return NULL;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400459
460 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700461 case FL_ERROR:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700462 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700463 case FL_NOT_FOUND: /* Not found, return (None, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700464 result = Py_BuildValue("O[]", Py_None);
465 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700466 case FL_MODULE_FOUND: /* Return (self, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700467 result = Py_BuildValue("O[]", self);
468 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700469 case FL_NS_FOUND: /* Return (None, [namespace_portion]) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700470 result = Py_BuildValue("O[O]", Py_None, namespace_portion);
Benjamin Peterson209e04c2012-05-24 22:35:39 -0700471 Py_DECREF(namespace_portion);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400472 return result;
473 }
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700474 return result;
Just van Rossum52e14d62002-12-30 22:08:05 +0000475}
476
477/* Load and return the module named by 'fullname'. */
478static PyObject *
479zipimporter_load_module(PyObject *obj, PyObject *args)
480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner26fabe12010-10-18 12:03:25 +0000482 PyObject *code = NULL, *mod, *dict;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400483 PyObject *fullname;
484 PyObject *modpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000486
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400487 if (!PyArg_ParseTuple(args, "U:zipimporter.load_module",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 &fullname))
489 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200490 if (PyUnicode_READY(fullname) == -1)
491 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 code = get_module_code(self, fullname, &ispackage, &modpath);
494 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000495 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000496
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400497 mod = PyImport_AddModuleObject(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000498 if (mod == NULL)
499 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* mod.__loader__ = self */
503 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
504 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (ispackage) {
507 /* add __path__ to the module *before* the code gets
508 executed */
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100509 PyObject *pkgpath, *fullpath, *subname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000511
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100512 subname = get_subname(fullname);
513 if (subname == NULL)
514 goto error;
515
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400516 fullpath = PyUnicode_FromFormat("%U%c%U%U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 self->archive, SEP,
518 self->prefix, subname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400519 Py_DECREF(subname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (fullpath == NULL)
521 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000522
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400523 pkgpath = Py_BuildValue("[N]", fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (pkgpath == NULL)
525 goto error;
526 err = PyDict_SetItemString(dict, "__path__", pkgpath);
527 Py_DECREF(pkgpath);
528 if (err != 0)
529 goto error;
530 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400531 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
Victor Stinner26fabe12010-10-18 12:03:25 +0000532 Py_CLEAR(code);
533 if (mod == NULL)
534 goto error;
535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400537 PySys_FormatStderr("import %U # loaded from Zip %U\n",
Victor Stinner08654e12010-10-18 12:09:02 +0000538 fullname, modpath);
539 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000541error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000542 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000543 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000545}
546
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000547/* Return a string matching __file__ for the named module */
548static PyObject *
549zipimporter_get_filename(PyObject *obj, PyObject *args)
550{
551 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400552 PyObject *fullname, *code, *modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000553 int ispackage;
554
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400555 if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename",
Victor Stinner9e40fad2010-10-18 22:34:46 +0000556 &fullname))
Victor Stinnerc342fca2010-10-18 11:39:05 +0000557 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000558
559 /* Deciding the filename requires working out where the code
560 would come from if the module was actually loaded */
561 code = get_module_code(self, fullname, &ispackage, &modpath);
562 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000563 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000564 Py_DECREF(code); /* Only need the path info */
565
Victor Stinner08654e12010-10-18 12:09:02 +0000566 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000567}
568
Just van Rossum52e14d62002-12-30 22:08:05 +0000569/* Return a bool signifying whether the module is a package or not. */
570static PyObject *
571zipimporter_is_package(PyObject *obj, PyObject *args)
572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400574 PyObject *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000576
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400577 if (!PyArg_ParseTuple(args, "U:zipimporter.is_package",
Victor Stinner965a8a12010-10-18 21:44:33 +0000578 &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 mi = get_module_info(self, fullname);
582 if (mi == MI_ERROR)
Victor Stinner965a8a12010-10-18 21:44:33 +0000583 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400585 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000586 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 }
588 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000589}
590
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200591
Just van Rossum52e14d62002-12-30 22:08:05 +0000592static PyObject *
593zipimporter_get_data(PyObject *obj, PyObject *args)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 ZipImporter *self = (ZipImporter *)obj;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100596 PyObject *path, *key;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800597 FILE *fp;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800598 PyObject *toc_entry, *data;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100599 Py_ssize_t path_start, path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000600
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100601 if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000603
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +0100605 path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100606 if (!path)
607 return NULL;
608#else
609 Py_INCREF(path);
Just van Rossum52e14d62002-12-30 22:08:05 +0000610#endif
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100611 if (PyUnicode_READY(path) == -1)
612 goto error;
613
614 path_len = PyUnicode_GET_LENGTH(path);
615
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 len = PyUnicode_GET_LENGTH(self->archive);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100617 path_start = 0;
618 if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
619 && PyUnicode_READ_CHAR(path, len) == SEP) {
620 path_start = len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000622
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100623 key = PyUnicode_Substring(path, path_start, path_len);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000624 if (key == NULL)
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100625 goto error;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800626
627 fp = safely_reopen_archive(self);
628 if (fp == NULL)
629 goto error;
630
Victor Stinner60fe8d92010-08-16 23:48:11 +0000631 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (toc_entry == NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000633 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
634 Py_DECREF(key);
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800635 fclose(fp);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100636 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000638 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100639 Py_DECREF(path);
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800640 data = get_data(fp, self->archive, toc_entry);
641 fclose(fp);
642 return data;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100643 error:
644 Py_DECREF(path);
645 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000646}
647
648static PyObject *
649zipimporter_get_code(PyObject *obj, PyObject *args)
650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400652 PyObject *fullname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000653
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400654 if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000658}
659
660static PyObject *
661zipimporter_get_source(PyObject *obj, PyObject *args)
662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 ZipImporter *self = (ZipImporter *)obj;
664 PyObject *toc_entry;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400665 PyObject *fullname, *subname, *path, *fullpath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 enum zi_module_info mi;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800667 FILE *fp;
Just van Rossum52e14d62002-12-30 22:08:05 +0000668
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400669 if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000673 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000675 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400676 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000677 return NULL;
678 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400679
Victor Stinner965a8a12010-10-18 21:44:33 +0000680 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400681 if (subname == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000683
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400684 path = make_filename(self->prefix, subname);
685 Py_DECREF(subname);
686 if (path == NULL)
687 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000688
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400689 if (mi == MI_PACKAGE)
690 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
691 else
692 fullpath = PyUnicode_FromFormat("%U.py", path);
693 Py_DECREF(path);
694 if (fullpath == NULL)
695 return NULL;
696
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800697 fp = safely_reopen_archive(self);
698 if (fp == NULL) {
699 Py_DECREF(fullpath);
700 return NULL;
701 }
702
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400703 toc_entry = PyDict_GetItem(self->files, fullpath);
704 Py_DECREF(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000706 PyObject *res, *bytes;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800707 bytes = get_data(fp, self->archive, toc_entry);
708 fclose(fp);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000709 if (bytes == NULL)
710 return NULL;
711 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
712 PyBytes_GET_SIZE(bytes));
713 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 return res;
715 }
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800716 fclose(fp);
Just van Rossum52e14d62002-12-30 22:08:05 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 /* we have the module, but no source */
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800719 Py_RETURN_NONE;
Just van Rossum52e14d62002-12-30 22:08:05 +0000720}
721
722PyDoc_STRVAR(doc_find_module,
723"find_module(fullname, path=None) -> self or None.\n\
724\n\
725Search for a module specified by 'fullname'. 'fullname' must be the\n\
726fully qualified (dotted) module name. It returns the zipimporter\n\
727instance itself if the module was found, or None if it wasn't.\n\
728The optional 'path' argument is ignored -- it's there for compatibility\n\
729with the importer protocol.");
730
Eric V. Smith984b11f2012-05-24 20:21:04 -0400731PyDoc_STRVAR(doc_find_loader,
732"find_loader(fullname, path=None) -> self, str or None.\n\
733\n\
734Search for a module specified by 'fullname'. 'fullname' must be the\n\
735fully qualified (dotted) module name. It returns the zipimporter\n\
736instance itself if the module was found, a string containing the\n\
737full path name if it's possibly a portion of a namespace package,\n\
738or None otherwise. The optional 'path' argument is ignored -- it's\n\
739 there for compatibility with the importer protocol.");
740
Just van Rossum52e14d62002-12-30 22:08:05 +0000741PyDoc_STRVAR(doc_load_module,
742"load_module(fullname) -> module.\n\
743\n\
744Load the module specified by 'fullname'. 'fullname' must be the\n\
745fully qualified (dotted) module name. It returns the imported\n\
746module, or raises ZipImportError if it wasn't found.");
747
748PyDoc_STRVAR(doc_get_data,
749"get_data(pathname) -> string with file data.\n\
750\n\
751Return the data associated with 'pathname'. Raise IOError if\n\
752the file wasn't found.");
753
754PyDoc_STRVAR(doc_is_package,
755"is_package(fullname) -> bool.\n\
756\n\
757Return True if the module specified by fullname is a package.\n\
Brian Curtin32839732010-07-21 01:44:19 +0000758Raise ZipImportError if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000759
760PyDoc_STRVAR(doc_get_code,
761"get_code(fullname) -> code object.\n\
762\n\
763Return the code object for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000764if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000765
766PyDoc_STRVAR(doc_get_source,
767"get_source(fullname) -> source string.\n\
768\n\
769Return the source code for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000770if the module couldn't be found, return None if the archive does\n\
Just van Rossum52e14d62002-12-30 22:08:05 +0000771contain the module, but has no source for it.");
772
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000773
774PyDoc_STRVAR(doc_get_filename,
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000775"get_filename(fullname) -> filename string.\n\
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000776\n\
777Return the filename for the specified module.");
778
Just van Rossum52e14d62002-12-30 22:08:05 +0000779static PyMethodDef zipimporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 {"find_module", zipimporter_find_module, METH_VARARGS,
781 doc_find_module},
Eric V. Smith984b11f2012-05-24 20:21:04 -0400782 {"find_loader", zipimporter_find_loader, METH_VARARGS,
783 doc_find_loader},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 {"load_module", zipimporter_load_module, METH_VARARGS,
785 doc_load_module},
786 {"get_data", zipimporter_get_data, METH_VARARGS,
787 doc_get_data},
788 {"get_code", zipimporter_get_code, METH_VARARGS,
789 doc_get_code},
790 {"get_source", zipimporter_get_source, METH_VARARGS,
791 doc_get_source},
792 {"get_filename", zipimporter_get_filename, METH_VARARGS,
793 doc_get_filename},
794 {"is_package", zipimporter_is_package, METH_VARARGS,
795 doc_is_package},
796 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000797};
798
799static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
801 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
802 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
803 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000804};
805
806PyDoc_STRVAR(zipimporter_doc,
807"zipimporter(archivepath) -> zipimporter object\n\
808\n\
809Create a new zipimporter instance. 'archivepath' must be a path to\n\
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000810a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
811'/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
812valid directory inside the archive.\n\
813\n\
814'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
815archive.\n\
816\n\
817The 'archive' attribute of zipimporter objects contains the name of the\n\
818zipfile targeted.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000819
820#define DEFERRED_ADDRESS(ADDR) 0
821
822static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
824 "zipimport.zipimporter",
825 sizeof(ZipImporter),
826 0, /* tp_itemsize */
827 (destructor)zipimporter_dealloc, /* tp_dealloc */
828 0, /* tp_print */
829 0, /* tp_getattr */
830 0, /* tp_setattr */
831 0, /* tp_reserved */
832 (reprfunc)zipimporter_repr, /* tp_repr */
833 0, /* tp_as_number */
834 0, /* tp_as_sequence */
835 0, /* tp_as_mapping */
836 0, /* tp_hash */
837 0, /* tp_call */
838 0, /* tp_str */
839 PyObject_GenericGetAttr, /* tp_getattro */
840 0, /* tp_setattro */
841 0, /* tp_as_buffer */
842 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
843 Py_TPFLAGS_HAVE_GC, /* tp_flags */
844 zipimporter_doc, /* tp_doc */
845 zipimporter_traverse, /* tp_traverse */
846 0, /* tp_clear */
847 0, /* tp_richcompare */
848 0, /* tp_weaklistoffset */
849 0, /* tp_iter */
850 0, /* tp_iternext */
851 zipimporter_methods, /* tp_methods */
852 zipimporter_members, /* tp_members */
853 0, /* tp_getset */
854 0, /* tp_base */
855 0, /* tp_dict */
856 0, /* tp_descr_get */
857 0, /* tp_descr_set */
858 0, /* tp_dictoffset */
859 (initproc)zipimporter_init, /* tp_init */
860 PyType_GenericAlloc, /* tp_alloc */
861 PyType_GenericNew, /* tp_new */
862 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000863};
864
865
866/* implementation */
867
Just van Rossum52e14d62002-12-30 22:08:05 +0000868/* Given a buffer, return the long that is represented by the first
869 4 bytes, encoded as little endian. This partially reimplements
870 marshal.c:r_long() */
871static long
872get_long(unsigned char *buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 long x;
874 x = buf[0];
875 x |= (long)buf[1] << 8;
876 x |= (long)buf[2] << 16;
877 x |= (long)buf[3] << 24;
Just van Rossum52e14d62002-12-30 22:08:05 +0000878#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 /* Sign extension for 64-bit machines */
880 x |= -(x & 0x80000000L);
Just van Rossum52e14d62002-12-30 22:08:05 +0000881#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000883}
884
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800885/* Return 1 if objects a and b fail a Py_EQ test for an attr. */
886static int
887compare_obj_attr_strings(PyObject *obj_a, PyObject *obj_b, char *attr_name)
888{
889 int problem = 0;
890 PyObject *attr_a = PyObject_GetAttrString(obj_a, attr_name);
891 PyObject *attr_b = PyObject_GetAttrString(obj_b, attr_name);
892 if (attr_a == NULL || attr_b == NULL)
893 problem = 1;
894 else
895 problem = (PyObject_RichCompareBool(attr_a, attr_b, Py_EQ) != 1);
896 Py_XDECREF(attr_a);
897 Py_XDECREF(attr_b);
898 return problem;
899}
Just van Rossum52e14d62002-12-30 22:08:05 +0000900
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800901/*
902 * Returns an open FILE * on success.
903 * Returns NULL on error with the Python error context set.
904 */
905static FILE *
906safely_reopen_archive(ZipImporter *self)
907{
908 FILE *fp;
909 PyObject *stat_now = NULL;
910
911 fp = fopen_rb_and_stat(self->archive, &stat_now);
912 if (!fp) {
913 PyErr_Format(ZipImportError,
914 "zipimport: can not open file %U", self->archive);
915 Py_XDECREF(stat_now);
916 return NULL;
917 }
918
919 if (stat_now != NULL) {
920 int problem = 0;
921 PyObject *files;
922 PyObject *prev_stat = PyDict_GetItem(zip_stat_cache, self->archive);
923 /* Test stat_now vs the old cached stat on some key attributes. */
924 if (prev_stat != NULL) {
925 problem = compare_obj_attr_strings(prev_stat, stat_now,
926 "st_ino");
927 problem |= compare_obj_attr_strings(prev_stat, stat_now,
928 "st_size");
929 problem |= compare_obj_attr_strings(prev_stat, stat_now,
930 "st_mtime");
931 } else {
932 if (Py_VerboseFlag)
933 PySys_FormatStderr("# zipimport: no stat data for %U!\n",
934 self->archive);
935 problem = 1;
936 }
937
938 if (problem) {
939 if (Py_VerboseFlag)
940 PySys_FormatStderr("# zipimport: %U modified since last"
941 " import, rereading TOC.\n", self->archive);
942 files = read_directory(fp, self->archive);
943 if (files == NULL) {
944 Py_DECREF(stat_now);
945 fclose(fp);
946 return NULL;
947 }
948 if (PyDict_SetItem(zip_directory_cache, self->archive,
949 files) != 0) {
950 Py_DECREF(files);
951 Py_DECREF(stat_now);
952 fclose(fp);
953 return NULL;
954 }
955 if (stat_now && PyDict_SetItem(zip_stat_cache, self->archive,
956 stat_now) != 0) {
957 Py_DECREF(files);
958 Py_DECREF(stat_now);
959 fclose(fp);
960 return NULL;
961 }
962 Py_XDECREF(self->files); /* free the old value. */
963 self->files = files;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800964 }
Benjamin Peterson21e7d4c2014-01-09 09:36:10 -0600965 Py_DECREF(stat_now);
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800966 } /* stat succeeded */
967
968 return fp;
969}
970
971/*
972 fopen_rb_and_stat(path, &py_stat) -> FILE *
973
974 Opens path in "rb" mode and populates the Python py_stat stat_result
975 with information about the opened file. *py_stat may not be changed
976 if there is no fstat_function or if fstat_function fails.
977
978 Returns NULL and does nothing to *py_stat if the open failed.
979*/
980static FILE *
981fopen_rb_and_stat(PyObject *path, PyObject **py_stat_p)
982{
983 FILE *fp;
984 assert(py_stat_p != NULL);
985 assert(*py_stat_p == NULL);
986
Gregory P. Smith2e385e22014-01-07 18:34:23 -0800987 fp = _Py_fopen_obj(path, "rb");
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800988 if (fp == NULL) {
989 if (!PyErr_Occurred())
990 PyErr_Format(ZipImportError,
991 "zipimport: can not open file %U", path);
992 return NULL;
993 }
994
995 if (fstat_function) {
996 PyObject *stat_result = PyObject_CallFunction(fstat_function,
997 "i", fileno(fp));
998 if (stat_result == NULL) {
999 PyErr_Clear(); /* We can function without it. */
1000 } else {
1001 *py_stat_p = stat_result;
1002 }
1003 }
1004
1005 return fp;
1006}
1007
1008/*
1009 read_directory(fp, archive) -> files dict (new reference)
1010
1011 Given an open Zip archive, build a dict, mapping file names
Just van Rossum52e14d62002-12-30 22:08:05 +00001012 (local to the archive, using SEP as a separator) to toc entries.
1013
1014 A toc_entry is a tuple:
1015
Victor Stinner08654e12010-10-18 12:09:02 +00001016 (__file__, # value to use for __file__, available for all files,
1017 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 compress, # compression kind; 0 for uncompressed
1019 data_size, # size of compressed data on disk
1020 file_size, # size of decompressed data
1021 file_offset, # offset of file header from start of archive
1022 time, # mod time of file (in dos format)
1023 date, # mod data of file (in dos format)
1024 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +00001025 )
Just van Rossum52e14d62002-12-30 22:08:05 +00001026
1027 Directories can be recognized by the trailing SEP in the name,
1028 data_size and file_offset are 0.
1029*/
1030static PyObject *
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001031read_directory(FILE *fp, PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +00001032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PyObject *files = NULL;
Victor Stinnerd36c8212010-10-18 12:13:46 +00001034 unsigned short flags;
Gregory P. Smithab320662012-01-30 15:17:33 -08001035 short compress, time, date, name_size;
1036 long crc, data_size, file_size, header_size;
1037 Py_ssize_t file_offset, header_position, header_offset;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001038 long l, count;
1039 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 char name[MAXPATHLEN + 5];
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001041 char dummy[8]; /* Buffer to read unused header values into */
Victor Stinner2460a432010-08-16 17:54:28 +00001042 PyObject *nameobj = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 char *p, endof_central_dir[22];
Gregory P. Smithab320662012-01-30 15:17:33 -08001044 Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +01001045 PyObject *path;
Victor Stinnerd36c8212010-10-18 12:13:46 +00001046 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +00001047 int bootstrap;
Just van Rossum52e14d62002-12-30 22:08:05 +00001048
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001049 assert(fp != NULL);
Jesus Cea09bf7a72012-10-03 02:13:05 +02001050 if (fseek(fp, -22, SEEK_END) == -1) {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001051 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1052 return NULL;
1053 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 header_position = ftell(fp);
1055 if (fread(endof_central_dir, 1, 22, fp) != 22) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001056 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 return NULL;
1058 }
1059 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
1060 /* Bad: End of Central Dir signature */
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001061 PyErr_Format(ZipImportError, "not a Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 return NULL;
1063 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 header_size = get_long((unsigned char *)endof_central_dir + 12);
1066 header_offset = get_long((unsigned char *)endof_central_dir + 16);
1067 arc_offset = header_position - header_offset - header_size;
1068 header_offset += arc_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 files = PyDict_New();
1071 if (files == NULL)
1072 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 /* Start of Central Directory */
1075 count = 0;
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001076 if (fseek(fp, header_offset, 0) == -1)
1077 goto file_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 for (;;) {
1079 PyObject *t;
1080 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +00001081
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001082 /* Start of file header */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 l = PyMarshal_ReadLongFromFile(fp);
Victor Stinner73660af2013-10-29 01:43:44 +01001084 if (l == -1 && PyErr_Occurred())
1085 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (l != 0x02014B50)
1087 break; /* Bad: Central Dir File Header */
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001088
1089 /* On Windows, calling fseek to skip over the fields we don't use is
1090 slower than reading the data into a dummy buffer because fseek flushes
1091 stdio's internal buffers. See issue #8745. */
1092 if (fread(dummy, 1, 4, fp) != 4) /* Skip unused fields, avoid fseek */
1093 goto file_error;
1094
Victor Stinnerd36c8212010-10-18 12:13:46 +00001095 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 compress = PyMarshal_ReadShortFromFile(fp);
1097 time = PyMarshal_ReadShortFromFile(fp);
1098 date = PyMarshal_ReadShortFromFile(fp);
1099 crc = PyMarshal_ReadLongFromFile(fp);
1100 data_size = PyMarshal_ReadLongFromFile(fp);
1101 file_size = PyMarshal_ReadLongFromFile(fp);
1102 name_size = PyMarshal_ReadShortFromFile(fp);
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001103 header_size = name_size +
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 PyMarshal_ReadShortFromFile(fp) +
1105 PyMarshal_ReadShortFromFile(fp);
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001106 if (fread(dummy, 1, 8, fp) != 8) /* Skip unused fields, avoid fseek */
1107 goto file_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
Victor Stinner73660af2013-10-29 01:43:44 +01001109 if (PyErr_Occurred())
1110 goto error;
1111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 if (name_size > MAXPATHLEN)
1113 name_size = MAXPATHLEN;
Just van Rossum52e14d62002-12-30 22:08:05 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 p = name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001116 for (i = 0; i < (Py_ssize_t)name_size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 *p = (char)getc(fp);
1118 if (*p == '/')
1119 *p = SEP;
1120 p++;
1121 }
1122 *p = 0; /* Add terminating null byte */
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001123 for (; i < header_size; i++) /* Skip the rest of the header */
1124 if(getc(fp) == EOF) /* Avoid fseek */
1125 goto file_error;
Just van Rossum52e14d62002-12-30 22:08:05 +00001126
Victor Stinner4ee65a92011-01-22 10:30:29 +00001127 bootstrap = 0;
Victor Stinnerd36c8212010-10-18 12:13:46 +00001128 if (flags & 0x0800)
1129 charset = "utf-8";
Victor Stinner4ee65a92011-01-22 10:30:29 +00001130 else if (!PyThreadState_GET()->interp->codecs_initialized) {
1131 /* During bootstrap, we may need to load the encodings
1132 package from a ZIP file. But the cp437 encoding is implemented
1133 in Python in the encodings package.
1134
1135 Break out of this dependency by assuming that the path to
1136 the encodings module is ASCII-only. */
1137 charset = "ascii";
1138 bootstrap = 1;
1139 }
Victor Stinnerd36c8212010-10-18 12:13:46 +00001140 else
1141 charset = "cp437";
1142 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner4ee65a92011-01-22 10:30:29 +00001143 if (nameobj == NULL) {
1144 if (bootstrap)
1145 PyErr_Format(PyExc_NotImplementedError,
1146 "bootstrap issue: python%i%i.zip contains non-ASCII "
1147 "filenames without the unicode flag",
1148 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Victor Stinner2460a432010-08-16 17:54:28 +00001149 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +00001150 }
Stefan Krah000fde92012-08-20 14:14:49 +02001151 if (PyUnicode_READY(nameobj) == -1)
1152 goto error;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +01001153 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
1154 if (path == NULL)
Victor Stinner2460a432010-08-16 17:54:28 +00001155 goto error;
Gregory P. Smithcc6abd52012-01-30 15:55:29 -08001156 t = Py_BuildValue("Nhllnhhl", path, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 file_size, file_offset, time, date, crc);
1158 if (t == NULL)
1159 goto error;
Victor Stinner2460a432010-08-16 17:54:28 +00001160 err = PyDict_SetItem(files, nameobj, t);
1161 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 Py_DECREF(t);
1163 if (err != 0)
1164 goto error;
1165 count++;
1166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001168 PySys_FormatStderr("# zipimport: found %ld names in %R\n",
1169 count, archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 return files;
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001171file_error:
Jesus Cea09bf7a72012-10-03 02:13:05 +02001172 Py_XDECREF(files);
1173 Py_XDECREF(nameobj);
1174 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1175 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001176error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +00001178 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001180}
1181
1182/* Return the zlib.decompress function object, or NULL if zlib couldn't
1183 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +02001184 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001185static PyObject *
1186get_decompress_func(void)
1187{
Victor Stinner4925cde2011-05-20 00:16:09 +02001188 static int importing_zlib = 0;
1189 PyObject *zlib;
1190 PyObject *decompress;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001191 _Py_IDENTIFIER(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001192
Victor Stinner4925cde2011-05-20 00:16:09 +02001193 if (importing_zlib != 0)
1194 /* Someone has a zlib.py[co] in their Zip file;
1195 let's avoid a stack overflow. */
1196 return NULL;
1197 importing_zlib = 1;
1198 zlib = PyImport_ImportModuleNoBlock("zlib");
1199 importing_zlib = 0;
1200 if (zlib != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001201 decompress = _PyObject_GetAttrId(zlib,
1202 &PyId_decompress);
Victor Stinner4925cde2011-05-20 00:16:09 +02001203 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 }
Victor Stinner4925cde2011-05-20 00:16:09 +02001205 else {
1206 PyErr_Clear();
1207 decompress = NULL;
1208 }
1209 if (Py_VerboseFlag)
1210 PySys_WriteStderr("# zipimport: zlib %s\n",
1211 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +00001213}
1214
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001215/* Given a FILE* to a Zip file and a toc_entry, return the (uncompressed)
Just van Rossum52e14d62002-12-30 22:08:05 +00001216 data as a new reference. */
1217static PyObject *
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001218get_data(FILE *fp, PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PyObject *raw_data, *data = NULL, *decompress;
1221 char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 int err;
1223 Py_ssize_t bytes_read = 0;
1224 long l;
Victor Stinner60fe8d92010-08-16 23:48:11 +00001225 PyObject *datapath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 long compress, data_size, file_size, file_offset, bytes_size;
1227 long time, date, crc;
Just van Rossum52e14d62002-12-30 22:08:05 +00001228
Victor Stinner60fe8d92010-08-16 23:48:11 +00001229 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 &data_size, &file_size, &file_offset, &time,
1231 &date, &crc)) {
1232 return NULL;
1233 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 /* Check to make sure the local file header is correct */
Jesus Cea09bf7a72012-10-03 02:13:05 +02001236 if (fseek(fp, file_offset, 0) == -1) {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001237 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1238 return NULL;
1239 }
1240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 l = PyMarshal_ReadLongFromFile(fp);
1242 if (l != 0x04034B50) {
1243 /* Bad: Local File Header */
Victor Stinner73660af2013-10-29 01:43:44 +01001244 if (!PyErr_Occurred())
1245 PyErr_Format(ZipImportError,
1246 "bad local file header in %U",
1247 archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 return NULL;
1249 }
Jesus Cea09bf7a72012-10-03 02:13:05 +02001250 if (fseek(fp, file_offset + 26, 0) == -1) {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001251 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1252 return NULL;
1253 }
1254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 l = 30 + PyMarshal_ReadShortFromFile(fp) +
1256 PyMarshal_ReadShortFromFile(fp); /* local header size */
Victor Stinner73660af2013-10-29 01:43:44 +01001257 if (PyErr_Occurred()) {
Victor Stinner73660af2013-10-29 01:43:44 +01001258 return NULL;
1259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 file_offset += l; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 bytes_size = compress == 0 ? data_size : data_size + 1;
1263 if (bytes_size == 0)
1264 bytes_size++;
1265 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Just van Rossum52e14d62002-12-30 22:08:05 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (raw_data == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return NULL;
1269 }
1270 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 err = fseek(fp, file_offset, 0);
Jesus Cea09bf7a72012-10-03 02:13:05 +02001273 if (err == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 bytes_read = fread(buf, 1, data_size, fp);
Jesus Cea09bf7a72012-10-03 02:13:05 +02001275 } else {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001276 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1277 return NULL;
1278 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (err || bytes_read != data_size) {
1280 PyErr_SetString(PyExc_IOError,
1281 "zipimport: can't read data");
1282 Py_DECREF(raw_data);
1283 return NULL;
1284 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 if (compress != 0) {
1287 buf[data_size] = 'Z'; /* saw this in zipfile.py */
1288 data_size++;
1289 }
1290 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (compress == 0) { /* data is not compressed */
1293 data = PyBytes_FromStringAndSize(buf, data_size);
1294 Py_DECREF(raw_data);
1295 return data;
1296 }
1297
1298 /* Decompress with zlib */
1299 decompress = get_decompress_func();
1300 if (decompress == NULL) {
1301 PyErr_SetString(ZipImportError,
1302 "can't decompress data; "
1303 "zlib not available");
1304 goto error;
1305 }
1306 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +02001307 Py_DECREF(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001308error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 Py_DECREF(raw_data);
1310 return data;
Just van Rossum52e14d62002-12-30 22:08:05 +00001311}
1312
1313/* Lenient date/time comparison function. The precision of the mtime
1314 in the archive is lower than the mtime stored in a .pyc: we
1315 must allow a difference of at most one second. */
1316static int
1317eq_mtime(time_t t1, time_t t2)
1318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 time_t d = t1 - t2;
1320 if (d < 0)
1321 d = -d;
1322 /* dostime only stores even seconds, so be lenient */
1323 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001324}
1325
1326/* Given the contents of a .py[co] file in a buffer, unmarshal the data
1327 and return the code object. Return None if it the magic word doesn't
1328 match (we do this instead of raising an exception as we fall back
1329 to .py if available and we don't want to mask other errors).
1330 Returns a new reference. */
1331static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001332unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 PyObject *code;
1335 char *buf = PyBytes_AsString(data);
1336 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (size <= 9) {
1339 PyErr_SetString(ZipImportError,
1340 "bad pyc data");
1341 return NULL;
1342 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
1345 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001346 PySys_FormatStderr("# %R has bad magic\n",
1347 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 Py_INCREF(Py_None);
1349 return Py_None; /* signal caller to try alternative */
1350 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
1353 mtime)) {
1354 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001355 PySys_FormatStderr("# %R has bad mtime\n",
1356 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 Py_INCREF(Py_None);
1358 return Py_None; /* signal caller to try alternative */
1359 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001360
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001361 /* XXX the pyc's size field is ignored; timestamp collisions are probably
1362 unimportant with zip files. */
1363 code = PyMarshal_ReadObjectFromString(buf + 12, size - 12);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if (code == NULL)
1365 return NULL;
1366 if (!PyCode_Check(code)) {
1367 Py_DECREF(code);
1368 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001369 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 pathname);
1371 return NULL;
1372 }
1373 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001374}
1375
1376/* Replace any occurances of "\r\n?" in the input string with "\n".
1377 This converts DOS and Mac line endings to Unix line endings.
1378 Also append a trailing "\n" to be compatible with
1379 PyParser_SimpleParseFile(). Returns a new reference. */
1380static PyObject *
1381normalize_line_endings(PyObject *source)
1382{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001383 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 PyObject *fixed_source;
1385 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001386
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001387 p = PyBytes_AsString(source);
1388 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 return PyBytes_FromStringAndSize("\n\0", 2);
1390 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 /* one char extra for trailing \n and one for terminating \0 */
1393 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1394 if (buf == NULL) {
1395 PyErr_SetString(PyExc_MemoryError,
1396 "zipimport: no memory to allocate "
1397 "source buffer");
1398 return NULL;
1399 }
1400 /* replace "\r\n?" by "\n" */
1401 for (q = buf; *p != '\0'; p++) {
1402 if (*p == '\r') {
1403 *q++ = '\n';
1404 if (*(p + 1) == '\n')
1405 p++;
1406 }
1407 else
1408 *q++ = *p;
1409 len++;
1410 }
1411 *q++ = '\n'; /* add trailing \n */
1412 *q = '\0';
1413 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1414 PyMem_Free(buf);
1415 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001416}
1417
1418/* Given a string buffer containing Python source code, compile it
Brett Cannon83358c92013-06-20 21:30:32 -04001419 and return a code object as a new reference. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001420static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001421compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001422{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001423 PyObject *code, *fixed_source, *pathbytes;
Just van Rossum52e14d62002-12-30 22:08:05 +00001424
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001425 pathbytes = PyUnicode_EncodeFSDefault(pathname);
1426 if (pathbytes == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001428
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001429 fixed_source = normalize_line_endings(source);
1430 if (fixed_source == NULL) {
1431 Py_DECREF(pathbytes);
1432 return NULL;
1433 }
1434
1435 code = Py_CompileString(PyBytes_AsString(fixed_source),
1436 PyBytes_AsString(pathbytes),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 Py_file_input);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001438 Py_DECREF(pathbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 Py_DECREF(fixed_source);
1440 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001441}
1442
1443/* Convert the date/time values found in the Zip archive to a value
1444 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001445static time_t
1446parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 stm.tm_sec = (dostime & 0x1f) * 2;
1453 stm.tm_min = (dostime >> 5) & 0x3f;
1454 stm.tm_hour = (dostime >> 11) & 0x1f;
1455 stm.tm_mday = dosdate & 0x1f;
1456 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1457 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1458 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001461}
1462
1463/* Given a path to a .pyc or .pyo file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001464 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001465 is available. */
1466static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001467get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001468{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001469 PyObject *toc_entry, *stripped;
1470 time_t mtime;
1471
1472 /* strip 'c' or 'o' from *.py[co] */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001473 if (PyUnicode_READY(path) == -1)
1474 return (time_t)-1;
1475 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
1476 PyUnicode_DATA(path),
1477 PyUnicode_GET_LENGTH(path) - 1);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001478 if (stripped == NULL)
1479 return (time_t)-1;
1480
1481 toc_entry = PyDict_GetItem(self->files, stripped);
1482 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1484 PyTuple_Size(toc_entry) == 8) {
1485 /* fetch the time stamp of the .py file for comparison
1486 with an embedded pyc time stamp */
1487 int time, date;
1488 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1489 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1490 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001491 } else
1492 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001494}
1495
1496/* Return the code object for the module named by 'fullname' from the
1497 Zip archive as a new reference. */
1498static PyObject *
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001499get_code_from_data(ZipImporter *self, FILE *fp, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001501{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001502 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001503
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001504 data = get_data(fp, self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (data == NULL)
1506 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001507
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001508 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001509 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001510 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001511 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001512 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 Py_DECREF(data);
1514 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001515}
1516
Ezio Melotti42da6632011-03-15 05:18:48 +02001517/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001518 'fullname'. */
1519static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001520get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001521 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001522{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001523 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001524 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 struct st_zip_searchorder *zso;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001526 FILE *fp;
Just van Rossum52e14d62002-12-30 22:08:05 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001529 if (subname == NULL)
1530 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001531
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001532 path = make_filename(self->prefix, subname);
1533 Py_DECREF(subname);
1534 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001536
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001537 fp = safely_reopen_archive(self);
1538 if (fp == NULL) {
1539 Py_DECREF(path);
1540 return NULL;
1541 }
1542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001544 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001545
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001546 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1547 if (fullpath == NULL)
1548 goto exit;
1549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001551 PySys_FormatStderr("# trying %U%c%U\n",
1552 self->archive, (int)SEP, fullpath);
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001553
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001554 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (toc_entry != NULL) {
1556 time_t mtime = 0;
1557 int ispackage = zso->type & IS_PACKAGE;
1558 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001559
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001560 if (isbytecode) {
1561 mtime = get_mtime_of_source(self, fullpath);
1562 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1563 goto exit;
1564 }
1565 }
1566 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 if (p_ispackage != NULL)
1568 *p_ispackage = ispackage;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001569 code = get_code_from_data(self, fp, ispackage,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 isbytecode, mtime,
1571 toc_entry);
1572 if (code == Py_None) {
1573 /* bad magic number or non-matching mtime
1574 in byte code, try next */
1575 Py_DECREF(code);
1576 continue;
1577 }
Victor Stinner08654e12010-10-18 12:09:02 +00001578 if (code != NULL && p_modpath != NULL) {
1579 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1580 Py_INCREF(*p_modpath);
1581 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001582 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001584 else
1585 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001587 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1588exit:
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001589 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001590 Py_DECREF(path);
1591 Py_XDECREF(fullpath);
1592 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001593}
1594
1595
1596/* Module init */
1597
1598PyDoc_STRVAR(zipimport_doc,
1599"zipimport provides support for importing Python modules from Zip archives.\n\
1600\n\
1601This module exports three objects:\n\
1602- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001603- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001604 subclass of ImportError, so it can be caught as ImportError, too.\n\
1605- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1606 info dicts, as used in zipimporter._files.\n\
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001607- _zip_stat_cache: a dict, mapping archive paths to stat_result\n\
1608 info for the .zip the last time anything was imported from it.\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001609\n\
1610It is usually not needed to use the zipimport module explicitly; it is\n\
1611used by the builtin import mechanism for sys.path items that are paths\n\
1612to Zip archives.");
1613
Martin v. Löwis1a214512008-06-11 05:26:20 +00001614static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 PyModuleDef_HEAD_INIT,
1616 "zipimport",
1617 zipimport_doc,
1618 -1,
1619 NULL,
1620 NULL,
1621 NULL,
1622 NULL,
1623 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001624};
1625
Just van Rossum52e14d62002-12-30 22:08:05 +00001626PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001627PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (PyType_Ready(&ZipImporter_Type) < 0)
1632 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 /* Correct directory separator */
1635 zip_searchorder[0].suffix[0] = SEP;
1636 zip_searchorder[1].suffix[0] = SEP;
1637 zip_searchorder[2].suffix[0] = SEP;
1638 if (Py_OptimizeFlag) {
1639 /* Reverse *.pyc and *.pyo */
1640 struct st_zip_searchorder tmp;
1641 tmp = zip_searchorder[0];
1642 zip_searchorder[0] = zip_searchorder[1];
1643 zip_searchorder[1] = tmp;
1644 tmp = zip_searchorder[3];
1645 zip_searchorder[3] = zip_searchorder[4];
1646 zip_searchorder[4] = tmp;
1647 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 mod = PyModule_Create(&zipimportmodule);
1650 if (mod == NULL)
1651 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1654 PyExc_ImportError, NULL);
1655 if (ZipImportError == NULL)
1656 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 Py_INCREF(ZipImportError);
1659 if (PyModule_AddObject(mod, "ZipImportError",
1660 ZipImportError) < 0)
1661 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 Py_INCREF(&ZipImporter_Type);
1664 if (PyModule_AddObject(mod, "zipimporter",
1665 (PyObject *)&ZipImporter_Type) < 0)
1666 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001667
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001668 Py_XDECREF(zip_directory_cache); /* Avoid embedded interpreter leaks. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 zip_directory_cache = PyDict_New();
1670 if (zip_directory_cache == NULL)
1671 return NULL;
1672 Py_INCREF(zip_directory_cache);
1673 if (PyModule_AddObject(mod, "_zip_directory_cache",
1674 zip_directory_cache) < 0)
1675 return NULL;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001676
1677 Py_XDECREF(zip_stat_cache); /* Avoid embedded interpreter leaks. */
1678 zip_stat_cache = PyDict_New();
1679 if (zip_stat_cache == NULL)
1680 return NULL;
1681 Py_INCREF(zip_stat_cache);
1682 if (PyModule_AddObject(mod, "_zip_stat_cache", zip_stat_cache) < 0)
1683 return NULL;
1684
1685 {
1686 /* We cannot import "os" here as that is a .py/.pyc file that could
1687 * live within a zipped up standard library. Import the posix or nt
1688 * builtin that provides the fstat() function we want instead. */
1689 PyObject *os_like_module;
1690 Py_CLEAR(fstat_function); /* Avoid embedded interpreter leaks. */
1691 os_like_module = PyImport_ImportModule("posix");
1692 if (os_like_module == NULL) {
1693 PyErr_Clear();
1694 os_like_module = PyImport_ImportModule("nt");
1695 }
1696 if (os_like_module != NULL) {
1697 fstat_function = PyObject_GetAttrString(os_like_module, "fstat");
1698 Py_DECREF(os_like_module);
1699 }
1700 if (fstat_function == NULL) {
1701 PyErr_Clear(); /* non-fatal, we'll go on without it. */
1702 if (Py_VerboseFlag)
1703 PySys_WriteStderr("# zipimport unable to use os.fstat().\n");
1704 }
1705 }
1706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001708}