blob: 141ada5adc545f559c328f538189a0df1bb4e3ef [file] [log] [blame]
Just van Rossum52e14d62002-12-30 22:08:05 +00001#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002#include "internal/pystate.h"
Just van Rossum52e14d62002-12-30 22:08:05 +00003#include "structmember.h"
4#include "osdefs.h"
5#include "marshal.h"
Just van Rossum52e14d62002-12-30 22:08:05 +00006#include <time.h>
7
8
9#define IS_SOURCE 0x0
10#define IS_BYTECODE 0x1
11#define IS_PACKAGE 0x2
12
13struct st_zip_searchorder {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 char suffix[14];
15 int type;
Just van Rossum52e14d62002-12-30 22:08:05 +000016};
17
Victor Stinner651f9f72013-11-12 21:44:18 +010018#ifdef ALTSEP
19_Py_IDENTIFIER(replace);
20#endif
21
Just van Rossum52e14d62002-12-30 22:08:05 +000022/* zip_searchorder defines how we search for a module in the Zip
23 archive: we first search for a package __init__, then for
Brett Cannonf299abd2015-04-13 14:21:02 -040024 non-package .pyc, and .py entries. The .pyc entries
Just van Rossum52e14d62002-12-30 22:08:05 +000025 are swapped by initzipimport() if we run in optimized mode. Also,
26 '/' is replaced by SEP there. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +000027static struct st_zip_searchorder zip_searchorder[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 {"/__init__.py", IS_PACKAGE | IS_SOURCE},
30 {".pyc", IS_BYTECODE},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 {".py", IS_SOURCE},
32 {"", 0}
Just van Rossum52e14d62002-12-30 22:08:05 +000033};
34
35/* zipimporter object definition and support */
36
37typedef struct _zipimporter ZipImporter;
38
39struct _zipimporter {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 PyObject_HEAD
Victor Stinner9e40fad2010-10-18 22:34:46 +000041 PyObject *archive; /* pathname of the Zip archive,
42 decoded from the filesystem encoding */
Victor Stinner72f767e2010-10-18 11:44:21 +000043 PyObject *prefix; /* file prefix: "a/sub/directory/",
44 encoded to the filesystem encoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 PyObject *files; /* dict with file info {path: toc_entry} */
Just van Rossum52e14d62002-12-30 22:08:05 +000046};
47
Just van Rossum52e14d62002-12-30 22:08:05 +000048static PyObject *ZipImportError;
Victor Stinnerc342fca2010-10-18 11:39:05 +000049/* read_directory() cache */
Just van Rossum52e14d62002-12-30 22:08:05 +000050static PyObject *zip_directory_cache = NULL;
51
52/* forward decls */
Benjamin Peterson34c15402014-02-16 14:17:28 -050053static PyObject *read_directory(PyObject *archive);
54static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
Victor Stinnerf6b563a2011-03-14 20:46:50 -040055static PyObject *get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +000056 int *p_ispackage, PyObject **p_modpath);
Just van Rossum52e14d62002-12-30 22:08:05 +000057
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040058static PyTypeObject ZipImporter_Type;
Just van Rossum52e14d62002-12-30 22:08:05 +000059
60#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
61
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040062/*[clinic input]
63module zipimport
64class zipimport.zipimporter "ZipImporter *" "&ZipImporter_Type"
65[clinic start generated code]*/
66/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9db8b61557d911e7]*/
67#include "clinic/zipimport.c.h"
68
Just van Rossum52e14d62002-12-30 22:08:05 +000069
70/* zipimporter.__init__
71 Split the "subdirectory" from the Zip archive path, lookup a matching
72 entry in sys.path_importer_cache, fetch the file directory from there
73 if found, or else read it from the archive. */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040074
75/*[clinic input]
76zipimport.zipimporter.__init__
77
78 archivepath as path: object(converter="PyUnicode_FSDecoder")
79 A path-like object to a zipfile, or to a specific path inside
80 a zipfile.
81 /
82
83Create a new zipimporter instance.
84
85'archivepath' must be a path-like object to a zipfile, or to a specific path
86inside a zipfile. For example, it can be '/tmp/myimport.zip', or
87'/tmp/myimport.zip/mydirectory', if mydirectory is a valid directory inside
88the archive.
89
90'ZipImportError' is raised if 'archivepath' doesn't point to a valid Zip
91archive.
92
93The 'archive' attribute of the zipimporter object contains the name of the
94zipfile targeted.
95
96[clinic start generated code]*/
97
Just van Rossum52e14d62002-12-30 22:08:05 +000098static int
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -040099zipimport_zipimporter___init___impl(ZipImporter *self, PyObject *path)
100/*[clinic end generated code: output=141558fefdb46dc8 input=92b9ebeed1f6a704]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000101{
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400102 PyObject *files, *tmp;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100103 PyObject *filename = NULL;
104 Py_ssize_t len, flen;
Just van Rossum52e14d62002-12-30 22:08:05 +0000105
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100106 if (PyUnicode_READY(path) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200107 return -1;
108
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100109 len = PyUnicode_GET_LENGTH(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 if (len == 0) {
111 PyErr_SetString(ZipImportError, "archive path is empty");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000112 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000114
115#ifdef ALTSEP
Martin v. Löwiscfa61292011-10-31 09:01:22 +0100116 tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100117 if (!tmp)
118 goto error;
119 Py_DECREF(path);
120 path = tmp;
Just van Rossum52e14d62002-12-30 22:08:05 +0000121#endif
122
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100123 filename = path;
124 Py_INCREF(filename);
125 flen = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 for (;;) {
127 struct stat statbuf;
128 int rv;
Just van Rossum52e14d62002-12-30 22:08:05 +0000129
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100130 rv = _Py_stat(filename, &statbuf);
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100131 if (rv == -2)
132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 if (rv == 0) {
134 /* it exists */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100135 if (!S_ISREG(statbuf.st_mode))
136 /* it's a not file */
137 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 break;
139 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100140 Py_CLEAR(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 /* back up one path element */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100142 flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);
143 if (flen == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 break;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100145 filename = PyUnicode_Substring(path, 0, flen);
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100146 if (filename == NULL)
147 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100149 if (filename == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 PyErr_SetString(ZipImportError, "not a Zip file");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000151 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000153
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100154 if (PyUnicode_READY(filename) < 0)
155 goto error;
156
157 files = PyDict_GetItem(zip_directory_cache, filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000158 if (files == NULL) {
Benjamin Peterson34c15402014-02-16 14:17:28 -0500159 files = read_directory(filename);
160 if (files == NULL)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000161 goto error;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500162 if (PyDict_SetItem(zip_directory_cache, filename, files) != 0)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000163 goto error;
164 }
165 else
166 Py_INCREF(files);
167 self->files = files;
168
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100169 /* Transfer reference */
170 self->archive = filename;
171 filename = NULL;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000172
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100173 /* Check if there is a prefix directory following the filename. */
174 if (flen != len) {
175 tmp = PyUnicode_Substring(path, flen+1,
176 PyUnicode_GET_LENGTH(path));
177 if (tmp == NULL)
178 goto error;
179 self->prefix = tmp;
180 if (PyUnicode_READ_CHAR(path, len-1) != SEP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 /* add trailing SEP */
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100182 tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
183 if (tmp == NULL)
184 goto error;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300185 Py_SETREF(self->prefix, tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 }
187 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000188 else
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100189 self->prefix = PyUnicode_New(0, 0);
190 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 return 0;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000192
193error:
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100194 Py_DECREF(path);
195 Py_XDECREF(filename);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000196 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000197}
198
199/* GC support. */
200static int
201zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 ZipImporter *self = (ZipImporter *)obj;
204 Py_VISIT(self->files);
205 return 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000206}
207
208static void
209zipimporter_dealloc(ZipImporter *self)
210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 PyObject_GC_UnTrack(self);
212 Py_XDECREF(self->archive);
213 Py_XDECREF(self->prefix);
214 Py_XDECREF(self->files);
215 Py_TYPE(self)->tp_free((PyObject *)self);
Just van Rossum52e14d62002-12-30 22:08:05 +0000216}
217
218static PyObject *
219zipimporter_repr(ZipImporter *self)
220{
Victor Stinner028dd972010-08-17 00:04:48 +0000221 if (self->archive == NULL)
222 return PyUnicode_FromString("<zipimporter object \"???\">");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200223 else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0)
Victor Stinner07298a12010-10-18 22:45:54 +0000224 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000225 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 else
Victor Stinner07298a12010-10-18 22:45:54 +0000227 return PyUnicode_FromFormat("<zipimporter object \"%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000228 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000229}
230
231/* return fullname.split(".")[-1] */
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400232static PyObject *
233get_subname(PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000234{
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100235 Py_ssize_t len, dot;
236 if (PyUnicode_READY(fullname) < 0)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200237 return NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100238 len = PyUnicode_GET_LENGTH(fullname);
239 dot = PyUnicode_FindChar(fullname, '.', 0, len, -1);
240 if (dot == -1) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400241 Py_INCREF(fullname);
242 return fullname;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100243 } else
244 return PyUnicode_Substring(fullname, dot+1, len);
Just van Rossum52e14d62002-12-30 22:08:05 +0000245}
246
247/* Given a (sub)modulename, write the potential file path in the
248 archive (without extension) to the path buffer. Return the
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400249 length of the resulting string.
250
251 return self.prefix + name.replace('.', os.sep) */
252static PyObject*
253make_filename(PyObject *prefix, PyObject *name)
Just van Rossum52e14d62002-12-30 22:08:05 +0000254{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400255 PyObject *pathobj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200256 Py_UCS4 *p, *buf;
257 Py_ssize_t len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000258
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200259 len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200260 p = buf = PyMem_New(Py_UCS4, len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200261 if (buf == NULL) {
262 PyErr_NoMemory();
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400263 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200264 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000265
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200266 if (!PyUnicode_AsUCS4(prefix, p, len, 0)) {
267 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200268 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200269 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200270 p += PyUnicode_GET_LENGTH(prefix);
271 len -= PyUnicode_GET_LENGTH(prefix);
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200272 if (!PyUnicode_AsUCS4(name, p, len, 1)) {
273 PyMem_Free(buf);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200274 return NULL;
Christian Heimes1b5c76a2012-09-10 02:00:34 +0200275 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400276 for (; *p; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 if (*p == '.')
278 *p = SEP;
279 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200280 pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
281 buf, p-buf);
282 PyMem_Free(buf);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400283 return pathobj;
Just van Rossum52e14d62002-12-30 22:08:05 +0000284}
285
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000286enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 MI_ERROR,
288 MI_NOT_FOUND,
289 MI_MODULE,
290 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000291};
292
Eric V. Smith984b11f2012-05-24 20:21:04 -0400293/* Does this path represent a directory?
294 on error, return < 0
295 if not a dir, return 0
296 if a dir, return 1
297*/
298static int
299check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path)
300{
301 PyObject *dirpath;
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700302 int res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400303
304 /* See if this is a "directory". If so, it's eligible to be part
305 of a namespace package. We test by seeing if the name, with an
306 appended path separator, exists. */
307 dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
308 if (dirpath == NULL)
309 return -1;
310 /* If dirpath is present in self->files, we have a directory. */
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700311 res = PyDict_Contains(self->files, dirpath);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400312 Py_DECREF(dirpath);
Benjamin Peterson18eac4a2012-05-25 00:24:42 -0700313 return res;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400314}
315
Just van Rossum52e14d62002-12-30 22:08:05 +0000316/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000317static enum zi_module_info
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400318get_module_info(ZipImporter *self, PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000319{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400320 PyObject *subname;
321 PyObject *path, *fullpath, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000323
Victor Stinner965a8a12010-10-18 21:44:33 +0000324 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400325 if (subname == NULL)
326 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000327
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400328 path = make_filename(self->prefix, subname);
329 Py_DECREF(subname);
330 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400334 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
335 if (fullpath == NULL) {
336 Py_DECREF(path);
337 return MI_ERROR;
338 }
339 item = PyDict_GetItem(self->files, fullpath);
340 Py_DECREF(fullpath);
341 if (item != NULL) {
342 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (zso->type & IS_PACKAGE)
344 return MI_PACKAGE;
345 else
346 return MI_MODULE;
347 }
348 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400349 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000351}
352
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700353typedef enum {
Brett Cannon56aae8f2016-01-15 11:22:19 -0800354 FL_ERROR = -1, /* error */
355 FL_NOT_FOUND, /* no loader or namespace portions found */
356 FL_MODULE_FOUND, /* module/package found */
357 FL_NS_FOUND /* namespace portion found: */
358 /* *namespace_portion will point to the name */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700359} find_loader_result;
360
Brett Cannon56aae8f2016-01-15 11:22:19 -0800361/* The guts of "find_loader" and "find_module".
Eric V. Smith984b11f2012-05-24 20:21:04 -0400362*/
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700363static find_loader_result
Eric V. Smith984b11f2012-05-24 20:21:04 -0400364find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
365{
366 enum zi_module_info mi;
367
368 *namespace_portion = NULL;
369
370 mi = get_module_info(self, fullname);
371 if (mi == MI_ERROR)
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700372 return FL_ERROR;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400373 if (mi == MI_NOT_FOUND) {
374 /* Not a module or regular package. See if this is a directory, and
375 therefore possibly a portion of a namespace package. */
Brett Cannon56aae8f2016-01-15 11:22:19 -0800376 find_loader_result result = FL_NOT_FOUND;
377 PyObject *subname;
378 int is_dir;
379
380 /* We're only interested in the last path component of fullname;
381 earlier components are recorded in self->prefix. */
382 subname = get_subname(fullname);
383 if (subname == NULL) {
384 return FL_ERROR;
385 }
386
387 is_dir = check_is_directory(self, self->prefix, subname);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400388 if (is_dir < 0)
Brett Cannon56aae8f2016-01-15 11:22:19 -0800389 result = FL_ERROR;
390 else if (is_dir) {
Eric V. Smith984b11f2012-05-24 20:21:04 -0400391 /* This is possibly a portion of a namespace
392 package. Return the string representing its path,
393 without a trailing separator. */
394 *namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
395 self->archive, SEP,
Brett Cannon56aae8f2016-01-15 11:22:19 -0800396 self->prefix, subname);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400397 if (*namespace_portion == NULL)
Brett Cannon56aae8f2016-01-15 11:22:19 -0800398 result = FL_ERROR;
399 else
400 result = FL_NS_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400401 }
Brett Cannon56aae8f2016-01-15 11:22:19 -0800402 Py_DECREF(subname);
403 return result;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400404 }
405 /* This is a module or package. */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700406 return FL_MODULE_FOUND;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400407}
408
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400409/*[clinic input]
410zipimport.zipimporter.find_module
Eric V. Smith984b11f2012-05-24 20:21:04 -0400411
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400412 fullname: unicode
413 path: object = None
414 /
415
416Search for a module specified by 'fullname'.
417
418'fullname' must be the fully qualified (dotted) module name. It returns the
419zipimporter instance itself if the module was found, or None if it wasn't.
420The optional 'path' argument is ignored -- it's there for compatibility
421with the importer protocol.
422
423[clinic start generated code]*/
424
Just van Rossum52e14d62002-12-30 22:08:05 +0000425static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400426zipimport_zipimporter_find_module_impl(ZipImporter *self, PyObject *fullname,
427 PyObject *path)
428/*[clinic end generated code: output=506087f609466dc7 input=e3528520e075063f]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000429{
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700430 PyObject *namespace_portion = NULL;
431 PyObject *result = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000432
Eric V. Smith984b11f2012-05-24 20:21:04 -0400433 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700434 case FL_ERROR:
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700435 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700436 case FL_NS_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700437 /* A namespace portion is not allowed via find_module, so return None. */
Eric V. Smith984b11f2012-05-24 20:21:04 -0400438 Py_DECREF(namespace_portion);
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700439 /* FALL THROUGH */
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700440 case FL_NOT_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700441 result = Py_None;
442 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700443 case FL_MODULE_FOUND:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700444 result = (PyObject *)self;
445 break;
Brett Cannon56aae8f2016-01-15 11:22:19 -0800446 default:
447 PyErr_BadInternalCall();
448 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 }
Benjamin Petersona6a7a1a2012-05-25 00:22:04 -0700450 Py_INCREF(result);
Benjamin Peterson2d12e142012-05-25 00:19:40 -0700451 return result;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400452}
453
454
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400455/*[clinic input]
456zipimport.zipimporter.find_loader
457
458 fullname: unicode
459 path: object = None
460 /
461
462Search for a module specified by 'fullname'.
463
464'fullname' must be the fully qualified (dotted) module name. It returns the
465zipimporter instance itself if the module was found, a string containing the
466full path name if it's possibly a portion of a namespace package,
467or None otherwise. The optional 'path' argument is ignored -- it's
468there for compatibility with the importer protocol.
469
470[clinic start generated code]*/
471
Eric V. Smith984b11f2012-05-24 20:21:04 -0400472static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400473zipimport_zipimporter_find_loader_impl(ZipImporter *self, PyObject *fullname,
474 PyObject *path)
475/*[clinic end generated code: output=601599a43bc0f49a input=dc73f275b0d5be23]*/
Eric V. Smith984b11f2012-05-24 20:21:04 -0400476{
Eric V. Smith984b11f2012-05-24 20:21:04 -0400477 PyObject *result = NULL;
478 PyObject *namespace_portion = NULL;
479
Eric V. Smith984b11f2012-05-24 20:21:04 -0400480 switch (find_loader(self, fullname, &namespace_portion)) {
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700481 case FL_ERROR:
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700482 return NULL;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700483 case FL_NOT_FOUND: /* Not found, return (None, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700484 result = Py_BuildValue("O[]", Py_None);
485 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700486 case FL_MODULE_FOUND: /* Return (self, []) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700487 result = Py_BuildValue("O[]", self);
488 break;
Benjamin Peterson46c214d2012-05-25 10:22:29 -0700489 case FL_NS_FOUND: /* Return (None, [namespace_portion]) */
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700490 result = Py_BuildValue("O[O]", Py_None, namespace_portion);
Benjamin Peterson209e04c2012-05-24 22:35:39 -0700491 Py_DECREF(namespace_portion);
Eric V. Smith984b11f2012-05-24 20:21:04 -0400492 return result;
Brett Cannon56aae8f2016-01-15 11:22:19 -0800493 default:
494 PyErr_BadInternalCall();
495 return NULL;
Eric V. Smith984b11f2012-05-24 20:21:04 -0400496 }
Benjamin Peterson5ed7bd72012-05-24 22:54:15 -0700497 return result;
Just van Rossum52e14d62002-12-30 22:08:05 +0000498}
499
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400500/*[clinic input]
501zipimport.zipimporter.load_module
502
503 fullname: unicode
504 /
505
506Load the module specified by 'fullname'.
507
508'fullname' must be the fully qualified (dotted) module name. It returns the
509imported module, or raises ZipImportError if it wasn't found.
510
511[clinic start generated code]*/
512
Just van Rossum52e14d62002-12-30 22:08:05 +0000513static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400514zipimport_zipimporter_load_module_impl(ZipImporter *self, PyObject *fullname)
515/*[clinic end generated code: output=7303cebf88d47953 input=c236e2e8621f04ef]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000516{
Victor Stinner26fabe12010-10-18 12:03:25 +0000517 PyObject *code = NULL, *mod, *dict;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400518 PyObject *modpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000520
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200521 if (PyUnicode_READY(fullname) == -1)
522 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 code = get_module_code(self, fullname, &ispackage, &modpath);
525 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000526 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000527
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400528 mod = PyImport_AddModuleObject(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000529 if (mod == NULL)
530 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 /* mod.__loader__ = self */
534 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
535 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (ispackage) {
538 /* add __path__ to the module *before* the code gets
539 executed */
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100540 PyObject *pkgpath, *fullpath, *subname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000542
Victor Stinneraf8b7e82013-10-29 01:46:24 +0100543 subname = get_subname(fullname);
544 if (subname == NULL)
545 goto error;
546
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400547 fullpath = PyUnicode_FromFormat("%U%c%U%U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 self->archive, SEP,
549 self->prefix, subname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400550 Py_DECREF(subname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if (fullpath == NULL)
552 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000553
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400554 pkgpath = Py_BuildValue("[N]", fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (pkgpath == NULL)
556 goto error;
557 err = PyDict_SetItemString(dict, "__path__", pkgpath);
558 Py_DECREF(pkgpath);
559 if (err != 0)
560 goto error;
561 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400562 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
Victor Stinner26fabe12010-10-18 12:03:25 +0000563 Py_CLEAR(code);
564 if (mod == NULL)
565 goto error;
566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400568 PySys_FormatStderr("import %U # loaded from Zip %U\n",
Victor Stinner08654e12010-10-18 12:09:02 +0000569 fullname, modpath);
570 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000572error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000573 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000574 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000576}
577
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400578/*[clinic input]
579zipimport.zipimporter.get_filename
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000580
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400581 fullname: unicode
582 /
583
584Return the filename for the specified module.
585[clinic start generated code]*/
586
587static PyObject *
588zipimport_zipimporter_get_filename_impl(ZipImporter *self,
589 PyObject *fullname)
590/*[clinic end generated code: output=c5b92b58bea86506 input=28d2eb57e4f25c8a]*/
591{
592 PyObject *code, *modpath;
593 int ispackage;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000594
595 /* Deciding the filename requires working out where the code
596 would come from if the module was actually loaded */
597 code = get_module_code(self, fullname, &ispackage, &modpath);
598 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000599 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000600 Py_DECREF(code); /* Only need the path info */
601
Victor Stinner08654e12010-10-18 12:09:02 +0000602 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000603}
604
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400605/*[clinic input]
606zipimport.zipimporter.is_package
Just van Rossum52e14d62002-12-30 22:08:05 +0000607
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400608 fullname: unicode
609 /
610
611Return True if the module specified by fullname is a package.
612
613Raise ZipImportError if the module couldn't be found.
614
615[clinic start generated code]*/
616
617static PyObject *
618zipimport_zipimporter_is_package_impl(ZipImporter *self, PyObject *fullname)
619/*[clinic end generated code: output=c32958c2a5216ae6 input=a7ba752f64345062]*/
620{
621 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 mi = get_module_info(self, fullname);
624 if (mi == MI_ERROR)
Victor Stinner965a8a12010-10-18 21:44:33 +0000625 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400627 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000628 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 }
630 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000631}
632
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400634/*[clinic input]
635zipimport.zipimporter.get_data
636
637 pathname as path: unicode
638 /
639
640Return the data associated with 'pathname'.
641
642Raise OSError if the file was not found.
643
644[clinic start generated code]*/
645
Just van Rossum52e14d62002-12-30 22:08:05 +0000646static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400647zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path)
648/*[clinic end generated code: output=65dc506aaa268436 input=fa6428b74843c4ae]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000649{
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400650 PyObject *key;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500651 PyObject *toc_entry;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100652 Py_ssize_t path_start, path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000653
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654#ifdef ALTSEP
Oren Milman631fdee2017-08-29 20:40:15 +0300655 path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace,
656 "OCC", path, ALTSEP, SEP);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100657 if (!path)
658 return NULL;
659#else
660 Py_INCREF(path);
Just van Rossum52e14d62002-12-30 22:08:05 +0000661#endif
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100662 if (PyUnicode_READY(path) == -1)
663 goto error;
664
665 path_len = PyUnicode_GET_LENGTH(path);
666
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200667 len = PyUnicode_GET_LENGTH(self->archive);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100668 path_start = 0;
669 if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
670 && PyUnicode_READ_CHAR(path, len) == SEP) {
671 path_start = len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000673
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100674 key = PyUnicode_Substring(path, path_start, path_len);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000675 if (key == NULL)
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100676 goto error;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000677 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (toc_entry == NULL) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300679 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, key);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000680 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100681 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000683 Py_DECREF(key);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100684 Py_DECREF(path);
Benjamin Peterson34c15402014-02-16 14:17:28 -0500685 return get_data(self->archive, toc_entry);
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100686 error:
687 Py_DECREF(path);
688 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000689}
690
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400691/*[clinic input]
692zipimport.zipimporter.get_code
693
694 fullname: unicode
695 /
696
697Return the code object for the specified module.
698
699Raise ZipImportError if the module couldn't be found.
700
701[clinic start generated code]*/
702
Just van Rossum52e14d62002-12-30 22:08:05 +0000703static PyObject *
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400704zipimport_zipimporter_get_code_impl(ZipImporter *self, PyObject *fullname)
705/*[clinic end generated code: output=b923c37fa99cbac4 input=2761412bc37f3549]*/
Just van Rossum52e14d62002-12-30 22:08:05 +0000706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000708}
709
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400710/*[clinic input]
711zipimport.zipimporter.get_source
Just van Rossum52e14d62002-12-30 22:08:05 +0000712
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400713 fullname: unicode
714 /
715
716Return the source code for the specified module.
717
718Raise ZipImportError if the module couldn't be found, return None if the
719archive does contain the module, but has no source for it.
720
721[clinic start generated code]*/
722
723static PyObject *
724zipimport_zipimporter_get_source_impl(ZipImporter *self, PyObject *fullname)
725/*[clinic end generated code: output=bc059301b0c33729 input=4e4b186f2e690716]*/
726{
727 PyObject *toc_entry;
728 PyObject *subname, *path, *fullpath;
729 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000732 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000734 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400735 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000736 return NULL;
737 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400738
Victor Stinner965a8a12010-10-18 21:44:33 +0000739 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400740 if (subname == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000742
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400743 path = make_filename(self->prefix, subname);
744 Py_DECREF(subname);
745 if (path == NULL)
746 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000747
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400748 if (mi == MI_PACKAGE)
749 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
750 else
751 fullpath = PyUnicode_FromFormat("%U.py", path);
752 Py_DECREF(path);
753 if (fullpath == NULL)
754 return NULL;
755
756 toc_entry = PyDict_GetItem(self->files, fullpath);
757 Py_DECREF(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000759 PyObject *res, *bytes;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500760 bytes = get_data(self->archive, toc_entry);
Victor Stinner60fe8d92010-08-16 23:48:11 +0000761 if (bytes == NULL)
762 return NULL;
763 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
764 PyBytes_GET_SIZE(bytes));
765 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 return res;
767 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 /* we have the module, but no source */
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200770 Py_RETURN_NONE;
Just van Rossum52e14d62002-12-30 22:08:05 +0000771}
772
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000773
Just van Rossum52e14d62002-12-30 22:08:05 +0000774static PyMethodDef zipimporter_methods[] = {
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400775 ZIPIMPORT_ZIPIMPORTER_FIND_MODULE_METHODDEF
776 ZIPIMPORT_ZIPIMPORTER_FIND_LOADER_METHODDEF
777 ZIPIMPORT_ZIPIMPORTER_LOAD_MODULE_METHODDEF
778 ZIPIMPORT_ZIPIMPORTER_GET_FILENAME_METHODDEF
779 ZIPIMPORT_ZIPIMPORTER_IS_PACKAGE_METHODDEF
780 ZIPIMPORT_ZIPIMPORTER_GET_DATA_METHODDEF
781 ZIPIMPORT_ZIPIMPORTER_GET_CODE_METHODDEF
782 ZIPIMPORT_ZIPIMPORTER_GET_SOURCE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000784};
785
786static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
788 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
789 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
790 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000791};
792
Just van Rossum52e14d62002-12-30 22:08:05 +0000793#define DEFERRED_ADDRESS(ADDR) 0
794
795static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
797 "zipimport.zipimporter",
798 sizeof(ZipImporter),
799 0, /* tp_itemsize */
800 (destructor)zipimporter_dealloc, /* tp_dealloc */
801 0, /* tp_print */
802 0, /* tp_getattr */
803 0, /* tp_setattr */
804 0, /* tp_reserved */
805 (reprfunc)zipimporter_repr, /* tp_repr */
806 0, /* tp_as_number */
807 0, /* tp_as_sequence */
808 0, /* tp_as_mapping */
809 0, /* tp_hash */
810 0, /* tp_call */
811 0, /* tp_str */
812 PyObject_GenericGetAttr, /* tp_getattro */
813 0, /* tp_setattro */
814 0, /* tp_as_buffer */
815 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
816 Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400817 zipimport_zipimporter___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 zipimporter_traverse, /* tp_traverse */
819 0, /* tp_clear */
820 0, /* tp_richcompare */
821 0, /* tp_weaklistoffset */
822 0, /* tp_iter */
823 0, /* tp_iternext */
824 zipimporter_methods, /* tp_methods */
825 zipimporter_members, /* tp_members */
826 0, /* tp_getset */
827 0, /* tp_base */
828 0, /* tp_dict */
829 0, /* tp_descr_get */
830 0, /* tp_descr_set */
831 0, /* tp_dictoffset */
Yaron de Leeuw02f3b7d2017-08-18 14:41:13 -0400832 (initproc)zipimport_zipimporter___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyType_GenericAlloc, /* tp_alloc */
834 PyType_GenericNew, /* tp_new */
835 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000836};
837
838
839/* implementation */
840
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200841/* Given a buffer, return the unsigned int that is represented by the first
Just van Rossum52e14d62002-12-30 22:08:05 +0000842 4 bytes, encoded as little endian. This partially reimplements
843 marshal.c:r_long() */
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200844static unsigned int
845get_uint32(const unsigned char *buf)
846{
847 unsigned int x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 x = buf[0];
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200849 x |= (unsigned int)buf[1] << 8;
850 x |= (unsigned int)buf[2] << 16;
851 x |= (unsigned int)buf[3] << 24;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000853}
854
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200855/* Given a buffer, return the unsigned int that is represented by the first
856 2 bytes, encoded as little endian. This partially reimplements
857 marshal.c:r_short() */
858static unsigned short
859get_uint16(const unsigned char *buf)
860{
861 unsigned short x;
862 x = buf[0];
863 x |= (unsigned short)buf[1] << 8;
864 return x;
865}
866
867static void
868set_file_error(PyObject *archive, int eof)
869{
870 if (eof) {
871 PyErr_SetString(PyExc_EOFError, "EOF read where not expected");
872 }
873 else {
874 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, archive);
875 }
876}
877
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800878/*
Benjamin Peterson34c15402014-02-16 14:17:28 -0500879 read_directory(archive) -> files dict (new reference)
Gregory P. Smith2bcbc142014-01-07 18:30:07 -0800880
Benjamin Peterson34c15402014-02-16 14:17:28 -0500881 Given a path to a Zip archive, build a dict, mapping file names
Just van Rossum52e14d62002-12-30 22:08:05 +0000882 (local to the archive, using SEP as a separator) to toc entries.
883
884 A toc_entry is a tuple:
885
Victor Stinner08654e12010-10-18 12:09:02 +0000886 (__file__, # value to use for __file__, available for all files,
887 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 compress, # compression kind; 0 for uncompressed
889 data_size, # size of compressed data on disk
890 file_size, # size of decompressed data
891 file_offset, # offset of file header from start of archive
892 time, # mod time of file (in dos format)
893 date, # mod data of file (in dos format)
894 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000895 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000896
897 Directories can be recognized by the trailing SEP in the name,
898 data_size and file_offset are 0.
899*/
900static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -0500901read_directory(PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +0000902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 PyObject *files = NULL;
Benjamin Peterson34c15402014-02-16 14:17:28 -0500904 FILE *fp;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200905 unsigned short flags, compress, time, date, name_size;
906 unsigned int crc, data_size, file_size, header_size, header_offset;
907 unsigned long file_offset, header_position;
908 unsigned long arc_offset; /* Absolute offset to start of the zip-archive. */
909 unsigned int count, i;
910 unsigned char buffer[46];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000912 PyObject *nameobj = NULL;
Martin v. Löwisa72e78b2011-10-31 08:33:37 +0100913 PyObject *path;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000914 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000915 int bootstrap;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200916 const char *errmsg = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000917
Benjamin Peterson34c15402014-02-16 14:17:28 -0500918 fp = _Py_fopen_obj(archive, "rb");
919 if (fp == NULL) {
Victor Stinnerfbd6f9e2015-03-20 10:52:25 +0100920 if (PyErr_ExceptionMatches(PyExc_OSError)) {
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300921 _PyErr_FormatFromCause(ZipImportError,
922 "can't open Zip file: %R", archive);
Victor Stinnerfbd6f9e2015-03-20 10:52:25 +0100923 }
Benjamin Peterson34c15402014-02-16 14:17:28 -0500924 return NULL;
925 }
926
Jesus Cea09bf7a72012-10-03 02:13:05 +0200927 if (fseek(fp, -22, SEEK_END) == -1) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200928 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +0200929 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200930 header_position = (unsigned long)ftell(fp);
931 if (header_position == (unsigned long)-1) {
932 goto file_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200934 assert(header_position <= (unsigned long)LONG_MAX);
935 if (fread(buffer, 1, 22, fp) != 22) {
936 goto file_error;
937 }
938 if (get_uint32(buffer) != 0x06054B50u) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 /* Bad: End of Central Dir signature */
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200940 errmsg = "not a Zip file";
941 goto invalid_header;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000943
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200944 header_size = get_uint32(buffer + 12);
945 header_offset = get_uint32(buffer + 16);
946 if (header_position < header_size) {
947 errmsg = "bad central directory size";
948 goto invalid_header;
949 }
950 if (header_position < header_offset) {
951 errmsg = "bad central directory offset";
952 goto invalid_header;
953 }
954 if (header_position - header_size < header_offset) {
955 errmsg = "bad central directory size or offset";
956 goto invalid_header;
957 }
958 header_position -= header_size;
959 arc_offset = header_position - header_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 files = PyDict_New();
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200962 if (files == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 /* Start of Central Directory */
966 count = 0;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200967 if (fseek(fp, (long)header_position, 0) == -1) {
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +0200968 goto file_error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200969 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 for (;;) {
971 PyObject *t;
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200972 size_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000974
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200975 n = fread(buffer, 1, 46, fp);
976 if (n < 4) {
977 goto eof_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +0200979 /* Start of file header */
980 if (get_uint32(buffer) != 0x02014B50u) {
981 break; /* Bad: Central Dir File Header */
982 }
983 if (n != 46) {
984 goto eof_error;
985 }
986 flags = get_uint16(buffer + 8);
987 compress = get_uint16(buffer + 10);
988 time = get_uint16(buffer + 12);
989 date = get_uint16(buffer + 14);
990 crc = get_uint32(buffer + 16);
991 data_size = get_uint32(buffer + 20);
992 file_size = get_uint32(buffer + 24);
993 name_size = get_uint16(buffer + 28);
994 header_size = (unsigned int)name_size +
995 get_uint16(buffer + 30) /* extra field */ +
996 get_uint16(buffer + 32) /* comment */;
997
998 file_offset = get_uint32(buffer + 42);
999 if (file_offset > header_offset) {
1000 errmsg = "bad local header offset";
1001 goto invalid_header;
1002 }
1003 file_offset += arc_offset;
1004
1005 if (name_size > MAXPATHLEN) {
1006 name_size = MAXPATHLEN;
1007 }
1008 if (fread(name, 1, name_size, fp) != name_size) {
1009 goto file_error;
1010 }
1011 name[name_size] = '\0'; /* Add terminating null byte */
Victor Stinner44d9bea2016-12-05 17:55:36 +01001012#if SEP != '/'
1013 for (i = 0; i < name_size; i++) {
1014 if (name[i] == '/') {
1015 name[i] = SEP;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001016 }
1017 }
Victor Stinner44d9bea2016-12-05 17:55:36 +01001018#endif
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001019 /* Skip the rest of the header.
1020 * On Windows, calling fseek to skip over the fields we don't use is
1021 * slower than reading the data because fseek flushes stdio's
1022 * internal buffers. See issue #8745. */
1023 assert(header_size <= 3*0xFFFFu);
1024 for (i = name_size; i < header_size; i++) {
1025 if (getc(fp) == EOF) {
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001026 goto file_error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001027 }
1028 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001029
Victor Stinner4ee65a92011-01-22 10:30:29 +00001030 bootstrap = 0;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001031 if (flags & 0x0800) {
Victor Stinnerd36c8212010-10-18 12:13:46 +00001032 charset = "utf-8";
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001033 }
Victor Stinner4ee65a92011-01-22 10:30:29 +00001034 else if (!PyThreadState_GET()->interp->codecs_initialized) {
1035 /* During bootstrap, we may need to load the encodings
1036 package from a ZIP file. But the cp437 encoding is implemented
1037 in Python in the encodings package.
1038
1039 Break out of this dependency by assuming that the path to
1040 the encodings module is ASCII-only. */
1041 charset = "ascii";
1042 bootstrap = 1;
1043 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001044 else {
Victor Stinnerd36c8212010-10-18 12:13:46 +00001045 charset = "cp437";
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001046 }
Victor Stinnerd36c8212010-10-18 12:13:46 +00001047 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner4ee65a92011-01-22 10:30:29 +00001048 if (nameobj == NULL) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001049 if (bootstrap) {
Victor Stinner4ee65a92011-01-22 10:30:29 +00001050 PyErr_Format(PyExc_NotImplementedError,
1051 "bootstrap issue: python%i%i.zip contains non-ASCII "
1052 "filenames without the unicode flag",
1053 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001054 }
Victor Stinner2460a432010-08-16 17:54:28 +00001055 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +00001056 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001057 if (PyUnicode_READY(nameobj) == -1) {
Stefan Krah000fde92012-08-20 14:14:49 +02001058 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001059 }
Martin v. Löwisa72e78b2011-10-31 08:33:37 +01001060 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001061 if (path == NULL) {
Victor Stinner2460a432010-08-16 17:54:28 +00001062 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001063 }
1064 t = Py_BuildValue("NHIIkHHI", path, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 file_size, file_offset, time, date, crc);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001066 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001068 }
Victor Stinner2460a432010-08-16 17:54:28 +00001069 err = PyDict_SetItem(files, nameobj, t);
1070 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 Py_DECREF(t);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001072 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 goto error;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001074 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 count++;
1076 }
Benjamin Peterson34c15402014-02-16 14:17:28 -05001077 fclose(fp);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001078 if (Py_VerboseFlag) {
1079 PySys_FormatStderr("# zipimport: found %u names in %R\n",
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001080 count, archive);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return files;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001083
1084eof_error:
1085 set_file_error(archive, !ferror(fp));
1086 goto error;
1087
Serhiy Storchaka0e6b7b52013-02-16 17:43:45 +02001088file_error:
Jesus Cea09bf7a72012-10-03 02:13:05 +02001089 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001090 goto error;
1091
1092invalid_header:
1093 assert(errmsg != NULL);
1094 PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
1095 goto error;
1096
Just van Rossum52e14d62002-12-30 22:08:05 +00001097error:
Benjamin Peterson34c15402014-02-16 14:17:28 -05001098 fclose(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +00001100 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001102}
1103
1104/* Return the zlib.decompress function object, or NULL if zlib couldn't
1105 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +02001106 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001107static PyObject *
1108get_decompress_func(void)
1109{
Victor Stinner4925cde2011-05-20 00:16:09 +02001110 static int importing_zlib = 0;
1111 PyObject *zlib;
1112 PyObject *decompress;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001113 _Py_IDENTIFIER(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +00001114
Victor Stinner4925cde2011-05-20 00:16:09 +02001115 if (importing_zlib != 0)
Xiang Zhang0710d752017-03-11 13:02:52 +08001116 /* Someone has a zlib.pyc in their Zip file;
Victor Stinner4925cde2011-05-20 00:16:09 +02001117 let's avoid a stack overflow. */
1118 return NULL;
1119 importing_zlib = 1;
1120 zlib = PyImport_ImportModuleNoBlock("zlib");
1121 importing_zlib = 0;
1122 if (zlib != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001123 decompress = _PyObject_GetAttrId(zlib,
1124 &PyId_decompress);
Victor Stinner4925cde2011-05-20 00:16:09 +02001125 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 }
Victor Stinner4925cde2011-05-20 00:16:09 +02001127 else {
1128 PyErr_Clear();
1129 decompress = NULL;
1130 }
1131 if (Py_VerboseFlag)
1132 PySys_WriteStderr("# zipimport: zlib %s\n",
1133 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +00001135}
1136
Benjamin Peterson34c15402014-02-16 14:17:28 -05001137/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
Just van Rossum52e14d62002-12-30 22:08:05 +00001138 data as a new reference. */
1139static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -05001140get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001141{
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001142 PyObject *raw_data = NULL, *data, *decompress;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 char *buf;
Benjamin Peterson34c15402014-02-16 14:17:28 -05001144 FILE *fp;
Victor Stinner60fe8d92010-08-16 23:48:11 +00001145 PyObject *datapath;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001146 unsigned short compress, time, date;
1147 unsigned int crc;
1148 Py_ssize_t data_size, file_size, bytes_size;
1149 long file_offset, header_size;
1150 unsigned char buffer[30];
1151 const char *errmsg = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001152
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001153 if (!PyArg_ParseTuple(toc_entry, "OHnnlHHI", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 &data_size, &file_size, &file_offset, &time,
1155 &date, &crc)) {
1156 return NULL;
1157 }
Benjamin Petersonb1db7582016-01-21 22:02:46 -08001158 if (data_size < 0) {
1159 PyErr_Format(ZipImportError, "negative data size");
1160 return NULL;
1161 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001162
Benjamin Peterson34c15402014-02-16 14:17:28 -05001163 fp = _Py_fopen_obj(archive, "rb");
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001164 if (!fp) {
Benjamin Peterson34c15402014-02-16 14:17:28 -05001165 return NULL;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 /* Check to make sure the local file header is correct */
Jesus Cea09bf7a72012-10-03 02:13:05 +02001168 if (fseek(fp, file_offset, 0) == -1) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001169 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001170 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001171 if (fread(buffer, 1, 30, fp) != 30) {
1172 goto eof_error;
1173 }
1174 if (get_uint32(buffer) != 0x04034B50u) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 /* Bad: Local File Header */
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001176 errmsg = "bad local file header";
1177 goto invalid_header;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001178 }
1179
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001180 header_size = (unsigned int)30 +
1181 get_uint16(buffer + 26) /* file name */ +
1182 get_uint16(buffer + 28) /* extra field */;
1183 if (file_offset > LONG_MAX - header_size) {
1184 errmsg = "bad local file header size";
1185 goto invalid_header;
Victor Stinner73660af2013-10-29 01:43:44 +01001186 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001187 file_offset += header_size; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +00001188
Benjamin Petersonc4032da2016-01-20 22:23:44 -08001189 if (data_size > LONG_MAX - 1) {
1190 fclose(fp);
1191 PyErr_NoMemory();
1192 return NULL;
1193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 bytes_size = compress == 0 ? data_size : data_size + 1;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001195 if (bytes_size == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 bytes_size++;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001197 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (raw_data == NULL) {
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001200 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 }
1202 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001203
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001204 if (fseek(fp, file_offset, 0) == -1) {
1205 goto file_error;
Jesus Cea09bf7a72012-10-03 02:13:05 +02001206 }
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001207 if (fread(buf, 1, data_size, fp) != (size_t)data_size) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001208 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 "zipimport: can't read data");
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001210 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001212
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001213 fclose(fp);
1214 fp = NULL;
1215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (compress != 0) {
1217 buf[data_size] = 'Z'; /* saw this in zipfile.py */
1218 data_size++;
1219 }
1220 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 if (compress == 0) { /* data is not compressed */
1223 data = PyBytes_FromStringAndSize(buf, data_size);
1224 Py_DECREF(raw_data);
1225 return data;
1226 }
1227
1228 /* Decompress with zlib */
1229 decompress = get_decompress_func();
1230 if (decompress == NULL) {
1231 PyErr_SetString(ZipImportError,
1232 "can't decompress data; "
1233 "zlib not available");
1234 goto error;
1235 }
1236 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +02001237 Py_DECREF(decompress);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 Py_DECREF(raw_data);
1239 return data;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001240
1241eof_error:
1242 set_file_error(archive, !ferror(fp));
1243 goto error;
1244
1245file_error:
1246 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
1247 goto error;
1248
1249invalid_header:
1250 assert(errmsg != NULL);
1251 PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
1252 goto error;
1253
1254error:
1255 if (fp != NULL) {
1256 fclose(fp);
1257 }
1258 Py_XDECREF(raw_data);
1259 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001260}
1261
1262/* Lenient date/time comparison function. The precision of the mtime
1263 in the archive is lower than the mtime stored in a .pyc: we
1264 must allow a difference of at most one second. */
1265static int
1266eq_mtime(time_t t1, time_t t2)
1267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 time_t d = t1 - t2;
1269 if (d < 0)
1270 d = -d;
1271 /* dostime only stores even seconds, so be lenient */
1272 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001273}
1274
Xiang Zhang0710d752017-03-11 13:02:52 +08001275/* Given the contents of a .pyc file in a buffer, unmarshal the data
Just van Rossum52e14d62002-12-30 22:08:05 +00001276 and return the code object. Return None if it the magic word doesn't
1277 match (we do this instead of raising an exception as we fall back
1278 to .py if available and we don't want to mask other errors).
1279 Returns a new reference. */
1280static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001281unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 PyObject *code;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001284 unsigned char *buf = (unsigned char *)PyBytes_AsString(data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001286
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001287 if (size < 12) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 PyErr_SetString(ZipImportError,
1289 "bad pyc data");
1290 return NULL;
1291 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001292
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001293 if (get_uint32(buf) != (unsigned int)PyImport_GetMagicNumber()) {
1294 if (Py_VerboseFlag) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001295 PySys_FormatStderr("# %R has bad magic\n",
1296 pathname);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001297 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001298 Py_RETURN_NONE; /* signal caller to try alternative */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001300
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001301 if (mtime != 0 && !eq_mtime(get_uint32(buf + 4), mtime)) {
1302 if (Py_VerboseFlag) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001303 PySys_FormatStderr("# %R has bad mtime\n",
1304 pathname);
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001305 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001306 Py_RETURN_NONE; /* signal caller to try alternative */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001308
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001309 /* XXX the pyc's size field is ignored; timestamp collisions are probably
1310 unimportant with zip files. */
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001311 code = PyMarshal_ReadObjectFromString((char *)buf + 12, size - 12);
1312 if (code == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 return NULL;
Serhiy Storchakad5db5732016-01-28 21:30:16 +02001314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (!PyCode_Check(code)) {
1316 Py_DECREF(code);
1317 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001318 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 pathname);
1320 return NULL;
1321 }
1322 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001323}
1324
Martin Panter0be894b2016-09-07 12:03:06 +00001325/* Replace any occurrences of "\r\n?" in the input string with "\n".
Just van Rossum52e14d62002-12-30 22:08:05 +00001326 This converts DOS and Mac line endings to Unix line endings.
1327 Also append a trailing "\n" to be compatible with
1328 PyParser_SimpleParseFile(). Returns a new reference. */
1329static PyObject *
1330normalize_line_endings(PyObject *source)
1331{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001332 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 PyObject *fixed_source;
1334 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001335
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001336 p = PyBytes_AsString(source);
1337 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 return PyBytes_FromStringAndSize("\n\0", 2);
1339 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 /* one char extra for trailing \n and one for terminating \0 */
1342 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1343 if (buf == NULL) {
1344 PyErr_SetString(PyExc_MemoryError,
1345 "zipimport: no memory to allocate "
1346 "source buffer");
1347 return NULL;
1348 }
1349 /* replace "\r\n?" by "\n" */
1350 for (q = buf; *p != '\0'; p++) {
1351 if (*p == '\r') {
1352 *q++ = '\n';
1353 if (*(p + 1) == '\n')
1354 p++;
1355 }
1356 else
1357 *q++ = *p;
1358 len++;
1359 }
1360 *q++ = '\n'; /* add trailing \n */
1361 *q = '\0';
1362 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1363 PyMem_Free(buf);
1364 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001365}
1366
1367/* Given a string buffer containing Python source code, compile it
Brett Cannon83358c92013-06-20 21:30:32 -04001368 and return a code object as a new reference. */
Just van Rossum52e14d62002-12-30 22:08:05 +00001369static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001370compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001371{
Steve Dower8dcc48e2016-09-09 17:27:33 -07001372 PyObject *code, *fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001373
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001374 fixed_source = normalize_line_endings(source);
1375 if (fixed_source == NULL) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001376 return NULL;
1377 }
1378
Steve Dower8dcc48e2016-09-09 17:27:33 -07001379 code = Py_CompileStringObject(PyBytes_AsString(fixed_source),
Berker Peksag4aa74c42016-09-14 08:09:48 +03001380 pathname, Py_file_input, NULL, -1);
Steve Dower8dcc48e2016-09-09 17:27:33 -07001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 Py_DECREF(fixed_source);
1383 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001384}
1385
1386/* Convert the date/time values found in the Zip archive to a value
1387 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001388static time_t
1389parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 stm.tm_sec = (dostime & 0x1f) * 2;
1396 stm.tm_min = (dostime >> 5) & 0x3f;
1397 stm.tm_hour = (dostime >> 11) & 0x1f;
1398 stm.tm_mday = dosdate & 0x1f;
1399 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1400 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1401 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001404}
1405
Brett Cannonf299abd2015-04-13 14:21:02 -04001406/* Given a path to a .pyc file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001407 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001408 is available. */
1409static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001410get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001411{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001412 PyObject *toc_entry, *stripped;
1413 time_t mtime;
1414
Xiang Zhang0710d752017-03-11 13:02:52 +08001415 /* strip 'c' from *.pyc */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001416 if (PyUnicode_READY(path) == -1)
1417 return (time_t)-1;
1418 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
1419 PyUnicode_DATA(path),
1420 PyUnicode_GET_LENGTH(path) - 1);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001421 if (stripped == NULL)
1422 return (time_t)-1;
1423
1424 toc_entry = PyDict_GetItem(self->files, stripped);
1425 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1427 PyTuple_Size(toc_entry) == 8) {
1428 /* fetch the time stamp of the .py file for comparison
1429 with an embedded pyc time stamp */
1430 int time, date;
1431 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1432 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1433 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001434 } else
1435 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001437}
1438
1439/* Return the code object for the module named by 'fullname' from the
1440 Zip archive as a new reference. */
1441static PyObject *
Benjamin Peterson34c15402014-02-16 14:17:28 -05001442get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001444{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001445 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001446
Benjamin Peterson34c15402014-02-16 14:17:28 -05001447 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 if (data == NULL)
1449 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001450
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001451 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001452 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001453 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001454 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001455 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 Py_DECREF(data);
1457 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001458}
1459
Ezio Melotti42da6632011-03-15 05:18:48 +02001460/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001461 'fullname'. */
1462static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001463get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001464 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001465{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001466 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001467 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001471 if (subname == NULL)
1472 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001473
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001474 path = make_filename(self->prefix, subname);
1475 Py_DECREF(subname);
1476 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001480 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001481
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001482 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1483 if (fullpath == NULL)
1484 goto exit;
1485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001487 PySys_FormatStderr("# trying %U%c%U\n",
1488 self->archive, (int)SEP, fullpath);
1489 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if (toc_entry != NULL) {
1491 time_t mtime = 0;
1492 int ispackage = zso->type & IS_PACKAGE;
1493 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001494
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001495 if (isbytecode) {
1496 mtime = get_mtime_of_source(self, fullpath);
1497 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1498 goto exit;
1499 }
1500 }
1501 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 if (p_ispackage != NULL)
1503 *p_ispackage = ispackage;
Benjamin Peterson34c15402014-02-16 14:17:28 -05001504 code = get_code_from_data(self, ispackage,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 isbytecode, mtime,
1506 toc_entry);
1507 if (code == Py_None) {
1508 /* bad magic number or non-matching mtime
1509 in byte code, try next */
1510 Py_DECREF(code);
1511 continue;
1512 }
Victor Stinner08654e12010-10-18 12:09:02 +00001513 if (code != NULL && p_modpath != NULL) {
1514 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1515 Py_INCREF(*p_modpath);
1516 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001517 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001519 else
1520 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001522 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1523exit:
1524 Py_DECREF(path);
1525 Py_XDECREF(fullpath);
1526 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001527}
1528
1529
1530/* Module init */
1531
1532PyDoc_STRVAR(zipimport_doc,
1533"zipimport provides support for importing Python modules from Zip archives.\n\
1534\n\
1535This module exports three objects:\n\
1536- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001537- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001538 subclass of ImportError, so it can be caught as ImportError, too.\n\
1539- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1540 info dicts, as used in zipimporter._files.\n\
1541\n\
1542It is usually not needed to use the zipimport module explicitly; it is\n\
1543used by the builtin import mechanism for sys.path items that are paths\n\
1544to Zip archives.");
1545
Martin v. Löwis1a214512008-06-11 05:26:20 +00001546static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 PyModuleDef_HEAD_INIT,
1548 "zipimport",
1549 zipimport_doc,
1550 -1,
1551 NULL,
1552 NULL,
1553 NULL,
1554 NULL,
1555 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001556};
1557
Just van Rossum52e14d62002-12-30 22:08:05 +00001558PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001559PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (PyType_Ready(&ZipImporter_Type) < 0)
1564 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 /* Correct directory separator */
1567 zip_searchorder[0].suffix[0] = SEP;
1568 zip_searchorder[1].suffix[0] = SEP;
Just van Rossum52e14d62002-12-30 22:08:05 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 mod = PyModule_Create(&zipimportmodule);
1571 if (mod == NULL)
1572 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1575 PyExc_ImportError, NULL);
1576 if (ZipImportError == NULL)
1577 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 Py_INCREF(ZipImportError);
1580 if (PyModule_AddObject(mod, "ZipImportError",
1581 ZipImportError) < 0)
1582 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 Py_INCREF(&ZipImporter_Type);
1585 if (PyModule_AddObject(mod, "zipimporter",
1586 (PyObject *)&ZipImporter_Type) < 0)
1587 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 zip_directory_cache = PyDict_New();
1590 if (zip_directory_cache == NULL)
1591 return NULL;
1592 Py_INCREF(zip_directory_cache);
1593 if (PyModule_AddObject(mod, "_zip_directory_cache",
1594 zip_directory_cache) < 0)
1595 return NULL;
1596 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001597}