blob: 1d0e0ba913709a95654cb629c6841b3f418a3072 [file] [log] [blame]
Just van Rossum52e14d62002-12-30 22:08:05 +00001#include "Python.h"
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08002#include "internal/import.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06003#include "internal/pystate.h"
Just van Rossum52e14d62002-12-30 22:08:05 +00004#include "structmember.h"
5#include "osdefs.h"
6#include "marshal.h"
Just van Rossum52e14d62002-12-30 22:08:05 +00007#include <time.h>
8
9
10#define IS_SOURCE 0x0
11#define IS_BYTECODE 0x1
12#define IS_PACKAGE 0x2
13
14struct st_zip_searchorder {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 char suffix[14];
16 int type;
Just van Rossum52e14d62002-12-30 22:08:05 +000017};
18
Victor Stinner651f9f72013-11-12 21:44:18 +010019#ifdef ALTSEP
20_Py_IDENTIFIER(replace);
21#endif
22
Just van Rossum52e14d62002-12-30 22:08:05 +000023/* zip_searchorder defines how we search for a module in the Zip
24 archive: we first search for a package __init__, then for
Brett Cannonf299abd2015-04-13 14:21:02 -040025 non-package .pyc, and .py entries. The .pyc entries
Just van Rossum52e14d62002-12-30 22:08:05 +000026 are swapped by initzipimport() if we run in optimized mode. Also,
27 '/' is replaced by SEP there. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +000028static struct st_zip_searchorder zip_searchorder[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 {"/__init__.py", IS_PACKAGE | IS_SOURCE},
31 {".pyc", IS_BYTECODE},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 {".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;
52
53/* forward decls */
Benjamin Peterson34c15402014-02-16 14:17:28 -050054static PyObject *read_directory(PyObject *archive);
55static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
Victor Stinnerf6b563a2011-03-14 20:46:50 -040056static PyObject *get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +000057 int *p_ispackage, PyObject **p_modpath);
Just van Rossum52e14d62002-12-30 22:08:05 +000058
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040059static PyTypeObject ZipImporter_Type;
Just van Rossum52e14d62002-12-30 22:08:05 +000060
61#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
62
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040063/*[clinic input]
64module zipimport
65class zipimport.zipimporter "ZipImporter *" "&ZipImporter_Type"
66[clinic start generated code]*/
67/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9db8b61557d911e7]*/
68#include "clinic/zipimport.c.h"
69
Just van Rossum52e14d62002-12-30 22:08:05 +000070
71/* zipimporter.__init__
72 Split the "subdirectory" from the Zip archive path, lookup a matching
73 entry in sys.path_importer_cache, fetch the file directory from there
74 if found, or else read it from the archive. */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040075
76/*[clinic input]
77zipimport.zipimporter.__init__
78
79 archivepath as path: object(converter="PyUnicode_FSDecoder")
80 A path-like object to a zipfile, or to a specific path inside
81 a zipfile.
82 /
83
84Create a new zipimporter instance.
85
86'archivepath' must be a path-like object to a zipfile, or to a specific path
87inside a zipfile. For example, it can be '/tmp/myimport.zip', or
88'/tmp/myimport.zip/mydirectory', if mydirectory is a valid directory inside
89the archive.
90
91'ZipImportError' is raised if 'archivepath' doesn't point to a valid Zip
92archive.
93
94The 'archive' attribute of the zipimporter object contains the name of the
95zipfile targeted.
96
97[clinic start generated code]*/
98
Just van Rossum52e14d62002-12-30 22:08:05 +000099static int
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400100zipimport_zipimporter___init___impl(ZipImporter *self, PyObject *path)
101/*[clinic end generated code: output=141558fefdb46dc8 input=92b9ebeed1f6a704]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000102{
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400103 PyObject *files, *tmp;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100104 PyObject *filename = NULL;
105 Py_ssize_t len, flen;
Just van Rossum52e14d62002-12-30 22:08:05 +0000106
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100107 if (PyUnicode_READY(path) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200108 return -1;
109
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100110 len = PyUnicode_GET_LENGTH(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 if (len == 0) {
112 PyErr_SetString(ZipImportError, "archive path is empty");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000113 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000115
116#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +0100117 tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100118 if (!tmp)
119 goto error;
120 Py_DECREF(path);
121 path = tmp;
Just van Rossum52e14d62002-12-30 22:08:05 +0000122#endif
123
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100124 filename = path;
125 Py_INCREF(filename);
126 flen = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 for (;;) {
128 struct stat statbuf;
129 int rv;
Just van Rossum52e14d62002-12-30 22:08:05 +0000130
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100131 rv = _Py_stat(filename, &statbuf);
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100132 if (rv == -2)
133 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 if (rv == 0) {
135 /* it exists */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100136 if (!S_ISREG(statbuf.st_mode))
137 /* it's a not file */
138 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 break;
140 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100141 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 /* back up one path element */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100143 flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);
144 if (flen == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 break;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100146 filename = PyUnicode_Substring(path, 0, flen);
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100147 if (filename == NULL)
148 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100150 if (filename == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyErr_SetString(ZipImportError, "not a Zip file");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000152 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000154
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100155 if (PyUnicode_READY(filename) < 0)
156 goto error;
157
158 files = PyDict_GetItem(zip_directory_cache, filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000159 if (files == NULL) {
Benjamin Peterson34c15402014-02-16 14:17:28 -0500160 files = read_directory(filename);
161 if (files == NULL)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000162 goto error;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500163 if (PyDict_SetItem(zip_directory_cache, filename, files) != 0)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000164 goto error;
165 }
166 else
167 Py_INCREF(files);
Oren Milmanc0cabc22017-10-09 18:06:19 +0300168 Py_XSETREF(self->files, files);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000169
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100170 /* Transfer reference */
Oren Milmanc0cabc22017-10-09 18:06:19 +0300171 Py_XSETREF(self->archive, filename);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100172 filename = NULL;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000173
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100174 /* Check if there is a prefix directory following the filename. */
175 if (flen != len) {
176 tmp = PyUnicode_Substring(path, flen+1,
177 PyUnicode_GET_LENGTH(path));
178 if (tmp == NULL)
179 goto error;
Oren Milmanc0cabc22017-10-09 18:06:19 +0300180 Py_XSETREF(self->prefix, tmp);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100181 if (PyUnicode_READ_CHAR(path, len-1) != SEP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 /* add trailing SEP */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100183 tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
184 if (tmp == NULL)
185 goto error;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300186 Py_SETREF(self->prefix, tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 }
188 }
Oren Milmanc0cabc22017-10-09 18:06:19 +0300189 else {
190 Py_XSETREF(self->prefix, PyUnicode_New(0, 0));
191 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100192 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 return 0;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000194
195error:
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100196 Py_DECREF(path);
197 Py_XDECREF(filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000198 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000199}
200
201/* GC support. */
202static int
203zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 ZipImporter *self = (ZipImporter *)obj;
206 Py_VISIT(self->files);
207 return 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000208}
209
210static void
211zipimporter_dealloc(ZipImporter *self)
212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 PyObject_GC_UnTrack(self);
214 Py_XDECREF(self->archive);
215 Py_XDECREF(self->prefix);
216 Py_XDECREF(self->files);
217 Py_TYPE(self)->tp_free((PyObject *)self);
Just van Rossum52e14d62002-12-30 22:08:05 +0000218}
219
220static PyObject *
221zipimporter_repr(ZipImporter *self)
222{
Victor Stinner028dd972010-08-17 00:04:48 +0000223 if (self->archive == NULL)
224 return PyUnicode_FromString("<zipimporter object \"???\">");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200225 else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0)
Victor Stinner07298a12010-10-18 22:45:54 +0000226 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000227 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 else
Victor Stinner07298a12010-10-18 22:45:54 +0000229 return PyUnicode_FromFormat("<zipimporter object \"%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000230 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000231}
232
233/* return fullname.split(".")[-1] */
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400234static PyObject *
235get_subname(PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000236{
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100237 Py_ssize_t len, dot;
238 if (PyUnicode_READY(fullname) < 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200239 return NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100240 len = PyUnicode_GET_LENGTH(fullname);
241 dot = PyUnicode_FindChar(fullname, '.', 0, len, -1);
242 if (dot == -1) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400243 Py_INCREF(fullname);
244 return fullname;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100245 } else
246 return PyUnicode_Substring(fullname, dot+1, len);
Just van Rossum52e14d62002-12-30 22:08:05 +0000247}
248
249/* Given a (sub)modulename, write the potential file path in the
250 archive (without extension) to the path buffer. Return the
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400251 length of the resulting string.
252
253 return self.prefix + name.replace('.', os.sep) */
254static PyObject*
255make_filename(PyObject *prefix, PyObject *name)
Just van Rossum52e14d62002-12-30 22:08:05 +0000256{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400257 PyObject *pathobj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200258 Py_UCS4 *p, *buf;
259 Py_ssize_t len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000260
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200261 len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200262 p = buf = PyMem_New(Py_UCS4, len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200263 if (buf == NULL) {
264 PyErr_NoMemory();
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400265 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200266 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000267
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200268 if (!PyUnicode_AsUCS4(prefix, p, len, 0)) {
269 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200270 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200271 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200272 p += PyUnicode_GET_LENGTH(prefix);
273 len -= PyUnicode_GET_LENGTH(prefix);
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200274 if (!PyUnicode_AsUCS4(name, p, len, 1)) {
275 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200276 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200277 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400278 for (; *p; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 if (*p == '.')
280 *p = SEP;
281 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200282 pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
283 buf, p-buf);
284 PyMem_Free(buf);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400285 return pathobj;
Just van Rossum52e14d62002-12-30 22:08:05 +0000286}
287
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000288enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 MI_ERROR,
290 MI_NOT_FOUND,
291 MI_MODULE,
292 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000293};
294
Eric V. Smith984b11f2012-05-24 20:21:04 -0400295/* Does this path represent a directory?
296 on error, return < 0
297 if not a dir, return 0
298 if a dir, return 1
299*/
300static int
301check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path)
302{
303 PyObject *dirpath;
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700304 int res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400305
306 /* See if this is a "directory". If so, it's eligible to be part
307 of a namespace package. We test by seeing if the name, with an
308 appended path separator, exists. */
309 dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
310 if (dirpath == NULL)
311 return -1;
312 /* If dirpath is present in self->files, we have a directory. */
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700313 res = PyDict_Contains(self->files, dirpath);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400314 Py_DECREF(dirpath);
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700315 return res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400316}
317
Just van Rossum52e14d62002-12-30 22:08:05 +0000318/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000319static enum zi_module_info
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400320get_module_info(ZipImporter *self, PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000321{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400322 PyObject *subname;
323 PyObject *path, *fullpath, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000325
Oren Milmandb60a5b2017-10-20 23:42:35 +0300326 if (self->prefix == NULL) {
327 PyErr_SetString(PyExc_ValueError,
328 "zipimporter.__init__() wasn't called");
329 return MI_ERROR;
330 }
331
Victor Stinner965a8a12010-10-18 21:44:33 +0000332 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400333 if (subname == NULL)
334 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000335
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400336 path = make_filename(self->prefix, subname);
337 Py_DECREF(subname);
338 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400342 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
343 if (fullpath == NULL) {
344 Py_DECREF(path);
345 return MI_ERROR;
346 }
347 item = PyDict_GetItem(self->files, fullpath);
348 Py_DECREF(fullpath);
349 if (item != NULL) {
350 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (zso->type & IS_PACKAGE)
352 return MI_PACKAGE;
353 else
354 return MI_MODULE;
355 }
356 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400357 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000359}
360
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700361typedef enum {
Brett Cannon56aae8f2016-01-15 11:22:19 -0800362 FL_ERROR = -1, /* error */
363 FL_NOT_FOUND, /* no loader or namespace portions found */
364 FL_MODULE_FOUND, /* module/package found */
365 FL_NS_FOUND /* namespace portion found: */
366 /* *namespace_portion will point to the name */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700367} find_loader_result;
368
Brett Cannon56aae8f2016-01-15 11:22:19 -0800369/* The guts of "find_loader" and "find_module".
Eric V. Smith984b11f2012-05-24 20:21:04 -0400370*/
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700371static find_loader_result
Eric V. Smith984b11f2012-05-24 20:21:04 -0400372find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
373{
374 enum zi_module_info mi;
375
376 *namespace_portion = NULL;
377
378 mi = get_module_info(self, fullname);
379 if (mi == MI_ERROR)
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700380 return FL_ERROR;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400381 if (mi == MI_NOT_FOUND) {
382 /* Not a module or regular package. See if this is a directory, and
383 therefore possibly a portion of a namespace package. */
Brett Cannon56aae8f2016-01-15 11:22:19 -0800384 find_loader_result result = FL_NOT_FOUND;
385 PyObject *subname;
386 int is_dir;
387
388 /* We're only interested in the last path component of fullname;
389 earlier components are recorded in self->prefix. */
390 subname = get_subname(fullname);
391 if (subname == NULL) {
392 return FL_ERROR;
393 }
394
395 is_dir = check_is_directory(self, self->prefix, subname);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400396 if (is_dir < 0)
Brett Cannon56aae8f2016-01-15 11:22:19 -0800397 result = FL_ERROR;
398 else if (is_dir) {
Eric V. Smith984b11f2012-05-24 20:21:04 -0400399 /* This is possibly a portion of a namespace
400 package. Return the string representing its path,
401 without a trailing separator. */
402 *namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
403 self->archive, SEP,
Brett Cannon56aae8f2016-01-15 11:22:19 -0800404 self->prefix, subname);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400405 if (*namespace_portion == NULL)
Brett Cannon56aae8f2016-01-15 11:22:19 -0800406 result = FL_ERROR;
407 else
408 result = FL_NS_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400409 }
Brett Cannon56aae8f2016-01-15 11:22:19 -0800410 Py_DECREF(subname);
411 return result;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400412 }
413 /* This is a module or package. */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700414 return FL_MODULE_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400415}
416
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400417/*[clinic input]
418zipimport.zipimporter.find_module
Eric V. Smith984b11f2012-05-24 20:21:04 -0400419
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400420 fullname: unicode
421 path: object = None
422 /
423
424Search for a module specified by 'fullname'.
425
426'fullname' must be the fully qualified (dotted) module name. It returns the
427zipimporter instance itself if the module was found, or None if it wasn't.
428The optional 'path' argument is ignored -- it's there for compatibility
429with the importer protocol.
430
431[clinic start generated code]*/
432
Just van Rossum52e14d62002-12-30 22:08:05 +0000433static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400434zipimport_zipimporter_find_module_impl(ZipImporter *self, PyObject *fullname,
435 PyObject *path)
436/*[clinic end generated code: output=506087f609466dc7 input=e3528520e075063f]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000437{
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700438 PyObject *namespace_portion = NULL;
439 PyObject *result = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000440
Eric V. Smith984b11f2012-05-24 20:21:04 -0400441 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700442 case FL_ERROR:
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700443 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700444 case FL_NS_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700445 /* A namespace portion is not allowed via find_module, so return None. */
Eric V. Smith984b11f2012-05-24 20:21:04 -0400446 Py_DECREF(namespace_portion);
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700447 /* FALL THROUGH */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700448 case FL_NOT_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700449 result = Py_None;
450 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700451 case FL_MODULE_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700452 result = (PyObject *)self;
453 break;
Brett Cannon56aae8f2016-01-15 11:22:19 -0800454 default:
455 PyErr_BadInternalCall();
456 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 }
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700458 Py_INCREF(result);
Benjamin Peterson2d12e142012-05-25 00:19:40 -0700459 return result;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400460}
461
462
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400463/*[clinic input]
464zipimport.zipimporter.find_loader
465
466 fullname: unicode
467 path: object = None
468 /
469
470Search for a module specified by 'fullname'.
471
472'fullname' must be the fully qualified (dotted) module name. It returns the
473zipimporter instance itself if the module was found, a string containing the
474full path name if it's possibly a portion of a namespace package,
475or None otherwise. The optional 'path' argument is ignored -- it's
476there for compatibility with the importer protocol.
477
478[clinic start generated code]*/
479
Eric V. Smith984b11f2012-05-24 20:21:04 -0400480static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400481zipimport_zipimporter_find_loader_impl(ZipImporter *self, PyObject *fullname,
482 PyObject *path)
483/*[clinic end generated code: output=601599a43bc0f49a input=dc73f275b0d5be23]*/
Eric V. Smith984b11f2012-05-24 20:21:04 -0400484{
Eric V. Smith984b11f2012-05-24 20:21:04 -0400485 PyObject *result = NULL;
486 PyObject *namespace_portion = NULL;
487
Eric V. Smith984b11f2012-05-24 20:21:04 -0400488 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700489 case FL_ERROR:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700490 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700491 case FL_NOT_FOUND: /* Not found, return (None, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700492 result = Py_BuildValue("O[]", Py_None);
493 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700494 case FL_MODULE_FOUND: /* Return (self, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700495 result = Py_BuildValue("O[]", self);
496 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700497 case FL_NS_FOUND: /* Return (None, [namespace_portion]) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700498 result = Py_BuildValue("O[O]", Py_None, namespace_portion);
Benjamin Peterson209e04c2012-05-24 22:35:39 -0700499 Py_DECREF(namespace_portion);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400500 return result;
Brett Cannon56aae8f2016-01-15 11:22:19 -0800501 default:
502 PyErr_BadInternalCall();
503 return NULL;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400504 }
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700505 return result;
Just van Rossum52e14d62002-12-30 22:08:05 +0000506}
507
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400508/*[clinic input]
509zipimport.zipimporter.load_module
510
511 fullname: unicode
512 /
513
514Load the module specified by 'fullname'.
515
516'fullname' must be the fully qualified (dotted) module name. It returns the
517imported module, or raises ZipImportError if it wasn't found.
518
519[clinic start generated code]*/
520
Just van Rossum52e14d62002-12-30 22:08:05 +0000521static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400522zipimport_zipimporter_load_module_impl(ZipImporter *self, PyObject *fullname)
523/*[clinic end generated code: output=7303cebf88d47953 input=c236e2e8621f04ef]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000524{
Victor Stinner26fabe12010-10-18 12:03:25 +0000525 PyObject *code = NULL, *mod, *dict;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400526 PyObject *modpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000528
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200529 if (PyUnicode_READY(fullname) == -1)
530 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 code = get_module_code(self, fullname, &ispackage, &modpath);
533 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000534 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000535
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400536 mod = PyImport_AddModuleObject(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000537 if (mod == NULL)
538 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 /* mod.__loader__ = self */
542 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
543 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (ispackage) {
546 /* add __path__ to the module *before* the code gets
547 executed */
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100548 PyObject *pkgpath, *fullpath, *subname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000550
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100551 subname = get_subname(fullname);
552 if (subname == NULL)
553 goto error;
554
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400555 fullpath = PyUnicode_FromFormat("%U%c%U%U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 self->archive, SEP,
557 self->prefix, subname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400558 Py_DECREF(subname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (fullpath == NULL)
560 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000561
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400562 pkgpath = Py_BuildValue("[N]", fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (pkgpath == NULL)
564 goto error;
565 err = PyDict_SetItemString(dict, "__path__", pkgpath);
566 Py_DECREF(pkgpath);
567 if (err != 0)
568 goto error;
569 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400570 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
Victor Stinner26fabe12010-10-18 12:03:25 +0000571 Py_CLEAR(code);
572 if (mod == NULL)
573 goto error;
574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400576 PySys_FormatStderr("import %U # loaded from Zip %U\n",
Victor Stinner08654e12010-10-18 12:09:02 +0000577 fullname, modpath);
578 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000580error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000581 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000582 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000584}
585
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400586/*[clinic input]
587zipimport.zipimporter.get_filename
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000588
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400589 fullname: unicode
590 /
591
592Return the filename for the specified module.
593[clinic start generated code]*/
594
595static PyObject *
596zipimport_zipimporter_get_filename_impl(ZipImporter *self,
597 PyObject *fullname)
598/*[clinic end generated code: output=c5b92b58bea86506 input=28d2eb57e4f25c8a]*/
599{
600 PyObject *code, *modpath;
601 int ispackage;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000602
603 /* Deciding the filename requires working out where the code
604 would come from if the module was actually loaded */
605 code = get_module_code(self, fullname, &ispackage, &modpath);
606 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000607 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000608 Py_DECREF(code); /* Only need the path info */
609
Victor Stinner08654e12010-10-18 12:09:02 +0000610 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000611}
612
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400613/*[clinic input]
614zipimport.zipimporter.is_package
Just van Rossum52e14d62002-12-30 22:08:05 +0000615
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400616 fullname: unicode
617 /
618
619Return True if the module specified by fullname is a package.
620
621Raise ZipImportError if the module couldn't be found.
622
623[clinic start generated code]*/
624
625static PyObject *
626zipimport_zipimporter_is_package_impl(ZipImporter *self, PyObject *fullname)
627/*[clinic end generated code: output=c32958c2a5216ae6 input=a7ba752f64345062]*/
628{
629 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 mi = get_module_info(self, fullname);
632 if (mi == MI_ERROR)
Victor Stinner965a8a12010-10-18 21:44:33 +0000633 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400635 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000636 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 }
638 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000639}
640
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200641
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400642/*[clinic input]
643zipimport.zipimporter.get_data
644
645 pathname as path: unicode
646 /
647
648Return the data associated with 'pathname'.
649
650Raise OSError if the file was not found.
651
652[clinic start generated code]*/
653
Just van Rossum52e14d62002-12-30 22:08:05 +0000654static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400655zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path)
656/*[clinic end generated code: output=65dc506aaa268436 input=fa6428b74843c4ae]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000657{
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400658 PyObject *key;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500659 PyObject *toc_entry;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100660 Py_ssize_t path_start, path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000661
Oren Milmandb60a5b2017-10-20 23:42:35 +0300662 if (self->archive == NULL) {
663 PyErr_SetString(PyExc_ValueError,
664 "zipimporter.__init__() wasn't called");
665 return NULL;
666 }
667
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200668#ifdef ALTSEP
Oren Milman631fdee2017-08-29 20:40:15 +0300669 path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace,
670 "OCC", path, ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100671 if (!path)
672 return NULL;
673#else
674 Py_INCREF(path);
Just van Rossum52e14d62002-12-30 22:08:05 +0000675#endif
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100676 if (PyUnicode_READY(path) == -1)
677 goto error;
678
679 path_len = PyUnicode_GET_LENGTH(path);
680
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200681 len = PyUnicode_GET_LENGTH(self->archive);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100682 path_start = 0;
683 if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
684 && PyUnicode_READ_CHAR(path, len) == SEP) {
685 path_start = len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000687
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100688 key = PyUnicode_Substring(path, path_start, path_len);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000689 if (key == NULL)
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100690 goto error;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000691 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (toc_entry == NULL) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300693 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, key);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000694 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100695 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000697 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100698 Py_DECREF(path);
Benjamin Peterson34c15402014-02-16 14:17:28 -0500699 return get_data(self->archive, toc_entry);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100700 error:
701 Py_DECREF(path);
702 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000703}
704
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400705/*[clinic input]
706zipimport.zipimporter.get_code
707
708 fullname: unicode
709 /
710
711Return the code object for the specified module.
712
713Raise ZipImportError if the module couldn't be found.
714
715[clinic start generated code]*/
716
Just van Rossum52e14d62002-12-30 22:08:05 +0000717static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400718zipimport_zipimporter_get_code_impl(ZipImporter *self, PyObject *fullname)
719/*[clinic end generated code: output=b923c37fa99cbac4 input=2761412bc37f3549]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000722}
723
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400724/*[clinic input]
725zipimport.zipimporter.get_source
Just van Rossum52e14d62002-12-30 22:08:05 +0000726
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400727 fullname: unicode
728 /
729
730Return the source code for the specified module.
731
732Raise ZipImportError if the module couldn't be found, return None if the
733archive does contain the module, but has no source for it.
734
735[clinic start generated code]*/
736
737static PyObject *
738zipimport_zipimporter_get_source_impl(ZipImporter *self, PyObject *fullname)
739/*[clinic end generated code: output=bc059301b0c33729 input=4e4b186f2e690716]*/
740{
741 PyObject *toc_entry;
742 PyObject *subname, *path, *fullpath;
743 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000746 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000748 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400749 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000750 return NULL;
751 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400752
Victor Stinner965a8a12010-10-18 21:44:33 +0000753 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400754 if (subname == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000756
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400757 path = make_filename(self->prefix, subname);
758 Py_DECREF(subname);
759 if (path == NULL)
760 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000761
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400762 if (mi == MI_PACKAGE)
763 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
764 else
765 fullpath = PyUnicode_FromFormat("%U.py", path);
766 Py_DECREF(path);
767 if (fullpath == NULL)
768 return NULL;
769
770 toc_entry = PyDict_GetItem(self->files, fullpath);
771 Py_DECREF(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000773 PyObject *res, *bytes;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500774 bytes = get_data(self->archive, toc_entry);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000775 if (bytes == NULL)
776 return NULL;
777 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
778 PyBytes_GET_SIZE(bytes));
779 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return res;
781 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /* we have the module, but no source */
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200784 Py_RETURN_NONE;
Just van Rossum52e14d62002-12-30 22:08:05 +0000785}
786
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000787
Just van Rossum52e14d62002-12-30 22:08:05 +0000788static PyMethodDef zipimporter_methods[] = {
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400789 ZIPIMPORT_ZIPIMPORTER_FIND_MODULE_METHODDEF
790 ZIPIMPORT_ZIPIMPORTER_FIND_LOADER_METHODDEF
791 ZIPIMPORT_ZIPIMPORTER_LOAD_MODULE_METHODDEF
792 ZIPIMPORT_ZIPIMPORTER_GET_FILENAME_METHODDEF
793 ZIPIMPORT_ZIPIMPORTER_IS_PACKAGE_METHODDEF
794 ZIPIMPORT_ZIPIMPORTER_GET_DATA_METHODDEF
795 ZIPIMPORT_ZIPIMPORTER_GET_CODE_METHODDEF
796 ZIPIMPORT_ZIPIMPORTER_GET_SOURCE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000798};
799
800static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
802 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
803 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
804 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000805};
806
Just van Rossum52e14d62002-12-30 22:08:05 +0000807#define DEFERRED_ADDRESS(ADDR) 0
808
809static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
811 "zipimport.zipimporter",
812 sizeof(ZipImporter),
813 0, /* tp_itemsize */
814 (destructor)zipimporter_dealloc, /* tp_dealloc */
815 0, /* tp_print */
816 0, /* tp_getattr */
817 0, /* tp_setattr */
818 0, /* tp_reserved */
819 (reprfunc)zipimporter_repr, /* tp_repr */
820 0, /* tp_as_number */
821 0, /* tp_as_sequence */
822 0, /* tp_as_mapping */
823 0, /* tp_hash */
824 0, /* tp_call */
825 0, /* tp_str */
826 PyObject_GenericGetAttr, /* tp_getattro */
827 0, /* tp_setattro */
828 0, /* tp_as_buffer */
829 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
830 Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400831 zipimport_zipimporter___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 zipimporter_traverse, /* tp_traverse */
833 0, /* tp_clear */
834 0, /* tp_richcompare */
835 0, /* tp_weaklistoffset */
836 0, /* tp_iter */
837 0, /* tp_iternext */
838 zipimporter_methods, /* tp_methods */
839 zipimporter_members, /* tp_members */
840 0, /* tp_getset */
841 0, /* tp_base */
842 0, /* tp_dict */
843 0, /* tp_descr_get */
844 0, /* tp_descr_set */
845 0, /* tp_dictoffset */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400846 (initproc)zipimport_zipimporter___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 PyType_GenericAlloc, /* tp_alloc */
848 PyType_GenericNew, /* tp_new */
849 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000850};
851
852
853/* implementation */
854
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200855/* Given a buffer, return the unsigned int that is represented by the first
Just van Rossum52e14d62002-12-30 22:08:05 +0000856 4 bytes, encoded as little endian. This partially reimplements
857 marshal.c:r_long() */
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200858static unsigned int
859get_uint32(const unsigned char *buf)
860{
861 unsigned int x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 x = buf[0];
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200863 x |= (unsigned int)buf[1] << 8;
864 x |= (unsigned int)buf[2] << 16;
865 x |= (unsigned int)buf[3] << 24;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000867}
868
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200869/* Given a buffer, return the unsigned int that is represented by the first
870 2 bytes, encoded as little endian. This partially reimplements
871 marshal.c:r_short() */
872static unsigned short
873get_uint16(const unsigned char *buf)
874{
875 unsigned short x;
876 x = buf[0];
877 x |= (unsigned short)buf[1] << 8;
878 return x;
879}
880
881static void
882set_file_error(PyObject *archive, int eof)
883{
884 if (eof) {
885 PyErr_SetString(PyExc_EOFError, "EOF read where not expected");
886 }
887 else {
888 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, archive);
889 }
890}
891
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800892/*
Benjamin Peterson34c15402014-02-16 14:17:28 -0500893 read_directory(archive) -> files dict (new reference)
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800894
Benjamin Peterson34c15402014-02-16 14:17:28 -0500895 Given a path to a Zip archive, build a dict, mapping file names
Just van Rossum52e14d62002-12-30 22:08:05 +0000896 (local to the archive, using SEP as a separator) to toc entries.
897
898 A toc_entry is a tuple:
899
Victor Stinner08654e12010-10-18 12:09:02 +0000900 (__file__, # value to use for __file__, available for all files,
901 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 compress, # compression kind; 0 for uncompressed
903 data_size, # size of compressed data on disk
904 file_size, # size of decompressed data
905 file_offset, # offset of file header from start of archive
906 time, # mod time of file (in dos format)
907 date, # mod data of file (in dos format)
908 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000909 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000910
911 Directories can be recognized by the trailing SEP in the name,
912 data_size and file_offset are 0.
913*/
914static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -0500915read_directory(PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +0000916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 PyObject *files = NULL;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500918 FILE *fp;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200919 unsigned short flags, compress, time, date, name_size;
920 unsigned int crc, data_size, file_size, header_size, header_offset;
921 unsigned long file_offset, header_position;
922 unsigned long arc_offset; /* Absolute offset to start of the zip-archive. */
923 unsigned int count, i;
924 unsigned char buffer[46];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000926 PyObject *nameobj = NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100927 PyObject *path;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000928 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000929 int bootstrap;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200930 const char *errmsg = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000931
Benjamin Peterson34c15402014-02-16 14:17:28 -0500932 fp = _Py_fopen_obj(archive, "rb");
933 if (fp == NULL) {
Victor Stinnerfbd6f9e2015-03-20 10:52:25 +0100934 if (PyErr_ExceptionMatches(PyExc_OSError)) {
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300935 _PyErr_FormatFromCause(ZipImportError,
936 "can't open Zip file: %R", archive);
Victor Stinnerfbd6f9e2015-03-20 10:52:25 +0100937 }
Benjamin Peterson34c15402014-02-16 14:17:28 -0500938 return NULL;
939 }
940
Jesus Cea09bf7a72012-10-03 02:13:05 +0200941 if (fseek(fp, -22, SEEK_END) == -1) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200942 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +0200943 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200944 header_position = (unsigned long)ftell(fp);
945 if (header_position == (unsigned long)-1) {
946 goto file_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200948 assert(header_position <= (unsigned long)LONG_MAX);
949 if (fread(buffer, 1, 22, fp) != 22) {
950 goto file_error;
951 }
952 if (get_uint32(buffer) != 0x06054B50u) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* Bad: End of Central Dir signature */
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200954 errmsg = "not a Zip file";
955 goto invalid_header;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000957
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200958 header_size = get_uint32(buffer + 12);
959 header_offset = get_uint32(buffer + 16);
960 if (header_position < header_size) {
961 errmsg = "bad central directory size";
962 goto invalid_header;
963 }
964 if (header_position < header_offset) {
965 errmsg = "bad central directory offset";
966 goto invalid_header;
967 }
968 if (header_position - header_size < header_offset) {
969 errmsg = "bad central directory size or offset";
970 goto invalid_header;
971 }
972 header_position -= header_size;
973 arc_offset = header_position - header_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 files = PyDict_New();
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200976 if (files == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 /* Start of Central Directory */
980 count = 0;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200981 if (fseek(fp, (long)header_position, 0) == -1) {
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +0200982 goto file_error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200983 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 for (;;) {
985 PyObject *t;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200986 size_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000988
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200989 n = fread(buffer, 1, 46, fp);
990 if (n < 4) {
991 goto eof_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200993 /* Start of file header */
994 if (get_uint32(buffer) != 0x02014B50u) {
995 break; /* Bad: Central Dir File Header */
996 }
997 if (n != 46) {
998 goto eof_error;
999 }
1000 flags = get_uint16(buffer + 8);
1001 compress = get_uint16(buffer + 10);
1002 time = get_uint16(buffer + 12);
1003 date = get_uint16(buffer + 14);
1004 crc = get_uint32(buffer + 16);
1005 data_size = get_uint32(buffer + 20);
1006 file_size = get_uint32(buffer + 24);
1007 name_size = get_uint16(buffer + 28);
1008 header_size = (unsigned int)name_size +
1009 get_uint16(buffer + 30) /* extra field */ +
1010 get_uint16(buffer + 32) /* comment */;
1011
1012 file_offset = get_uint32(buffer + 42);
1013 if (file_offset > header_offset) {
1014 errmsg = "bad local header offset";
1015 goto invalid_header;
1016 }
1017 file_offset += arc_offset;
1018
1019 if (name_size > MAXPATHLEN) {
1020 name_size = MAXPATHLEN;
1021 }
1022 if (fread(name, 1, name_size, fp) != name_size) {
1023 goto file_error;
1024 }
1025 name[name_size] = '\0'; /* Add terminating null byte */
Victor Stinner44d9bea2016-12-05 17:55:36 +01001026#if SEP != '/'
1027 for (i = 0; i < name_size; i++) {
1028 if (name[i] == '/') {
1029 name[i] = SEP;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001030 }
1031 }
Victor Stinner44d9bea2016-12-05 17:55:36 +01001032#endif
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001033 /* Skip the rest of the header.
1034 * On Windows, calling fseek to skip over the fields we don't use is
1035 * slower than reading the data because fseek flushes stdio's
1036 * internal buffers. See issue #8745. */
1037 assert(header_size <= 3*0xFFFFu);
1038 for (i = name_size; i < header_size; i++) {
1039 if (getc(fp) == EOF) {
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001040 goto file_error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001041 }
1042 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001043
Victor Stinner4ee65a92011-01-22 10:30:29 +00001044 bootstrap = 0;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001045 if (flags & 0x0800) {
Victor Stinnerd36c8212010-10-18 12:13:46 +00001046 charset = "utf-8";
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001047 }
Victor Stinner4ee65a92011-01-22 10:30:29 +00001048 else if (!PyThreadState_GET()->interp->codecs_initialized) {
1049 /* During bootstrap, we may need to load the encodings
1050 package from a ZIP file. But the cp437 encoding is implemented
1051 in Python in the encodings package.
1052
1053 Break out of this dependency by assuming that the path to
1054 the encodings module is ASCII-only. */
1055 charset = "ascii";
1056 bootstrap = 1;
1057 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001058 else {
Victor Stinnerd36c8212010-10-18 12:13:46 +00001059 charset = "cp437";
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001060 }
Victor Stinnerd36c8212010-10-18 12:13:46 +00001061 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner4ee65a92011-01-22 10:30:29 +00001062 if (nameobj == NULL) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001063 if (bootstrap) {
Victor Stinner4ee65a92011-01-22 10:30:29 +00001064 PyErr_Format(PyExc_NotImplementedError,
1065 "bootstrap issue: python%i%i.zip contains non-ASCII "
1066 "filenames without the unicode flag",
1067 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001068 }
Victor Stinner2460a432010-08-16 17:54:28 +00001069 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +00001070 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001071 if (PyUnicode_READY(nameobj) == -1) {
Stefan Krah000fde92012-08-20 14:14:49 +02001072 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001073 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +01001074 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001075 if (path == NULL) {
Victor Stinner2460a432010-08-16 17:54:28 +00001076 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001077 }
1078 t = Py_BuildValue("NHIIkHHI", path, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 file_size, file_offset, time, date, crc);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001080 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001082 }
Victor Stinner2460a432010-08-16 17:54:28 +00001083 err = PyDict_SetItem(files, nameobj, t);
1084 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 Py_DECREF(t);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001086 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 count++;
1090 }
Benjamin Peterson34c15402014-02-16 14:17:28 -05001091 fclose(fp);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001092 if (Py_VerboseFlag) {
1093 PySys_FormatStderr("# zipimport: found %u names in %R\n",
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001094 count, archive);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001095 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 return files;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001097
1098eof_error:
1099 set_file_error(archive, !ferror(fp));
1100 goto error;
1101
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001102file_error:
Jesus Cea09bf7a72012-10-03 02:13:05 +02001103 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001104 goto error;
1105
1106invalid_header:
1107 assert(errmsg != NULL);
1108 PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
1109 goto error;
1110
Just van Rossum52e14d62002-12-30 22:08:05 +00001111error:
Benjamin Peterson34c15402014-02-16 14:17:28 -05001112 fclose(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +00001114 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001116}
1117
1118/* Return the zlib.decompress function object, or NULL if zlib couldn't
1119 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +02001120 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001121static PyObject *
1122get_decompress_func(void)
1123{
Victor Stinner4925cde2011-05-20 00:16:09 +02001124 static int importing_zlib = 0;
1125 PyObject *zlib;
1126 PyObject *decompress;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001127 _Py_IDENTIFIER(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001128
Victor Stinner4925cde2011-05-20 00:16:09 +02001129 if (importing_zlib != 0)
Xiang Zhang0710d752017-03-11 13:02:52 +08001130 /* Someone has a zlib.pyc in their Zip file;
Victor Stinner4925cde2011-05-20 00:16:09 +02001131 let's avoid a stack overflow. */
1132 return NULL;
1133 importing_zlib = 1;
1134 zlib = PyImport_ImportModuleNoBlock("zlib");
1135 importing_zlib = 0;
1136 if (zlib != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001137 decompress = _PyObject_GetAttrId(zlib,
1138 &PyId_decompress);
Victor Stinner4925cde2011-05-20 00:16:09 +02001139 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 }
Victor Stinner4925cde2011-05-20 00:16:09 +02001141 else {
1142 PyErr_Clear();
1143 decompress = NULL;
1144 }
1145 if (Py_VerboseFlag)
1146 PySys_WriteStderr("# zipimport: zlib %s\n",
1147 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +00001149}
1150
Benjamin Peterson34c15402014-02-16 14:17:28 -05001151/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
Just van Rossum52e14d62002-12-30 22:08:05 +00001152 data as a new reference. */
1153static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -05001154get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001155{
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001156 PyObject *raw_data = NULL, *data, *decompress;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 char *buf;
Benjamin Peterson34c15402014-02-16 14:17:28 -05001158 FILE *fp;
Victor Stinner60fe8d92010-08-16 23:48:11 +00001159 PyObject *datapath;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001160 unsigned short compress, time, date;
1161 unsigned int crc;
1162 Py_ssize_t data_size, file_size, bytes_size;
1163 long file_offset, header_size;
1164 unsigned char buffer[30];
1165 const char *errmsg = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001166
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001167 if (!PyArg_ParseTuple(toc_entry, "OHnnlHHI", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 &data_size, &file_size, &file_offset, &time,
1169 &date, &crc)) {
1170 return NULL;
1171 }
Benjamin Petersonb1db7582016-01-21 22:02:46 -08001172 if (data_size < 0) {
1173 PyErr_Format(ZipImportError, "negative data size");
1174 return NULL;
1175 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001176
Benjamin Peterson34c15402014-02-16 14:17:28 -05001177 fp = _Py_fopen_obj(archive, "rb");
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001178 if (!fp) {
Benjamin Peterson34c15402014-02-16 14:17:28 -05001179 return NULL;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001180 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 /* Check to make sure the local file header is correct */
Jesus Cea09bf7a72012-10-03 02:13:05 +02001182 if (fseek(fp, file_offset, 0) == -1) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001183 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001184 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001185 if (fread(buffer, 1, 30, fp) != 30) {
1186 goto eof_error;
1187 }
1188 if (get_uint32(buffer) != 0x04034B50u) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 /* Bad: Local File Header */
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001190 errmsg = "bad local file header";
1191 goto invalid_header;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001192 }
1193
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001194 header_size = (unsigned int)30 +
1195 get_uint16(buffer + 26) /* file name */ +
1196 get_uint16(buffer + 28) /* extra field */;
1197 if (file_offset > LONG_MAX - header_size) {
1198 errmsg = "bad local file header size";
1199 goto invalid_header;
Victor Stinner73660af2013-10-29 01:43:44 +01001200 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001201 file_offset += header_size; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +00001202
Benjamin Petersonc4032da2016-01-20 22:23:44 -08001203 if (data_size > LONG_MAX - 1) {
1204 fclose(fp);
1205 PyErr_NoMemory();
1206 return NULL;
1207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 bytes_size = compress == 0 ? data_size : data_size + 1;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001209 if (bytes_size == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 bytes_size++;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001211 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (raw_data == NULL) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001214 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 }
1216 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001217
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001218 if (fseek(fp, file_offset, 0) == -1) {
1219 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001220 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001221 if (fread(buf, 1, data_size, fp) != (size_t)data_size) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001222 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 "zipimport: can't read data");
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001224 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001226
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001227 fclose(fp);
1228 fp = NULL;
1229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (compress != 0) {
1231 buf[data_size] = 'Z'; /* saw this in zipfile.py */
1232 data_size++;
1233 }
1234 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 if (compress == 0) { /* data is not compressed */
1237 data = PyBytes_FromStringAndSize(buf, data_size);
1238 Py_DECREF(raw_data);
1239 return data;
1240 }
1241
1242 /* Decompress with zlib */
1243 decompress = get_decompress_func();
1244 if (decompress == NULL) {
1245 PyErr_SetString(ZipImportError,
1246 "can't decompress data; "
1247 "zlib not available");
1248 goto error;
1249 }
1250 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +02001251 Py_DECREF(decompress);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 Py_DECREF(raw_data);
Oren Milman01c6a882017-09-29 21:34:31 +03001253 if (data != NULL && !PyBytes_Check(data)) {
1254 PyErr_Format(PyExc_TypeError,
1255 "zlib.decompress() must return a bytes object, not "
1256 "%.200s",
1257 Py_TYPE(data)->tp_name);
1258 Py_DECREF(data);
1259 return NULL;
1260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 return data;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001262
1263eof_error:
1264 set_file_error(archive, !ferror(fp));
1265 goto error;
1266
1267file_error:
1268 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1269 goto error;
1270
1271invalid_header:
1272 assert(errmsg != NULL);
1273 PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
1274 goto error;
1275
1276error:
1277 if (fp != NULL) {
1278 fclose(fp);
1279 }
1280 Py_XDECREF(raw_data);
1281 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001282}
1283
1284/* Lenient date/time comparison function. The precision of the mtime
1285 in the archive is lower than the mtime stored in a .pyc: we
1286 must allow a difference of at most one second. */
1287static int
1288eq_mtime(time_t t1, time_t t2)
1289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 time_t d = t1 - t2;
1291 if (d < 0)
1292 d = -d;
1293 /* dostime only stores even seconds, so be lenient */
1294 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001295}
1296
Xiang Zhang0710d752017-03-11 13:02:52 +08001297/* Given the contents of a .pyc file in a buffer, unmarshal the data
Just van Rossum52e14d62002-12-30 22:08:05 +00001298 and return the code object. Return None if it the magic word doesn't
1299 match (we do this instead of raising an exception as we fall back
1300 to .py if available and we don't want to mask other errors).
1301 Returns a new reference. */
1302static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001303unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 PyObject *code;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001306 unsigned char *buf = (unsigned char *)PyBytes_AsString(data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001308
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08001309 if (size < 16) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 PyErr_SetString(ZipImportError,
1311 "bad pyc data");
1312 return NULL;
1313 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001314
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001315 if (get_uint32(buf) != (unsigned int)PyImport_GetMagicNumber()) {
1316 if (Py_VerboseFlag) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001317 PySys_FormatStderr("# %R has bad magic\n",
1318 pathname);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001319 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001320 Py_RETURN_NONE; /* signal caller to try alternative */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001322
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08001323 uint32_t flags = get_uint32(buf + 4);
1324 if (flags != 0) {
1325 // Hash-based pyc. We currently refuse to handle checked hash-based
1326 // pycs. We could validate hash-based pycs against the source, but it
1327 // seems likely that most people putting hash-based pycs in a zipfile
1328 // will use unchecked ones.
1329 if (strcmp(_Py_CheckHashBasedPycsMode, "never") &&
1330 (flags != 0x1 || !strcmp(_Py_CheckHashBasedPycsMode, "always")))
1331 Py_RETURN_NONE;
1332 } else if ((mtime != 0 && !eq_mtime(get_uint32(buf + 8), mtime))) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001333 if (Py_VerboseFlag) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001334 PySys_FormatStderr("# %R has bad mtime\n",
1335 pathname);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001336 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001337 Py_RETURN_NONE; /* signal caller to try alternative */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001339
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001340 /* XXX the pyc's size field is ignored; timestamp collisions are probably
1341 unimportant with zip files. */
Benjamin Peterson42aa93b2017-12-09 10:26:52 -08001342 code = PyMarshal_ReadObjectFromString((char *)buf + 16, size - 16);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001343 if (code == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 return NULL;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001345 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (!PyCode_Check(code)) {
1347 Py_DECREF(code);
1348 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001349 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 pathname);
1351 return NULL;
1352 }
1353 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001354}
1355
Martin Panter0be894b2016-09-07 12:03:06 +00001356/* Replace any occurrences of "\r\n?" in the input string with "\n".
Just van Rossum52e14d62002-12-30 22:08:05 +00001357 This converts DOS and Mac line endings to Unix line endings.
1358 Also append a trailing "\n" to be compatible with
1359 PyParser_SimpleParseFile(). Returns a new reference. */
1360static PyObject *
1361normalize_line_endings(PyObject *source)
1362{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001363 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 PyObject *fixed_source;
1365 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001366
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001367 p = PyBytes_AsString(source);
1368 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 return PyBytes_FromStringAndSize("\n\0", 2);
1370 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 /* one char extra for trailing \n and one for terminating \0 */
1373 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1374 if (buf == NULL) {
1375 PyErr_SetString(PyExc_MemoryError,
1376 "zipimport: no memory to allocate "
1377 "source buffer");
1378 return NULL;
1379 }
1380 /* replace "\r\n?" by "\n" */
1381 for (q = buf; *p != '\0'; p++) {
1382 if (*p == '\r') {
1383 *q++ = '\n';
1384 if (*(p + 1) == '\n')
1385 p++;
1386 }
1387 else
1388 *q++ = *p;
1389 len++;
1390 }
1391 *q++ = '\n'; /* add trailing \n */
1392 *q = '\0';
1393 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1394 PyMem_Free(buf);
1395 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001396}
1397
1398/* Given a string buffer containing Python source code, compile it
Brett Cannon83358c92013-06-20 21:30:32 -04001399 and return a code object as a new reference. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001400static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001401compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001402{
Steve Dower8dcc48e2016-09-09 17:27:33 -07001403 PyObject *code, *fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001404
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001405 fixed_source = normalize_line_endings(source);
1406 if (fixed_source == NULL) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001407 return NULL;
1408 }
1409
Steve Dower8dcc48e2016-09-09 17:27:33 -07001410 code = Py_CompileStringObject(PyBytes_AsString(fixed_source),
Berker Peksag4aa74c42016-09-14 08:09:48 +03001411 pathname, Py_file_input, NULL, -1);
Steve Dower8dcc48e2016-09-09 17:27:33 -07001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 Py_DECREF(fixed_source);
1414 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001415}
1416
1417/* Convert the date/time values found in the Zip archive to a value
1418 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001419static time_t
1420parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 stm.tm_sec = (dostime & 0x1f) * 2;
1427 stm.tm_min = (dostime >> 5) & 0x3f;
1428 stm.tm_hour = (dostime >> 11) & 0x1f;
1429 stm.tm_mday = dosdate & 0x1f;
1430 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1431 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1432 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001435}
1436
Brett Cannonf299abd2015-04-13 14:21:02 -04001437/* Given a path to a .pyc file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001438 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001439 is available. */
1440static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001441get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001442{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001443 PyObject *toc_entry, *stripped;
1444 time_t mtime;
1445
Xiang Zhang0710d752017-03-11 13:02:52 +08001446 /* strip 'c' from *.pyc */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001447 if (PyUnicode_READY(path) == -1)
1448 return (time_t)-1;
1449 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
1450 PyUnicode_DATA(path),
1451 PyUnicode_GET_LENGTH(path) - 1);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001452 if (stripped == NULL)
1453 return (time_t)-1;
1454
1455 toc_entry = PyDict_GetItem(self->files, stripped);
1456 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1458 PyTuple_Size(toc_entry) == 8) {
1459 /* fetch the time stamp of the .py file for comparison
1460 with an embedded pyc time stamp */
1461 int time, date;
1462 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1463 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1464 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001465 } else
1466 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001468}
1469
1470/* Return the code object for the module named by 'fullname' from the
1471 Zip archive as a new reference. */
1472static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -05001473get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001475{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001476 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001477
Benjamin Peterson34c15402014-02-16 14:17:28 -05001478 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (data == NULL)
1480 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001481
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001482 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001483 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001484 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001485 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001486 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 Py_DECREF(data);
1488 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001489}
1490
Ezio Melotti42da6632011-03-15 05:18:48 +02001491/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001492 'fullname'. */
1493static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001494get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001495 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001496{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001497 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001498 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001500
Oren Milmandb60a5b2017-10-20 23:42:35 +03001501 if (self->prefix == NULL) {
1502 PyErr_SetString(PyExc_ValueError,
1503 "zipimporter.__init__() wasn't called");
1504 return NULL;
1505 }
1506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001508 if (subname == NULL)
1509 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001510
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001511 path = make_filename(self->prefix, subname);
1512 Py_DECREF(subname);
1513 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001517 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001518
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001519 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1520 if (fullpath == NULL)
1521 goto exit;
1522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001524 PySys_FormatStderr("# trying %U%c%U\n",
1525 self->archive, (int)SEP, fullpath);
1526 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if (toc_entry != NULL) {
1528 time_t mtime = 0;
1529 int ispackage = zso->type & IS_PACKAGE;
1530 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001531
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001532 if (isbytecode) {
1533 mtime = get_mtime_of_source(self, fullpath);
1534 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1535 goto exit;
1536 }
1537 }
1538 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 if (p_ispackage != NULL)
1540 *p_ispackage = ispackage;
Benjamin Peterson34c15402014-02-16 14:17:28 -05001541 code = get_code_from_data(self, ispackage,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 isbytecode, mtime,
1543 toc_entry);
1544 if (code == Py_None) {
1545 /* bad magic number or non-matching mtime
1546 in byte code, try next */
1547 Py_DECREF(code);
1548 continue;
1549 }
Victor Stinner08654e12010-10-18 12:09:02 +00001550 if (code != NULL && p_modpath != NULL) {
1551 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1552 Py_INCREF(*p_modpath);
1553 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001554 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001556 else
1557 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001559 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1560exit:
1561 Py_DECREF(path);
1562 Py_XDECREF(fullpath);
1563 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001564}
1565
1566
1567/* Module init */
1568
1569PyDoc_STRVAR(zipimport_doc,
1570"zipimport provides support for importing Python modules from Zip archives.\n\
1571\n\
1572This module exports three objects:\n\
1573- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001574- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001575 subclass of ImportError, so it can be caught as ImportError, too.\n\
1576- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1577 info dicts, as used in zipimporter._files.\n\
1578\n\
1579It is usually not needed to use the zipimport module explicitly; it is\n\
1580used by the builtin import mechanism for sys.path items that are paths\n\
1581to Zip archives.");
1582
Martin v. Löwis1a214512008-06-11 05:26:20 +00001583static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 PyModuleDef_HEAD_INIT,
1585 "zipimport",
1586 zipimport_doc,
1587 -1,
1588 NULL,
1589 NULL,
1590 NULL,
1591 NULL,
1592 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001593};
1594
Just van Rossum52e14d62002-12-30 22:08:05 +00001595PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001596PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (PyType_Ready(&ZipImporter_Type) < 0)
1601 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 /* Correct directory separator */
1604 zip_searchorder[0].suffix[0] = SEP;
1605 zip_searchorder[1].suffix[0] = SEP;
Just van Rossum52e14d62002-12-30 22:08:05 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 mod = PyModule_Create(&zipimportmodule);
1608 if (mod == NULL)
1609 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1612 PyExc_ImportError, NULL);
1613 if (ZipImportError == NULL)
1614 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 Py_INCREF(ZipImportError);
1617 if (PyModule_AddObject(mod, "ZipImportError",
1618 ZipImportError) < 0)
1619 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 Py_INCREF(&ZipImporter_Type);
1622 if (PyModule_AddObject(mod, "zipimporter",
1623 (PyObject *)&ZipImporter_Type) < 0)
1624 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 zip_directory_cache = PyDict_New();
1627 if (zip_directory_cache == NULL)
1628 return NULL;
1629 Py_INCREF(zip_directory_cache);
1630 if (PyModule_AddObject(mod, "_zip_directory_cache",
1631 zip_directory_cache) < 0)
1632 return NULL;
1633 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001634}