blob: a83bf8be13e598ed094ab6246b4dfceb228af400 [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);
Victor Stinnerf6b563a2011-03-14 20:46:50 -040052static PyObject *get_module_code(ZipImporter *self, PyObject *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)
Victor Stinner07298a12010-10-18 22:45:54 +0000197 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000198 self->archive, SEP, self->prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 else
Victor Stinner07298a12010-10-18 22:45:54 +0000200 return PyUnicode_FromFormat("<zipimporter object \"%U\">",
Victor Stinner028dd972010-08-17 00:04:48 +0000201 self->archive);
Just van Rossum52e14d62002-12-30 22:08:05 +0000202}
203
204/* return fullname.split(".")[-1] */
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400205static PyObject *
206get_subname(PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000207{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400208 Py_ssize_t len;
209 Py_UNICODE *subname;
210 subname = Py_UNICODE_strrchr(PyUnicode_AS_UNICODE(fullname), '.');
211 if (subname == NULL) {
212 Py_INCREF(fullname);
213 return fullname;
214 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 subname++;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400216 len = PyUnicode_GET_SIZE(fullname);
217 len -= subname - PyUnicode_AS_UNICODE(fullname);
218 return PyUnicode_FromUnicode(subname, len);
219 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000220}
221
222/* Given a (sub)modulename, write the potential file path in the
223 archive (without extension) to the path buffer. Return the
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400224 length of the resulting string.
225
226 return self.prefix + name.replace('.', os.sep) */
227static PyObject*
228make_filename(PyObject *prefix, PyObject *name)
Just van Rossum52e14d62002-12-30 22:08:05 +0000229{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400230 PyObject *pathobj;
231 Py_UNICODE *p;
Just van Rossum52e14d62002-12-30 22:08:05 +0000232
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400233 pathobj = PyUnicode_FromUnicode(NULL,
234 PyUnicode_GET_SIZE(prefix)
235 + PyUnicode_GET_SIZE(name));
236 if (pathobj == NULL)
237 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000238
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400239 p = PyUnicode_AS_UNICODE(pathobj);
Just van Rossum52e14d62002-12-30 22:08:05 +0000240
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400241 Py_UNICODE_strcpy(p, PyUnicode_AS_UNICODE(prefix));
242 p += PyUnicode_GET_SIZE(prefix);
243 Py_UNICODE_strcpy(p, PyUnicode_AS_UNICODE(name));
244 for (; *p; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (*p == '.')
246 *p = SEP;
247 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400248 return pathobj;
Just van Rossum52e14d62002-12-30 22:08:05 +0000249}
250
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000251enum zi_module_info {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 MI_ERROR,
253 MI_NOT_FOUND,
254 MI_MODULE,
255 MI_PACKAGE
Just van Rossum52e14d62002-12-30 22:08:05 +0000256};
257
258/* Return some information about a module. */
Raymond Hettinger2c45c9a2004-11-10 13:08:35 +0000259static enum zi_module_info
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400260get_module_info(ZipImporter *self, PyObject *fullname)
Just van Rossum52e14d62002-12-30 22:08:05 +0000261{
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400262 PyObject *subname;
263 PyObject *path, *fullpath, *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +0000265
Victor Stinner965a8a12010-10-18 21:44:33 +0000266 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400267 if (subname == NULL)
268 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000269
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400270 path = make_filename(self->prefix, subname);
271 Py_DECREF(subname);
272 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 return MI_ERROR;
Just van Rossum52e14d62002-12-30 22:08:05 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400276 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
277 if (fullpath == NULL) {
278 Py_DECREF(path);
279 return MI_ERROR;
280 }
281 item = PyDict_GetItem(self->files, fullpath);
282 Py_DECREF(fullpath);
283 if (item != NULL) {
284 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (zso->type & IS_PACKAGE)
286 return MI_PACKAGE;
287 else
288 return MI_MODULE;
289 }
290 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400291 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 return MI_NOT_FOUND;
Just van Rossum52e14d62002-12-30 22:08:05 +0000293}
294
295/* Check whether we can satisfy the import of the module named by
296 'fullname'. Return self if we can, None if we can't. */
297static PyObject *
298zipimporter_find_module(PyObject *obj, PyObject *args)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 ZipImporter *self = (ZipImporter *)obj;
301 PyObject *path = NULL;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400302 PyObject *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000304
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400305 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module",
Victor Stinner965a8a12010-10-18 21:44:33 +0000306 &fullname, &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 mi = get_module_info(self, fullname);
310 if (mi == MI_ERROR)
311 return NULL;
312 if (mi == MI_NOT_FOUND) {
313 Py_INCREF(Py_None);
314 return Py_None;
315 }
316 Py_INCREF(self);
317 return (PyObject *)self;
Just van Rossum52e14d62002-12-30 22:08:05 +0000318}
319
320/* Load and return the module named by 'fullname'. */
321static PyObject *
322zipimporter_load_module(PyObject *obj, PyObject *args)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner26fabe12010-10-18 12:03:25 +0000325 PyObject *code = NULL, *mod, *dict;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400326 PyObject *fullname;
327 PyObject *modpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 int ispackage;
Just van Rossum52e14d62002-12-30 22:08:05 +0000329
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400330 if (!PyArg_ParseTuple(args, "U:zipimporter.load_module",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 &fullname))
332 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 code = get_module_code(self, fullname, &ispackage, &modpath);
335 if (code == NULL)
Victor Stinner26fabe12010-10-18 12:03:25 +0000336 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000337
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400338 mod = PyImport_AddModuleObject(fullname);
Victor Stinner26fabe12010-10-18 12:03:25 +0000339 if (mod == NULL)
340 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 dict = PyModule_GetDict(mod);
Just van Rossum52e14d62002-12-30 22:08:05 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 /* mod.__loader__ = self */
344 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
345 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (ispackage) {
348 /* add __path__ to the module *before* the code gets
349 executed */
350 PyObject *pkgpath, *fullpath;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400351 PyObject *subname = get_subname(fullname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000353
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400354 fullpath = PyUnicode_FromFormat("%U%c%U%U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 self->archive, SEP,
356 self->prefix, subname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400357 Py_DECREF(subname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 if (fullpath == NULL)
359 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000360
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400361 pkgpath = Py_BuildValue("[N]", fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (pkgpath == NULL)
363 goto error;
364 err = PyDict_SetItemString(dict, "__path__", pkgpath);
365 Py_DECREF(pkgpath);
366 if (err != 0)
367 goto error;
368 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400369 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
Victor Stinner26fabe12010-10-18 12:03:25 +0000370 Py_CLEAR(code);
371 if (mod == NULL)
372 goto error;
373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400375 PySys_FormatStderr("import %U # loaded from Zip %U\n",
Victor Stinner08654e12010-10-18 12:09:02 +0000376 fullname, modpath);
377 Py_DECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +0000379error:
Victor Stinner26fabe12010-10-18 12:03:25 +0000380 Py_XDECREF(code);
Victor Stinner08654e12010-10-18 12:09:02 +0000381 Py_XDECREF(modpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000383}
384
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000385/* Return a string matching __file__ for the named module */
386static PyObject *
387zipimporter_get_filename(PyObject *obj, PyObject *args)
388{
389 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400390 PyObject *fullname, *code, *modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000391 int ispackage;
392
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400393 if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename",
Victor Stinner9e40fad2010-10-18 22:34:46 +0000394 &fullname))
Victor Stinnerc342fca2010-10-18 11:39:05 +0000395 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000396
397 /* Deciding the filename requires working out where the code
398 would come from if the module was actually loaded */
399 code = get_module_code(self, fullname, &ispackage, &modpath);
400 if (code == NULL)
Victor Stinnerc342fca2010-10-18 11:39:05 +0000401 return NULL;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000402 Py_DECREF(code); /* Only need the path info */
403
Victor Stinner08654e12010-10-18 12:09:02 +0000404 return modpath;
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000405}
406
Just van Rossum52e14d62002-12-30 22:08:05 +0000407/* Return a bool signifying whether the module is a package or not. */
408static PyObject *
409zipimporter_is_package(PyObject *obj, PyObject *args)
410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400412 PyObject *fullname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000414
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400415 if (!PyArg_ParseTuple(args, "U:zipimporter.is_package",
Victor Stinner965a8a12010-10-18 21:44:33 +0000416 &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 mi = get_module_info(self, fullname);
420 if (mi == MI_ERROR)
Victor Stinner965a8a12010-10-18 21:44:33 +0000421 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400423 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000424 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 }
426 return PyBool_FromLong(mi == MI_PACKAGE);
Just van Rossum52e14d62002-12-30 22:08:05 +0000427}
428
429static PyObject *
430zipimporter_get_data(PyObject *obj, PyObject *args)
431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 ZipImporter *self = (ZipImporter *)obj;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000433 PyObject *pathobj, *key;
434 const Py_UNICODE *path;
Just van Rossum52e14d62002-12-30 22:08:05 +0000435#ifdef ALTSEP
Victor Stinner60fe8d92010-08-16 23:48:11 +0000436 Py_UNICODE *p, buf[MAXPATHLEN + 1];
Just van Rossum52e14d62002-12-30 22:08:05 +0000437#endif
Victor Stinner60fe8d92010-08-16 23:48:11 +0000438 Py_UNICODE *archive;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 PyObject *toc_entry;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000440 Py_ssize_t path_len, len;
Just van Rossum52e14d62002-12-30 22:08:05 +0000441
Victor Stinner60fe8d92010-08-16 23:48:11 +0000442 if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &pathobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000444
Victor Stinner60fe8d92010-08-16 23:48:11 +0000445 path_len = PyUnicode_GET_SIZE(pathobj);
446 path = PyUnicode_AS_UNICODE(pathobj);
Just van Rossum52e14d62002-12-30 22:08:05 +0000447#ifdef ALTSEP
Victor Stinner60fe8d92010-08-16 23:48:11 +0000448 if (path_len >= MAXPATHLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyErr_SetString(ZipImportError, "path too long");
450 return NULL;
451 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000452 Py_UNICODE_strcpy(buf, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 for (p = buf; *p; p++) {
454 if (*p == ALTSEP)
455 *p = SEP;
456 }
457 path = buf;
Just van Rossum52e14d62002-12-30 22:08:05 +0000458#endif
Victor Stinner60fe8d92010-08-16 23:48:11 +0000459 archive = PyUnicode_AS_UNICODE(self->archive);
460 len = PyUnicode_GET_SIZE(self->archive);
461 if ((size_t)len < Py_UNICODE_strlen(path) &&
462 Py_UNICODE_strncmp(path, archive, len) == 0 &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 path[len] == SEP) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000464 path += len + 1;
465 path_len -= len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000467
Victor Stinner60fe8d92010-08-16 23:48:11 +0000468 key = PyUnicode_FromUnicode(path, path_len);
469 if (key == NULL)
470 return NULL;
471 toc_entry = PyDict_GetItem(self->files, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (toc_entry == NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000473 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
474 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 return NULL;
476 }
Victor Stinner60fe8d92010-08-16 23:48:11 +0000477 Py_DECREF(key);
478 return get_data(self->archive, toc_entry);
Just van Rossum52e14d62002-12-30 22:08:05 +0000479}
480
481static PyObject *
482zipimporter_get_code(PyObject *obj, PyObject *args)
483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 ZipImporter *self = (ZipImporter *)obj;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400485 PyObject *fullname;
Just van Rossum52e14d62002-12-30 22:08:05 +0000486
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400487 if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 return get_module_code(self, fullname, NULL, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +0000491}
492
493static PyObject *
494zipimporter_get_source(PyObject *obj, PyObject *args)
495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 ZipImporter *self = (ZipImporter *)obj;
497 PyObject *toc_entry;
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400498 PyObject *fullname, *subname, *path, *fullpath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 enum zi_module_info mi;
Just van Rossum52e14d62002-12-30 22:08:05 +0000500
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400501 if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 mi = get_module_info(self, fullname);
Victor Stinner965a8a12010-10-18 21:44:33 +0000505 if (mi == MI_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return NULL;
Victor Stinner04106562010-10-18 20:44:08 +0000507 if (mi == MI_NOT_FOUND) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400508 PyErr_Format(ZipImportError, "can't find module %R", fullname);
Victor Stinner04106562010-10-18 20:44:08 +0000509 return NULL;
510 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400511
Victor Stinner965a8a12010-10-18 21:44:33 +0000512 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400513 if (subname == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000515
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400516 path = make_filename(self->prefix, subname);
517 Py_DECREF(subname);
518 if (path == NULL)
519 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000520
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400521 if (mi == MI_PACKAGE)
522 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
523 else
524 fullpath = PyUnicode_FromFormat("%U.py", path);
525 Py_DECREF(path);
526 if (fullpath == NULL)
527 return NULL;
528
529 toc_entry = PyDict_GetItem(self->files, fullpath);
530 Py_DECREF(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (toc_entry != NULL) {
Victor Stinner60fe8d92010-08-16 23:48:11 +0000532 PyObject *res, *bytes;
533 bytes = get_data(self->archive, toc_entry);
534 if (bytes == NULL)
535 return NULL;
536 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
537 PyBytes_GET_SIZE(bytes));
538 Py_DECREF(bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return res;
540 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 /* we have the module, but no source */
543 Py_INCREF(Py_None);
544 return Py_None;
Just van Rossum52e14d62002-12-30 22:08:05 +0000545}
546
547PyDoc_STRVAR(doc_find_module,
548"find_module(fullname, path=None) -> self or None.\n\
549\n\
550Search for a module specified by 'fullname'. 'fullname' must be the\n\
551fully qualified (dotted) module name. It returns the zipimporter\n\
552instance itself if the module was found, or None if it wasn't.\n\
553The optional 'path' argument is ignored -- it's there for compatibility\n\
554with the importer protocol.");
555
556PyDoc_STRVAR(doc_load_module,
557"load_module(fullname) -> module.\n\
558\n\
559Load the module specified by 'fullname'. 'fullname' must be the\n\
560fully qualified (dotted) module name. It returns the imported\n\
561module, or raises ZipImportError if it wasn't found.");
562
563PyDoc_STRVAR(doc_get_data,
564"get_data(pathname) -> string with file data.\n\
565\n\
566Return the data associated with 'pathname'. Raise IOError if\n\
567the file wasn't found.");
568
569PyDoc_STRVAR(doc_is_package,
570"is_package(fullname) -> bool.\n\
571\n\
572Return True if the module specified by fullname is a package.\n\
Brian Curtin32839732010-07-21 01:44:19 +0000573Raise ZipImportError if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000574
575PyDoc_STRVAR(doc_get_code,
576"get_code(fullname) -> code object.\n\
577\n\
578Return the code object for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000579if the module couldn't be found.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000580
581PyDoc_STRVAR(doc_get_source,
582"get_source(fullname) -> source string.\n\
583\n\
584Return the source code for the specified module. Raise ZipImportError\n\
Brian Curtin32839732010-07-21 01:44:19 +0000585if the module couldn't be found, return None if the archive does\n\
Just van Rossum52e14d62002-12-30 22:08:05 +0000586contain the module, but has no source for it.");
587
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000588
589PyDoc_STRVAR(doc_get_filename,
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000590"get_filename(fullname) -> filename string.\n\
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000591\n\
592Return the filename for the specified module.");
593
Just van Rossum52e14d62002-12-30 22:08:05 +0000594static PyMethodDef zipimporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 {"find_module", zipimporter_find_module, METH_VARARGS,
596 doc_find_module},
597 {"load_module", zipimporter_load_module, METH_VARARGS,
598 doc_load_module},
599 {"get_data", zipimporter_get_data, METH_VARARGS,
600 doc_get_data},
601 {"get_code", zipimporter_get_code, METH_VARARGS,
602 doc_get_code},
603 {"get_source", zipimporter_get_source, METH_VARARGS,
604 doc_get_source},
605 {"get_filename", zipimporter_get_filename, METH_VARARGS,
606 doc_get_filename},
607 {"is_package", zipimporter_is_package, METH_VARARGS,
608 doc_is_package},
609 {NULL, NULL} /* sentinel */
Just van Rossum52e14d62002-12-30 22:08:05 +0000610};
611
612static PyMemberDef zipimporter_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
614 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
615 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
616 {NULL}
Just van Rossum52e14d62002-12-30 22:08:05 +0000617};
618
619PyDoc_STRVAR(zipimporter_doc,
620"zipimporter(archivepath) -> zipimporter object\n\
621\n\
622Create a new zipimporter instance. 'archivepath' must be a path to\n\
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000623a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
624'/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
625valid directory inside the archive.\n\
626\n\
627'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
628archive.\n\
629\n\
630The 'archive' attribute of zipimporter objects contains the name of the\n\
631zipfile targeted.");
Just van Rossum52e14d62002-12-30 22:08:05 +0000632
633#define DEFERRED_ADDRESS(ADDR) 0
634
635static PyTypeObject ZipImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
637 "zipimport.zipimporter",
638 sizeof(ZipImporter),
639 0, /* tp_itemsize */
640 (destructor)zipimporter_dealloc, /* tp_dealloc */
641 0, /* tp_print */
642 0, /* tp_getattr */
643 0, /* tp_setattr */
644 0, /* tp_reserved */
645 (reprfunc)zipimporter_repr, /* tp_repr */
646 0, /* tp_as_number */
647 0, /* tp_as_sequence */
648 0, /* tp_as_mapping */
649 0, /* tp_hash */
650 0, /* tp_call */
651 0, /* tp_str */
652 PyObject_GenericGetAttr, /* tp_getattro */
653 0, /* tp_setattro */
654 0, /* tp_as_buffer */
655 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
656 Py_TPFLAGS_HAVE_GC, /* tp_flags */
657 zipimporter_doc, /* tp_doc */
658 zipimporter_traverse, /* tp_traverse */
659 0, /* tp_clear */
660 0, /* tp_richcompare */
661 0, /* tp_weaklistoffset */
662 0, /* tp_iter */
663 0, /* tp_iternext */
664 zipimporter_methods, /* tp_methods */
665 zipimporter_members, /* tp_members */
666 0, /* tp_getset */
667 0, /* tp_base */
668 0, /* tp_dict */
669 0, /* tp_descr_get */
670 0, /* tp_descr_set */
671 0, /* tp_dictoffset */
672 (initproc)zipimporter_init, /* tp_init */
673 PyType_GenericAlloc, /* tp_alloc */
674 PyType_GenericNew, /* tp_new */
675 PyObject_GC_Del, /* tp_free */
Just van Rossum52e14d62002-12-30 22:08:05 +0000676};
677
678
679/* implementation */
680
Just van Rossum52e14d62002-12-30 22:08:05 +0000681/* Given a buffer, return the long that is represented by the first
682 4 bytes, encoded as little endian. This partially reimplements
683 marshal.c:r_long() */
684static long
685get_long(unsigned char *buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 long x;
687 x = buf[0];
688 x |= (long)buf[1] << 8;
689 x |= (long)buf[2] << 16;
690 x |= (long)buf[3] << 24;
Just van Rossum52e14d62002-12-30 22:08:05 +0000691#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 /* Sign extension for 64-bit machines */
693 x |= -(x & 0x80000000L);
Just van Rossum52e14d62002-12-30 22:08:05 +0000694#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 return x;
Just van Rossum52e14d62002-12-30 22:08:05 +0000696}
697
698/*
699 read_directory(archive) -> files dict (new reference)
700
701 Given a path to a Zip archive, build a dict, mapping file names
702 (local to the archive, using SEP as a separator) to toc entries.
703
704 A toc_entry is a tuple:
705
Victor Stinner08654e12010-10-18 12:09:02 +0000706 (__file__, # value to use for __file__, available for all files,
707 # encoded to the filesystem encoding
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 compress, # compression kind; 0 for uncompressed
709 data_size, # size of compressed data on disk
710 file_size, # size of decompressed data
711 file_offset, # offset of file header from start of archive
712 time, # mod time of file (in dos format)
713 date, # mod data of file (in dos format)
714 crc, # crc checksum of the data
Victor Stinnerc342fca2010-10-18 11:39:05 +0000715 )
Just van Rossum52e14d62002-12-30 22:08:05 +0000716
717 Directories can be recognized by the trailing SEP in the name,
718 data_size and file_offset are 0.
719*/
720static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400721read_directory(PyObject *archive)
Just van Rossum52e14d62002-12-30 22:08:05 +0000722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 PyObject *files = NULL;
724 FILE *fp;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000725 unsigned short flags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 long compress, crc, data_size, file_size, file_offset, date, time;
727 long header_offset, name_size, header_size, header_position;
728 long i, l, count;
729 size_t length;
Victor Stinner2460a432010-08-16 17:54:28 +0000730 Py_UNICODE path[MAXPATHLEN + 5];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 char name[MAXPATHLEN + 5];
Victor Stinner2460a432010-08-16 17:54:28 +0000732 PyObject *nameobj = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 char *p, endof_central_dir[22];
734 long arc_offset; /* offset from beginning of file to start of zip-archive */
Victor Stinner2460a432010-08-16 17:54:28 +0000735 PyObject *pathobj;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000736 const char *charset;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000737 int bootstrap;
Just van Rossum52e14d62002-12-30 22:08:05 +0000738
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400739 if (PyUnicode_GET_SIZE(archive) > MAXPATHLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PyErr_SetString(PyExc_OverflowError,
741 "Zip path name is too long");
742 return NULL;
743 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400744 Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(archive));
Just van Rossum52e14d62002-12-30 22:08:05 +0000745
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400746 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (fp == NULL) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400748 PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 return NULL;
750 }
751 fseek(fp, -22, SEEK_END);
752 header_position = ftell(fp);
753 if (fread(endof_central_dir, 1, 22, fp) != 22) {
754 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400755 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 return NULL;
757 }
758 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
759 /* Bad: End of Central Dir signature */
760 fclose(fp);
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400761 PyErr_Format(ZipImportError, "not a Zip file: %R", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 return NULL;
763 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 header_size = get_long((unsigned char *)endof_central_dir + 12);
766 header_offset = get_long((unsigned char *)endof_central_dir + 16);
767 arc_offset = header_position - header_offset - header_size;
768 header_offset += arc_offset;
Just van Rossum52e14d62002-12-30 22:08:05 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 files = PyDict_New();
771 if (files == NULL)
772 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000773
Victor Stinner2460a432010-08-16 17:54:28 +0000774 length = Py_UNICODE_strlen(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 path[length] = SEP;
Just van Rossum52e14d62002-12-30 22:08:05 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 /* Start of Central Directory */
778 count = 0;
779 for (;;) {
780 PyObject *t;
781 int err;
Just van Rossum52e14d62002-12-30 22:08:05 +0000782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 fseek(fp, header_offset, 0); /* Start of file header */
784 l = PyMarshal_ReadLongFromFile(fp);
785 if (l != 0x02014B50)
786 break; /* Bad: Central Dir File Header */
Victor Stinnerd36c8212010-10-18 12:13:46 +0000787 fseek(fp, header_offset + 8, 0);
788 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 compress = PyMarshal_ReadShortFromFile(fp);
790 time = PyMarshal_ReadShortFromFile(fp);
791 date = PyMarshal_ReadShortFromFile(fp);
792 crc = PyMarshal_ReadLongFromFile(fp);
793 data_size = PyMarshal_ReadLongFromFile(fp);
794 file_size = PyMarshal_ReadLongFromFile(fp);
795 name_size = PyMarshal_ReadShortFromFile(fp);
796 header_size = 46 + name_size +
797 PyMarshal_ReadShortFromFile(fp) +
798 PyMarshal_ReadShortFromFile(fp);
799 fseek(fp, header_offset + 42, 0);
800 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
801 if (name_size > MAXPATHLEN)
802 name_size = MAXPATHLEN;
Just van Rossum52e14d62002-12-30 22:08:05 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 p = name;
805 for (i = 0; i < name_size; i++) {
806 *p = (char)getc(fp);
807 if (*p == '/')
808 *p = SEP;
809 p++;
810 }
811 *p = 0; /* Add terminating null byte */
812 header_offset += header_size;
Just van Rossum52e14d62002-12-30 22:08:05 +0000813
Victor Stinner4ee65a92011-01-22 10:30:29 +0000814 bootstrap = 0;
Victor Stinnerd36c8212010-10-18 12:13:46 +0000815 if (flags & 0x0800)
816 charset = "utf-8";
Victor Stinner4ee65a92011-01-22 10:30:29 +0000817 else if (!PyThreadState_GET()->interp->codecs_initialized) {
818 /* During bootstrap, we may need to load the encodings
819 package from a ZIP file. But the cp437 encoding is implemented
820 in Python in the encodings package.
821
822 Break out of this dependency by assuming that the path to
823 the encodings module is ASCII-only. */
824 charset = "ascii";
825 bootstrap = 1;
826 }
Victor Stinnerd36c8212010-10-18 12:13:46 +0000827 else
828 charset = "cp437";
829 nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
Victor Stinner4ee65a92011-01-22 10:30:29 +0000830 if (nameobj == NULL) {
831 if (bootstrap)
832 PyErr_Format(PyExc_NotImplementedError,
833 "bootstrap issue: python%i%i.zip contains non-ASCII "
834 "filenames without the unicode flag",
835 PY_MAJOR_VERSION, PY_MINOR_VERSION);
Victor Stinner2460a432010-08-16 17:54:28 +0000836 goto error;
Victor Stinner4ee65a92011-01-22 10:30:29 +0000837 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400838 Py_UNICODE_strncpy(path + length + 1,
839 PyUnicode_AS_UNICODE(nameobj),
840 MAXPATHLEN - length - 1);
Just van Rossum52e14d62002-12-30 22:08:05 +0000841
Victor Stinner2460a432010-08-16 17:54:28 +0000842 pathobj = PyUnicode_FromUnicode(path, Py_UNICODE_strlen(path));
843 if (pathobj == NULL)
844 goto error;
845 t = Py_BuildValue("Niiiiiii", pathobj, compress, data_size,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 file_size, file_offset, time, date, crc);
847 if (t == NULL)
848 goto error;
Victor Stinner2460a432010-08-16 17:54:28 +0000849 err = PyDict_SetItem(files, nameobj, t);
850 Py_CLEAR(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 Py_DECREF(t);
852 if (err != 0)
853 goto error;
854 count++;
855 }
856 fclose(fp);
857 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -0400858 PySys_FormatStderr("# zipimport: found %ld names in %R\n",
859 count, archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 return files;
Just van Rossum52e14d62002-12-30 22:08:05 +0000861error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 fclose(fp);
863 Py_XDECREF(files);
Victor Stinner2460a432010-08-16 17:54:28 +0000864 Py_XDECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000866}
867
868/* Return the zlib.decompress function object, or NULL if zlib couldn't
869 be imported. The function is cached when found, so subsequent calls
Victor Stinner4925cde2011-05-20 00:16:09 +0200870 don't import zlib again. */
Just van Rossum52e14d62002-12-30 22:08:05 +0000871static PyObject *
872get_decompress_func(void)
873{
Victor Stinner4925cde2011-05-20 00:16:09 +0200874 static int importing_zlib = 0;
875 PyObject *zlib;
876 PyObject *decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +0000877
Victor Stinner4925cde2011-05-20 00:16:09 +0200878 if (importing_zlib != 0)
879 /* Someone has a zlib.py[co] in their Zip file;
880 let's avoid a stack overflow. */
881 return NULL;
882 importing_zlib = 1;
883 zlib = PyImport_ImportModuleNoBlock("zlib");
884 importing_zlib = 0;
885 if (zlib != NULL) {
886 decompress = PyObject_GetAttrString(zlib,
887 "decompress");
888 Py_DECREF(zlib);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 }
Victor Stinner4925cde2011-05-20 00:16:09 +0200890 else {
891 PyErr_Clear();
892 decompress = NULL;
893 }
894 if (Py_VerboseFlag)
895 PySys_WriteStderr("# zipimport: zlib %s\n",
896 zlib != NULL ? "available": "UNAVAILABLE");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 return decompress;
Just van Rossum52e14d62002-12-30 22:08:05 +0000898}
899
900/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
901 data as a new reference. */
902static PyObject *
Victor Stinner60fe8d92010-08-16 23:48:11 +0000903get_data(PyObject *archive, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +0000904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 PyObject *raw_data, *data = NULL, *decompress;
906 char *buf;
907 FILE *fp;
908 int err;
909 Py_ssize_t bytes_read = 0;
910 long l;
Victor Stinner60fe8d92010-08-16 23:48:11 +0000911 PyObject *datapath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 long compress, data_size, file_size, file_offset, bytes_size;
913 long time, date, crc;
Just van Rossum52e14d62002-12-30 22:08:05 +0000914
Victor Stinner60fe8d92010-08-16 23:48:11 +0000915 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 &data_size, &file_size, &file_offset, &time,
917 &date, &crc)) {
918 return NULL;
919 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000920
Victor Stinner60fe8d92010-08-16 23:48:11 +0000921 fp = _Py_fopen(archive, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 if (!fp) {
923 PyErr_Format(PyExc_IOError,
Victor Stinner60fe8d92010-08-16 23:48:11 +0000924 "zipimport: can not open file %U", archive);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return NULL;
926 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* Check to make sure the local file header is correct */
929 fseek(fp, file_offset, 0);
930 l = PyMarshal_ReadLongFromFile(fp);
931 if (l != 0x04034B50) {
932 /* Bad: Local File Header */
933 PyErr_Format(ZipImportError,
Victor Stinner60fe8d92010-08-16 23:48:11 +0000934 "bad local file header in %U",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 archive);
936 fclose(fp);
937 return NULL;
938 }
939 fseek(fp, file_offset + 26, 0);
940 l = 30 + PyMarshal_ReadShortFromFile(fp) +
941 PyMarshal_ReadShortFromFile(fp); /* local header size */
942 file_offset += l; /* Start of file data */
Just van Rossum52e14d62002-12-30 22:08:05 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 bytes_size = compress == 0 ? data_size : data_size + 1;
945 if (bytes_size == 0)
946 bytes_size++;
947 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
Just van Rossum52e14d62002-12-30 22:08:05 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (raw_data == NULL) {
950 fclose(fp);
951 return NULL;
952 }
953 buf = PyBytes_AsString(raw_data);
Just van Rossum52e14d62002-12-30 22:08:05 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 err = fseek(fp, file_offset, 0);
956 if (err == 0)
957 bytes_read = fread(buf, 1, data_size, fp);
958 fclose(fp);
959 if (err || bytes_read != data_size) {
960 PyErr_SetString(PyExc_IOError,
961 "zipimport: can't read data");
962 Py_DECREF(raw_data);
963 return NULL;
964 }
Just van Rossum52e14d62002-12-30 22:08:05 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (compress != 0) {
967 buf[data_size] = 'Z'; /* saw this in zipfile.py */
968 data_size++;
969 }
970 buf[data_size] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (compress == 0) { /* data is not compressed */
973 data = PyBytes_FromStringAndSize(buf, data_size);
974 Py_DECREF(raw_data);
975 return data;
976 }
977
978 /* Decompress with zlib */
979 decompress = get_decompress_func();
980 if (decompress == NULL) {
981 PyErr_SetString(ZipImportError,
982 "can't decompress data; "
983 "zlib not available");
984 goto error;
985 }
986 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Victor Stinner4925cde2011-05-20 00:16:09 +0200987 Py_DECREF(decompress);
Just van Rossum52e14d62002-12-30 22:08:05 +0000988error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 Py_DECREF(raw_data);
990 return data;
Just van Rossum52e14d62002-12-30 22:08:05 +0000991}
992
993/* Lenient date/time comparison function. The precision of the mtime
994 in the archive is lower than the mtime stored in a .pyc: we
995 must allow a difference of at most one second. */
996static int
997eq_mtime(time_t t1, time_t t2)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 time_t d = t1 - t2;
1000 if (d < 0)
1001 d = -d;
1002 /* dostime only stores even seconds, so be lenient */
1003 return d <= 1;
Just van Rossum52e14d62002-12-30 22:08:05 +00001004}
1005
1006/* Given the contents of a .py[co] file in a buffer, unmarshal the data
1007 and return the code object. Return None if it the magic word doesn't
1008 match (we do this instead of raising an exception as we fall back
1009 to .py if available and we don't want to mask other errors).
1010 Returns a new reference. */
1011static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001012unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
Just van Rossum52e14d62002-12-30 22:08:05 +00001013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyObject *code;
1015 char *buf = PyBytes_AsString(data);
1016 Py_ssize_t size = PyBytes_Size(data);
Just van Rossum52e14d62002-12-30 22:08:05 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 if (size <= 9) {
1019 PyErr_SetString(ZipImportError,
1020 "bad pyc data");
1021 return NULL;
1022 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
1025 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001026 PySys_FormatStderr("# %R has bad magic\n",
1027 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 Py_INCREF(Py_None);
1029 return Py_None; /* signal caller to try alternative */
1030 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
1033 mtime)) {
1034 if (Py_VerboseFlag)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001035 PySys_FormatStderr("# %R has bad mtime\n",
1036 pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 Py_INCREF(Py_None);
1038 return Py_None; /* signal caller to try alternative */
1039 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
1042 if (code == NULL)
1043 return NULL;
1044 if (!PyCode_Check(code)) {
1045 Py_DECREF(code);
1046 PyErr_Format(PyExc_TypeError,
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001047 "compiled module %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 pathname);
1049 return NULL;
1050 }
1051 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001052}
1053
1054/* Replace any occurances of "\r\n?" in the input string with "\n".
1055 This converts DOS and Mac line endings to Unix line endings.
1056 Also append a trailing "\n" to be compatible with
1057 PyParser_SimpleParseFile(). Returns a new reference. */
1058static PyObject *
1059normalize_line_endings(PyObject *source)
1060{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001061 char *buf, *q, *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyObject *fixed_source;
1063 int len = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +00001064
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001065 p = PyBytes_AsString(source);
1066 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 return PyBytes_FromStringAndSize("\n\0", 2);
1068 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* one char extra for trailing \n and one for terminating \0 */
1071 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
1072 if (buf == NULL) {
1073 PyErr_SetString(PyExc_MemoryError,
1074 "zipimport: no memory to allocate "
1075 "source buffer");
1076 return NULL;
1077 }
1078 /* replace "\r\n?" by "\n" */
1079 for (q = buf; *p != '\0'; p++) {
1080 if (*p == '\r') {
1081 *q++ = '\n';
1082 if (*(p + 1) == '\n')
1083 p++;
1084 }
1085 else
1086 *q++ = *p;
1087 len++;
1088 }
1089 *q++ = '\n'; /* add trailing \n */
1090 *q = '\0';
1091 fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
1092 PyMem_Free(buf);
1093 return fixed_source;
Just van Rossum52e14d62002-12-30 22:08:05 +00001094}
1095
1096/* Given a string buffer containing Python source code, compile it
1097 return and return a code object as a new reference. */
1098static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001099compile_source(PyObject *pathname, PyObject *source)
Just van Rossum52e14d62002-12-30 22:08:05 +00001100{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001101 PyObject *code, *fixed_source, *pathbytes;
Just van Rossum52e14d62002-12-30 22:08:05 +00001102
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001103 pathbytes = PyUnicode_EncodeFSDefault(pathname);
1104 if (pathbytes == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001106
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001107 fixed_source = normalize_line_endings(source);
1108 if (fixed_source == NULL) {
1109 Py_DECREF(pathbytes);
1110 return NULL;
1111 }
1112
1113 code = Py_CompileString(PyBytes_AsString(fixed_source),
1114 PyBytes_AsString(pathbytes),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 Py_file_input);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001116 Py_DECREF(pathbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 Py_DECREF(fixed_source);
1118 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001119}
1120
1121/* Convert the date/time values found in the Zip archive to a value
1122 that's compatible with the time stamp stored in .pyc files. */
Neal Norwitz29fd2ba2003-03-23 13:21:03 +00001123static time_t
1124parse_dostime(int dostime, int dosdate)
Just van Rossum52e14d62002-12-30 22:08:05 +00001125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 struct tm stm;
Just van Rossum52e14d62002-12-30 22:08:05 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 memset((void *) &stm, '\0', sizeof(stm));
Christian Heimes679db4a2008-01-18 09:56:22 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 stm.tm_sec = (dostime & 0x1f) * 2;
1131 stm.tm_min = (dostime >> 5) & 0x3f;
1132 stm.tm_hour = (dostime >> 11) & 0x1f;
1133 stm.tm_mday = dosdate & 0x1f;
1134 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
1135 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
1136 stm.tm_isdst = -1; /* wday/yday is ignored */
Just van Rossum52e14d62002-12-30 22:08:05 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 return mktime(&stm);
Just van Rossum52e14d62002-12-30 22:08:05 +00001139}
1140
1141/* Given a path to a .pyc or .pyo file in the archive, return the
Ezio Melotti13925002011-03-16 11:05:33 +02001142 modification time of the matching .py file, or 0 if no source
Just van Rossum52e14d62002-12-30 22:08:05 +00001143 is available. */
1144static time_t
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001145get_mtime_of_source(ZipImporter *self, PyObject *path)
Just van Rossum52e14d62002-12-30 22:08:05 +00001146{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001147 PyObject *toc_entry, *stripped;
1148 time_t mtime;
1149
1150 /* strip 'c' or 'o' from *.py[co] */
1151 stripped = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(path),
1152 PyUnicode_GET_SIZE(path) - 1);
1153 if (stripped == NULL)
1154 return (time_t)-1;
1155
1156 toc_entry = PyDict_GetItem(self->files, stripped);
1157 Py_DECREF(stripped);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
1159 PyTuple_Size(toc_entry) == 8) {
1160 /* fetch the time stamp of the .py file for comparison
1161 with an embedded pyc time stamp */
1162 int time, date;
1163 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
1164 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
1165 mtime = parse_dostime(time, date);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001166 } else
1167 mtime = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 return mtime;
Just van Rossum52e14d62002-12-30 22:08:05 +00001169}
1170
1171/* Return the code object for the module named by 'fullname' from the
1172 Zip archive as a new reference. */
1173static PyObject *
1174get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 time_t mtime, PyObject *toc_entry)
Just van Rossum52e14d62002-12-30 22:08:05 +00001176{
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001177 PyObject *data, *modpath, *code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001178
Victor Stinner60fe8d92010-08-16 23:48:11 +00001179 data = get_data(self->archive, toc_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (data == NULL)
1181 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001182
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001183 modpath = PyTuple_GetItem(toc_entry, 0);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001184 if (isbytecode)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001185 code = unmarshal_code(modpath, data, mtime);
Victor Stinner2a94f4c2010-10-18 12:15:34 +00001186 else
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001187 code = compile_source(modpath, data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 Py_DECREF(data);
1189 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001190}
1191
Ezio Melotti42da6632011-03-15 05:18:48 +02001192/* Get the code object associated with the module specified by
Just van Rossum52e14d62002-12-30 22:08:05 +00001193 'fullname'. */
1194static PyObject *
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001195get_module_code(ZipImporter *self, PyObject *fullname,
Victor Stinner08654e12010-10-18 12:09:02 +00001196 int *p_ispackage, PyObject **p_modpath)
Just van Rossum52e14d62002-12-30 22:08:05 +00001197{
Gregory P. Smith95c7c462011-05-21 05:19:42 -07001198 PyObject *code = NULL, *toc_entry, *subname;
Victor Stinner9a2261a2011-05-26 13:59:41 +02001199 PyObject *path, *fullpath = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 struct st_zip_searchorder *zso;
Just van Rossum52e14d62002-12-30 22:08:05 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 subname = get_subname(fullname);
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001203 if (subname == NULL)
1204 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001205
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001206 path = make_filename(self->prefix, subname);
1207 Py_DECREF(subname);
1208 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 for (zso = zip_searchorder; *zso->suffix; zso++) {
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001212 code = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001213
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001214 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
1215 if (fullpath == NULL)
1216 goto exit;
1217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (Py_VerboseFlag > 1)
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001219 PySys_FormatStderr("# trying %U%c%U\n",
1220 self->archive, (int)SEP, fullpath);
1221 toc_entry = PyDict_GetItem(self->files, fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 if (toc_entry != NULL) {
1223 time_t mtime = 0;
1224 int ispackage = zso->type & IS_PACKAGE;
1225 int isbytecode = zso->type & IS_BYTECODE;
Just van Rossum52e14d62002-12-30 22:08:05 +00001226
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001227 if (isbytecode) {
1228 mtime = get_mtime_of_source(self, fullpath);
1229 if (mtime == (time_t)-1 && PyErr_Occurred()) {
1230 goto exit;
1231 }
1232 }
1233 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 if (p_ispackage != NULL)
1235 *p_ispackage = ispackage;
1236 code = get_code_from_data(self, ispackage,
1237 isbytecode, mtime,
1238 toc_entry);
1239 if (code == Py_None) {
1240 /* bad magic number or non-matching mtime
1241 in byte code, try next */
1242 Py_DECREF(code);
1243 continue;
1244 }
Victor Stinner08654e12010-10-18 12:09:02 +00001245 if (code != NULL && p_modpath != NULL) {
1246 *p_modpath = PyTuple_GetItem(toc_entry, 0);
1247 Py_INCREF(*p_modpath);
1248 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001249 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001251 else
1252 Py_CLEAR(fullpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 }
Victor Stinnerf6b563a2011-03-14 20:46:50 -04001254 PyErr_Format(ZipImportError, "can't find module %R", fullname);
1255exit:
1256 Py_DECREF(path);
1257 Py_XDECREF(fullpath);
1258 return code;
Just van Rossum52e14d62002-12-30 22:08:05 +00001259}
1260
1261
1262/* Module init */
1263
1264PyDoc_STRVAR(zipimport_doc,
1265"zipimport provides support for importing Python modules from Zip archives.\n\
1266\n\
1267This module exports three objects:\n\
1268- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
Fredrik Lundhb84b35f2006-01-15 15:00:40 +00001269- ZipImportError: exception raised by zipimporter objects. It's a\n\
Just van Rossum52e14d62002-12-30 22:08:05 +00001270 subclass of ImportError, so it can be caught as ImportError, too.\n\
1271- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
1272 info dicts, as used in zipimporter._files.\n\
1273\n\
1274It is usually not needed to use the zipimport module explicitly; it is\n\
1275used by the builtin import mechanism for sys.path items that are paths\n\
1276to Zip archives.");
1277
Martin v. Löwis1a214512008-06-11 05:26:20 +00001278static struct PyModuleDef zipimportmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 PyModuleDef_HEAD_INIT,
1280 "zipimport",
1281 zipimport_doc,
1282 -1,
1283 NULL,
1284 NULL,
1285 NULL,
1286 NULL,
1287 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001288};
1289
Just van Rossum52e14d62002-12-30 22:08:05 +00001290PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001291PyInit_zipimport(void)
Just van Rossum52e14d62002-12-30 22:08:05 +00001292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 PyObject *mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (PyType_Ready(&ZipImporter_Type) < 0)
1296 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 /* Correct directory separator */
1299 zip_searchorder[0].suffix[0] = SEP;
1300 zip_searchorder[1].suffix[0] = SEP;
1301 zip_searchorder[2].suffix[0] = SEP;
1302 if (Py_OptimizeFlag) {
1303 /* Reverse *.pyc and *.pyo */
1304 struct st_zip_searchorder tmp;
1305 tmp = zip_searchorder[0];
1306 zip_searchorder[0] = zip_searchorder[1];
1307 zip_searchorder[1] = tmp;
1308 tmp = zip_searchorder[3];
1309 zip_searchorder[3] = zip_searchorder[4];
1310 zip_searchorder[4] = tmp;
1311 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 mod = PyModule_Create(&zipimportmodule);
1314 if (mod == NULL)
1315 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
1318 PyExc_ImportError, NULL);
1319 if (ZipImportError == NULL)
1320 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 Py_INCREF(ZipImportError);
1323 if (PyModule_AddObject(mod, "ZipImportError",
1324 ZipImportError) < 0)
1325 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 Py_INCREF(&ZipImporter_Type);
1328 if (PyModule_AddObject(mod, "zipimporter",
1329 (PyObject *)&ZipImporter_Type) < 0)
1330 return NULL;
Just van Rossumf8b6de12002-12-31 09:51:59 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 zip_directory_cache = PyDict_New();
1333 if (zip_directory_cache == NULL)
1334 return NULL;
1335 Py_INCREF(zip_directory_cache);
1336 if (PyModule_AddObject(mod, "_zip_directory_cache",
1337 zip_directory_cache) < 0)
1338 return NULL;
1339 return mod;
Just van Rossum52e14d62002-12-30 22:08:05 +00001340}