blob: 02bdb28dfa2a0feca23447c6b4da1e77ea717125 [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;
964 } else {
965 /* No problem, discard the new stat data. */
966 Py_DECREF(stat_now);
967 }
968 } /* stat succeeded */
969
970 return fp;
971}
972
973/*
974 fopen_rb_and_stat(path, &py_stat) -> FILE *
975
976 Opens path in "rb" mode and populates the Python py_stat stat_result
977 with information about the opened file. *py_stat may not be changed
978 if there is no fstat_function or if fstat_function fails.
979
980 Returns NULL and does nothing to *py_stat if the open failed.
981*/
982static FILE *
983fopen_rb_and_stat(PyObject *path, PyObject **py_stat_p)
984{
985 FILE *fp;
986 assert(py_stat_p != NULL);
987 assert(*py_stat_p == NULL);
988
Gregory P. Smith2e385e22014-01-07 18:34:23 -0800989 fp = _Py_fopen_obj(path, "rb");
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800990 if (fp == NULL) {
991 if (!PyErr_Occurred())
992 PyErr_Format(ZipImportError,
993 "zipimport: can not open file %U", path);
994 return NULL;
995 }
996
997 if (fstat_function) {
998 PyObject *stat_result = PyObject_CallFunction(fstat_function,
999 "i", fileno(fp));
1000 if (stat_result == NULL) {
1001 PyErr_Clear(); /* We can function without it. */
1002 } else {
1003 *py_stat_p = stat_result;
1004 }
1005 }
1006
1007 return fp;
1008}
1009
1010/*
1011 read_directory(fp, archive) -> files dict (new reference)
1012
1013 Given an open Zip archive, build a dict, mapping file names
Just van Rossum52e14d62002-12-30 22:08:05 +00001014 (local to the archive, using SEP as a separator) to toc entries.
1015
1016 A toc_entry is a tuple:
1017
Victor Stinner08654e12010-10-18 12:09:02 +00001018 (__file__, # value to use for __file__, available for all files,
1019 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 compress, # compression kind; 0 for uncompressed
1021 data_size, # size of compressed data on disk
1022 file_size, # size of decompressed data
1023 file_offset, # offset of file header from start of archive
1024 time, # mod time of file (in dos format)
1025 date, # mod data of file (in dos format)
1026 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +00001027 )
Just van Rossum52e14d62002-12-30 22:08:05 +00001028
1029 Directories can be recognized by the trailing SEP in the name,
1030 data_size and file_offset are 0.
1031*/
1032static PyObject *
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001033read_directory(FILE *fp, PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +00001034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyObject *files = NULL;
Victor Stinnerd36c8212010-10-18 12:13:46 +00001036 unsigned short flags;
Gregory P. Smithab320662012-01-30 15:17:33 -08001037 short compress, time, date, name_size;
1038 long crc, data_size, file_size, header_size;
1039 Py_ssize_t file_offset, header_position, header_offset;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001040 long l, count;
1041 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 char name[MAXPATHLEN + 5];
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001043 char dummy[8]; /* Buffer to read unused header values into */
Victor Stinner2460a432010-08-16 17:54:28 +00001044 PyObject *nameobj = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 char *p, endof_central_dir[22];
Gregory P. Smithab320662012-01-30 15:17:33 -08001046 Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +01001047 PyObject *path;
Victor Stinnerd36c8212010-10-18 12:13:46 +00001048 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +00001049 int bootstrap;
Just van Rossum52e14d62002-12-30 22:08:05 +00001050
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001051 assert(fp != NULL);
Jesus Cea09bf7a72012-10-03 02:13:05 +02001052 if (fseek(fp, -22, SEEK_END) == -1) {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001053 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1054 return NULL;
1055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 header_position = ftell(fp);
1057 if (fread(endof_central_dir, 1, 22, fp) != 22) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001058 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 return NULL;
1060 }
1061 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
1062 /* Bad: End of Central Dir signature */
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001063 PyErr_Format(ZipImportError, "not a Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 return NULL;
1065 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 header_size = get_long((unsigned char *)endof_central_dir + 12);
1068 header_offset = get_long((unsigned char *)endof_central_dir + 16);
1069 arc_offset = header_position - header_offset - header_size;
1070 header_offset += arc_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 files = PyDict_New();
1073 if (files == NULL)
1074 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 /* Start of Central Directory */
1077 count = 0;
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001078 if (fseek(fp, header_offset, 0) == -1)
1079 goto file_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 for (;;) {
1081 PyObject *t;
1082 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +00001083
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001084 /* Start of file header */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 l = PyMarshal_ReadLongFromFile(fp);
Victor Stinner73660af2013-10-29 01:43:44 +01001086 if (l == -1 && PyErr_Occurred())
1087 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 if (l != 0x02014B50)
1089 break; /* Bad: Central Dir File Header */
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001090
1091 /* On Windows, calling fseek to skip over the fields we don't use is
1092 slower than reading the data into a dummy buffer because fseek flushes
1093 stdio's internal buffers. See issue #8745. */
1094 if (fread(dummy, 1, 4, fp) != 4) /* Skip unused fields, avoid fseek */
1095 goto file_error;
1096
Victor Stinnerd36c8212010-10-18 12:13:46 +00001097 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 compress = PyMarshal_ReadShortFromFile(fp);
1099 time = PyMarshal_ReadShortFromFile(fp);
1100 date = PyMarshal_ReadShortFromFile(fp);
1101 crc = PyMarshal_ReadLongFromFile(fp);
1102 data_size = PyMarshal_ReadLongFromFile(fp);
1103 file_size = PyMarshal_ReadLongFromFile(fp);
1104 name_size = PyMarshal_ReadShortFromFile(fp);
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001105 header_size = name_size +
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 PyMarshal_ReadShortFromFile(fp) +
1107 PyMarshal_ReadShortFromFile(fp);
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001108 if (fread(dummy, 1, 8, fp) != 8) /* Skip unused fields, avoid fseek */
1109 goto file_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
Victor Stinner73660af2013-10-29 01:43:44 +01001111 if (PyErr_Occurred())
1112 goto error;
1113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (name_size > MAXPATHLEN)
1115 name_size = MAXPATHLEN;
Just van Rossum52e14d62002-12-30 22:08:05 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 p = name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001118 for (i = 0; i < (Py_ssize_t)name_size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 *p = (char)getc(fp);
1120 if (*p == '/')
1121 *p = SEP;
1122 p++;
1123 }
1124 *p = 0; /* Add terminating null byte */
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001125 for (; i < header_size; i++) /* Skip the rest of the header */
1126 if(getc(fp) == EOF) /* Avoid fseek */
1127 goto file_error;
Just van Rossum52e14d62002-12-30 22:08:05 +00001128
Victor Stinner4ee65a92011-01-22 10:30:29 +00001129 bootstrap = 0;
Victor Stinnerd36c8212010-10-18 12:13:46 +00001130 if (flags & 0x0800)
1131 charset = "utf-8";
Victor Stinner4ee65a92011-01-22 10:30:29 +00001132 else if (!PyThreadState_GET()->interp->codecs_initialized) {
1133 /* During bootstrap, we may need to load the encodings
1134 package from a ZIP file. But the cp437 encoding is implemented
1135 in Python in the encodings package.
1136
1137 Break out of this dependency by assuming that the path to
1138 the encodings module is ASCII-only. */
1139 charset = "ascii";
1140 bootstrap = 1;
1141 }
Victor Stinnerd36c8212010-10-18 12:13:46 +00001142 else
1143 charset = "cp437";
1144 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner4ee65a92011-01-22 10:30:29 +00001145 if (nameobj == NULL) {
1146 if (bootstrap)
1147 PyErr_Format(PyExc_NotImplementedError,
1148 "bootstrap issue: python%i%i.zip contains non-ASCII "
1149 "filenames without the unicode flag",
1150 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Victor Stinner2460a432010-08-16 17:54:28 +00001151 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +00001152 }
Stefan Krah000fde92012-08-20 14:14:49 +02001153 if (PyUnicode_READY(nameobj) == -1)
1154 goto error;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +01001155 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
1156 if (path == NULL)
Victor Stinner2460a432010-08-16 17:54:28 +00001157 goto error;
Gregory P. Smithcc6abd52012-01-30 15:55:29 -08001158 t = Py_BuildValue("Nhllnhhl", path, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 file_size, file_offset, time, date, crc);
1160 if (t == NULL)
1161 goto error;
Victor Stinner2460a432010-08-16 17:54:28 +00001162 err = PyDict_SetItem(files, nameobj, t);
1163 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 Py_DECREF(t);
1165 if (err != 0)
1166 goto error;
1167 count++;
1168 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001170 PySys_FormatStderr("# zipimport: found %ld names in %R\n",
1171 count, archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 return files;
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001173file_error:
Jesus Cea09bf7a72012-10-03 02:13:05 +02001174 Py_XDECREF(files);
1175 Py_XDECREF(nameobj);
1176 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1177 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001178error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +00001180 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001182}
1183
1184/* Return the zlib.decompress function object, or NULL if zlib couldn't
1185 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +02001186 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001187static PyObject *
1188get_decompress_func(void)
1189{
Victor Stinner4925cde2011-05-20 00:16:09 +02001190 static int importing_zlib = 0;
1191 PyObject *zlib;
1192 PyObject *decompress;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001193 _Py_IDENTIFIER(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001194
Victor Stinner4925cde2011-05-20 00:16:09 +02001195 if (importing_zlib != 0)
1196 /* Someone has a zlib.py[co] in their Zip file;
1197 let's avoid a stack overflow. */
1198 return NULL;
1199 importing_zlib = 1;
1200 zlib = PyImport_ImportModuleNoBlock("zlib");
1201 importing_zlib = 0;
1202 if (zlib != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001203 decompress = _PyObject_GetAttrId(zlib,
1204 &PyId_decompress);
Victor Stinner4925cde2011-05-20 00:16:09 +02001205 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 }
Victor Stinner4925cde2011-05-20 00:16:09 +02001207 else {
1208 PyErr_Clear();
1209 decompress = NULL;
1210 }
1211 if (Py_VerboseFlag)
1212 PySys_WriteStderr("# zipimport: zlib %s\n",
1213 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +00001215}
1216
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001217/* Given a FILE* to a Zip file and a toc_entry, return the (uncompressed)
Just van Rossum52e14d62002-12-30 22:08:05 +00001218 data as a new reference. */
1219static PyObject *
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001220get_data(FILE *fp, PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 PyObject *raw_data, *data = NULL, *decompress;
1223 char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 int err;
1225 Py_ssize_t bytes_read = 0;
1226 long l;
Victor Stinner60fe8d92010-08-16 23:48:11 +00001227 PyObject *datapath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 long compress, data_size, file_size, file_offset, bytes_size;
1229 long time, date, crc;
Just van Rossum52e14d62002-12-30 22:08:05 +00001230
Victor Stinner60fe8d92010-08-16 23:48:11 +00001231 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 &data_size, &file_size, &file_offset, &time,
1233 &date, &crc)) {
1234 return NULL;
1235 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* Check to make sure the local file header is correct */
Jesus Cea09bf7a72012-10-03 02:13:05 +02001238 if (fseek(fp, file_offset, 0) == -1) {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001239 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1240 return NULL;
1241 }
1242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 l = PyMarshal_ReadLongFromFile(fp);
1244 if (l != 0x04034B50) {
1245 /* Bad: Local File Header */
Victor Stinner73660af2013-10-29 01:43:44 +01001246 if (!PyErr_Occurred())
1247 PyErr_Format(ZipImportError,
1248 "bad local file header in %U",
1249 archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 return NULL;
1251 }
Jesus Cea09bf7a72012-10-03 02:13:05 +02001252 if (fseek(fp, file_offset + 26, 0) == -1) {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001253 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1254 return NULL;
1255 }
1256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 l = 30 + PyMarshal_ReadShortFromFile(fp) +
1258 PyMarshal_ReadShortFromFile(fp); /* local header size */
Victor Stinner73660af2013-10-29 01:43:44 +01001259 if (PyErr_Occurred()) {
Victor Stinner73660af2013-10-29 01:43:44 +01001260 return NULL;
1261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 file_offset += l; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 bytes_size = compress == 0 ? data_size : data_size + 1;
1265 if (bytes_size == 0)
1266 bytes_size++;
1267 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Just van Rossum52e14d62002-12-30 22:08:05 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (raw_data == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 return NULL;
1271 }
1272 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 err = fseek(fp, file_offset, 0);
Jesus Cea09bf7a72012-10-03 02:13:05 +02001275 if (err == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 bytes_read = fread(buf, 1, data_size, fp);
Jesus Cea09bf7a72012-10-03 02:13:05 +02001277 } else {
Jesus Cea09bf7a72012-10-03 02:13:05 +02001278 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1279 return NULL;
1280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (err || bytes_read != data_size) {
1282 PyErr_SetString(PyExc_IOError,
1283 "zipimport: can't read data");
1284 Py_DECREF(raw_data);
1285 return NULL;
1286 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (compress != 0) {
1289 buf[data_size] = 'Z'; /* saw this in zipfile.py */
1290 data_size++;
1291 }
1292 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (compress == 0) { /* data is not compressed */
1295 data = PyBytes_FromStringAndSize(buf, data_size);
1296 Py_DECREF(raw_data);
1297 return data;
1298 }
1299
1300 /* Decompress with zlib */
1301 decompress = get_decompress_func();
1302 if (decompress == NULL) {
1303 PyErr_SetString(ZipImportError,
1304 "can't decompress data; "
1305 "zlib not available");
1306 goto error;
1307 }
1308 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +02001309 Py_DECREF(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001310error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 Py_DECREF(raw_data);
1312 return data;
Just van Rossum52e14d62002-12-30 22:08:05 +00001313}
1314
1315/* Lenient date/time comparison function. The precision of the mtime
1316 in the archive is lower than the mtime stored in a .pyc: we
1317 must allow a difference of at most one second. */
1318static int
1319eq_mtime(time_t t1, time_t t2)
1320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 time_t d = t1 - t2;
1322 if (d < 0)
1323 d = -d;
1324 /* dostime only stores even seconds, so be lenient */
1325 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001326}
1327
1328/* Given the contents of a .py[co] file in a buffer, unmarshal the data
1329 and return the code object. Return None if it the magic word doesn't
1330 match (we do this instead of raising an exception as we fall back
1331 to .py if available and we don't want to mask other errors).
1332 Returns a new reference. */
1333static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001334unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 PyObject *code;
1337 char *buf = PyBytes_AsString(data);
1338 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (size <= 9) {
1341 PyErr_SetString(ZipImportError,
1342 "bad pyc data");
1343 return NULL;
1344 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
1347 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001348 PySys_FormatStderr("# %R has bad magic\n",
1349 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 Py_INCREF(Py_None);
1351 return Py_None; /* signal caller to try alternative */
1352 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
1355 mtime)) {
1356 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001357 PySys_FormatStderr("# %R has bad mtime\n",
1358 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 Py_INCREF(Py_None);
1360 return Py_None; /* signal caller to try alternative */
1361 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001362
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001363 /* XXX the pyc's size field is ignored; timestamp collisions are probably
1364 unimportant with zip files. */
1365 code = PyMarshal_ReadObjectFromString(buf + 12, size - 12);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (code == NULL)
1367 return NULL;
1368 if (!PyCode_Check(code)) {
1369 Py_DECREF(code);
1370 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001371 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 pathname);
1373 return NULL;
1374 }
1375 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001376}
1377
1378/* Replace any occurances of "\r\n?" in the input string with "\n".
1379 This converts DOS and Mac line endings to Unix line endings.
1380 Also append a trailing "\n" to be compatible with
1381 PyParser_SimpleParseFile(). Returns a new reference. */
1382static PyObject *
1383normalize_line_endings(PyObject *source)
1384{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001385 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 PyObject *fixed_source;
1387 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001388
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001389 p = PyBytes_AsString(source);
1390 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 return PyBytes_FromStringAndSize("\n\0", 2);
1392 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 /* one char extra for trailing \n and one for terminating \0 */
1395 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1396 if (buf == NULL) {
1397 PyErr_SetString(PyExc_MemoryError,
1398 "zipimport: no memory to allocate "
1399 "source buffer");
1400 return NULL;
1401 }
1402 /* replace "\r\n?" by "\n" */
1403 for (q = buf; *p != '\0'; p++) {
1404 if (*p == '\r') {
1405 *q++ = '\n';
1406 if (*(p + 1) == '\n')
1407 p++;
1408 }
1409 else
1410 *q++ = *p;
1411 len++;
1412 }
1413 *q++ = '\n'; /* add trailing \n */
1414 *q = '\0';
1415 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1416 PyMem_Free(buf);
1417 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001418}
1419
1420/* Given a string buffer containing Python source code, compile it
Brett Cannon83358c92013-06-20 21:30:32 -04001421 and return a code object as a new reference. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001422static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001423compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001424{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001425 PyObject *code, *fixed_source, *pathbytes;
Just van Rossum52e14d62002-12-30 22:08:05 +00001426
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001427 pathbytes = PyUnicode_EncodeFSDefault(pathname);
1428 if (pathbytes == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001430
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001431 fixed_source = normalize_line_endings(source);
1432 if (fixed_source == NULL) {
1433 Py_DECREF(pathbytes);
1434 return NULL;
1435 }
1436
1437 code = Py_CompileString(PyBytes_AsString(fixed_source),
1438 PyBytes_AsString(pathbytes),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 Py_file_input);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001440 Py_DECREF(pathbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 Py_DECREF(fixed_source);
1442 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001443}
1444
1445/* Convert the date/time values found in the Zip archive to a value
1446 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001447static time_t
1448parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 stm.tm_sec = (dostime & 0x1f) * 2;
1455 stm.tm_min = (dostime >> 5) & 0x3f;
1456 stm.tm_hour = (dostime >> 11) & 0x1f;
1457 stm.tm_mday = dosdate & 0x1f;
1458 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1459 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1460 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001463}
1464
1465/* Given a path to a .pyc or .pyo file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001466 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001467 is available. */
1468static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001469get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001470{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001471 PyObject *toc_entry, *stripped;
1472 time_t mtime;
1473
1474 /* strip 'c' or 'o' from *.py[co] */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001475 if (PyUnicode_READY(path) == -1)
1476 return (time_t)-1;
1477 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
1478 PyUnicode_DATA(path),
1479 PyUnicode_GET_LENGTH(path) - 1);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001480 if (stripped == NULL)
1481 return (time_t)-1;
1482
1483 toc_entry = PyDict_GetItem(self->files, stripped);
1484 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1486 PyTuple_Size(toc_entry) == 8) {
1487 /* fetch the time stamp of the .py file for comparison
1488 with an embedded pyc time stamp */
1489 int time, date;
1490 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1491 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1492 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001493 } else
1494 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001496}
1497
1498/* Return the code object for the module named by 'fullname' from the
1499 Zip archive as a new reference. */
1500static PyObject *
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001501get_code_from_data(ZipImporter *self, FILE *fp, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001503{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001504 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001505
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001506 data = get_data(fp, self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if (data == NULL)
1508 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001509
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001510 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001511 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001512 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001513 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001514 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 Py_DECREF(data);
1516 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001517}
1518
Ezio Melotti42da6632011-03-15 05:18:48 +02001519/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001520 'fullname'. */
1521static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001522get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001523 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001524{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001525 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001526 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 struct st_zip_searchorder *zso;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001528 FILE *fp;
Just van Rossum52e14d62002-12-30 22:08:05 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001531 if (subname == NULL)
1532 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001533
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001534 path = make_filename(self->prefix, subname);
1535 Py_DECREF(subname);
1536 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001538
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001539 fp = safely_reopen_archive(self);
1540 if (fp == NULL) {
1541 Py_DECREF(path);
1542 return NULL;
1543 }
1544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001546 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001547
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001548 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1549 if (fullpath == NULL)
1550 goto exit;
1551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001553 PySys_FormatStderr("# trying %U%c%U\n",
1554 self->archive, (int)SEP, fullpath);
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001555
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001556 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (toc_entry != NULL) {
1558 time_t mtime = 0;
1559 int ispackage = zso->type & IS_PACKAGE;
1560 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001561
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001562 if (isbytecode) {
1563 mtime = get_mtime_of_source(self, fullpath);
1564 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1565 goto exit;
1566 }
1567 }
1568 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if (p_ispackage != NULL)
1570 *p_ispackage = ispackage;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001571 code = get_code_from_data(self, fp, ispackage,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 isbytecode, mtime,
1573 toc_entry);
1574 if (code == Py_None) {
1575 /* bad magic number or non-matching mtime
1576 in byte code, try next */
1577 Py_DECREF(code);
1578 continue;
1579 }
Victor Stinner08654e12010-10-18 12:09:02 +00001580 if (code != NULL && p_modpath != NULL) {
1581 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1582 Py_INCREF(*p_modpath);
1583 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001584 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001586 else
1587 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001589 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1590exit:
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001591 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001592 Py_DECREF(path);
1593 Py_XDECREF(fullpath);
1594 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001595}
1596
1597
1598/* Module init */
1599
1600PyDoc_STRVAR(zipimport_doc,
1601"zipimport provides support for importing Python modules from Zip archives.\n\
1602\n\
1603This module exports three objects:\n\
1604- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001605- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001606 subclass of ImportError, so it can be caught as ImportError, too.\n\
1607- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1608 info dicts, as used in zipimporter._files.\n\
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001609- _zip_stat_cache: a dict, mapping archive paths to stat_result\n\
1610 info for the .zip the last time anything was imported from it.\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001611\n\
1612It is usually not needed to use the zipimport module explicitly; it is\n\
1613used by the builtin import mechanism for sys.path items that are paths\n\
1614to Zip archives.");
1615
Martin v. Löwis1a214512008-06-11 05:26:20 +00001616static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 PyModuleDef_HEAD_INIT,
1618 "zipimport",
1619 zipimport_doc,
1620 -1,
1621 NULL,
1622 NULL,
1623 NULL,
1624 NULL,
1625 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001626};
1627
Just van Rossum52e14d62002-12-30 22:08:05 +00001628PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001629PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (PyType_Ready(&ZipImporter_Type) < 0)
1634 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 /* Correct directory separator */
1637 zip_searchorder[0].suffix[0] = SEP;
1638 zip_searchorder[1].suffix[0] = SEP;
1639 zip_searchorder[2].suffix[0] = SEP;
1640 if (Py_OptimizeFlag) {
1641 /* Reverse *.pyc and *.pyo */
1642 struct st_zip_searchorder tmp;
1643 tmp = zip_searchorder[0];
1644 zip_searchorder[0] = zip_searchorder[1];
1645 zip_searchorder[1] = tmp;
1646 tmp = zip_searchorder[3];
1647 zip_searchorder[3] = zip_searchorder[4];
1648 zip_searchorder[4] = tmp;
1649 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 mod = PyModule_Create(&zipimportmodule);
1652 if (mod == NULL)
1653 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1656 PyExc_ImportError, NULL);
1657 if (ZipImportError == NULL)
1658 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 Py_INCREF(ZipImportError);
1661 if (PyModule_AddObject(mod, "ZipImportError",
1662 ZipImportError) < 0)
1663 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 Py_INCREF(&ZipImporter_Type);
1666 if (PyModule_AddObject(mod, "zipimporter",
1667 (PyObject *)&ZipImporter_Type) < 0)
1668 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001669
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001670 Py_XDECREF(zip_directory_cache); /* Avoid embedded interpreter leaks. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 zip_directory_cache = PyDict_New();
1672 if (zip_directory_cache == NULL)
1673 return NULL;
1674 Py_INCREF(zip_directory_cache);
1675 if (PyModule_AddObject(mod, "_zip_directory_cache",
1676 zip_directory_cache) < 0)
1677 return NULL;
Gregory P. Smith2bcbc142014-01-07 18:30:07 -08001678
1679 Py_XDECREF(zip_stat_cache); /* Avoid embedded interpreter leaks. */
1680 zip_stat_cache = PyDict_New();
1681 if (zip_stat_cache == NULL)
1682 return NULL;
1683 Py_INCREF(zip_stat_cache);
1684 if (PyModule_AddObject(mod, "_zip_stat_cache", zip_stat_cache) < 0)
1685 return NULL;
1686
1687 {
1688 /* We cannot import "os" here as that is a .py/.pyc file that could
1689 * live within a zipped up standard library. Import the posix or nt
1690 * builtin that provides the fstat() function we want instead. */
1691 PyObject *os_like_module;
1692 Py_CLEAR(fstat_function); /* Avoid embedded interpreter leaks. */
1693 os_like_module = PyImport_ImportModule("posix");
1694 if (os_like_module == NULL) {
1695 PyErr_Clear();
1696 os_like_module = PyImport_ImportModule("nt");
1697 }
1698 if (os_like_module != NULL) {
1699 fstat_function = PyObject_GetAttrString(os_like_module, "fstat");
1700 Py_DECREF(os_like_module);
1701 }
1702 if (fstat_function == NULL) {
1703 PyErr_Clear(); /* non-fatal, we'll go on without it. */
1704 if (Py_VerboseFlag)
1705 PySys_WriteStderr("# zipimport unable to use os.fstat().\n");
1706 }
1707 }
1708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001710}