blob: ac6d45edc7d00b9c104c0ad229baa92c8477fb9e [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
Victor Stinner9e40fad2010-10-18 22:34:46 +000038 PyObject *archive; /* pathname of the Zip archive,
39 decoded from the filesystem encoding */
Victor Stinner72f767e2010-10-18 11:44:21 +000040 PyObject *prefix; /* file prefix: "a/sub/directory/",
41 encoded to the filesystem encoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 PyObject *files; /* dict with file info {path: toc_entry} */
Just van Rossum52e14d62002-12-30 22:08:05 +000043};
44
Just van Rossum52e14d62002-12-30 22:08:05 +000045static PyObject *ZipImportError;
Victor Stinnerc342fca2010-10-18 11:39:05 +000046/* read_directory() cache */
Just van Rossum52e14d62002-12-30 22:08:05 +000047static PyObject *zip_directory_cache = NULL;
48
49/* forward decls */
Victor Stinner2460a432010-08-16 17:54:28 +000050static PyObject *read_directory(PyObject *archive);
Victor Stinner60fe8d92010-08-16 23:48:11 +000051static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
Just van Rossum52e14d62002-12-30 22:08:05 +000052static PyObject *get_module_code(ZipImporter *self, char *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +000053 int *p_ispackage, PyObject **p_modpath);
Just van Rossum52e14d62002-12-30 22:08:05 +000054
55
56#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
57
58
59/* zipimporter.__init__
60 Split the "subdirectory" from the Zip archive path, lookup a matching
61 entry in sys.path_importer_cache, fetch the file directory from there
62 if found, or else read it from the archive. */
63static int
64zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
65{
Victor Stinner2460a432010-08-16 17:54:28 +000066 PyObject *pathobj, *files;
Victor Stinner2b8dab72010-08-14 14:54:10 +000067 Py_UNICODE *path, *p, *prefix, buf[MAXPATHLEN+2];
68 Py_ssize_t len;
Just van Rossum52e14d62002-12-30 22:08:05 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (!_PyArg_NoKeywords("zipimporter()", kwds))
71 return -1;
Georg Brandl02c42872005-08-26 06:42:30 +000072
Victor Stinner2b8dab72010-08-14 14:54:10 +000073 if (!PyArg_ParseTuple(args, "O&:zipimporter",
Victor Stinner9e40fad2010-10-18 22:34:46 +000074 PyUnicode_FSDecoder, &pathobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +000076
Victor Stinner2b8dab72010-08-14 14:54:10 +000077 /* copy path to buf */
78 len = PyUnicode_GET_SIZE(pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if (len == 0) {
80 PyErr_SetString(ZipImportError, "archive path is empty");
Victor Stinner2b8dab72010-08-14 14:54:10 +000081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 }
83 if (len >= MAXPATHLEN) {
84 PyErr_SetString(ZipImportError,
85 "archive path too long");
Victor Stinner2b8dab72010-08-14 14:54:10 +000086 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 }
Victor Stinner2b8dab72010-08-14 14:54:10 +000088 Py_UNICODE_strcpy(buf, PyUnicode_AS_UNICODE(pathobj));
Just van Rossum52e14d62002-12-30 22:08:05 +000089
90#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 for (p = buf; *p; p++) {
92 if (*p == ALTSEP)
93 *p = SEP;
94 }
Just van Rossum52e14d62002-12-30 22:08:05 +000095#endif
96
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 path = NULL;
98 prefix = NULL;
99 for (;;) {
100 struct stat statbuf;
101 int rv;
Just van Rossum52e14d62002-12-30 22:08:05 +0000102
Victor Stinner2b8dab72010-08-14 14:54:10 +0000103 if (pathobj == NULL) {
104 pathobj = PyUnicode_FromUnicode(buf, len);
105 if (pathobj == NULL)
106 goto error;
107 }
108 rv = _Py_stat(pathobj, &statbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (rv == 0) {
110 /* it exists */
111 if (S_ISREG(statbuf.st_mode))
112 /* it's a file */
113 path = buf;
114 break;
115 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000116 else if (PyErr_Occurred())
117 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* back up one path element */
Victor Stinner2b8dab72010-08-14 14:54:10 +0000119 p = Py_UNICODE_strrchr(buf, SEP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 if (prefix != NULL)
121 *prefix = SEP;
122 if (p == NULL)
123 break;
124 *p = '\0';
Victor Stinner2b8dab72010-08-14 14:54:10 +0000125 len = p - buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 prefix = p;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000127 Py_CLEAR(pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000129 if (path == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyErr_SetString(ZipImportError, "not a Zip file");
Victor Stinner2b8dab72010-08-14 14:54:10 +0000131 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000133
Victor Stinner2b8dab72010-08-14 14:54:10 +0000134 files = PyDict_GetItem(zip_directory_cache, pathobj);
135 if (files == NULL) {
Victor Stinner2460a432010-08-16 17:54:28 +0000136 files = read_directory(pathobj);
Victor Stinner2b8dab72010-08-14 14:54:10 +0000137 if (files == NULL)
138 goto error;
139 if (PyDict_SetItem(zip_directory_cache, pathobj, files) != 0)
140 goto error;
141 }
142 else
143 Py_INCREF(files);
144 self->files = files;
145
146 self->archive = pathobj;
147 pathobj = NULL;
148
149 if (prefix != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 prefix++;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000151 len = Py_UNICODE_strlen(prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 if (prefix[len-1] != SEP) {
153 /* add trailing SEP */
154 prefix[len] = SEP;
155 prefix[len + 1] = '\0';
Victor Stinner2b8dab72010-08-14 14:54:10 +0000156 len++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 }
158 }
Victor Stinner2b8dab72010-08-14 14:54:10 +0000159 else
160 len = 0;
161 self->prefix = PyUnicode_FromUnicode(prefix, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 if (self->prefix == NULL)
Victor Stinner2b8dab72010-08-14 14:54:10 +0000163 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return 0;
Victor Stinner2b8dab72010-08-14 14:54:10 +0000166
167error:
168 Py_XDECREF(pathobj);
169 return -1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000170}
171
172/* GC support. */
173static int
174zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 ZipImporter *self = (ZipImporter *)obj;
177 Py_VISIT(self->files);
178 return 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000179}
180
181static void
182zipimporter_dealloc(ZipImporter *self)
183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyObject_GC_UnTrack(self);
185 Py_XDECREF(self->archive);
186 Py_XDECREF(self->prefix);
187 Py_XDECREF(self->files);
188 Py_TYPE(self)->tp_free((PyObject *)self);
Just van Rossum52e14d62002-12-30 22:08:05 +0000189}
190
191static PyObject *
192zipimporter_repr(ZipImporter *self)
193{
Victor Stinner028dd972010-08-17 00:04:48 +0000194 if (self->archive == NULL)
195 return PyUnicode_FromString("<zipimporter object \"???\">");
196 else if (self->prefix != NULL && PyUnicode_GET_SIZE(self->prefix) != 0)
197 return PyUnicode_FromFormat("<zipimporter object \"%.300U%c%.150U\">",
198 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 else
Victor Stinner028dd972010-08-17 00:04:48 +0000200 return PyUnicode_FromFormat("<zipimporter object \"%.300U\">",
201 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000202}
203
204/* return fullname.split(".")[-1] */
205static char *
206get_subname(char *fullname)
207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 char *subname = strrchr(fullname, '.');
209 if (subname == NULL)
210 subname = fullname;
211 else
212 subname++;
213 return subname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000214}
215
216/* Given a (sub)modulename, write the potential file path in the
217 archive (without extension) to the path buffer. Return the
218 length of the resulting string. */
219static int
Victor Stinner269aeb72010-10-18 20:40:59 +0000220make_filename(PyObject *prefix_obj, char *name, char *path, size_t pathsize)
Just van Rossum52e14d62002-12-30 22:08:05 +0000221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 size_t len;
223 char *p;
Victor Stinner72f767e2010-10-18 11:44:21 +0000224 PyObject *prefix;
Just van Rossum52e14d62002-12-30 22:08:05 +0000225
Victor Stinner72f767e2010-10-18 11:44:21 +0000226 prefix = PyUnicode_EncodeFSDefault(prefix_obj);
227 if (prefix == NULL)
228 return -1;
229 len = PyBytes_GET_SIZE(prefix);
Just van Rossum52e14d62002-12-30 22:08:05 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 /* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */
Victor Stinner269aeb72010-10-18 20:40:59 +0000232 if (len + strlen(name) + 13 >= pathsize - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 PyErr_SetString(ZipImportError, "path too long");
Victor Stinner72f767e2010-10-18 11:44:21 +0000234 Py_DECREF(prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 return -1;
236 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000237
Victor Stinner72f767e2010-10-18 11:44:21 +0000238 strcpy(path, PyBytes_AS_STRING(prefix));
239 Py_DECREF(prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 strcpy(path + len, name);
241 for (p = path + len; *p; p++) {
242 if (*p == '.')
243 *p = SEP;
244 }
245 len += strlen(name);
246 assert(len < INT_MAX);
247 return (int)len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000248}
249
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000250enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 MI_ERROR,
252 MI_NOT_FOUND,
253 MI_MODULE,
254 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000255};
256
257/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000258static enum zi_module_info
Victor Stinner965a8a12010-10-18 21:44:33 +0000259get_module_info(ZipImporter *self, char *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 char *subname, path[MAXPATHLEN + 1];
262 int len;
263 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000264
Victor Stinner965a8a12010-10-18 21:44:33 +0000265 subname = get_subname(fullname);
Just van Rossum52e14d62002-12-30 22:08:05 +0000266
Victor Stinner269aeb72010-10-18 20:40:59 +0000267 len = make_filename(self->prefix, subname, path, sizeof(path));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (len < 0)
269 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 for (zso = zip_searchorder; *zso->suffix; zso++) {
272 strcpy(path + len, zso->suffix);
273 if (PyDict_GetItemString(self->files, path) != NULL) {
274 if (zso->type & IS_PACKAGE)
275 return MI_PACKAGE;
276 else
277 return MI_MODULE;
278 }
279 }
280 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000281}
282
283/* Check whether we can satisfy the import of the module named by
284 'fullname'. Return self if we can, None if we can't. */
285static PyObject *
286zipimporter_find_module(PyObject *obj, PyObject *args)
287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 ZipImporter *self = (ZipImporter *)obj;
289 PyObject *path = NULL;
Victor Stinner965a8a12010-10-18 21:44:33 +0000290 char *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000292
Victor Stinner965a8a12010-10-18 21:44:33 +0000293 if (!PyArg_ParseTuple(args, "s|O:zipimporter.find_module",
294 &fullname, &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 mi = get_module_info(self, fullname);
298 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",
Victor Stinner9e40fad2010-10-18 22:34:46 +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 Stinner965a8a12010-10-18 21:44:33 +0000407 char *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000409
Victor Stinner965a8a12010-10-18 21:44:33 +0000410 if (!PyArg_ParseTuple(args, "s:zipimporter.is_package",
411 &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 Stinner965a8a12010-10-18 21:44:33 +0000416 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (mi == MI_NOT_FOUND) {
Victor Stinner965a8a12010-10-18 21:44:33 +0000418 PyErr_Format(ZipImportError, "can't find module '%.200s'",
419 fullname);
420 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 }
422 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000423}
424
425static PyObject *
426zipimporter_get_data(PyObject *obj, PyObject *args)
427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000429 PyObject *pathobj, *key;
430 const Py_UNICODE *path;
Just van Rossum52e14d62002-12-30 22:08:05 +0000431#ifdef ALTSEP
Victor Stinner60fe8d92010-08-16 23:48:11 +0000432 Py_UNICODE *p, buf[MAXPATHLEN + 1];
Just van Rossum52e14d62002-12-30 22:08:05 +0000433#endif
Victor Stinner60fe8d92010-08-16 23:48:11 +0000434 Py_UNICODE *archive;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyObject *toc_entry;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000436 Py_ssize_t path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000437
Victor Stinner60fe8d92010-08-16 23:48:11 +0000438 if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &pathobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000440
Victor Stinner60fe8d92010-08-16 23:48:11 +0000441 path_len = PyUnicode_GET_SIZE(pathobj);
442 path = PyUnicode_AS_UNICODE(pathobj);
Just van Rossum52e14d62002-12-30 22:08:05 +0000443#ifdef ALTSEP
Victor Stinner60fe8d92010-08-16 23:48:11 +0000444 if (path_len >= MAXPATHLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyErr_SetString(ZipImportError, "path too long");
446 return NULL;
447 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000448 Py_UNICODE_strcpy(buf, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 for (p = buf; *p; p++) {
450 if (*p == ALTSEP)
451 *p = SEP;
452 }
453 path = buf;
Just van Rossum52e14d62002-12-30 22:08:05 +0000454#endif
Victor Stinner60fe8d92010-08-16 23:48:11 +0000455 archive = PyUnicode_AS_UNICODE(self->archive);
456 len = PyUnicode_GET_SIZE(self->archive);
457 if ((size_t)len < Py_UNICODE_strlen(path) &&
458 Py_UNICODE_strncmp(path, archive, len) == 0 &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 path[len] == SEP) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000460 path += len + 1;
461 path_len -= len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000463
Victor Stinner60fe8d92010-08-16 23:48:11 +0000464 key = PyUnicode_FromUnicode(path, path_len);
465 if (key == NULL)
466 return NULL;
467 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 if (toc_entry == NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000469 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
470 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 return NULL;
472 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000473 Py_DECREF(key);
474 return get_data(self->archive, toc_entry);
Just van Rossum52e14d62002-12-30 22:08:05 +0000475}
476
477static PyObject *
478zipimporter_get_code(PyObject *obj, PyObject *args)
479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 ZipImporter *self = (ZipImporter *)obj;
481 char *fullname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname))
484 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000487}
488
489static PyObject *
490zipimporter_get_source(PyObject *obj, PyObject *args)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 ZipImporter *self = (ZipImporter *)obj;
493 PyObject *toc_entry;
Victor Stinner965a8a12010-10-18 21:44:33 +0000494 char *fullname, *subname, path[MAXPATHLEN+1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 int len;
496 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000497
Victor Stinner965a8a12010-10-18 21:44:33 +0000498 if (!PyArg_ParseTuple(args, "s:zipimporter.get_source", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000502 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000504 if (mi == MI_NOT_FOUND) {
Victor Stinner965a8a12010-10-18 21:44:33 +0000505 PyErr_Format(ZipImportError, "can't find module '%.200s'",
506 fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000507 return NULL;
508 }
Victor Stinner965a8a12010-10-18 21:44:33 +0000509 subname = get_subname(fullname);
Just van Rossum52e14d62002-12-30 22:08:05 +0000510
Victor Stinner269aeb72010-10-18 20:40:59 +0000511 len = make_filename(self->prefix, subname, path, sizeof(path));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (len < 0)
513 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (mi == MI_PACKAGE) {
516 path[len] = SEP;
517 strcpy(path + len + 1, "__init__.py");
518 }
519 else
520 strcpy(path + len, ".py");
Just van Rossum52e14d62002-12-30 22:08:05 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 toc_entry = PyDict_GetItemString(self->files, path);
523 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000524 PyObject *res, *bytes;
525 bytes = get_data(self->archive, toc_entry);
526 if (bytes == NULL)
527 return NULL;
528 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
529 PyBytes_GET_SIZE(bytes));
530 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 return res;
532 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* we have the module, but no source */
535 Py_INCREF(Py_None);
536 return Py_None;
Just van Rossum52e14d62002-12-30 22:08:05 +0000537}
538
539PyDoc_STRVAR(doc_find_module,
540"find_module(fullname, path=None) -> self or None.\n\
541\n\
542Search for a module specified by 'fullname'. 'fullname' must be the\n\
543fully qualified (dotted) module name. It returns the zipimporter\n\
544instance itself if the module was found, or None if it wasn't.\n\
545The optional 'path' argument is ignored -- it's there for compatibility\n\
546with the importer protocol.");
547
548PyDoc_STRVAR(doc_load_module,
549"load_module(fullname) -> module.\n\
550\n\
551Load the module specified by 'fullname'. 'fullname' must be the\n\
552fully qualified (dotted) module name. It returns the imported\n\
553module, or raises ZipImportError if it wasn't found.");
554
555PyDoc_STRVAR(doc_get_data,
556"get_data(pathname) -> string with file data.\n\
557\n\
558Return the data associated with 'pathname'. Raise IOError if\n\
559the file wasn't found.");
560
561PyDoc_STRVAR(doc_is_package,
562"is_package(fullname) -> bool.\n\
563\n\
564Return True if the module specified by fullname is a package.\n\
Brian Curtin32839732010-07-21 01:44:19 +0000565Raise ZipImportError if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000566
567PyDoc_STRVAR(doc_get_code,
568"get_code(fullname) -> code object.\n\
569\n\
570Return the code object for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000571if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000572
573PyDoc_STRVAR(doc_get_source,
574"get_source(fullname) -> source string.\n\
575\n\
576Return the source code for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000577if the module couldn't be found, return None if the archive does\n\
Just van Rossum52e14d62002-12-30 22:08:05 +0000578contain the module, but has no source for it.");
579
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000580
581PyDoc_STRVAR(doc_get_filename,
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000582"get_filename(fullname) -> filename string.\n\
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000583\n\
584Return the filename for the specified module.");
585
Just van Rossum52e14d62002-12-30 22:08:05 +0000586static PyMethodDef zipimporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 {"find_module", zipimporter_find_module, METH_VARARGS,
588 doc_find_module},
589 {"load_module", zipimporter_load_module, METH_VARARGS,
590 doc_load_module},
591 {"get_data", zipimporter_get_data, METH_VARARGS,
592 doc_get_data},
593 {"get_code", zipimporter_get_code, METH_VARARGS,
594 doc_get_code},
595 {"get_source", zipimporter_get_source, METH_VARARGS,
596 doc_get_source},
597 {"get_filename", zipimporter_get_filename, METH_VARARGS,
598 doc_get_filename},
599 {"is_package", zipimporter_is_package, METH_VARARGS,
600 doc_is_package},
601 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000602};
603
604static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
606 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
607 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
608 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000609};
610
611PyDoc_STRVAR(zipimporter_doc,
612"zipimporter(archivepath) -> zipimporter object\n\
613\n\
614Create a new zipimporter instance. 'archivepath' must be a path to\n\
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000615a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
616'/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
617valid directory inside the archive.\n\
618\n\
619'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
620archive.\n\
621\n\
622The 'archive' attribute of zipimporter objects contains the name of the\n\
623zipfile targeted.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000624
625#define DEFERRED_ADDRESS(ADDR) 0
626
627static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
629 "zipimport.zipimporter",
630 sizeof(ZipImporter),
631 0, /* tp_itemsize */
632 (destructor)zipimporter_dealloc, /* tp_dealloc */
633 0, /* tp_print */
634 0, /* tp_getattr */
635 0, /* tp_setattr */
636 0, /* tp_reserved */
637 (reprfunc)zipimporter_repr, /* tp_repr */
638 0, /* tp_as_number */
639 0, /* tp_as_sequence */
640 0, /* tp_as_mapping */
641 0, /* tp_hash */
642 0, /* tp_call */
643 0, /* tp_str */
644 PyObject_GenericGetAttr, /* tp_getattro */
645 0, /* tp_setattro */
646 0, /* tp_as_buffer */
647 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
648 Py_TPFLAGS_HAVE_GC, /* tp_flags */
649 zipimporter_doc, /* tp_doc */
650 zipimporter_traverse, /* tp_traverse */
651 0, /* tp_clear */
652 0, /* tp_richcompare */
653 0, /* tp_weaklistoffset */
654 0, /* tp_iter */
655 0, /* tp_iternext */
656 zipimporter_methods, /* tp_methods */
657 zipimporter_members, /* tp_members */
658 0, /* tp_getset */
659 0, /* tp_base */
660 0, /* tp_dict */
661 0, /* tp_descr_get */
662 0, /* tp_descr_set */
663 0, /* tp_dictoffset */
664 (initproc)zipimporter_init, /* tp_init */
665 PyType_GenericAlloc, /* tp_alloc */
666 PyType_GenericNew, /* tp_new */
667 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000668};
669
670
671/* implementation */
672
Just van Rossum52e14d62002-12-30 22:08:05 +0000673/* Given a buffer, return the long that is represented by the first
674 4 bytes, encoded as little endian. This partially reimplements
675 marshal.c:r_long() */
676static long
677get_long(unsigned char *buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 long x;
679 x = buf[0];
680 x |= (long)buf[1] << 8;
681 x |= (long)buf[2] << 16;
682 x |= (long)buf[3] << 24;
Just van Rossum52e14d62002-12-30 22:08:05 +0000683#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 /* Sign extension for 64-bit machines */
685 x |= -(x & 0x80000000L);
Just van Rossum52e14d62002-12-30 22:08:05 +0000686#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000688}
689
690/*
691 read_directory(archive) -> files dict (new reference)
692
693 Given a path to a Zip archive, build a dict, mapping file names
694 (local to the archive, using SEP as a separator) to toc entries.
695
696 A toc_entry is a tuple:
697
Victor Stinner08654e12010-10-18 12:09:02 +0000698 (__file__, # value to use for __file__, available for all files,
699 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 compress, # compression kind; 0 for uncompressed
701 data_size, # size of compressed data on disk
702 file_size, # size of decompressed data
703 file_offset, # offset of file header from start of archive
704 time, # mod time of file (in dos format)
705 date, # mod data of file (in dos format)
706 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000707 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000708
709 Directories can be recognized by the trailing SEP in the name,
710 data_size and file_offset are 0.
711*/
712static PyObject *
Victor Stinner2460a432010-08-16 17:54:28 +0000713read_directory(PyObject *archive_obj)
Just van Rossum52e14d62002-12-30 22:08:05 +0000714{
Victor Stinner2460a432010-08-16 17:54:28 +0000715 /* FIXME: work on Py_UNICODE* instead of char* */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyObject *files = NULL;
717 FILE *fp;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000718 unsigned short flags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 long compress, crc, data_size, file_size, file_offset, date, time;
720 long header_offset, name_size, header_size, header_position;
721 long i, l, count;
722 size_t length;
Victor Stinner2460a432010-08-16 17:54:28 +0000723 Py_UNICODE path[MAXPATHLEN + 5];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000725 PyObject *nameobj = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 char *p, endof_central_dir[22];
727 long arc_offset; /* offset from beginning of file to start of zip-archive */
Victor Stinner2460a432010-08-16 17:54:28 +0000728 PyObject *pathobj;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000729 const char *charset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000730
Victor Stinner2460a432010-08-16 17:54:28 +0000731 if (PyUnicode_GET_SIZE(archive_obj) > MAXPATHLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 PyErr_SetString(PyExc_OverflowError,
733 "Zip path name is too long");
734 return NULL;
735 }
Victor Stinner2460a432010-08-16 17:54:28 +0000736 Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(archive_obj));
Just van Rossum52e14d62002-12-30 22:08:05 +0000737
Victor Stinner2460a432010-08-16 17:54:28 +0000738 fp = _Py_fopen(archive_obj, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 if (fp == NULL) {
740 PyErr_Format(ZipImportError, "can't open Zip file: "
Victor Stinner2460a432010-08-16 17:54:28 +0000741 "'%.200U'", archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 return NULL;
743 }
744 fseek(fp, -22, SEEK_END);
745 header_position = ftell(fp);
746 if (fread(endof_central_dir, 1, 22, fp) != 22) {
747 fclose(fp);
748 PyErr_Format(ZipImportError, "can't read Zip file: "
Victor Stinner2460a432010-08-16 17:54:28 +0000749 "'%.200U'", archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 return NULL;
751 }
752 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
753 /* Bad: End of Central Dir signature */
754 fclose(fp);
755 PyErr_Format(ZipImportError, "not a Zip file: "
Victor Stinner2460a432010-08-16 17:54:28 +0000756 "'%.200U'", archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 return NULL;
758 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 header_size = get_long((unsigned char *)endof_central_dir + 12);
761 header_offset = get_long((unsigned char *)endof_central_dir + 16);
762 arc_offset = header_position - header_offset - header_size;
763 header_offset += arc_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 files = PyDict_New();
766 if (files == NULL)
767 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000768
Victor Stinner2460a432010-08-16 17:54:28 +0000769 length = Py_UNICODE_strlen(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 path[length] = SEP;
Just van Rossum52e14d62002-12-30 22:08:05 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 /* Start of Central Directory */
773 count = 0;
774 for (;;) {
775 PyObject *t;
776 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 fseek(fp, header_offset, 0); /* Start of file header */
779 l = PyMarshal_ReadLongFromFile(fp);
780 if (l != 0x02014B50)
781 break; /* Bad: Central Dir File Header */
Victor Stinnerd36c8212010-10-18 12:13:46 +0000782 fseek(fp, header_offset + 8, 0);
783 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 compress = PyMarshal_ReadShortFromFile(fp);
785 time = PyMarshal_ReadShortFromFile(fp);
786 date = PyMarshal_ReadShortFromFile(fp);
787 crc = PyMarshal_ReadLongFromFile(fp);
788 data_size = PyMarshal_ReadLongFromFile(fp);
789 file_size = PyMarshal_ReadLongFromFile(fp);
790 name_size = PyMarshal_ReadShortFromFile(fp);
791 header_size = 46 + name_size +
792 PyMarshal_ReadShortFromFile(fp) +
793 PyMarshal_ReadShortFromFile(fp);
794 fseek(fp, header_offset + 42, 0);
795 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
796 if (name_size > MAXPATHLEN)
797 name_size = MAXPATHLEN;
Just van Rossum52e14d62002-12-30 22:08:05 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 p = name;
800 for (i = 0; i < name_size; i++) {
801 *p = (char)getc(fp);
802 if (*p == '/')
803 *p = SEP;
804 p++;
805 }
806 *p = 0; /* Add terminating null byte */
807 header_offset += header_size;
Just van Rossum52e14d62002-12-30 22:08:05 +0000808
Victor Stinnerd36c8212010-10-18 12:13:46 +0000809 if (flags & 0x0800)
810 charset = "utf-8";
811 else
812 charset = "cp437";
813 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner2460a432010-08-16 17:54:28 +0000814 if (nameobj == NULL)
815 goto error;
816 Py_UNICODE_strncpy(path + length + 1, PyUnicode_AS_UNICODE(nameobj), MAXPATHLEN - length - 1);
Just van Rossum52e14d62002-12-30 22:08:05 +0000817
Victor Stinner2460a432010-08-16 17:54:28 +0000818 pathobj = PyUnicode_FromUnicode(path, Py_UNICODE_strlen(path));
819 if (pathobj == NULL)
820 goto error;
821 t = Py_BuildValue("Niiiiiii", pathobj, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 file_size, file_offset, time, date, crc);
823 if (t == NULL)
824 goto error;
Victor Stinner2460a432010-08-16 17:54:28 +0000825 err = PyDict_SetItem(files, nameobj, t);
826 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 Py_DECREF(t);
828 if (err != 0)
829 goto error;
830 count++;
831 }
832 fclose(fp);
833 if (Py_VerboseFlag)
Victor Stinner2460a432010-08-16 17:54:28 +0000834 PySys_FormatStderr("# zipimport: found %ld names in %U\n",
835 count, archive_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 return files;
Just van Rossum52e14d62002-12-30 22:08:05 +0000837error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 fclose(fp);
839 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +0000840 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000842}
843
844/* Return the zlib.decompress function object, or NULL if zlib couldn't
845 be imported. The function is cached when found, so subsequent calls
846 don't import zlib again. Returns a *borrowed* reference.
847 XXX This makes zlib.decompress immortal. */
848static PyObject *
849get_decompress_func(void)
850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 static PyObject *decompress = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 if (decompress == NULL) {
854 PyObject *zlib;
855 static int importing_zlib = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (importing_zlib != 0)
858 /* Someone has a zlib.py[co] in their Zip file;
859 let's avoid a stack overflow. */
860 return NULL;
861 importing_zlib = 1;
862 zlib = PyImport_ImportModuleNoBlock("zlib");
863 importing_zlib = 0;
864 if (zlib != NULL) {
865 decompress = PyObject_GetAttrString(zlib,
866 "decompress");
867 Py_DECREF(zlib);
868 }
869 else
870 PyErr_Clear();
871 if (Py_VerboseFlag)
872 PySys_WriteStderr("# zipimport: zlib %s\n",
873 zlib != NULL ? "available": "UNAVAILABLE");
874 }
875 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +0000876}
877
878/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
879 data as a new reference. */
880static PyObject *
Victor Stinner60fe8d92010-08-16 23:48:11 +0000881get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +0000882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 PyObject *raw_data, *data = NULL, *decompress;
884 char *buf;
885 FILE *fp;
886 int err;
887 Py_ssize_t bytes_read = 0;
888 long l;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000889 PyObject *datapath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 long compress, data_size, file_size, file_offset, bytes_size;
891 long time, date, crc;
Just van Rossum52e14d62002-12-30 22:08:05 +0000892
Victor Stinner60fe8d92010-08-16 23:48:11 +0000893 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 &data_size, &file_size, &file_offset, &time,
895 &date, &crc)) {
896 return NULL;
897 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000898
Victor Stinner60fe8d92010-08-16 23:48:11 +0000899 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 if (!fp) {
901 PyErr_Format(PyExc_IOError,
Victor Stinner60fe8d92010-08-16 23:48:11 +0000902 "zipimport: can not open file %U", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 return NULL;
904 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* Check to make sure the local file header is correct */
907 fseek(fp, file_offset, 0);
908 l = PyMarshal_ReadLongFromFile(fp);
909 if (l != 0x04034B50) {
910 /* Bad: Local File Header */
911 PyErr_Format(ZipImportError,
Victor Stinner60fe8d92010-08-16 23:48:11 +0000912 "bad local file header in %U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 archive);
914 fclose(fp);
915 return NULL;
916 }
917 fseek(fp, file_offset + 26, 0);
918 l = 30 + PyMarshal_ReadShortFromFile(fp) +
919 PyMarshal_ReadShortFromFile(fp); /* local header size */
920 file_offset += l; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 bytes_size = compress == 0 ? data_size : data_size + 1;
923 if (bytes_size == 0)
924 bytes_size++;
925 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Just van Rossum52e14d62002-12-30 22:08:05 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 if (raw_data == NULL) {
928 fclose(fp);
929 return NULL;
930 }
931 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 err = fseek(fp, file_offset, 0);
934 if (err == 0)
935 bytes_read = fread(buf, 1, data_size, fp);
936 fclose(fp);
937 if (err || bytes_read != data_size) {
938 PyErr_SetString(PyExc_IOError,
939 "zipimport: can't read data");
940 Py_DECREF(raw_data);
941 return NULL;
942 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 if (compress != 0) {
945 buf[data_size] = 'Z'; /* saw this in zipfile.py */
946 data_size++;
947 }
948 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (compress == 0) { /* data is not compressed */
951 data = PyBytes_FromStringAndSize(buf, data_size);
952 Py_DECREF(raw_data);
953 return data;
954 }
955
956 /* Decompress with zlib */
957 decompress = get_decompress_func();
958 if (decompress == NULL) {
959 PyErr_SetString(ZipImportError,
960 "can't decompress data; "
961 "zlib not available");
962 goto error;
963 }
964 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Just van Rossum52e14d62002-12-30 22:08:05 +0000965error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_DECREF(raw_data);
967 return data;
Just van Rossum52e14d62002-12-30 22:08:05 +0000968}
969
970/* Lenient date/time comparison function. The precision of the mtime
971 in the archive is lower than the mtime stored in a .pyc: we
972 must allow a difference of at most one second. */
973static int
974eq_mtime(time_t t1, time_t t2)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 time_t d = t1 - t2;
977 if (d < 0)
978 d = -d;
979 /* dostime only stores even seconds, so be lenient */
980 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000981}
982
983/* Given the contents of a .py[co] file in a buffer, unmarshal the data
984 and return the code object. Return None if it the magic word doesn't
985 match (we do this instead of raising an exception as we fall back
986 to .py if available and we don't want to mask other errors).
987 Returns a new reference. */
988static PyObject *
989unmarshal_code(char *pathname, PyObject *data, time_t mtime)
990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyObject *code;
992 char *buf = PyBytes_AsString(data);
993 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (size <= 9) {
996 PyErr_SetString(ZipImportError,
997 "bad pyc data");
998 return NULL;
999 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
1002 if (Py_VerboseFlag)
1003 PySys_WriteStderr("# %s has bad magic\n",
1004 pathname);
1005 Py_INCREF(Py_None);
1006 return Py_None; /* signal caller to try alternative */
1007 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
1010 mtime)) {
1011 if (Py_VerboseFlag)
1012 PySys_WriteStderr("# %s has bad mtime\n",
1013 pathname);
1014 Py_INCREF(Py_None);
1015 return Py_None; /* signal caller to try alternative */
1016 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
1019 if (code == NULL)
1020 return NULL;
1021 if (!PyCode_Check(code)) {
1022 Py_DECREF(code);
1023 PyErr_Format(PyExc_TypeError,
1024 "compiled module %.200s is not a code object",
1025 pathname);
1026 return NULL;
1027 }
1028 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001029}
1030
1031/* Replace any occurances of "\r\n?" in the input string with "\n".
1032 This converts DOS and Mac line endings to Unix line endings.
1033 Also append a trailing "\n" to be compatible with
1034 PyParser_SimpleParseFile(). Returns a new reference. */
1035static PyObject *
1036normalize_line_endings(PyObject *source)
1037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 char *buf, *q, *p = PyBytes_AsString(source);
1039 PyObject *fixed_source;
1040 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (!p) {
1043 return PyBytes_FromStringAndSize("\n\0", 2);
1044 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 /* one char extra for trailing \n and one for terminating \0 */
1047 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1048 if (buf == NULL) {
1049 PyErr_SetString(PyExc_MemoryError,
1050 "zipimport: no memory to allocate "
1051 "source buffer");
1052 return NULL;
1053 }
1054 /* replace "\r\n?" by "\n" */
1055 for (q = buf; *p != '\0'; p++) {
1056 if (*p == '\r') {
1057 *q++ = '\n';
1058 if (*(p + 1) == '\n')
1059 p++;
1060 }
1061 else
1062 *q++ = *p;
1063 len++;
1064 }
1065 *q++ = '\n'; /* add trailing \n */
1066 *q = '\0';
1067 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1068 PyMem_Free(buf);
1069 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001070}
1071
1072/* Given a string buffer containing Python source code, compile it
1073 return and return a code object as a new reference. */
1074static PyObject *
1075compile_source(char *pathname, PyObject *source)
1076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 PyObject *code, *fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 fixed_source = normalize_line_endings(source);
1080 if (fixed_source == NULL)
1081 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 code = Py_CompileString(PyBytes_AsString(fixed_source), pathname,
1084 Py_file_input);
1085 Py_DECREF(fixed_source);
1086 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001087}
1088
1089/* Convert the date/time values found in the Zip archive to a value
1090 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001091static time_t
1092parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 stm.tm_sec = (dostime & 0x1f) * 2;
1099 stm.tm_min = (dostime >> 5) & 0x3f;
1100 stm.tm_hour = (dostime >> 11) & 0x1f;
1101 stm.tm_mday = dosdate & 0x1f;
1102 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1103 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1104 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001107}
1108
1109/* Given a path to a .pyc or .pyo file in the archive, return the
1110 modifictaion time of the matching .py file, or 0 if no source
1111 is available. */
1112static time_t
1113get_mtime_of_source(ZipImporter *self, char *path)
1114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 PyObject *toc_entry;
1116 time_t mtime = 0;
1117 Py_ssize_t lastchar = strlen(path) - 1;
1118 char savechar = path[lastchar];
1119 path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */
1120 toc_entry = PyDict_GetItemString(self->files, path);
1121 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1122 PyTuple_Size(toc_entry) == 8) {
1123 /* fetch the time stamp of the .py file for comparison
1124 with an embedded pyc time stamp */
1125 int time, date;
1126 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1127 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1128 mtime = parse_dostime(time, date);
1129 }
1130 path[lastchar] = savechar;
1131 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001132}
1133
1134/* Return the code object for the module named by 'fullname' from the
1135 Zip archive as a new reference. */
1136static PyObject *
1137get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 PyObject *data, *code;
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001141 PyObject *modpath;
Just van Rossum52e14d62002-12-30 22:08:05 +00001142
Victor Stinner60fe8d92010-08-16 23:48:11 +00001143 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (data == NULL)
1145 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001146
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001147 modpath = PyUnicode_EncodeFSDefault(PyTuple_GetItem(toc_entry, 0));
Victor Stinner5a7913e2010-10-16 11:29:07 +00001148 if (modpath == NULL) {
1149 Py_DECREF(data);
1150 return NULL;
1151 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001152
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001153 if (isbytecode)
1154 code = unmarshal_code(PyBytes_AS_STRING(modpath), data, mtime);
1155 else
1156 code = compile_source(PyBytes_AS_STRING(modpath), data);
1157 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 Py_DECREF(data);
1159 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001160}
1161
1162/* Get the code object assoiciated with the module specified by
1163 'fullname'. */
1164static PyObject *
1165get_module_code(ZipImporter *self, char *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001166 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 PyObject *toc_entry;
1169 char *subname, path[MAXPATHLEN + 1];
1170 int len;
1171 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 subname = get_subname(fullname);
Just van Rossum52e14d62002-12-30 22:08:05 +00001174
Victor Stinner269aeb72010-10-18 20:40:59 +00001175 len = make_filename(self->prefix, subname, path, sizeof(path));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 if (len < 0)
1177 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 for (zso = zip_searchorder; *zso->suffix; zso++) {
1180 PyObject *code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 strcpy(path + len, zso->suffix);
1183 if (Py_VerboseFlag > 1)
Victor Stinner353349c2010-10-18 11:40:40 +00001184 PySys_FormatStderr("# trying %U%c%s\n",
Victor Stinner72f767e2010-10-18 11:44:21 +00001185 self->archive, (int)SEP, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 toc_entry = PyDict_GetItemString(self->files, path);
1187 if (toc_entry != NULL) {
1188 time_t mtime = 0;
1189 int ispackage = zso->type & IS_PACKAGE;
1190 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (isbytecode)
1193 mtime = get_mtime_of_source(self, path);
1194 if (p_ispackage != NULL)
1195 *p_ispackage = ispackage;
1196 code = get_code_from_data(self, ispackage,
1197 isbytecode, mtime,
1198 toc_entry);
1199 if (code == Py_None) {
1200 /* bad magic number or non-matching mtime
1201 in byte code, try next */
1202 Py_DECREF(code);
1203 continue;
1204 }
Victor Stinner08654e12010-10-18 12:09:02 +00001205 if (code != NULL && p_modpath != NULL) {
1206 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1207 Py_INCREF(*p_modpath);
1208 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 return code;
1210 }
1211 }
1212 PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname);
1213 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001214}
1215
1216
1217/* Module init */
1218
1219PyDoc_STRVAR(zipimport_doc,
1220"zipimport provides support for importing Python modules from Zip archives.\n\
1221\n\
1222This module exports three objects:\n\
1223- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001224- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001225 subclass of ImportError, so it can be caught as ImportError, too.\n\
1226- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1227 info dicts, as used in zipimporter._files.\n\
1228\n\
1229It is usually not needed to use the zipimport module explicitly; it is\n\
1230used by the builtin import mechanism for sys.path items that are paths\n\
1231to Zip archives.");
1232
Martin v. Löwis1a214512008-06-11 05:26:20 +00001233static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 PyModuleDef_HEAD_INIT,
1235 "zipimport",
1236 zipimport_doc,
1237 -1,
1238 NULL,
1239 NULL,
1240 NULL,
1241 NULL,
1242 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001243};
1244
Just van Rossum52e14d62002-12-30 22:08:05 +00001245PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001246PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (PyType_Ready(&ZipImporter_Type) < 0)
1251 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 /* Correct directory separator */
1254 zip_searchorder[0].suffix[0] = SEP;
1255 zip_searchorder[1].suffix[0] = SEP;
1256 zip_searchorder[2].suffix[0] = SEP;
1257 if (Py_OptimizeFlag) {
1258 /* Reverse *.pyc and *.pyo */
1259 struct st_zip_searchorder tmp;
1260 tmp = zip_searchorder[0];
1261 zip_searchorder[0] = zip_searchorder[1];
1262 zip_searchorder[1] = tmp;
1263 tmp = zip_searchorder[3];
1264 zip_searchorder[3] = zip_searchorder[4];
1265 zip_searchorder[4] = tmp;
1266 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 mod = PyModule_Create(&zipimportmodule);
1269 if (mod == NULL)
1270 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1273 PyExc_ImportError, NULL);
1274 if (ZipImportError == NULL)
1275 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 Py_INCREF(ZipImportError);
1278 if (PyModule_AddObject(mod, "ZipImportError",
1279 ZipImportError) < 0)
1280 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 Py_INCREF(&ZipImporter_Type);
1283 if (PyModule_AddObject(mod, "zipimporter",
1284 (PyObject *)&ZipImporter_Type) < 0)
1285 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 zip_directory_cache = PyDict_New();
1288 if (zip_directory_cache == NULL)
1289 return NULL;
1290 Py_INCREF(zip_directory_cache);
1291 if (PyModule_AddObject(mod, "_zip_directory_cache",
1292 zip_directory_cache) < 0)
1293 return NULL;
1294 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001295}