blob: d710360f95d6d47d59e334ba3cae47c8542b94b5 [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
Brett Cannonf299abd2015-04-13 14:21:02 -040023 non-package .pyc, and .py entries. The .pyc entries
Just van Rossum52e14d62002-12-30 22:08:05 +000024 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},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 {"/__init__.py", IS_PACKAGE | IS_SOURCE},
29 {".pyc", IS_BYTECODE},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 {".py", IS_SOURCE},
31 {"", 0}
Just van Rossum52e14d62002-12-30 22:08:05 +000032};
33
34/* zipimporter object definition and support */
35
36typedef struct _zipimporter ZipImporter;
37
38struct _zipimporter {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 PyObject_HEAD
Victor Stinner9e40fad2010-10-18 22:34:46 +000040 PyObject *archive; /* pathname of the Zip archive,
41 decoded from the filesystem encoding */
Victor Stinner72f767e2010-10-18 11:44:21 +000042 PyObject *prefix; /* file prefix: "a/sub/directory/",
43 encoded to the filesystem encoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 PyObject *files; /* dict with file info {path: toc_entry} */
Just van Rossum52e14d62002-12-30 22:08:05 +000045};
46
Just van Rossum52e14d62002-12-30 22:08:05 +000047static PyObject *ZipImportError;
Victor Stinnerc342fca2010-10-18 11:39:05 +000048/* read_directory() cache */
Just van Rossum52e14d62002-12-30 22:08:05 +000049static PyObject *zip_directory_cache = NULL;
50
51/* forward decls */
Benjamin Peterson34c15402014-02-16 14:17:28 -050052static PyObject *read_directory(PyObject *archive);
53static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
Victor Stinnerf6b563a2011-03-14 20:46:50 -040054static PyObject *get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +000055 int *p_ispackage, PyObject **p_modpath);
Just van Rossum52e14d62002-12-30 22:08:05 +000056
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040057static PyTypeObject ZipImporter_Type;
Just van Rossum52e14d62002-12-30 22:08:05 +000058
59#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
60
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040061/*[clinic input]
62module zipimport
63class zipimport.zipimporter "ZipImporter *" "&ZipImporter_Type"
64[clinic start generated code]*/
65/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9db8b61557d911e7]*/
66#include "clinic/zipimport.c.h"
67
Just van Rossum52e14d62002-12-30 22:08:05 +000068
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. */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040073
74/*[clinic input]
75zipimport.zipimporter.__init__
76
77 archivepath as path: object(converter="PyUnicode_FSDecoder")
78 A path-like object to a zipfile, or to a specific path inside
79 a zipfile.
80 /
81
82Create a new zipimporter instance.
83
84'archivepath' must be a path-like object to a zipfile, or to a specific path
85inside a zipfile. For example, it can be '/tmp/myimport.zip', or
86'/tmp/myimport.zip/mydirectory', if mydirectory is a valid directory inside
87the archive.
88
89'ZipImportError' is raised if 'archivepath' doesn't point to a valid Zip
90archive.
91
92The 'archive' attribute of the zipimporter object contains the name of the
93zipfile targeted.
94
95[clinic start generated code]*/
96
Just van Rossum52e14d62002-12-30 22:08:05 +000097static int
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040098zipimport_zipimporter___init___impl(ZipImporter *self, PyObject *path)
99/*[clinic end generated code: output=141558fefdb46dc8 input=92b9ebeed1f6a704]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000100{
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400101 PyObject *files, *tmp;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100102 PyObject *filename = NULL;
103 Py_ssize_t len, flen;
Just van Rossum52e14d62002-12-30 22:08:05 +0000104
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100105 if (PyUnicode_READY(path) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200106 return -1;
107
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100108 len = PyUnicode_GET_LENGTH(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (len == 0) {
110 PyErr_SetString(ZipImportError, "archive path is empty");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000111 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000113
114#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +0100115 tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100116 if (!tmp)
117 goto error;
118 Py_DECREF(path);
119 path = tmp;
Just van Rossum52e14d62002-12-30 22:08:05 +0000120#endif
121
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100122 filename = path;
123 Py_INCREF(filename);
124 flen = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 for (;;) {
126 struct stat statbuf;
127 int rv;
Just van Rossum52e14d62002-12-30 22:08:05 +0000128
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100129 rv = _Py_stat(filename, &statbuf);
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100130 if (rv == -2)
131 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 if (rv == 0) {
133 /* it exists */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100134 if (!S_ISREG(statbuf.st_mode))
135 /* it's a not file */
136 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 break;
138 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100139 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 /* back up one path element */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100141 flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);
142 if (flen == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 break;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100144 filename = PyUnicode_Substring(path, 0, flen);
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100145 if (filename == NULL)
146 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100148 if (filename == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 PyErr_SetString(ZipImportError, "not a Zip file");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000150 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000152
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100153 if (PyUnicode_READY(filename) < 0)
154 goto error;
155
156 files = PyDict_GetItem(zip_directory_cache, filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000157 if (files == NULL) {
Benjamin Peterson34c15402014-02-16 14:17:28 -0500158 files = read_directory(filename);
159 if (files == NULL)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000160 goto error;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500161 if (PyDict_SetItem(zip_directory_cache, filename, files) != 0)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000162 goto error;
163 }
164 else
165 Py_INCREF(files);
166 self->files = files;
167
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100168 /* Transfer reference */
169 self->archive = filename;
170 filename = NULL;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000171
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100172 /* Check if there is a prefix directory following the filename. */
173 if (flen != len) {
174 tmp = PyUnicode_Substring(path, flen+1,
175 PyUnicode_GET_LENGTH(path));
176 if (tmp == NULL)
177 goto error;
178 self->prefix = tmp;
179 if (PyUnicode_READ_CHAR(path, len-1) != SEP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 /* add trailing SEP */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100181 tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
182 if (tmp == NULL)
183 goto error;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300184 Py_SETREF(self->prefix, tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 }
186 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000187 else
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100188 self->prefix = PyUnicode_New(0, 0);
189 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 return 0;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000191
192error:
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100193 Py_DECREF(path);
194 Py_XDECREF(filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000195 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000196}
197
198/* GC support. */
199static int
200zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 ZipImporter *self = (ZipImporter *)obj;
203 Py_VISIT(self->files);
204 return 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000205}
206
207static void
208zipimporter_dealloc(ZipImporter *self)
209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 PyObject_GC_UnTrack(self);
211 Py_XDECREF(self->archive);
212 Py_XDECREF(self->prefix);
213 Py_XDECREF(self->files);
214 Py_TYPE(self)->tp_free((PyObject *)self);
Just van Rossum52e14d62002-12-30 22:08:05 +0000215}
216
217static PyObject *
218zipimporter_repr(ZipImporter *self)
219{
Victor Stinner028dd972010-08-17 00:04:48 +0000220 if (self->archive == NULL)
221 return PyUnicode_FromString("<zipimporter object \"???\">");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200222 else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0)
Victor Stinner07298a12010-10-18 22:45:54 +0000223 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000224 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 else
Victor Stinner07298a12010-10-18 22:45:54 +0000226 return PyUnicode_FromFormat("<zipimporter object \"%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000227 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000228}
229
230/* return fullname.split(".")[-1] */
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400231static PyObject *
232get_subname(PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000233{
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100234 Py_ssize_t len, dot;
235 if (PyUnicode_READY(fullname) < 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200236 return NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100237 len = PyUnicode_GET_LENGTH(fullname);
238 dot = PyUnicode_FindChar(fullname, '.', 0, len, -1);
239 if (dot == -1) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400240 Py_INCREF(fullname);
241 return fullname;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100242 } else
243 return PyUnicode_Substring(fullname, dot+1, len);
Just van Rossum52e14d62002-12-30 22:08:05 +0000244}
245
246/* Given a (sub)modulename, write the potential file path in the
247 archive (without extension) to the path buffer. Return the
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400248 length of the resulting string.
249
250 return self.prefix + name.replace('.', os.sep) */
251static PyObject*
252make_filename(PyObject *prefix, PyObject *name)
Just van Rossum52e14d62002-12-30 22:08:05 +0000253{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400254 PyObject *pathobj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200255 Py_UCS4 *p, *buf;
256 Py_ssize_t len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000257
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200258 len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200259 p = buf = PyMem_New(Py_UCS4, len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200260 if (buf == NULL) {
261 PyErr_NoMemory();
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400262 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200263 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000264
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200265 if (!PyUnicode_AsUCS4(prefix, p, len, 0)) {
266 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200267 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200268 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200269 p += PyUnicode_GET_LENGTH(prefix);
270 len -= PyUnicode_GET_LENGTH(prefix);
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200271 if (!PyUnicode_AsUCS4(name, p, len, 1)) {
272 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200273 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200274 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400275 for (; *p; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (*p == '.')
277 *p = SEP;
278 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200279 pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
280 buf, p-buf);
281 PyMem_Free(buf);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400282 return pathobj;
Just van Rossum52e14d62002-12-30 22:08:05 +0000283}
284
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000285enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 MI_ERROR,
287 MI_NOT_FOUND,
288 MI_MODULE,
289 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000290};
291
Eric V. Smith984b11f2012-05-24 20:21:04 -0400292/* Does this path represent a directory?
293 on error, return < 0
294 if not a dir, return 0
295 if a dir, return 1
296*/
297static int
298check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path)
299{
300 PyObject *dirpath;
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700301 int res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400302
303 /* See if this is a "directory". If so, it's eligible to be part
304 of a namespace package. We test by seeing if the name, with an
305 appended path separator, exists. */
306 dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
307 if (dirpath == NULL)
308 return -1;
309 /* If dirpath is present in self->files, we have a directory. */
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700310 res = PyDict_Contains(self->files, dirpath);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400311 Py_DECREF(dirpath);
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700312 return res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400313}
314
Just van Rossum52e14d62002-12-30 22:08:05 +0000315/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000316static enum zi_module_info
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400317get_module_info(ZipImporter *self, PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000318{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400319 PyObject *subname;
320 PyObject *path, *fullpath, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000322
Victor Stinner965a8a12010-10-18 21:44:33 +0000323 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400324 if (subname == NULL)
325 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000326
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400327 path = make_filename(self->prefix, subname);
328 Py_DECREF(subname);
329 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400333 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
334 if (fullpath == NULL) {
335 Py_DECREF(path);
336 return MI_ERROR;
337 }
338 item = PyDict_GetItem(self->files, fullpath);
339 Py_DECREF(fullpath);
340 if (item != NULL) {
341 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (zso->type & IS_PACKAGE)
343 return MI_PACKAGE;
344 else
345 return MI_MODULE;
346 }
347 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400348 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000350}
351
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700352typedef enum {
Brett Cannon56aae8f2016-01-15 11:22:19 -0800353 FL_ERROR = -1, /* error */
354 FL_NOT_FOUND, /* no loader or namespace portions found */
355 FL_MODULE_FOUND, /* module/package found */
356 FL_NS_FOUND /* namespace portion found: */
357 /* *namespace_portion will point to the name */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700358} find_loader_result;
359
Brett Cannon56aae8f2016-01-15 11:22:19 -0800360/* The guts of "find_loader" and "find_module".
Eric V. Smith984b11f2012-05-24 20:21:04 -0400361*/
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700362static find_loader_result
Eric V. Smith984b11f2012-05-24 20:21:04 -0400363find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
364{
365 enum zi_module_info mi;
366
367 *namespace_portion = NULL;
368
369 mi = get_module_info(self, fullname);
370 if (mi == MI_ERROR)
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700371 return FL_ERROR;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400372 if (mi == MI_NOT_FOUND) {
373 /* Not a module or regular package. See if this is a directory, and
374 therefore possibly a portion of a namespace package. */
Brett Cannon56aae8f2016-01-15 11:22:19 -0800375 find_loader_result result = FL_NOT_FOUND;
376 PyObject *subname;
377 int is_dir;
378
379 /* We're only interested in the last path component of fullname;
380 earlier components are recorded in self->prefix. */
381 subname = get_subname(fullname);
382 if (subname == NULL) {
383 return FL_ERROR;
384 }
385
386 is_dir = check_is_directory(self, self->prefix, subname);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400387 if (is_dir < 0)
Brett Cannon56aae8f2016-01-15 11:22:19 -0800388 result = FL_ERROR;
389 else if (is_dir) {
Eric V. Smith984b11f2012-05-24 20:21:04 -0400390 /* This is possibly a portion of a namespace
391 package. Return the string representing its path,
392 without a trailing separator. */
393 *namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
394 self->archive, SEP,
Brett Cannon56aae8f2016-01-15 11:22:19 -0800395 self->prefix, subname);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400396 if (*namespace_portion == NULL)
Brett Cannon56aae8f2016-01-15 11:22:19 -0800397 result = FL_ERROR;
398 else
399 result = FL_NS_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400400 }
Brett Cannon56aae8f2016-01-15 11:22:19 -0800401 Py_DECREF(subname);
402 return result;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400403 }
404 /* This is a module or package. */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700405 return FL_MODULE_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400406}
407
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400408/*[clinic input]
409zipimport.zipimporter.find_module
Eric V. Smith984b11f2012-05-24 20:21:04 -0400410
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400411 fullname: unicode
412 path: object = None
413 /
414
415Search for a module specified by 'fullname'.
416
417'fullname' must be the fully qualified (dotted) module name. It returns the
418zipimporter instance itself if the module was found, or None if it wasn't.
419The optional 'path' argument is ignored -- it's there for compatibility
420with the importer protocol.
421
422[clinic start generated code]*/
423
Just van Rossum52e14d62002-12-30 22:08:05 +0000424static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400425zipimport_zipimporter_find_module_impl(ZipImporter *self, PyObject *fullname,
426 PyObject *path)
427/*[clinic end generated code: output=506087f609466dc7 input=e3528520e075063f]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000428{
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700429 PyObject *namespace_portion = NULL;
430 PyObject *result = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000431
Eric V. Smith984b11f2012-05-24 20:21:04 -0400432 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700433 case FL_ERROR:
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700434 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700435 case FL_NS_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700436 /* A namespace portion is not allowed via find_module, so return None. */
Eric V. Smith984b11f2012-05-24 20:21:04 -0400437 Py_DECREF(namespace_portion);
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700438 /* FALL THROUGH */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700439 case FL_NOT_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700440 result = Py_None;
441 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700442 case FL_MODULE_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700443 result = (PyObject *)self;
444 break;
Brett Cannon56aae8f2016-01-15 11:22:19 -0800445 default:
446 PyErr_BadInternalCall();
447 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 }
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700449 Py_INCREF(result);
Benjamin Peterson2d12e142012-05-25 00:19:40 -0700450 return result;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400451}
452
453
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400454/*[clinic input]
455zipimport.zipimporter.find_loader
456
457 fullname: unicode
458 path: object = None
459 /
460
461Search for a module specified by 'fullname'.
462
463'fullname' must be the fully qualified (dotted) module name. It returns the
464zipimporter instance itself if the module was found, a string containing the
465full path name if it's possibly a portion of a namespace package,
466or None otherwise. The optional 'path' argument is ignored -- it's
467there for compatibility with the importer protocol.
468
469[clinic start generated code]*/
470
Eric V. Smith984b11f2012-05-24 20:21:04 -0400471static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400472zipimport_zipimporter_find_loader_impl(ZipImporter *self, PyObject *fullname,
473 PyObject *path)
474/*[clinic end generated code: output=601599a43bc0f49a input=dc73f275b0d5be23]*/
Eric V. Smith984b11f2012-05-24 20:21:04 -0400475{
Eric V. Smith984b11f2012-05-24 20:21:04 -0400476 PyObject *result = NULL;
477 PyObject *namespace_portion = NULL;
478
Eric V. Smith984b11f2012-05-24 20:21:04 -0400479 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700480 case FL_ERROR:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700481 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700482 case FL_NOT_FOUND: /* Not found, return (None, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700483 result = Py_BuildValue("O[]", Py_None);
484 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700485 case FL_MODULE_FOUND: /* Return (self, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700486 result = Py_BuildValue("O[]", self);
487 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700488 case FL_NS_FOUND: /* Return (None, [namespace_portion]) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700489 result = Py_BuildValue("O[O]", Py_None, namespace_portion);
Benjamin Peterson209e04c2012-05-24 22:35:39 -0700490 Py_DECREF(namespace_portion);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400491 return result;
Brett Cannon56aae8f2016-01-15 11:22:19 -0800492 default:
493 PyErr_BadInternalCall();
494 return NULL;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400495 }
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700496 return result;
Just van Rossum52e14d62002-12-30 22:08:05 +0000497}
498
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400499/*[clinic input]
500zipimport.zipimporter.load_module
501
502 fullname: unicode
503 /
504
505Load the module specified by 'fullname'.
506
507'fullname' must be the fully qualified (dotted) module name. It returns the
508imported module, or raises ZipImportError if it wasn't found.
509
510[clinic start generated code]*/
511
Just van Rossum52e14d62002-12-30 22:08:05 +0000512static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400513zipimport_zipimporter_load_module_impl(ZipImporter *self, PyObject *fullname)
514/*[clinic end generated code: output=7303cebf88d47953 input=c236e2e8621f04ef]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000515{
Victor Stinner26fabe12010-10-18 12:03:25 +0000516 PyObject *code = NULL, *mod, *dict;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400517 PyObject *modpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000519
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200520 if (PyUnicode_READY(fullname) == -1)
521 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 code = get_module_code(self, fullname, &ispackage, &modpath);
524 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000525 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000526
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400527 mod = PyImport_AddModuleObject(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000528 if (mod == NULL)
529 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 /* mod.__loader__ = self */
533 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
534 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (ispackage) {
537 /* add __path__ to the module *before* the code gets
538 executed */
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100539 PyObject *pkgpath, *fullpath, *subname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000541
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100542 subname = get_subname(fullname);
543 if (subname == NULL)
544 goto error;
545
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400546 fullpath = PyUnicode_FromFormat("%U%c%U%U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 self->archive, SEP,
548 self->prefix, subname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400549 Py_DECREF(subname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 if (fullpath == NULL)
551 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000552
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400553 pkgpath = Py_BuildValue("[N]", fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (pkgpath == NULL)
555 goto error;
556 err = PyDict_SetItemString(dict, "__path__", pkgpath);
557 Py_DECREF(pkgpath);
558 if (err != 0)
559 goto error;
560 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400561 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
Victor Stinner26fabe12010-10-18 12:03:25 +0000562 Py_CLEAR(code);
563 if (mod == NULL)
564 goto error;
565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400567 PySys_FormatStderr("import %U # loaded from Zip %U\n",
Victor Stinner08654e12010-10-18 12:09:02 +0000568 fullname, modpath);
569 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000571error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000572 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000573 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000575}
576
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400577/*[clinic input]
578zipimport.zipimporter.get_filename
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000579
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400580 fullname: unicode
581 /
582
583Return the filename for the specified module.
584[clinic start generated code]*/
585
586static PyObject *
587zipimport_zipimporter_get_filename_impl(ZipImporter *self,
588 PyObject *fullname)
589/*[clinic end generated code: output=c5b92b58bea86506 input=28d2eb57e4f25c8a]*/
590{
591 PyObject *code, *modpath;
592 int ispackage;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000593
594 /* Deciding the filename requires working out where the code
595 would come from if the module was actually loaded */
596 code = get_module_code(self, fullname, &ispackage, &modpath);
597 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000598 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000599 Py_DECREF(code); /* Only need the path info */
600
Victor Stinner08654e12010-10-18 12:09:02 +0000601 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000602}
603
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400604/*[clinic input]
605zipimport.zipimporter.is_package
Just van Rossum52e14d62002-12-30 22:08:05 +0000606
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400607 fullname: unicode
608 /
609
610Return True if the module specified by fullname is a package.
611
612Raise ZipImportError if the module couldn't be found.
613
614[clinic start generated code]*/
615
616static PyObject *
617zipimport_zipimporter_is_package_impl(ZipImporter *self, PyObject *fullname)
618/*[clinic end generated code: output=c32958c2a5216ae6 input=a7ba752f64345062]*/
619{
620 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 mi = get_module_info(self, fullname);
623 if (mi == MI_ERROR)
Victor Stinner965a8a12010-10-18 21:44:33 +0000624 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400626 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000627 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 }
629 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000630}
631
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200632
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400633/*[clinic input]
634zipimport.zipimporter.get_data
635
636 pathname as path: unicode
637 /
638
639Return the data associated with 'pathname'.
640
641Raise OSError if the file was not found.
642
643[clinic start generated code]*/
644
Just van Rossum52e14d62002-12-30 22:08:05 +0000645static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400646zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path)
647/*[clinic end generated code: output=65dc506aaa268436 input=fa6428b74843c4ae]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000648{
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400649 PyObject *key;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500650 PyObject *toc_entry;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100651 Py_ssize_t path_start, path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000652
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200653#ifdef ALTSEP
Oren Milman631fdee2017-08-29 20:40:15 +0300654 path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace,
655 "OCC", path, ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100656 if (!path)
657 return NULL;
658#else
659 Py_INCREF(path);
Just van Rossum52e14d62002-12-30 22:08:05 +0000660#endif
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100661 if (PyUnicode_READY(path) == -1)
662 goto error;
663
664 path_len = PyUnicode_GET_LENGTH(path);
665
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200666 len = PyUnicode_GET_LENGTH(self->archive);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100667 path_start = 0;
668 if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
669 && PyUnicode_READ_CHAR(path, len) == SEP) {
670 path_start = len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000672
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100673 key = PyUnicode_Substring(path, path_start, path_len);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000674 if (key == NULL)
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100675 goto error;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000676 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (toc_entry == NULL) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300678 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, key);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000679 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100680 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000682 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100683 Py_DECREF(path);
Benjamin Peterson34c15402014-02-16 14:17:28 -0500684 return get_data(self->archive, toc_entry);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100685 error:
686 Py_DECREF(path);
687 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000688}
689
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400690/*[clinic input]
691zipimport.zipimporter.get_code
692
693 fullname: unicode
694 /
695
696Return the code object for the specified module.
697
698Raise ZipImportError if the module couldn't be found.
699
700[clinic start generated code]*/
701
Just van Rossum52e14d62002-12-30 22:08:05 +0000702static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400703zipimport_zipimporter_get_code_impl(ZipImporter *self, PyObject *fullname)
704/*[clinic end generated code: output=b923c37fa99cbac4 input=2761412bc37f3549]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000707}
708
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400709/*[clinic input]
710zipimport.zipimporter.get_source
Just van Rossum52e14d62002-12-30 22:08:05 +0000711
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400712 fullname: unicode
713 /
714
715Return the source code for the specified module.
716
717Raise ZipImportError if the module couldn't be found, return None if the
718archive does contain the module, but has no source for it.
719
720[clinic start generated code]*/
721
722static PyObject *
723zipimport_zipimporter_get_source_impl(ZipImporter *self, PyObject *fullname)
724/*[clinic end generated code: output=bc059301b0c33729 input=4e4b186f2e690716]*/
725{
726 PyObject *toc_entry;
727 PyObject *subname, *path, *fullpath;
728 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000731 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000733 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400734 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000735 return NULL;
736 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400737
Victor Stinner965a8a12010-10-18 21:44:33 +0000738 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400739 if (subname == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000741
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400742 path = make_filename(self->prefix, subname);
743 Py_DECREF(subname);
744 if (path == NULL)
745 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000746
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400747 if (mi == MI_PACKAGE)
748 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
749 else
750 fullpath = PyUnicode_FromFormat("%U.py", path);
751 Py_DECREF(path);
752 if (fullpath == NULL)
753 return NULL;
754
755 toc_entry = PyDict_GetItem(self->files, fullpath);
756 Py_DECREF(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000758 PyObject *res, *bytes;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500759 bytes = get_data(self->archive, toc_entry);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000760 if (bytes == NULL)
761 return NULL;
762 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
763 PyBytes_GET_SIZE(bytes));
764 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 return res;
766 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 /* we have the module, but no source */
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200769 Py_RETURN_NONE;
Just van Rossum52e14d62002-12-30 22:08:05 +0000770}
771
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000772
Just van Rossum52e14d62002-12-30 22:08:05 +0000773static PyMethodDef zipimporter_methods[] = {
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400774 ZIPIMPORT_ZIPIMPORTER_FIND_MODULE_METHODDEF
775 ZIPIMPORT_ZIPIMPORTER_FIND_LOADER_METHODDEF
776 ZIPIMPORT_ZIPIMPORTER_LOAD_MODULE_METHODDEF
777 ZIPIMPORT_ZIPIMPORTER_GET_FILENAME_METHODDEF
778 ZIPIMPORT_ZIPIMPORTER_IS_PACKAGE_METHODDEF
779 ZIPIMPORT_ZIPIMPORTER_GET_DATA_METHODDEF
780 ZIPIMPORT_ZIPIMPORTER_GET_CODE_METHODDEF
781 ZIPIMPORT_ZIPIMPORTER_GET_SOURCE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000783};
784
785static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
787 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
788 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
789 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000790};
791
Just van Rossum52e14d62002-12-30 22:08:05 +0000792#define DEFERRED_ADDRESS(ADDR) 0
793
794static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
796 "zipimport.zipimporter",
797 sizeof(ZipImporter),
798 0, /* tp_itemsize */
799 (destructor)zipimporter_dealloc, /* tp_dealloc */
800 0, /* tp_print */
801 0, /* tp_getattr */
802 0, /* tp_setattr */
803 0, /* tp_reserved */
804 (reprfunc)zipimporter_repr, /* tp_repr */
805 0, /* tp_as_number */
806 0, /* tp_as_sequence */
807 0, /* tp_as_mapping */
808 0, /* tp_hash */
809 0, /* tp_call */
810 0, /* tp_str */
811 PyObject_GenericGetAttr, /* tp_getattro */
812 0, /* tp_setattro */
813 0, /* tp_as_buffer */
814 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
815 Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400816 zipimport_zipimporter___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 zipimporter_traverse, /* tp_traverse */
818 0, /* tp_clear */
819 0, /* tp_richcompare */
820 0, /* tp_weaklistoffset */
821 0, /* tp_iter */
822 0, /* tp_iternext */
823 zipimporter_methods, /* tp_methods */
824 zipimporter_members, /* tp_members */
825 0, /* tp_getset */
826 0, /* tp_base */
827 0, /* tp_dict */
828 0, /* tp_descr_get */
829 0, /* tp_descr_set */
830 0, /* tp_dictoffset */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400831 (initproc)zipimport_zipimporter___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyType_GenericAlloc, /* tp_alloc */
833 PyType_GenericNew, /* tp_new */
834 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000835};
836
837
838/* implementation */
839
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200840/* Given a buffer, return the unsigned int that is represented by the first
Just van Rossum52e14d62002-12-30 22:08:05 +0000841 4 bytes, encoded as little endian. This partially reimplements
842 marshal.c:r_long() */
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200843static unsigned int
844get_uint32(const unsigned char *buf)
845{
846 unsigned int x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 x = buf[0];
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200848 x |= (unsigned int)buf[1] << 8;
849 x |= (unsigned int)buf[2] << 16;
850 x |= (unsigned int)buf[3] << 24;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000852}
853
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200854/* Given a buffer, return the unsigned int that is represented by the first
855 2 bytes, encoded as little endian. This partially reimplements
856 marshal.c:r_short() */
857static unsigned short
858get_uint16(const unsigned char *buf)
859{
860 unsigned short x;
861 x = buf[0];
862 x |= (unsigned short)buf[1] << 8;
863 return x;
864}
865
866static void
867set_file_error(PyObject *archive, int eof)
868{
869 if (eof) {
870 PyErr_SetString(PyExc_EOFError, "EOF read where not expected");
871 }
872 else {
873 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, archive);
874 }
875}
876
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800877/*
Benjamin Peterson34c15402014-02-16 14:17:28 -0500878 read_directory(archive) -> files dict (new reference)
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800879
Benjamin Peterson34c15402014-02-16 14:17:28 -0500880 Given a path to a Zip archive, build a dict, mapping file names
Just van Rossum52e14d62002-12-30 22:08:05 +0000881 (local to the archive, using SEP as a separator) to toc entries.
882
883 A toc_entry is a tuple:
884
Victor Stinner08654e12010-10-18 12:09:02 +0000885 (__file__, # value to use for __file__, available for all files,
886 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 compress, # compression kind; 0 for uncompressed
888 data_size, # size of compressed data on disk
889 file_size, # size of decompressed data
890 file_offset, # offset of file header from start of archive
891 time, # mod time of file (in dos format)
892 date, # mod data of file (in dos format)
893 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000894 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000895
896 Directories can be recognized by the trailing SEP in the name,
897 data_size and file_offset are 0.
898*/
899static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -0500900read_directory(PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +0000901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyObject *files = NULL;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500903 FILE *fp;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200904 unsigned short flags, compress, time, date, name_size;
905 unsigned int crc, data_size, file_size, header_size, header_offset;
906 unsigned long file_offset, header_position;
907 unsigned long arc_offset; /* Absolute offset to start of the zip-archive. */
908 unsigned int count, i;
909 unsigned char buffer[46];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000911 PyObject *nameobj = NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100912 PyObject *path;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000913 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000914 int bootstrap;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200915 const char *errmsg = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000916
Benjamin Peterson34c15402014-02-16 14:17:28 -0500917 fp = _Py_fopen_obj(archive, "rb");
918 if (fp == NULL) {
Victor Stinnerfbd6f9e2015-03-20 10:52:25 +0100919 if (PyErr_ExceptionMatches(PyExc_OSError)) {
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300920 _PyErr_FormatFromCause(ZipImportError,
921 "can't open Zip file: %R", archive);
Victor Stinnerfbd6f9e2015-03-20 10:52:25 +0100922 }
Benjamin Peterson34c15402014-02-16 14:17:28 -0500923 return NULL;
924 }
925
Jesus Cea09bf7a72012-10-03 02:13:05 +0200926 if (fseek(fp, -22, SEEK_END) == -1) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200927 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +0200928 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200929 header_position = (unsigned long)ftell(fp);
930 if (header_position == (unsigned long)-1) {
931 goto file_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200933 assert(header_position <= (unsigned long)LONG_MAX);
934 if (fread(buffer, 1, 22, fp) != 22) {
935 goto file_error;
936 }
937 if (get_uint32(buffer) != 0x06054B50u) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 /* Bad: End of Central Dir signature */
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200939 errmsg = "not a Zip file";
940 goto invalid_header;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000942
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200943 header_size = get_uint32(buffer + 12);
944 header_offset = get_uint32(buffer + 16);
945 if (header_position < header_size) {
946 errmsg = "bad central directory size";
947 goto invalid_header;
948 }
949 if (header_position < header_offset) {
950 errmsg = "bad central directory offset";
951 goto invalid_header;
952 }
953 if (header_position - header_size < header_offset) {
954 errmsg = "bad central directory size or offset";
955 goto invalid_header;
956 }
957 header_position -= header_size;
958 arc_offset = header_position - header_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 files = PyDict_New();
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200961 if (files == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200963 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 /* Start of Central Directory */
965 count = 0;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200966 if (fseek(fp, (long)header_position, 0) == -1) {
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +0200967 goto file_error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200968 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 for (;;) {
970 PyObject *t;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200971 size_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000973
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200974 n = fread(buffer, 1, 46, fp);
975 if (n < 4) {
976 goto eof_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200978 /* Start of file header */
979 if (get_uint32(buffer) != 0x02014B50u) {
980 break; /* Bad: Central Dir File Header */
981 }
982 if (n != 46) {
983 goto eof_error;
984 }
985 flags = get_uint16(buffer + 8);
986 compress = get_uint16(buffer + 10);
987 time = get_uint16(buffer + 12);
988 date = get_uint16(buffer + 14);
989 crc = get_uint32(buffer + 16);
990 data_size = get_uint32(buffer + 20);
991 file_size = get_uint32(buffer + 24);
992 name_size = get_uint16(buffer + 28);
993 header_size = (unsigned int)name_size +
994 get_uint16(buffer + 30) /* extra field */ +
995 get_uint16(buffer + 32) /* comment */;
996
997 file_offset = get_uint32(buffer + 42);
998 if (file_offset > header_offset) {
999 errmsg = "bad local header offset";
1000 goto invalid_header;
1001 }
1002 file_offset += arc_offset;
1003
1004 if (name_size > MAXPATHLEN) {
1005 name_size = MAXPATHLEN;
1006 }
1007 if (fread(name, 1, name_size, fp) != name_size) {
1008 goto file_error;
1009 }
1010 name[name_size] = '\0'; /* Add terminating null byte */
Victor Stinner44d9bea2016-12-05 17:55:36 +01001011#if SEP != '/'
1012 for (i = 0; i < name_size; i++) {
1013 if (name[i] == '/') {
1014 name[i] = SEP;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001015 }
1016 }
Victor Stinner44d9bea2016-12-05 17:55:36 +01001017#endif
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001018 /* Skip the rest of the header.
1019 * On Windows, calling fseek to skip over the fields we don't use is
1020 * slower than reading the data because fseek flushes stdio's
1021 * internal buffers. See issue #8745. */
1022 assert(header_size <= 3*0xFFFFu);
1023 for (i = name_size; i < header_size; i++) {
1024 if (getc(fp) == EOF) {
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001025 goto file_error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001026 }
1027 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001028
Victor Stinner4ee65a92011-01-22 10:30:29 +00001029 bootstrap = 0;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001030 if (flags & 0x0800) {
Victor Stinnerd36c8212010-10-18 12:13:46 +00001031 charset = "utf-8";
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001032 }
Victor Stinner4ee65a92011-01-22 10:30:29 +00001033 else if (!PyThreadState_GET()->interp->codecs_initialized) {
1034 /* During bootstrap, we may need to load the encodings
1035 package from a ZIP file. But the cp437 encoding is implemented
1036 in Python in the encodings package.
1037
1038 Break out of this dependency by assuming that the path to
1039 the encodings module is ASCII-only. */
1040 charset = "ascii";
1041 bootstrap = 1;
1042 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001043 else {
Victor Stinnerd36c8212010-10-18 12:13:46 +00001044 charset = "cp437";
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001045 }
Victor Stinnerd36c8212010-10-18 12:13:46 +00001046 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner4ee65a92011-01-22 10:30:29 +00001047 if (nameobj == NULL) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001048 if (bootstrap) {
Victor Stinner4ee65a92011-01-22 10:30:29 +00001049 PyErr_Format(PyExc_NotImplementedError,
1050 "bootstrap issue: python%i%i.zip contains non-ASCII "
1051 "filenames without the unicode flag",
1052 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001053 }
Victor Stinner2460a432010-08-16 17:54:28 +00001054 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +00001055 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001056 if (PyUnicode_READY(nameobj) == -1) {
Stefan Krah000fde92012-08-20 14:14:49 +02001057 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001058 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +01001059 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001060 if (path == NULL) {
Victor Stinner2460a432010-08-16 17:54:28 +00001061 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001062 }
1063 t = Py_BuildValue("NHIIkHHI", path, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 file_size, file_offset, time, date, crc);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001065 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001067 }
Victor Stinner2460a432010-08-16 17:54:28 +00001068 err = PyDict_SetItem(files, nameobj, t);
1069 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 Py_DECREF(t);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001071 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001073 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 count++;
1075 }
Benjamin Peterson34c15402014-02-16 14:17:28 -05001076 fclose(fp);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001077 if (Py_VerboseFlag) {
1078 PySys_FormatStderr("# zipimport: found %u names in %R\n",
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001079 count, archive);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001080 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 return files;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001082
1083eof_error:
1084 set_file_error(archive, !ferror(fp));
1085 goto error;
1086
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001087file_error:
Jesus Cea09bf7a72012-10-03 02:13:05 +02001088 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001089 goto error;
1090
1091invalid_header:
1092 assert(errmsg != NULL);
1093 PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
1094 goto error;
1095
Just van Rossum52e14d62002-12-30 22:08:05 +00001096error:
Benjamin Peterson34c15402014-02-16 14:17:28 -05001097 fclose(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +00001099 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001101}
1102
1103/* Return the zlib.decompress function object, or NULL if zlib couldn't
1104 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +02001105 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001106static PyObject *
1107get_decompress_func(void)
1108{
Victor Stinner4925cde2011-05-20 00:16:09 +02001109 static int importing_zlib = 0;
1110 PyObject *zlib;
1111 PyObject *decompress;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001112 _Py_IDENTIFIER(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001113
Victor Stinner4925cde2011-05-20 00:16:09 +02001114 if (importing_zlib != 0)
Xiang Zhang0710d752017-03-11 13:02:52 +08001115 /* Someone has a zlib.pyc in their Zip file;
Victor Stinner4925cde2011-05-20 00:16:09 +02001116 let's avoid a stack overflow. */
1117 return NULL;
1118 importing_zlib = 1;
1119 zlib = PyImport_ImportModuleNoBlock("zlib");
1120 importing_zlib = 0;
1121 if (zlib != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001122 decompress = _PyObject_GetAttrId(zlib,
1123 &PyId_decompress);
Victor Stinner4925cde2011-05-20 00:16:09 +02001124 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 }
Victor Stinner4925cde2011-05-20 00:16:09 +02001126 else {
1127 PyErr_Clear();
1128 decompress = NULL;
1129 }
1130 if (Py_VerboseFlag)
1131 PySys_WriteStderr("# zipimport: zlib %s\n",
1132 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +00001134}
1135
Benjamin Peterson34c15402014-02-16 14:17:28 -05001136/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
Just van Rossum52e14d62002-12-30 22:08:05 +00001137 data as a new reference. */
1138static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -05001139get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001140{
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001141 PyObject *raw_data = NULL, *data, *decompress;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 char *buf;
Benjamin Peterson34c15402014-02-16 14:17:28 -05001143 FILE *fp;
Victor Stinner60fe8d92010-08-16 23:48:11 +00001144 PyObject *datapath;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001145 unsigned short compress, time, date;
1146 unsigned int crc;
1147 Py_ssize_t data_size, file_size, bytes_size;
1148 long file_offset, header_size;
1149 unsigned char buffer[30];
1150 const char *errmsg = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001151
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001152 if (!PyArg_ParseTuple(toc_entry, "OHnnlHHI", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 &data_size, &file_size, &file_offset, &time,
1154 &date, &crc)) {
1155 return NULL;
1156 }
Benjamin Petersonb1db7582016-01-21 22:02:46 -08001157 if (data_size < 0) {
1158 PyErr_Format(ZipImportError, "negative data size");
1159 return NULL;
1160 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001161
Benjamin Peterson34c15402014-02-16 14:17:28 -05001162 fp = _Py_fopen_obj(archive, "rb");
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001163 if (!fp) {
Benjamin Peterson34c15402014-02-16 14:17:28 -05001164 return NULL;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001165 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* Check to make sure the local file header is correct */
Jesus Cea09bf7a72012-10-03 02:13:05 +02001167 if (fseek(fp, file_offset, 0) == -1) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001168 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001169 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001170 if (fread(buffer, 1, 30, fp) != 30) {
1171 goto eof_error;
1172 }
1173 if (get_uint32(buffer) != 0x04034B50u) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 /* Bad: Local File Header */
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001175 errmsg = "bad local file header";
1176 goto invalid_header;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001177 }
1178
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001179 header_size = (unsigned int)30 +
1180 get_uint16(buffer + 26) /* file name */ +
1181 get_uint16(buffer + 28) /* extra field */;
1182 if (file_offset > LONG_MAX - header_size) {
1183 errmsg = "bad local file header size";
1184 goto invalid_header;
Victor Stinner73660af2013-10-29 01:43:44 +01001185 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001186 file_offset += header_size; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +00001187
Benjamin Petersonc4032da2016-01-20 22:23:44 -08001188 if (data_size > LONG_MAX - 1) {
1189 fclose(fp);
1190 PyErr_NoMemory();
1191 return NULL;
1192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 bytes_size = compress == 0 ? data_size : data_size + 1;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001194 if (bytes_size == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 bytes_size++;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001196 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 if (raw_data == NULL) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001199 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 }
1201 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001202
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001203 if (fseek(fp, file_offset, 0) == -1) {
1204 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001205 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001206 if (fread(buf, 1, data_size, fp) != (size_t)data_size) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001207 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 "zipimport: can't read data");
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001209 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001211
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001212 fclose(fp);
1213 fp = NULL;
1214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (compress != 0) {
1216 buf[data_size] = 'Z'; /* saw this in zipfile.py */
1217 data_size++;
1218 }
1219 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (compress == 0) { /* data is not compressed */
1222 data = PyBytes_FromStringAndSize(buf, data_size);
1223 Py_DECREF(raw_data);
1224 return data;
1225 }
1226
1227 /* Decompress with zlib */
1228 decompress = get_decompress_func();
1229 if (decompress == NULL) {
1230 PyErr_SetString(ZipImportError,
1231 "can't decompress data; "
1232 "zlib not available");
1233 goto error;
1234 }
1235 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +02001236 Py_DECREF(decompress);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 Py_DECREF(raw_data);
1238 return data;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001239
1240eof_error:
1241 set_file_error(archive, !ferror(fp));
1242 goto error;
1243
1244file_error:
1245 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1246 goto error;
1247
1248invalid_header:
1249 assert(errmsg != NULL);
1250 PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
1251 goto error;
1252
1253error:
1254 if (fp != NULL) {
1255 fclose(fp);
1256 }
1257 Py_XDECREF(raw_data);
1258 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001259}
1260
1261/* Lenient date/time comparison function. The precision of the mtime
1262 in the archive is lower than the mtime stored in a .pyc: we
1263 must allow a difference of at most one second. */
1264static int
1265eq_mtime(time_t t1, time_t t2)
1266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 time_t d = t1 - t2;
1268 if (d < 0)
1269 d = -d;
1270 /* dostime only stores even seconds, so be lenient */
1271 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001272}
1273
Xiang Zhang0710d752017-03-11 13:02:52 +08001274/* Given the contents of a .pyc file in a buffer, unmarshal the data
Just van Rossum52e14d62002-12-30 22:08:05 +00001275 and return the code object. Return None if it the magic word doesn't
1276 match (we do this instead of raising an exception as we fall back
1277 to .py if available and we don't want to mask other errors).
1278 Returns a new reference. */
1279static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001280unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 PyObject *code;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001283 unsigned char *buf = (unsigned char *)PyBytes_AsString(data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001285
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001286 if (size < 12) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 PyErr_SetString(ZipImportError,
1288 "bad pyc data");
1289 return NULL;
1290 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001291
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001292 if (get_uint32(buf) != (unsigned int)PyImport_GetMagicNumber()) {
1293 if (Py_VerboseFlag) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001294 PySys_FormatStderr("# %R has bad magic\n",
1295 pathname);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001296 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001297 Py_RETURN_NONE; /* signal caller to try alternative */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001299
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001300 if (mtime != 0 && !eq_mtime(get_uint32(buf + 4), mtime)) {
1301 if (Py_VerboseFlag) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001302 PySys_FormatStderr("# %R has bad mtime\n",
1303 pathname);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001304 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001305 Py_RETURN_NONE; /* signal caller to try alternative */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001307
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001308 /* XXX the pyc's size field is ignored; timestamp collisions are probably
1309 unimportant with zip files. */
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001310 code = PyMarshal_ReadObjectFromString((char *)buf + 12, size - 12);
1311 if (code == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 return NULL;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (!PyCode_Check(code)) {
1315 Py_DECREF(code);
1316 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001317 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 pathname);
1319 return NULL;
1320 }
1321 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001322}
1323
Martin Panter0be894b2016-09-07 12:03:06 +00001324/* Replace any occurrences of "\r\n?" in the input string with "\n".
Just van Rossum52e14d62002-12-30 22:08:05 +00001325 This converts DOS and Mac line endings to Unix line endings.
1326 Also append a trailing "\n" to be compatible with
1327 PyParser_SimpleParseFile(). Returns a new reference. */
1328static PyObject *
1329normalize_line_endings(PyObject *source)
1330{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001331 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 PyObject *fixed_source;
1333 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001334
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001335 p = PyBytes_AsString(source);
1336 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 return PyBytes_FromStringAndSize("\n\0", 2);
1338 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 /* one char extra for trailing \n and one for terminating \0 */
1341 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1342 if (buf == NULL) {
1343 PyErr_SetString(PyExc_MemoryError,
1344 "zipimport: no memory to allocate "
1345 "source buffer");
1346 return NULL;
1347 }
1348 /* replace "\r\n?" by "\n" */
1349 for (q = buf; *p != '\0'; p++) {
1350 if (*p == '\r') {
1351 *q++ = '\n';
1352 if (*(p + 1) == '\n')
1353 p++;
1354 }
1355 else
1356 *q++ = *p;
1357 len++;
1358 }
1359 *q++ = '\n'; /* add trailing \n */
1360 *q = '\0';
1361 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1362 PyMem_Free(buf);
1363 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001364}
1365
1366/* Given a string buffer containing Python source code, compile it
Brett Cannon83358c92013-06-20 21:30:32 -04001367 and return a code object as a new reference. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001368static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001369compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001370{
Steve Dower8dcc48e2016-09-09 17:27:33 -07001371 PyObject *code, *fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001372
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001373 fixed_source = normalize_line_endings(source);
1374 if (fixed_source == NULL) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001375 return NULL;
1376 }
1377
Steve Dower8dcc48e2016-09-09 17:27:33 -07001378 code = Py_CompileStringObject(PyBytes_AsString(fixed_source),
Berker Peksag4aa74c42016-09-14 08:09:48 +03001379 pathname, Py_file_input, NULL, -1);
Steve Dower8dcc48e2016-09-09 17:27:33 -07001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 Py_DECREF(fixed_source);
1382 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001383}
1384
1385/* Convert the date/time values found in the Zip archive to a value
1386 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001387static time_t
1388parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 stm.tm_sec = (dostime & 0x1f) * 2;
1395 stm.tm_min = (dostime >> 5) & 0x3f;
1396 stm.tm_hour = (dostime >> 11) & 0x1f;
1397 stm.tm_mday = dosdate & 0x1f;
1398 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1399 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1400 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001403}
1404
Brett Cannonf299abd2015-04-13 14:21:02 -04001405/* Given a path to a .pyc file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001406 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001407 is available. */
1408static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001409get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001410{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001411 PyObject *toc_entry, *stripped;
1412 time_t mtime;
1413
Xiang Zhang0710d752017-03-11 13:02:52 +08001414 /* strip 'c' from *.pyc */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001415 if (PyUnicode_READY(path) == -1)
1416 return (time_t)-1;
1417 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
1418 PyUnicode_DATA(path),
1419 PyUnicode_GET_LENGTH(path) - 1);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001420 if (stripped == NULL)
1421 return (time_t)-1;
1422
1423 toc_entry = PyDict_GetItem(self->files, stripped);
1424 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1426 PyTuple_Size(toc_entry) == 8) {
1427 /* fetch the time stamp of the .py file for comparison
1428 with an embedded pyc time stamp */
1429 int time, date;
1430 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1431 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1432 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001433 } else
1434 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001436}
1437
1438/* Return the code object for the module named by 'fullname' from the
1439 Zip archive as a new reference. */
1440static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -05001441get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001443{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001444 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001445
Benjamin Peterson34c15402014-02-16 14:17:28 -05001446 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (data == NULL)
1448 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001449
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001450 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001451 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001452 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001453 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001454 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 Py_DECREF(data);
1456 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001457}
1458
Ezio Melotti42da6632011-03-15 05:18:48 +02001459/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001460 'fullname'. */
1461static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001462get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001463 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001464{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001465 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001466 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001470 if (subname == NULL)
1471 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001472
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001473 path = make_filename(self->prefix, subname);
1474 Py_DECREF(subname);
1475 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001479 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001480
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001481 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1482 if (fullpath == NULL)
1483 goto exit;
1484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001486 PySys_FormatStderr("# trying %U%c%U\n",
1487 self->archive, (int)SEP, fullpath);
1488 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if (toc_entry != NULL) {
1490 time_t mtime = 0;
1491 int ispackage = zso->type & IS_PACKAGE;
1492 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001493
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001494 if (isbytecode) {
1495 mtime = get_mtime_of_source(self, fullpath);
1496 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1497 goto exit;
1498 }
1499 }
1500 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (p_ispackage != NULL)
1502 *p_ispackage = ispackage;
Benjamin Peterson34c15402014-02-16 14:17:28 -05001503 code = get_code_from_data(self, ispackage,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 isbytecode, mtime,
1505 toc_entry);
1506 if (code == Py_None) {
1507 /* bad magic number or non-matching mtime
1508 in byte code, try next */
1509 Py_DECREF(code);
1510 continue;
1511 }
Victor Stinner08654e12010-10-18 12:09:02 +00001512 if (code != NULL && p_modpath != NULL) {
1513 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1514 Py_INCREF(*p_modpath);
1515 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001516 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001518 else
1519 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001521 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1522exit:
1523 Py_DECREF(path);
1524 Py_XDECREF(fullpath);
1525 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001526}
1527
1528
1529/* Module init */
1530
1531PyDoc_STRVAR(zipimport_doc,
1532"zipimport provides support for importing Python modules from Zip archives.\n\
1533\n\
1534This module exports three objects:\n\
1535- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001536- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001537 subclass of ImportError, so it can be caught as ImportError, too.\n\
1538- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1539 info dicts, as used in zipimporter._files.\n\
1540\n\
1541It is usually not needed to use the zipimport module explicitly; it is\n\
1542used by the builtin import mechanism for sys.path items that are paths\n\
1543to Zip archives.");
1544
Martin v. Löwis1a214512008-06-11 05:26:20 +00001545static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 PyModuleDef_HEAD_INIT,
1547 "zipimport",
1548 zipimport_doc,
1549 -1,
1550 NULL,
1551 NULL,
1552 NULL,
1553 NULL,
1554 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001555};
1556
Just van Rossum52e14d62002-12-30 22:08:05 +00001557PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001558PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (PyType_Ready(&ZipImporter_Type) < 0)
1563 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 /* Correct directory separator */
1566 zip_searchorder[0].suffix[0] = SEP;
1567 zip_searchorder[1].suffix[0] = SEP;
Just van Rossum52e14d62002-12-30 22:08:05 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 mod = PyModule_Create(&zipimportmodule);
1570 if (mod == NULL)
1571 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1574 PyExc_ImportError, NULL);
1575 if (ZipImportError == NULL)
1576 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 Py_INCREF(ZipImportError);
1579 if (PyModule_AddObject(mod, "ZipImportError",
1580 ZipImportError) < 0)
1581 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 Py_INCREF(&ZipImporter_Type);
1584 if (PyModule_AddObject(mod, "zipimporter",
1585 (PyObject *)&ZipImporter_Type) < 0)
1586 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 zip_directory_cache = PyDict_New();
1589 if (zip_directory_cache == NULL)
1590 return NULL;
1591 Py_INCREF(zip_directory_cache);
1592 if (PyModule_AddObject(mod, "_zip_directory_cache",
1593 zip_directory_cache) < 0)
1594 return NULL;
1595 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001596}