blob: c943a41ffe6d9065acec34a8ee284ac7f29e549d [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
17/* zip_searchorder defines how we search for a module in the Zip
18 archive: we first search for a package __init__, then for
19 non-package .pyc, .pyo and .py entries. The .pyc and .pyo entries
20 are swapped by initzipimport() if we run in optimized mode. Also,
21 '/' is replaced by SEP there. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +000022static struct st_zip_searchorder zip_searchorder[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
24 {"/__init__.pyo", IS_PACKAGE | IS_BYTECODE},
25 {"/__init__.py", IS_PACKAGE | IS_SOURCE},
26 {".pyc", IS_BYTECODE},
27 {".pyo", IS_BYTECODE},
28 {".py", IS_SOURCE},
29 {"", 0}
Just van Rossum52e14d62002-12-30 22:08:05 +000030};
31
32/* zipimporter object definition and support */
33
34typedef struct _zipimporter ZipImporter;
35
36struct _zipimporter {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 PyObject_HEAD
38 PyObject *archive; /* pathname of the Zip archive */
Victor Stinner72f767e2010-10-18 11:44:21 +000039 PyObject *prefix; /* file prefix: "a/sub/directory/",
40 encoded to the filesystem encoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 PyObject *files; /* dict with file info {path: toc_entry} */
Just van Rossum52e14d62002-12-30 22:08:05 +000042};
43
Just van Rossum52e14d62002-12-30 22:08:05 +000044static PyObject *ZipImportError;
Victor Stinnerc342fca2010-10-18 11:39:05 +000045/* read_directory() cache */
Just van Rossum52e14d62002-12-30 22:08:05 +000046static PyObject *zip_directory_cache = NULL;
47
48/* forward decls */
Victor Stinner2460a432010-08-16 17:54:28 +000049static PyObject *read_directory(PyObject *archive);
Victor Stinner60fe8d92010-08-16 23:48:11 +000050static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
Just van Rossum52e14d62002-12-30 22:08:05 +000051static PyObject *get_module_code(ZipImporter *self, char *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +000052 int *p_ispackage, PyObject **p_modpath);
Just van Rossum52e14d62002-12-30 22:08:05 +000053
54
55#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
56
57
58/* zipimporter.__init__
59 Split the "subdirectory" from the Zip archive path, lookup a matching
60 entry in sys.path_importer_cache, fetch the file directory from there
61 if found, or else read it from the archive. */
62static int
63zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
64{
Victor Stinner2460a432010-08-16 17:54:28 +000065 PyObject *pathobj, *files;
Victor Stinner2b8dab72010-08-14 14:54:10 +000066 Py_UNICODE *path, *p, *prefix, buf[MAXPATHLEN+2];
67 Py_ssize_t len;
Just van Rossum52e14d62002-12-30 22:08:05 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (!_PyArg_NoKeywords("zipimporter()", kwds))
70 return -1;
Georg Brandl02c42872005-08-26 06:42:30 +000071
Victor Stinner2b8dab72010-08-14 14:54:10 +000072 if (!PyArg_ParseTuple(args, "O&:zipimporter",
73 PyUnicode_FSDecoder, &pathobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +000075
Victor Stinner2b8dab72010-08-14 14:54:10 +000076 /* copy path to buf */
77 len = PyUnicode_GET_SIZE(pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (len == 0) {
79 PyErr_SetString(ZipImportError, "archive path is empty");
Victor Stinner2b8dab72010-08-14 14:54:10 +000080 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 }
82 if (len >= MAXPATHLEN) {
83 PyErr_SetString(ZipImportError,
84 "archive path too long");
Victor Stinner2b8dab72010-08-14 14:54:10 +000085 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 }
Victor Stinner2b8dab72010-08-14 14:54:10 +000087 Py_UNICODE_strcpy(buf, PyUnicode_AS_UNICODE(pathobj));
Just van Rossum52e14d62002-12-30 22:08:05 +000088
89#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 for (p = buf; *p; p++) {
91 if (*p == ALTSEP)
92 *p = SEP;
93 }
Just van Rossum52e14d62002-12-30 22:08:05 +000094#endif
95
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 path = NULL;
97 prefix = NULL;
98 for (;;) {
99 struct stat statbuf;
100 int rv;
Just van Rossum52e14d62002-12-30 22:08:05 +0000101
Victor Stinner2b8dab72010-08-14 14:54:10 +0000102 if (pathobj == NULL) {
103 pathobj = PyUnicode_FromUnicode(buf, len);
104 if (pathobj == NULL)
105 goto error;
106 }
107 rv = _Py_stat(pathobj, &statbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (rv == 0) {
109 /* it exists */
110 if (S_ISREG(statbuf.st_mode))
111 /* it's a file */
112 path = buf;
113 break;
114 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000115 else if (PyErr_Occurred())
116 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 /* back up one path element */
Victor Stinner2b8dab72010-08-14 14:54:10 +0000118 p = Py_UNICODE_strrchr(buf, SEP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 if (prefix != NULL)
120 *prefix = SEP;
121 if (p == NULL)
122 break;
123 *p = '\0';
Victor Stinner2b8dab72010-08-14 14:54:10 +0000124 len = p - buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 prefix = p;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000126 Py_CLEAR(pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000128 if (path == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 PyErr_SetString(ZipImportError, "not a Zip file");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000130 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000132
Victor Stinner2b8dab72010-08-14 14:54:10 +0000133 files = PyDict_GetItem(zip_directory_cache, pathobj);
134 if (files == NULL) {
Victor Stinner2460a432010-08-16 17:54:28 +0000135 files = read_directory(pathobj);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000136 if (files == NULL)
137 goto error;
138 if (PyDict_SetItem(zip_directory_cache, pathobj, files) != 0)
139 goto error;
140 }
141 else
142 Py_INCREF(files);
143 self->files = files;
144
145 self->archive = pathobj;
146 pathobj = NULL;
147
148 if (prefix != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 prefix++;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000150 len = Py_UNICODE_strlen(prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (prefix[len-1] != SEP) {
152 /* add trailing SEP */
153 prefix[len] = SEP;
154 prefix[len + 1] = '\0';
Victor Stinner2b8dab72010-08-14 14:54:10 +0000155 len++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 }
157 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000158 else
159 len = 0;
160 self->prefix = PyUnicode_FromUnicode(prefix, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 if (self->prefix == NULL)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000162 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return 0;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000165
166error:
167 Py_XDECREF(pathobj);
168 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000169}
170
171/* GC support. */
172static int
173zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 ZipImporter *self = (ZipImporter *)obj;
176 Py_VISIT(self->files);
177 return 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000178}
179
180static void
181zipimporter_dealloc(ZipImporter *self)
182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 PyObject_GC_UnTrack(self);
184 Py_XDECREF(self->archive);
185 Py_XDECREF(self->prefix);
186 Py_XDECREF(self->files);
187 Py_TYPE(self)->tp_free((PyObject *)self);
Just van Rossum52e14d62002-12-30 22:08:05 +0000188}
189
190static PyObject *
191zipimporter_repr(ZipImporter *self)
192{
Victor Stinner028dd972010-08-17 00:04:48 +0000193 if (self->archive == NULL)
194 return PyUnicode_FromString("<zipimporter object \"???\">");
195 else if (self->prefix != NULL && PyUnicode_GET_SIZE(self->prefix) != 0)
196 return PyUnicode_FromFormat("<zipimporter object \"%.300U%c%.150U\">",
197 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 else
Victor Stinner028dd972010-08-17 00:04:48 +0000199 return PyUnicode_FromFormat("<zipimporter object \"%.300U\">",
200 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000201}
202
203/* return fullname.split(".")[-1] */
204static char *
205get_subname(char *fullname)
206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 char *subname = strrchr(fullname, '.');
208 if (subname == NULL)
209 subname = fullname;
210 else
211 subname++;
212 return subname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000213}
214
215/* Given a (sub)modulename, write the potential file path in the
216 archive (without extension) to the path buffer. Return the
217 length of the resulting string. */
218static int
Victor Stinner269aeb72010-10-18 20:40:59 +0000219make_filename(PyObject *prefix_obj, char *name, char *path, size_t pathsize)
Just van Rossum52e14d62002-12-30 22:08:05 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 size_t len;
222 char *p;
Victor Stinner72f767e2010-10-18 11:44:21 +0000223 PyObject *prefix;
Just van Rossum52e14d62002-12-30 22:08:05 +0000224
Victor Stinner72f767e2010-10-18 11:44:21 +0000225 prefix = PyUnicode_EncodeFSDefault(prefix_obj);
226 if (prefix == NULL)
227 return -1;
228 len = PyBytes_GET_SIZE(prefix);
Just van Rossum52e14d62002-12-30 22:08:05 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 /* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */
Victor Stinner269aeb72010-10-18 20:40:59 +0000231 if (len + strlen(name) + 13 >= pathsize - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 PyErr_SetString(ZipImportError, "path too long");
Victor Stinner72f767e2010-10-18 11:44:21 +0000233 Py_DECREF(prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 return -1;
235 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000236
Victor Stinner72f767e2010-10-18 11:44:21 +0000237 strcpy(path, PyBytes_AS_STRING(prefix));
238 Py_DECREF(prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 strcpy(path + len, name);
240 for (p = path + len; *p; p++) {
241 if (*p == '.')
242 *p = SEP;
243 }
244 len += strlen(name);
245 assert(len < INT_MAX);
246 return (int)len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000247}
248
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000249enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 MI_ERROR,
251 MI_NOT_FOUND,
252 MI_MODULE,
253 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000254};
255
256/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000257static enum zi_module_info
Victor Stinner04106562010-10-18 20:44:08 +0000258get_module_info(ZipImporter *self, PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 char *subname, path[MAXPATHLEN + 1];
261 int len;
262 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000263
Victor Stinner04106562010-10-18 20:44:08 +0000264 subname = get_subname(PyBytes_AS_STRING(fullname));
Just van Rossum52e14d62002-12-30 22:08:05 +0000265
Victor Stinner269aeb72010-10-18 20:40:59 +0000266 len = make_filename(self->prefix, subname, path, sizeof(path));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (len < 0)
268 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 for (zso = zip_searchorder; *zso->suffix; zso++) {
271 strcpy(path + len, zso->suffix);
272 if (PyDict_GetItemString(self->files, path) != NULL) {
273 if (zso->type & IS_PACKAGE)
274 return MI_PACKAGE;
275 else
276 return MI_MODULE;
277 }
278 }
279 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000280}
281
282/* Check whether we can satisfy the import of the module named by
283 'fullname'. Return self if we can, None if we can't. */
284static PyObject *
285zipimporter_find_module(PyObject *obj, PyObject *args)
286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 ZipImporter *self = (ZipImporter *)obj;
288 PyObject *path = NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000289 PyObject *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000291
Victor Stinner04106562010-10-18 20:44:08 +0000292 if (!PyArg_ParseTuple(args, "O&|O:zipimporter.find_module",
293 PyUnicode_FSConverter, &fullname, &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 mi = get_module_info(self, fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000297 Py_DECREF(fullname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (mi == MI_ERROR)
299 return NULL;
300 if (mi == MI_NOT_FOUND) {
301 Py_INCREF(Py_None);
302 return Py_None;
303 }
304 Py_INCREF(self);
305 return (PyObject *)self;
Just van Rossum52e14d62002-12-30 22:08:05 +0000306}
307
308/* Load and return the module named by 'fullname'. */
309static PyObject *
310zipimporter_load_module(PyObject *obj, PyObject *args)
311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner26fabe12010-10-18 12:03:25 +0000313 PyObject *code = NULL, *mod, *dict;
Victor Stinner08654e12010-10-18 12:09:02 +0000314 char *fullname;
315 PyObject *modpath = NULL, *modpath_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 if (!PyArg_ParseTuple(args, "s:zipimporter.load_module",
319 &fullname))
320 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 code = get_module_code(self, fullname, &ispackage, &modpath);
323 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000324 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 mod = PyImport_AddModule(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000327 if (mod == NULL)
328 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 /* mod.__loader__ = self */
332 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
333 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (ispackage) {
336 /* add __path__ to the module *before* the code gets
337 executed */
338 PyObject *pkgpath, *fullpath;
339 char *subname = get_subname(fullname);
340 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 fullpath = PyUnicode_FromFormat("%U%c%U%s",
343 self->archive, SEP,
344 self->prefix, subname);
345 if (fullpath == NULL)
346 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 pkgpath = Py_BuildValue("[O]", fullpath);
349 Py_DECREF(fullpath);
350 if (pkgpath == NULL)
351 goto error;
352 err = PyDict_SetItemString(dict, "__path__", pkgpath);
353 Py_DECREF(pkgpath);
354 if (err != 0)
355 goto error;
356 }
Victor Stinner08654e12010-10-18 12:09:02 +0000357 modpath_bytes = PyUnicode_EncodeFSDefault(modpath);
358 if (modpath_bytes == NULL)
359 goto error;
360 mod = PyImport_ExecCodeModuleEx(fullname, code,
361 PyBytes_AS_STRING(modpath_bytes));
362 Py_DECREF(modpath_bytes);
Victor Stinner26fabe12010-10-18 12:03:25 +0000363 Py_CLEAR(code);
364 if (mod == NULL)
365 goto error;
366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (Py_VerboseFlag)
Victor Stinner08654e12010-10-18 12:09:02 +0000368 PySys_FormatStderr("import %s # loaded from Zip %U\n",
369 fullname, modpath);
370 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000372error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000373 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000374 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000376}
377
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000378/* Return a string matching __file__ for the named module */
379static PyObject *
380zipimporter_get_filename(PyObject *obj, PyObject *args)
381{
382 ZipImporter *self = (ZipImporter *)obj;
383 PyObject *code;
Victor Stinner08654e12010-10-18 12:09:02 +0000384 char *fullname;
385 PyObject *modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000386 int ispackage;
387
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000388 if (!PyArg_ParseTuple(args, "s:zipimporter.get_filename",
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000389 &fullname))
Victor Stinnerc342fca2010-10-18 11:39:05 +0000390 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000391
392 /* Deciding the filename requires working out where the code
393 would come from if the module was actually loaded */
394 code = get_module_code(self, fullname, &ispackage, &modpath);
395 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000396 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000397 Py_DECREF(code); /* Only need the path info */
398
Victor Stinner08654e12010-10-18 12:09:02 +0000399 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000400}
401
Just van Rossum52e14d62002-12-30 22:08:05 +0000402/* Return a bool signifying whether the module is a package or not. */
403static PyObject *
404zipimporter_is_package(PyObject *obj, PyObject *args)
405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner04106562010-10-18 20:44:08 +0000407 PyObject *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000409
Victor Stinner04106562010-10-18 20:44:08 +0000410 if (!PyArg_ParseTuple(args, "O&:zipimporter.is_package",
411 PyUnicode_FSConverter, &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 mi = get_module_info(self, fullname);
415 if (mi == MI_ERROR)
Victor Stinner04106562010-10-18 20:44:08 +0000416 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (mi == MI_NOT_FOUND) {
Victor Stinner04106562010-10-18 20:44:08 +0000418 PyErr_Format(ZipImportError, "can't find module '%.200U'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000420 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 }
Victor Stinner04106562010-10-18 20:44:08 +0000422 Py_DECREF(fullname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 return PyBool_FromLong(mi == MI_PACKAGE);
Victor Stinner04106562010-10-18 20:44:08 +0000424
425error:
426 Py_DECREF(fullname);
427 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000428}
429
430static PyObject *
431zipimporter_get_data(PyObject *obj, PyObject *args)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000434 PyObject *pathobj, *key;
435 const Py_UNICODE *path;
Just van Rossum52e14d62002-12-30 22:08:05 +0000436#ifdef ALTSEP
Victor Stinner60fe8d92010-08-16 23:48:11 +0000437 Py_UNICODE *p, buf[MAXPATHLEN + 1];
Just van Rossum52e14d62002-12-30 22:08:05 +0000438#endif
Victor Stinner60fe8d92010-08-16 23:48:11 +0000439 Py_UNICODE *archive;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 PyObject *toc_entry;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000441 Py_ssize_t path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000442
Victor Stinner60fe8d92010-08-16 23:48:11 +0000443 if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &pathobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000445
Victor Stinner60fe8d92010-08-16 23:48:11 +0000446 path_len = PyUnicode_GET_SIZE(pathobj);
447 path = PyUnicode_AS_UNICODE(pathobj);
Just van Rossum52e14d62002-12-30 22:08:05 +0000448#ifdef ALTSEP
Victor Stinner60fe8d92010-08-16 23:48:11 +0000449 if (path_len >= MAXPATHLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyErr_SetString(ZipImportError, "path too long");
451 return NULL;
452 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000453 Py_UNICODE_strcpy(buf, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 for (p = buf; *p; p++) {
455 if (*p == ALTSEP)
456 *p = SEP;
457 }
458 path = buf;
Just van Rossum52e14d62002-12-30 22:08:05 +0000459#endif
Victor Stinner60fe8d92010-08-16 23:48:11 +0000460 archive = PyUnicode_AS_UNICODE(self->archive);
461 len = PyUnicode_GET_SIZE(self->archive);
462 if ((size_t)len < Py_UNICODE_strlen(path) &&
463 Py_UNICODE_strncmp(path, archive, len) == 0 &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 path[len] == SEP) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000465 path += len + 1;
466 path_len -= len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000468
Victor Stinner60fe8d92010-08-16 23:48:11 +0000469 key = PyUnicode_FromUnicode(path, path_len);
470 if (key == NULL)
471 return NULL;
472 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (toc_entry == NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000474 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
475 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 return NULL;
477 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000478 Py_DECREF(key);
479 return get_data(self->archive, toc_entry);
Just van Rossum52e14d62002-12-30 22:08:05 +0000480}
481
482static PyObject *
483zipimporter_get_code(PyObject *obj, PyObject *args)
484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 ZipImporter *self = (ZipImporter *)obj;
486 char *fullname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname))
489 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000492}
493
494static PyObject *
495zipimporter_get_source(PyObject *obj, PyObject *args)
496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 ZipImporter *self = (ZipImporter *)obj;
498 PyObject *toc_entry;
Victor Stinner04106562010-10-18 20:44:08 +0000499 PyObject *fullname;
500 char *subname, path[MAXPATHLEN+1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 int len;
502 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000503
Victor Stinner04106562010-10-18 20:44:08 +0000504 if (!PyArg_ParseTuple(args, "O&:zipimporter.get_source",
505 PyUnicode_FSConverter, &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 mi = get_module_info(self, fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000509 if (mi == MI_ERROR) {
510 Py_DECREF(fullname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return NULL;
512 }
Victor Stinner04106562010-10-18 20:44:08 +0000513 if (mi == MI_NOT_FOUND) {
514 PyErr_Format(ZipImportError, "can't find module '%.200U'",
515 fullname);
516 Py_DECREF(fullname);
517 return NULL;
518 }
519 subname = get_subname(PyBytes_AS_STRING(fullname));
Just van Rossum52e14d62002-12-30 22:08:05 +0000520
Victor Stinner269aeb72010-10-18 20:40:59 +0000521 len = make_filename(self->prefix, subname, path, sizeof(path));
Victor Stinner04106562010-10-18 20:44:08 +0000522 Py_DECREF(fullname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (len < 0)
524 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (mi == MI_PACKAGE) {
527 path[len] = SEP;
528 strcpy(path + len + 1, "__init__.py");
529 }
530 else
531 strcpy(path + len, ".py");
Just van Rossum52e14d62002-12-30 22:08:05 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 toc_entry = PyDict_GetItemString(self->files, path);
534 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000535 PyObject *res, *bytes;
536 bytes = get_data(self->archive, toc_entry);
537 if (bytes == NULL)
538 return NULL;
539 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
540 PyBytes_GET_SIZE(bytes));
541 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return res;
543 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* we have the module, but no source */
546 Py_INCREF(Py_None);
547 return Py_None;
Just van Rossum52e14d62002-12-30 22:08:05 +0000548}
549
550PyDoc_STRVAR(doc_find_module,
551"find_module(fullname, path=None) -> self or None.\n\
552\n\
553Search for a module specified by 'fullname'. 'fullname' must be the\n\
554fully qualified (dotted) module name. It returns the zipimporter\n\
555instance itself if the module was found, or None if it wasn't.\n\
556The optional 'path' argument is ignored -- it's there for compatibility\n\
557with the importer protocol.");
558
559PyDoc_STRVAR(doc_load_module,
560"load_module(fullname) -> module.\n\
561\n\
562Load the module specified by 'fullname'. 'fullname' must be the\n\
563fully qualified (dotted) module name. It returns the imported\n\
564module, or raises ZipImportError if it wasn't found.");
565
566PyDoc_STRVAR(doc_get_data,
567"get_data(pathname) -> string with file data.\n\
568\n\
569Return the data associated with 'pathname'. Raise IOError if\n\
570the file wasn't found.");
571
572PyDoc_STRVAR(doc_is_package,
573"is_package(fullname) -> bool.\n\
574\n\
575Return True if the module specified by fullname is a package.\n\
Brian Curtin32839732010-07-21 01:44:19 +0000576Raise ZipImportError if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000577
578PyDoc_STRVAR(doc_get_code,
579"get_code(fullname) -> code object.\n\
580\n\
581Return the code object for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000582if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000583
584PyDoc_STRVAR(doc_get_source,
585"get_source(fullname) -> source string.\n\
586\n\
587Return the source code for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000588if the module couldn't be found, return None if the archive does\n\
Just van Rossum52e14d62002-12-30 22:08:05 +0000589contain the module, but has no source for it.");
590
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000591
592PyDoc_STRVAR(doc_get_filename,
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000593"get_filename(fullname) -> filename string.\n\
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000594\n\
595Return the filename for the specified module.");
596
Just van Rossum52e14d62002-12-30 22:08:05 +0000597static PyMethodDef zipimporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 {"find_module", zipimporter_find_module, METH_VARARGS,
599 doc_find_module},
600 {"load_module", zipimporter_load_module, METH_VARARGS,
601 doc_load_module},
602 {"get_data", zipimporter_get_data, METH_VARARGS,
603 doc_get_data},
604 {"get_code", zipimporter_get_code, METH_VARARGS,
605 doc_get_code},
606 {"get_source", zipimporter_get_source, METH_VARARGS,
607 doc_get_source},
608 {"get_filename", zipimporter_get_filename, METH_VARARGS,
609 doc_get_filename},
610 {"is_package", zipimporter_is_package, METH_VARARGS,
611 doc_is_package},
612 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000613};
614
615static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
617 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
618 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
619 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000620};
621
622PyDoc_STRVAR(zipimporter_doc,
623"zipimporter(archivepath) -> zipimporter object\n\
624\n\
625Create a new zipimporter instance. 'archivepath' must be a path to\n\
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000626a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
627'/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
628valid directory inside the archive.\n\
629\n\
630'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
631archive.\n\
632\n\
633The 'archive' attribute of zipimporter objects contains the name of the\n\
634zipfile targeted.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000635
636#define DEFERRED_ADDRESS(ADDR) 0
637
638static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
640 "zipimport.zipimporter",
641 sizeof(ZipImporter),
642 0, /* tp_itemsize */
643 (destructor)zipimporter_dealloc, /* tp_dealloc */
644 0, /* tp_print */
645 0, /* tp_getattr */
646 0, /* tp_setattr */
647 0, /* tp_reserved */
648 (reprfunc)zipimporter_repr, /* tp_repr */
649 0, /* tp_as_number */
650 0, /* tp_as_sequence */
651 0, /* tp_as_mapping */
652 0, /* tp_hash */
653 0, /* tp_call */
654 0, /* tp_str */
655 PyObject_GenericGetAttr, /* tp_getattro */
656 0, /* tp_setattro */
657 0, /* tp_as_buffer */
658 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
659 Py_TPFLAGS_HAVE_GC, /* tp_flags */
660 zipimporter_doc, /* tp_doc */
661 zipimporter_traverse, /* tp_traverse */
662 0, /* tp_clear */
663 0, /* tp_richcompare */
664 0, /* tp_weaklistoffset */
665 0, /* tp_iter */
666 0, /* tp_iternext */
667 zipimporter_methods, /* tp_methods */
668 zipimporter_members, /* tp_members */
669 0, /* tp_getset */
670 0, /* tp_base */
671 0, /* tp_dict */
672 0, /* tp_descr_get */
673 0, /* tp_descr_set */
674 0, /* tp_dictoffset */
675 (initproc)zipimporter_init, /* tp_init */
676 PyType_GenericAlloc, /* tp_alloc */
677 PyType_GenericNew, /* tp_new */
678 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000679};
680
681
682/* implementation */
683
Just van Rossum52e14d62002-12-30 22:08:05 +0000684/* Given a buffer, return the long that is represented by the first
685 4 bytes, encoded as little endian. This partially reimplements
686 marshal.c:r_long() */
687static long
688get_long(unsigned char *buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 long x;
690 x = buf[0];
691 x |= (long)buf[1] << 8;
692 x |= (long)buf[2] << 16;
693 x |= (long)buf[3] << 24;
Just van Rossum52e14d62002-12-30 22:08:05 +0000694#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 /* Sign extension for 64-bit machines */
696 x |= -(x & 0x80000000L);
Just van Rossum52e14d62002-12-30 22:08:05 +0000697#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000699}
700
701/*
702 read_directory(archive) -> files dict (new reference)
703
704 Given a path to a Zip archive, build a dict, mapping file names
705 (local to the archive, using SEP as a separator) to toc entries.
706
707 A toc_entry is a tuple:
708
Victor Stinner08654e12010-10-18 12:09:02 +0000709 (__file__, # value to use for __file__, available for all files,
710 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 compress, # compression kind; 0 for uncompressed
712 data_size, # size of compressed data on disk
713 file_size, # size of decompressed data
714 file_offset, # offset of file header from start of archive
715 time, # mod time of file (in dos format)
716 date, # mod data of file (in dos format)
717 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000718 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000719
720 Directories can be recognized by the trailing SEP in the name,
721 data_size and file_offset are 0.
722*/
723static PyObject *
Victor Stinner2460a432010-08-16 17:54:28 +0000724read_directory(PyObject *archive_obj)
Just van Rossum52e14d62002-12-30 22:08:05 +0000725{
Victor Stinner2460a432010-08-16 17:54:28 +0000726 /* FIXME: work on Py_UNICODE* instead of char* */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyObject *files = NULL;
728 FILE *fp;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000729 unsigned short flags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 long compress, crc, data_size, file_size, file_offset, date, time;
731 long header_offset, name_size, header_size, header_position;
732 long i, l, count;
733 size_t length;
Victor Stinner2460a432010-08-16 17:54:28 +0000734 Py_UNICODE path[MAXPATHLEN + 5];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000736 PyObject *nameobj = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 char *p, endof_central_dir[22];
738 long arc_offset; /* offset from beginning of file to start of zip-archive */
Victor Stinner2460a432010-08-16 17:54:28 +0000739 PyObject *pathobj;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000740 const char *charset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000741
Victor Stinner2460a432010-08-16 17:54:28 +0000742 if (PyUnicode_GET_SIZE(archive_obj) > MAXPATHLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 PyErr_SetString(PyExc_OverflowError,
744 "Zip path name is too long");
745 return NULL;
746 }
Victor Stinner2460a432010-08-16 17:54:28 +0000747 Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(archive_obj));
Just van Rossum52e14d62002-12-30 22:08:05 +0000748
Victor Stinner2460a432010-08-16 17:54:28 +0000749 fp = _Py_fopen(archive_obj, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 if (fp == NULL) {
751 PyErr_Format(ZipImportError, "can't open Zip file: "
Victor Stinner2460a432010-08-16 17:54:28 +0000752 "'%.200U'", archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 return NULL;
754 }
755 fseek(fp, -22, SEEK_END);
756 header_position = ftell(fp);
757 if (fread(endof_central_dir, 1, 22, fp) != 22) {
758 fclose(fp);
759 PyErr_Format(ZipImportError, "can't read Zip file: "
Victor Stinner2460a432010-08-16 17:54:28 +0000760 "'%.200U'", archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 return NULL;
762 }
763 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
764 /* Bad: End of Central Dir signature */
765 fclose(fp);
766 PyErr_Format(ZipImportError, "not a Zip file: "
Victor Stinner2460a432010-08-16 17:54:28 +0000767 "'%.200U'", archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return NULL;
769 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 header_size = get_long((unsigned char *)endof_central_dir + 12);
772 header_offset = get_long((unsigned char *)endof_central_dir + 16);
773 arc_offset = header_position - header_offset - header_size;
774 header_offset += arc_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 files = PyDict_New();
777 if (files == NULL)
778 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000779
Victor Stinner2460a432010-08-16 17:54:28 +0000780 length = Py_UNICODE_strlen(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 path[length] = SEP;
Just van Rossum52e14d62002-12-30 22:08:05 +0000782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /* Start of Central Directory */
784 count = 0;
785 for (;;) {
786 PyObject *t;
787 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 fseek(fp, header_offset, 0); /* Start of file header */
790 l = PyMarshal_ReadLongFromFile(fp);
791 if (l != 0x02014B50)
792 break; /* Bad: Central Dir File Header */
Victor Stinnerd36c8212010-10-18 12:13:46 +0000793 fseek(fp, header_offset + 8, 0);
794 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 compress = PyMarshal_ReadShortFromFile(fp);
796 time = PyMarshal_ReadShortFromFile(fp);
797 date = PyMarshal_ReadShortFromFile(fp);
798 crc = PyMarshal_ReadLongFromFile(fp);
799 data_size = PyMarshal_ReadLongFromFile(fp);
800 file_size = PyMarshal_ReadLongFromFile(fp);
801 name_size = PyMarshal_ReadShortFromFile(fp);
802 header_size = 46 + name_size +
803 PyMarshal_ReadShortFromFile(fp) +
804 PyMarshal_ReadShortFromFile(fp);
805 fseek(fp, header_offset + 42, 0);
806 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
807 if (name_size > MAXPATHLEN)
808 name_size = MAXPATHLEN;
Just van Rossum52e14d62002-12-30 22:08:05 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 p = name;
811 for (i = 0; i < name_size; i++) {
812 *p = (char)getc(fp);
813 if (*p == '/')
814 *p = SEP;
815 p++;
816 }
817 *p = 0; /* Add terminating null byte */
818 header_offset += header_size;
Just van Rossum52e14d62002-12-30 22:08:05 +0000819
Victor Stinnerd36c8212010-10-18 12:13:46 +0000820 if (flags & 0x0800)
821 charset = "utf-8";
822 else
823 charset = "cp437";
824 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner2460a432010-08-16 17:54:28 +0000825 if (nameobj == NULL)
826 goto error;
827 Py_UNICODE_strncpy(path + length + 1, PyUnicode_AS_UNICODE(nameobj), MAXPATHLEN - length - 1);
Just van Rossum52e14d62002-12-30 22:08:05 +0000828
Victor Stinner2460a432010-08-16 17:54:28 +0000829 pathobj = PyUnicode_FromUnicode(path, Py_UNICODE_strlen(path));
830 if (pathobj == NULL)
831 goto error;
832 t = Py_BuildValue("Niiiiiii", pathobj, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 file_size, file_offset, time, date, crc);
834 if (t == NULL)
835 goto error;
Victor Stinner2460a432010-08-16 17:54:28 +0000836 err = PyDict_SetItem(files, nameobj, t);
837 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 Py_DECREF(t);
839 if (err != 0)
840 goto error;
841 count++;
842 }
843 fclose(fp);
844 if (Py_VerboseFlag)
Victor Stinner2460a432010-08-16 17:54:28 +0000845 PySys_FormatStderr("# zipimport: found %ld names in %U\n",
846 count, archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 return files;
Just van Rossum52e14d62002-12-30 22:08:05 +0000848error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 fclose(fp);
850 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +0000851 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000853}
854
855/* Return the zlib.decompress function object, or NULL if zlib couldn't
856 be imported. The function is cached when found, so subsequent calls
857 don't import zlib again. Returns a *borrowed* reference.
858 XXX This makes zlib.decompress immortal. */
859static PyObject *
860get_decompress_func(void)
861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 static PyObject *decompress = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 if (decompress == NULL) {
865 PyObject *zlib;
866 static int importing_zlib = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (importing_zlib != 0)
869 /* Someone has a zlib.py[co] in their Zip file;
870 let's avoid a stack overflow. */
871 return NULL;
872 importing_zlib = 1;
873 zlib = PyImport_ImportModuleNoBlock("zlib");
874 importing_zlib = 0;
875 if (zlib != NULL) {
876 decompress = PyObject_GetAttrString(zlib,
877 "decompress");
878 Py_DECREF(zlib);
879 }
880 else
881 PyErr_Clear();
882 if (Py_VerboseFlag)
883 PySys_WriteStderr("# zipimport: zlib %s\n",
884 zlib != NULL ? "available": "UNAVAILABLE");
885 }
886 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +0000887}
888
889/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
890 data as a new reference. */
891static PyObject *
Victor Stinner60fe8d92010-08-16 23:48:11 +0000892get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +0000893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyObject *raw_data, *data = NULL, *decompress;
895 char *buf;
896 FILE *fp;
897 int err;
898 Py_ssize_t bytes_read = 0;
899 long l;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000900 PyObject *datapath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 long compress, data_size, file_size, file_offset, bytes_size;
902 long time, date, crc;
Just van Rossum52e14d62002-12-30 22:08:05 +0000903
Victor Stinner60fe8d92010-08-16 23:48:11 +0000904 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 &data_size, &file_size, &file_offset, &time,
906 &date, &crc)) {
907 return NULL;
908 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000909
Victor Stinner60fe8d92010-08-16 23:48:11 +0000910 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 if (!fp) {
912 PyErr_Format(PyExc_IOError,
Victor Stinner60fe8d92010-08-16 23:48:11 +0000913 "zipimport: can not open file %U", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return NULL;
915 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 /* Check to make sure the local file header is correct */
918 fseek(fp, file_offset, 0);
919 l = PyMarshal_ReadLongFromFile(fp);
920 if (l != 0x04034B50) {
921 /* Bad: Local File Header */
922 PyErr_Format(ZipImportError,
Victor Stinner60fe8d92010-08-16 23:48:11 +0000923 "bad local file header in %U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 archive);
925 fclose(fp);
926 return NULL;
927 }
928 fseek(fp, file_offset + 26, 0);
929 l = 30 + PyMarshal_ReadShortFromFile(fp) +
930 PyMarshal_ReadShortFromFile(fp); /* local header size */
931 file_offset += l; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 bytes_size = compress == 0 ? data_size : data_size + 1;
934 if (bytes_size == 0)
935 bytes_size++;
936 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Just van Rossum52e14d62002-12-30 22:08:05 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 if (raw_data == NULL) {
939 fclose(fp);
940 return NULL;
941 }
942 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 err = fseek(fp, file_offset, 0);
945 if (err == 0)
946 bytes_read = fread(buf, 1, data_size, fp);
947 fclose(fp);
948 if (err || bytes_read != data_size) {
949 PyErr_SetString(PyExc_IOError,
950 "zipimport: can't read data");
951 Py_DECREF(raw_data);
952 return NULL;
953 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 if (compress != 0) {
956 buf[data_size] = 'Z'; /* saw this in zipfile.py */
957 data_size++;
958 }
959 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (compress == 0) { /* data is not compressed */
962 data = PyBytes_FromStringAndSize(buf, data_size);
963 Py_DECREF(raw_data);
964 return data;
965 }
966
967 /* Decompress with zlib */
968 decompress = get_decompress_func();
969 if (decompress == NULL) {
970 PyErr_SetString(ZipImportError,
971 "can't decompress data; "
972 "zlib not available");
973 goto error;
974 }
975 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Just van Rossum52e14d62002-12-30 22:08:05 +0000976error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 Py_DECREF(raw_data);
978 return data;
Just van Rossum52e14d62002-12-30 22:08:05 +0000979}
980
981/* Lenient date/time comparison function. The precision of the mtime
982 in the archive is lower than the mtime stored in a .pyc: we
983 must allow a difference of at most one second. */
984static int
985eq_mtime(time_t t1, time_t t2)
986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 time_t d = t1 - t2;
988 if (d < 0)
989 d = -d;
990 /* dostime only stores even seconds, so be lenient */
991 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000992}
993
994/* Given the contents of a .py[co] file in a buffer, unmarshal the data
995 and return the code object. Return None if it the magic word doesn't
996 match (we do this instead of raising an exception as we fall back
997 to .py if available and we don't want to mask other errors).
998 Returns a new reference. */
999static PyObject *
1000unmarshal_code(char *pathname, PyObject *data, time_t mtime)
1001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyObject *code;
1003 char *buf = PyBytes_AsString(data);
1004 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (size <= 9) {
1007 PyErr_SetString(ZipImportError,
1008 "bad pyc data");
1009 return NULL;
1010 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
1013 if (Py_VerboseFlag)
1014 PySys_WriteStderr("# %s has bad magic\n",
1015 pathname);
1016 Py_INCREF(Py_None);
1017 return Py_None; /* signal caller to try alternative */
1018 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
1021 mtime)) {
1022 if (Py_VerboseFlag)
1023 PySys_WriteStderr("# %s has bad mtime\n",
1024 pathname);
1025 Py_INCREF(Py_None);
1026 return Py_None; /* signal caller to try alternative */
1027 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
1030 if (code == NULL)
1031 return NULL;
1032 if (!PyCode_Check(code)) {
1033 Py_DECREF(code);
1034 PyErr_Format(PyExc_TypeError,
1035 "compiled module %.200s is not a code object",
1036 pathname);
1037 return NULL;
1038 }
1039 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001040}
1041
1042/* Replace any occurances of "\r\n?" in the input string with "\n".
1043 This converts DOS and Mac line endings to Unix line endings.
1044 Also append a trailing "\n" to be compatible with
1045 PyParser_SimpleParseFile(). Returns a new reference. */
1046static PyObject *
1047normalize_line_endings(PyObject *source)
1048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 char *buf, *q, *p = PyBytes_AsString(source);
1050 PyObject *fixed_source;
1051 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (!p) {
1054 return PyBytes_FromStringAndSize("\n\0", 2);
1055 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 /* one char extra for trailing \n and one for terminating \0 */
1058 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1059 if (buf == NULL) {
1060 PyErr_SetString(PyExc_MemoryError,
1061 "zipimport: no memory to allocate "
1062 "source buffer");
1063 return NULL;
1064 }
1065 /* replace "\r\n?" by "\n" */
1066 for (q = buf; *p != '\0'; p++) {
1067 if (*p == '\r') {
1068 *q++ = '\n';
1069 if (*(p + 1) == '\n')
1070 p++;
1071 }
1072 else
1073 *q++ = *p;
1074 len++;
1075 }
1076 *q++ = '\n'; /* add trailing \n */
1077 *q = '\0';
1078 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1079 PyMem_Free(buf);
1080 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001081}
1082
1083/* Given a string buffer containing Python source code, compile it
1084 return and return a code object as a new reference. */
1085static PyObject *
1086compile_source(char *pathname, PyObject *source)
1087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 PyObject *code, *fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 fixed_source = normalize_line_endings(source);
1091 if (fixed_source == NULL)
1092 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 code = Py_CompileString(PyBytes_AsString(fixed_source), pathname,
1095 Py_file_input);
1096 Py_DECREF(fixed_source);
1097 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001098}
1099
1100/* Convert the date/time values found in the Zip archive to a value
1101 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001102static time_t
1103parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 stm.tm_sec = (dostime & 0x1f) * 2;
1110 stm.tm_min = (dostime >> 5) & 0x3f;
1111 stm.tm_hour = (dostime >> 11) & 0x1f;
1112 stm.tm_mday = dosdate & 0x1f;
1113 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1114 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1115 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001118}
1119
1120/* Given a path to a .pyc or .pyo file in the archive, return the
1121 modifictaion time of the matching .py file, or 0 if no source
1122 is available. */
1123static time_t
1124get_mtime_of_source(ZipImporter *self, char *path)
1125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 PyObject *toc_entry;
1127 time_t mtime = 0;
1128 Py_ssize_t lastchar = strlen(path) - 1;
1129 char savechar = path[lastchar];
1130 path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */
1131 toc_entry = PyDict_GetItemString(self->files, path);
1132 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1133 PyTuple_Size(toc_entry) == 8) {
1134 /* fetch the time stamp of the .py file for comparison
1135 with an embedded pyc time stamp */
1136 int time, date;
1137 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1138 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1139 mtime = parse_dostime(time, date);
1140 }
1141 path[lastchar] = savechar;
1142 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001143}
1144
1145/* Return the code object for the module named by 'fullname' from the
1146 Zip archive as a new reference. */
1147static PyObject *
1148get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 PyObject *data, *code;
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001152 PyObject *modpath;
Just van Rossum52e14d62002-12-30 22:08:05 +00001153
Victor Stinner60fe8d92010-08-16 23:48:11 +00001154 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (data == NULL)
1156 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001157
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001158 modpath = PyUnicode_EncodeFSDefault(PyTuple_GetItem(toc_entry, 0));
Victor Stinner5a7913e2010-10-16 11:29:07 +00001159 if (modpath == NULL) {
1160 Py_DECREF(data);
1161 return NULL;
1162 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001163
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001164 if (isbytecode)
1165 code = unmarshal_code(PyBytes_AS_STRING(modpath), data, mtime);
1166 else
1167 code = compile_source(PyBytes_AS_STRING(modpath), data);
1168 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 Py_DECREF(data);
1170 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001171}
1172
1173/* Get the code object assoiciated with the module specified by
1174 'fullname'. */
1175static PyObject *
1176get_module_code(ZipImporter *self, char *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001177 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 PyObject *toc_entry;
1180 char *subname, path[MAXPATHLEN + 1];
1181 int len;
1182 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 subname = get_subname(fullname);
Just van Rossum52e14d62002-12-30 22:08:05 +00001185
Victor Stinner269aeb72010-10-18 20:40:59 +00001186 len = make_filename(self->prefix, subname, path, sizeof(path));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (len < 0)
1188 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 for (zso = zip_searchorder; *zso->suffix; zso++) {
1191 PyObject *code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 strcpy(path + len, zso->suffix);
1194 if (Py_VerboseFlag > 1)
Victor Stinner353349c2010-10-18 11:40:40 +00001195 PySys_FormatStderr("# trying %U%c%s\n",
Victor Stinner72f767e2010-10-18 11:44:21 +00001196 self->archive, (int)SEP, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 toc_entry = PyDict_GetItemString(self->files, path);
1198 if (toc_entry != NULL) {
1199 time_t mtime = 0;
1200 int ispackage = zso->type & IS_PACKAGE;
1201 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 if (isbytecode)
1204 mtime = get_mtime_of_source(self, path);
1205 if (p_ispackage != NULL)
1206 *p_ispackage = ispackage;
1207 code = get_code_from_data(self, ispackage,
1208 isbytecode, mtime,
1209 toc_entry);
1210 if (code == Py_None) {
1211 /* bad magic number or non-matching mtime
1212 in byte code, try next */
1213 Py_DECREF(code);
1214 continue;
1215 }
Victor Stinner08654e12010-10-18 12:09:02 +00001216 if (code != NULL && p_modpath != NULL) {
1217 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1218 Py_INCREF(*p_modpath);
1219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 return code;
1221 }
1222 }
1223 PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname);
1224 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001225}
1226
1227
1228/* Module init */
1229
1230PyDoc_STRVAR(zipimport_doc,
1231"zipimport provides support for importing Python modules from Zip archives.\n\
1232\n\
1233This module exports three objects:\n\
1234- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001235- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001236 subclass of ImportError, so it can be caught as ImportError, too.\n\
1237- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1238 info dicts, as used in zipimporter._files.\n\
1239\n\
1240It is usually not needed to use the zipimport module explicitly; it is\n\
1241used by the builtin import mechanism for sys.path items that are paths\n\
1242to Zip archives.");
1243
Martin v. Löwis1a214512008-06-11 05:26:20 +00001244static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PyModuleDef_HEAD_INIT,
1246 "zipimport",
1247 zipimport_doc,
1248 -1,
1249 NULL,
1250 NULL,
1251 NULL,
1252 NULL,
1253 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001254};
1255
Just van Rossum52e14d62002-12-30 22:08:05 +00001256PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001257PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (PyType_Ready(&ZipImporter_Type) < 0)
1262 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* Correct directory separator */
1265 zip_searchorder[0].suffix[0] = SEP;
1266 zip_searchorder[1].suffix[0] = SEP;
1267 zip_searchorder[2].suffix[0] = SEP;
1268 if (Py_OptimizeFlag) {
1269 /* Reverse *.pyc and *.pyo */
1270 struct st_zip_searchorder tmp;
1271 tmp = zip_searchorder[0];
1272 zip_searchorder[0] = zip_searchorder[1];
1273 zip_searchorder[1] = tmp;
1274 tmp = zip_searchorder[3];
1275 zip_searchorder[3] = zip_searchorder[4];
1276 zip_searchorder[4] = tmp;
1277 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 mod = PyModule_Create(&zipimportmodule);
1280 if (mod == NULL)
1281 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1284 PyExc_ImportError, NULL);
1285 if (ZipImportError == NULL)
1286 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 Py_INCREF(ZipImportError);
1289 if (PyModule_AddObject(mod, "ZipImportError",
1290 ZipImportError) < 0)
1291 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 Py_INCREF(&ZipImporter_Type);
1294 if (PyModule_AddObject(mod, "zipimporter",
1295 (PyObject *)&ZipImporter_Type) < 0)
1296 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 zip_directory_cache = PyDict_New();
1299 if (zip_directory_cache == NULL)
1300 return NULL;
1301 Py_INCREF(zip_directory_cache);
1302 if (PyModule_AddObject(mod, "_zip_directory_cache",
1303 zip_directory_cache) < 0)
1304 return NULL;
1305 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001306}