annotate Modules/zipimport.c @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 3da31778ecd7
children 5e5eefa25f3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1 #include "Python.h"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
2 #include "structmember.h"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
3 #include "osdefs.h"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
4 #include "marshal.h"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
5 #include "compile.h"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
6 #include <time.h>
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
7
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
8
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
9 #define IS_SOURCE 0x0
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
10 #define IS_BYTECODE 0x1
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
11 #define IS_PACKAGE 0x2
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
12
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
13 struct st_zip_searchorder {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
14 char suffix[14];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
15 int type;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
16 };
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
17
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
18 /* zip_searchorder defines how we search for a module in the Zip
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
19 archive: we first search for a package __init__, then for
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
20 non-package .pyc, .pyo and .py entries. The .pyc and .pyo entries
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
21 are swapped by initzipimport() if we run in optimized mode. Also,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
22 '/' is replaced by SEP there. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
23 struct st_zip_searchorder zip_searchorder[] = {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
24 {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
25 {"/__init__.pyo", IS_PACKAGE | IS_BYTECODE},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
26 {"/__init__.py", IS_PACKAGE | IS_SOURCE},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
27 {".pyc", IS_BYTECODE},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
28 {".pyo", IS_BYTECODE},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
29 {".py", IS_SOURCE},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
30 {"", 0}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
31 };
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
32
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
33 /* zipimporter object definition and support */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
34
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
35 typedef struct _zipimporter ZipImporter;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
36
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
37 struct _zipimporter {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
38 PyObject_HEAD
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
39 PyObject *archive; /* pathname of the Zip archive */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
40 PyObject *prefix; /* file prefix: "a/sub/directory/" */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
41 PyObject *files; /* dict with file info {path: toc_entry} */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
42 };
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
43
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
44 static PyTypeObject ZipImporter_Type;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
45 static PyObject *ZipImportError;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
46 static PyObject *zip_directory_cache = NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
47
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
48 /* forward decls */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
49 static PyObject *read_directory(char *archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
50 static PyObject *get_data(char *archive, PyObject *toc_entry);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
51 static PyObject *get_module_code(ZipImporter *self, char *fullname,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
52 int *p_ispackage, char **p_modpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
53
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
54
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
55 #define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
56
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
57
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
58 /* zipimporter.__init__
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
59 Split the "subdirectory" from the Zip archive path, lookup a matching
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
60 entry in sys.path_importer_cache, fetch the file directory from there
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
61 if found, or else read it from the archive. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
62 static int
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
63 zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
64 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
65 char *path, *p, *prefix, buf[MAXPATHLEN+2];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
66 int len;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
67
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
68 if (!PyArg_ParseTuple(args, "s:zipimporter",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
69 &path))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
70 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
71
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
72 len = strlen(path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
73 if (len == 0) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
74 PyErr_SetString(ZipImportError, "archive path is empty");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
75 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
76 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
77 if (len >= MAXPATHLEN) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
78 PyErr_SetString(ZipImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
79 "archive path too long");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
80 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
81 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
82 strcpy(buf, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
83
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
84 #ifdef ALTSEP
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
85 for (p = buf; *p; p++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
86 if (*p == ALTSEP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
87 *p = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
88 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
89 #endif
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
90
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
91 path = NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
92 prefix = NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
93 for (;;) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
94 struct stat statbuf;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
95 int rv;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
96
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
97 rv = stat(buf, &statbuf);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
98 if (rv == 0) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
99 /* it exists */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
100 if (S_ISREG(statbuf.st_mode))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
101 /* it's a file */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
102 path = buf;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
103 break;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
104 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
105 /* back up one path element */
27021
727bcb811c50 Ugh, zipimport is virtually broken in 2.3a1 :-( It worked by accident in
Just van Rossum <just@letterror.com>
parents: 26957
diff changeset
106 p = strrchr(buf, SEP);
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
107 if (prefix != NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
108 *prefix = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
109 if (p == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
110 break;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
111 *p = '\0';
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
112 prefix = p;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
113 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
114 if (path != NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
115 PyObject *files;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
116 files = PyDict_GetItemString(zip_directory_cache, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
117 if (files == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
118 files = read_directory(buf);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
119 if (files == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
120 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
121 if (PyDict_SetItemString(zip_directory_cache, path,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
122 files) != 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
123 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
124 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
125 else
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
126 Py_INCREF(files);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
127 self->files = files;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
128 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
129 else {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
130 PyErr_SetString(ZipImportError, "not a Zip file");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
131 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
132 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
133
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
134 if (prefix == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
135 prefix = "";
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
136 else {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
137 prefix++;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
138 len = strlen(prefix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
139 if (prefix[len-1] != SEP) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
140 /* add trailing SEP */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
141 prefix[len] = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
142 prefix[len + 1] = '\0';
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
143 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
144 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
145
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
146 self->archive = PyString_FromString(buf);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
147 if (self->archive == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
148 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
149
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
150 self->prefix = PyString_FromString(prefix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
151 if (self->prefix == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
152 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
153
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
154 return 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
155 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
156
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
157 /* GC support. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
158 static int
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
159 zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
160 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
161 ZipImporter *self = (ZipImporter *)obj;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
162 int err;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
163
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
164 if (self->files != NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
165 err = visit(self->files, arg);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
166 if (err)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
167 return err;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
168 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
169 return 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
170 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
171
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
172 static void
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
173 zipimporter_dealloc(ZipImporter *self)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
174 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
175 PyObject_GC_UnTrack(self);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
176 Py_XDECREF(self->archive);
26931
6c535dcedad3 - added missing decref
Just van Rossum <just@letterror.com>
parents: 26901
diff changeset
177 Py_XDECREF(self->prefix);
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
178 Py_XDECREF(self->files);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
179 self->ob_type->tp_free((PyObject *)self);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
180 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
181
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
182 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
183 zipimporter_repr(ZipImporter *self)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
184 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
185 char buf[500];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
186 char *archive = "???";
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
187 char *prefix = "";
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
188
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
189 if (self->archive != NULL && PyString_Check(self->archive))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
190 archive = PyString_AsString(self->archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
191 if (self->prefix != NULL && PyString_Check(self->prefix))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
192 prefix = PyString_AsString(self->prefix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
193 if (prefix != NULL && *prefix)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
194 PyOS_snprintf(buf, sizeof(buf),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
195 "<zipimporter object \"%.300s%c%.150s\">",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
196 archive, SEP, prefix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
197 else
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
198 PyOS_snprintf(buf, sizeof(buf),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
199 "<zipimporter object \"%.300s\">",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
200 archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
201 return PyString_FromString(buf);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
202 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
203
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
204 /* return fullname.split(".")[-1] */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
205 static char *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
206 get_subname(char *fullname)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
207 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
208 char *subname = strrchr(fullname, '.');
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
209 if (subname == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
210 subname = fullname;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
211 else
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
212 subname++;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
213 return subname;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
214 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
215
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
216 /* Given a (sub)modulename, write the potential file path in the
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
217 archive (without extension) to the path buffer. Return the
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
218 length of the resulting string. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
219 static int
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
220 make_filename(char *prefix, char *name, char *path)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
221 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
222 int len;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
223 char *p;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
224
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
225 len = strlen(prefix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
226
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
227 /* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
228 if (len + strlen(name) + 13 >= MAXPATHLEN) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
229 PyErr_SetString(ZipImportError, "path too long");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
230 return -1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
231 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
232
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
233 strcpy(path, prefix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
234 strcpy(path + len, name);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
235 for (p = path + len; *p; p++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
236 if (*p == '.')
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
237 *p = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
238 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
239 len += strlen(name);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
240 return len;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
241 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
242
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
243 enum module_info {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
244 MI_ERROR,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
245 MI_NOT_FOUND,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
246 MI_MODULE,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
247 MI_PACKAGE
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
248 };
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
249
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
250 /* Return some information about a module. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
251 static enum module_info
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
252 get_module_info(ZipImporter *self, char *fullname)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
253 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
254 char *subname, path[MAXPATHLEN + 1];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
255 int len;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
256 struct st_zip_searchorder *zso;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
257
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
258 subname = get_subname(fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
259
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
260 len = make_filename(PyString_AsString(self->prefix), subname, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
261 if (len < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
262 return MI_ERROR;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
263
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
264 for (zso = zip_searchorder; *zso->suffix; zso++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
265 strcpy(path + len, zso->suffix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
266 if (PyDict_GetItemString(self->files, path) != NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
267 if (zso->type & IS_PACKAGE)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
268 return MI_PACKAGE;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
269 else
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
270 return MI_MODULE;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
271 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
272 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
273 return MI_NOT_FOUND;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
274 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
275
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
276 /* Check whether we can satisfy the import of the module named by
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
277 'fullname'. Return self if we can, None if we can't. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
278 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
279 zipimporter_find_module(PyObject *obj, PyObject *args)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
280 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
281 ZipImporter *self = (ZipImporter *)obj;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
282 PyObject *path = NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
283 char *fullname;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
284 enum module_info mi;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
285
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
286 if (!PyArg_ParseTuple(args, "s|O:zipimporter.find_module",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
287 &fullname, &path))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
288 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
289
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
290 mi = get_module_info(self, fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
291 if (mi == MI_ERROR)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
292 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
293 if (mi == MI_NOT_FOUND) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
294 Py_INCREF(Py_None);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
295 return Py_None;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
296 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
297 Py_INCREF(self);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
298 return (PyObject *)self;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
299 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
300
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
301 /* Load and return the module named by 'fullname'. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
302 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
303 zipimporter_load_module(PyObject *obj, PyObject *args)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
304 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
305 ZipImporter *self = (ZipImporter *)obj;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
306 PyObject *code, *mod, *dict;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
307 char *fullname, *modpath;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
308 int ispackage;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
309
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
310 if (!PyArg_ParseTuple(args, "s:zipimporter.load_module",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
311 &fullname))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
312 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
313
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
314 code = get_module_code(self, fullname, &ispackage, &modpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
315 if (code == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
316 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
317
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
318 mod = PyImport_AddModule(fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
319 if (mod == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
320 Py_DECREF(code);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
321 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
322 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
323 dict = PyModule_GetDict(mod);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
324
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
325 /* mod.__loader__ = self */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
326 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
327 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
328
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
329 if (ispackage) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
330 /* add __path__ to the module *before* the code gets
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
331 executed */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
332 PyObject *pkgpath, *fullpath;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
333 char *prefix = PyString_AsString(self->prefix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
334 char *subname = get_subname(fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
335 int err;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
336
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
337 fullpath = PyString_FromFormat("%s%c%s%s",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
338 PyString_AsString(self->archive),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
339 SEP,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
340 *prefix ? prefix : "",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
341 subname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
342 if (fullpath == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
343 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
344
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
345 pkgpath = Py_BuildValue("[O]", fullpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
346 Py_DECREF(fullpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
347 if (pkgpath == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
348 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
349 err = PyDict_SetItemString(dict, "__path__", pkgpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
350 Py_DECREF(pkgpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
351 if (err != 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
352 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
353 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
354 mod = PyImport_ExecCodeModuleEx(fullname, code, modpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
355 Py_DECREF(code);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
356 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
357 PySys_WriteStderr("import %s # loaded from Zip %s\n",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
358 fullname, modpath);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
359 return mod;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
360 error:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
361 Py_DECREF(code);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
362 Py_DECREF(mod);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
363 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
364 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
365
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
366 /* Return a bool signifying whether the module is a package or not. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
367 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
368 zipimporter_is_package(PyObject *obj, PyObject *args)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
369 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
370 ZipImporter *self = (ZipImporter *)obj;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
371 char *fullname;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
372 enum module_info mi;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
373
27915
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27047
diff changeset
374 if (!PyArg_ParseTuple(args, "s:zipimporter.is_package",
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
375 &fullname))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
376 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
377
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
378 mi = get_module_info(self, fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
379 if (mi == MI_ERROR)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
380 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
381 if (mi == MI_NOT_FOUND) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
382 PyErr_Format(ZipImportError, "can't find module '%.200s'",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
383 fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
384 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
385 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
386 return PyBool_FromLong(mi == MI_PACKAGE);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
387 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
388
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
389 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
390 zipimporter_get_data(PyObject *obj, PyObject *args)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
391 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
392 ZipImporter *self = (ZipImporter *)obj;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
393 char *path;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
394 #ifdef ALTSEP
26894
140644f22f64 Wouldn't compile on Windows; fixed.
Tim Peters <tim.peters@gmail.com>
parents: 26885
diff changeset
395 char *p, buf[MAXPATHLEN + 1];
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
396 #endif
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
397 PyObject *toc_entry;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
398 int len;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
399
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
400 if (!PyArg_ParseTuple(args, "s:zipimporter.get_data", &path))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
401 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
402
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
403 #ifdef ALTSEP
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
404 if (strlen(path) >= MAXPATHLEN) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
405 PyErr_SetString(ZipImportError, "path too long");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
406 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
407 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
408 strcpy(buf, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
409 for (p = buf; *p; p++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
410 if (*p == ALTSEP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
411 *p = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
412 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
413 path = buf;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
414 #endif
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
415 len = PyString_Size(self->archive);
26895
0a0c759ed167 Squashed compiler wng from MSVC6.
Tim Peters <tim.peters@gmail.com>
parents: 26894
diff changeset
416 if ((size_t)len < strlen(path) &&
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
417 strncmp(path, PyString_AsString(self->archive), len) == 0 &&
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
418 path[len] == SEP) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
419 path = path + len + 1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
420 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
421
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
422 toc_entry = PyDict_GetItemString(self->files, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
423 if (toc_entry == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
424 PyErr_Format(PyExc_IOError, "file not found [%.200s]",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
425 path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
426 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
427 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
428 return get_data(PyString_AsString(self->archive), toc_entry);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
429 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
430
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
431 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
432 zipimporter_get_code(PyObject *obj, PyObject *args)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
433 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
434 ZipImporter *self = (ZipImporter *)obj;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
435 char *fullname;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
436
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
437 if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
438 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
439
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
440 return get_module_code(self, fullname, NULL, NULL);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
441 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
442
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
443 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
444 zipimporter_get_source(PyObject *obj, PyObject *args)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
445 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
446 ZipImporter *self = (ZipImporter *)obj;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
447 PyObject *toc_entry;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
448 char *fullname, *subname, path[MAXPATHLEN+1];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
449 int len;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
450 enum module_info mi;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
451
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
452 if (!PyArg_ParseTuple(args, "s:zipimporter.get_source", &fullname))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
453 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
454
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
455 mi = get_module_info(self, fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
456 if (mi == MI_ERROR)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
457 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
458 if (mi == MI_NOT_FOUND) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
459 PyErr_Format(ZipImportError, "can't find module '%.200s'",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
460 fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
461 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
462 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
463 subname = get_subname(fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
464
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
465 len = make_filename(PyString_AsString(self->prefix), subname, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
466 if (len < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
467 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
468
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
469 if (mi == MI_PACKAGE) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
470 path[len] = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
471 strcpy(path + len + 1, "__init__.py");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
472 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
473 else
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
474 strcpy(path + len, ".py");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
475
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
476 toc_entry = PyDict_GetItemString(self->files, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
477 if (toc_entry != NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
478 return get_data(PyString_AsString(self->archive), toc_entry);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
479
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
480 /* we have the module, but no source */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
481 Py_INCREF(Py_None);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
482 return Py_None;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
483 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
484
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
485 PyDoc_STRVAR(doc_find_module,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
486 "find_module(fullname, path=None) -> self or None.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
487 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
488 Search for a module specified by 'fullname'. 'fullname' must be the\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
489 fully qualified (dotted) module name. It returns the zipimporter\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
490 instance itself if the module was found, or None if it wasn't.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
491 The optional 'path' argument is ignored -- it's there for compatibility\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
492 with the importer protocol.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
493
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
494 PyDoc_STRVAR(doc_load_module,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
495 "load_module(fullname) -> module.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
496 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
497 Load the module specified by 'fullname'. 'fullname' must be the\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
498 fully qualified (dotted) module name. It returns the imported\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
499 module, or raises ZipImportError if it wasn't found.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
500
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
501 PyDoc_STRVAR(doc_get_data,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
502 "get_data(pathname) -> string with file data.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
503 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
504 Return the data associated with 'pathname'. Raise IOError if\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
505 the file wasn't found.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
506
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
507 PyDoc_STRVAR(doc_is_package,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
508 "is_package(fullname) -> bool.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
509 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
510 Return True if the module specified by fullname is a package.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
511 Raise ZipImportError is the module couldn't be found.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
512
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
513 PyDoc_STRVAR(doc_get_code,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
514 "get_code(fullname) -> code object.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
515 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
516 Return the code object for the specified module. Raise ZipImportError\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
517 is the module couldn't be found.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
518
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
519 PyDoc_STRVAR(doc_get_source,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
520 "get_source(fullname) -> source string.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
521 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
522 Return the source code for the specified module. Raise ZipImportError\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
523 is the module couldn't be found, return None if the archive does\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
524 contain the module, but has no source for it.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
525
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
526 static PyMethodDef zipimporter_methods[] = {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
527 {"find_module", zipimporter_find_module, METH_VARARGS,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
528 doc_find_module},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
529 {"load_module", zipimporter_load_module, METH_VARARGS,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
530 doc_load_module},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
531 {"get_data", zipimporter_get_data, METH_VARARGS,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
532 doc_get_data},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
533 {"get_code", zipimporter_get_code, METH_VARARGS,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
534 doc_get_code},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
535 {"get_source", zipimporter_get_source, METH_VARARGS,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
536 doc_get_source},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
537 {"is_package", zipimporter_is_package, METH_VARARGS,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
538 doc_is_package},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
539 {NULL, NULL} /* sentinel */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
540 };
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
541
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
542 static PyMemberDef zipimporter_members[] = {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
543 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
544 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
545 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
546 {NULL}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
547 };
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
548
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
549 PyDoc_STRVAR(zipimporter_doc,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
550 "zipimporter(archivepath) -> zipimporter object\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
551 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
552 Create a new zipimporter instance. 'archivepath' must be a path to\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
553 a zipfile. ZipImportError is raised if 'archivepath' doesn't point to\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
554 a valid Zip archive.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
555
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
556 #define DEFERRED_ADDRESS(ADDR) 0
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
557
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
558 static PyTypeObject ZipImporter_Type = {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
559 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
560 0,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
561 "zipimport.zipimporter",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
562 sizeof(ZipImporter),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
563 0, /* tp_itemsize */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
564 (destructor)zipimporter_dealloc, /* tp_dealloc */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
565 0, /* tp_print */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
566 0, /* tp_getattr */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
567 0, /* tp_setattr */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
568 0, /* tp_compare */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
569 (reprfunc)zipimporter_repr, /* tp_repr */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
570 0, /* tp_as_number */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
571 0, /* tp_as_sequence */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
572 0, /* tp_as_mapping */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
573 0, /* tp_hash */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
574 0, /* tp_call */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
575 0, /* tp_str */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
576 PyObject_GenericGetAttr, /* tp_getattro */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
577 0, /* tp_setattro */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
578 0, /* tp_as_buffer */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
579 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
580 Py_TPFLAGS_HAVE_GC, /* tp_flags */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
581 zipimporter_doc, /* tp_doc */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
582 zipimporter_traverse, /* tp_traverse */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
583 0, /* tp_clear */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
584 0, /* tp_richcompare */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
585 0, /* tp_weaklistoffset */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
586 0, /* tp_iter */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
587 0, /* tp_iternext */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
588 zipimporter_methods, /* tp_methods */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
589 zipimporter_members, /* tp_members */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
590 0, /* tp_getset */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
591 0, /* tp_base */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
592 0, /* tp_dict */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
593 0, /* tp_descr_get */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
594 0, /* tp_descr_set */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
595 0, /* tp_dictoffset */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
596 (initproc)zipimporter_init, /* tp_init */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
597 PyType_GenericAlloc, /* tp_alloc */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
598 PyType_GenericNew, /* tp_new */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
599 PyObject_GC_Del, /* tp_free */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
600 };
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
601
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
602
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
603 /* implementation */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
604
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
605 /* Given a buffer, return the long that is represented by the first
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
606 4 bytes, encoded as little endian. This partially reimplements
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
607 marshal.c:r_long() */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
608 static long
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
609 get_long(unsigned char *buf) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
610 long x;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
611 x = buf[0];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
612 x |= (long)buf[1] << 8;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
613 x |= (long)buf[2] << 16;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
614 x |= (long)buf[3] << 24;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
615 #if SIZEOF_LONG > 4
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
616 /* Sign extension for 64-bit machines */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
617 x |= -(x & 0x80000000L);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
618 #endif
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
619 return x;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
620 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
621
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
622 /*
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
623 read_directory(archive) -> files dict (new reference)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
624
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
625 Given a path to a Zip archive, build a dict, mapping file names
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
626 (local to the archive, using SEP as a separator) to toc entries.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
627
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
628 A toc_entry is a tuple:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
629
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
630 (compress, # compression kind; 0 for uncompressed
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
631 data_size, # size of compressed data on disk
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
632 file_size, # size of decompressed data
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
633 file_offset, # offset of file header from start of archive
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
634 time, # mod time of file (in dos format)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
635 date, # mod data of file (in dos format)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
636 crc, # crc checksum of the data
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
637 )
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
638
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
639 Directories can be recognized by the trailing SEP in the name,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
640 data_size and file_offset are 0.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
641 */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
642 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
643 read_directory(char *archive)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
644 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
645 PyObject *files = NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
646 FILE *fp;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
647 long compress, crc, data_size, file_size, file_offset, date, time;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
648 long header_offset, name_size, header_size, header_end;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
649 long i, l, length, count;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
650 char path[MAXPATHLEN + 5];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
651 char name[MAXPATHLEN + 5];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
652 char *p, endof_central_dir[22];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
653
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
654 if (strlen(archive) > MAXPATHLEN) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
655 PyErr_SetString(PyExc_OverflowError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
656 "Zip path name is too long");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
657 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
658 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
659 strcpy(path, archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
660
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
661 fp = fopen(archive, "rb");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
662 if (fp == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
663 PyErr_Format(ZipImportError, "can't open Zip file: "
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
664 "'%.200s'", archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
665 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
666 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
667 fseek(fp, -22, 2); /* Seek from end of file */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
668 header_end = ftell(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
669 if (fread(endof_central_dir, 1, 22, fp) != 22) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
670 fclose(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
671 PyErr_Format(ZipImportError, "can't read Zip file: "
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
672 "'%.200s'", archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
673 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
674 }
26901
d5ad29a473e1 Added casts to forestall warnings with MetroWerks.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26895
diff changeset
675 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
676 /* Bad: End of Central Dir signature */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
677 fclose(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
678 PyErr_Format(ZipImportError, "not a Zip file: "
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
679 "'%.200s'", archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
680 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
681 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
682
26901
d5ad29a473e1 Added casts to forestall warnings with MetroWerks.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26895
diff changeset
683 header_offset = get_long((unsigned char *)endof_central_dir + 16);
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
684
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
685 files = PyDict_New();
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
686 if (files == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
687 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
688
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
689 length = (long)strlen(path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
690 path[length] = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
691
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
692 /* Start of Central Directory */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
693 count = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
694 for (;;) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
695 PyObject *t;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
696 int err;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
697
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
698 fseek(fp, header_offset, 0); /* Start of file header */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
699 l = PyMarshal_ReadLongFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
700 if (l != 0x02014B50)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
701 break; /* Bad: Central Dir File Header */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
702 fseek(fp, header_offset + 10, 0);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
703 compress = PyMarshal_ReadShortFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
704 time = PyMarshal_ReadShortFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
705 date = PyMarshal_ReadShortFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
706 crc = PyMarshal_ReadLongFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
707 data_size = PyMarshal_ReadLongFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
708 file_size = PyMarshal_ReadLongFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
709 name_size = PyMarshal_ReadShortFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
710 header_size = 46 + name_size +
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
711 PyMarshal_ReadShortFromFile(fp) +
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
712 PyMarshal_ReadShortFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
713 fseek(fp, header_offset + 42, 0);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
714 file_offset = PyMarshal_ReadLongFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
715 if (name_size > MAXPATHLEN)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
716 name_size = MAXPATHLEN;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
717
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
718 p = name;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
719 for (i = 0; i < name_size; i++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
720 *p = (char)getc(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
721 if (*p == '/')
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
722 *p = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
723 p++;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
724 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
725 *p = 0; /* Add terminating null byte */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
726 header_offset += header_size;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
727
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
728 strncpy(path + length + 1, name, MAXPATHLEN - length - 1);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
729
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
730 t = Py_BuildValue("siiiiiii", path, compress, data_size,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
731 file_size, file_offset, time, date, crc);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
732 if (t == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
733 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
734 err = PyDict_SetItemString(files, name, t);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
735 Py_DECREF(t);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
736 if (err != 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
737 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
738 count++;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
739 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
740 fclose(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
741 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
742 PySys_WriteStderr("# zipimport: found %ld names in %s\n",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
743 count, archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
744 return files;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
745 error:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
746 fclose(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
747 Py_XDECREF(files);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
748 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
749 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
750
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
751 /* Return the zlib.decompress function object, or NULL if zlib couldn't
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
752 be imported. The function is cached when found, so subsequent calls
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
753 don't import zlib again. Returns a *borrowed* reference.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
754 XXX This makes zlib.decompress immortal. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
755 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
756 get_decompress_func(void)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
757 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
758 static PyObject *decompress = NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
759
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
760 if (decompress == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
761 PyObject *zlib;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
762 static int importing_zlib = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
763
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
764 if (importing_zlib != 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
765 /* Someone has a zlib.py[co] in their Zip file;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
766 let's avoid a stack overflow. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
767 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
768 importing_zlib = 1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
769 zlib = PyImport_ImportModule("zlib"); /* import zlib */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
770 importing_zlib = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
771 if (zlib != NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
772 decompress = PyObject_GetAttrString(zlib,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
773 "decompress");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
774 Py_DECREF(zlib);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
775 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
776 else
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
777 PyErr_Clear();
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
778 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
779 PySys_WriteStderr("# zipimport: zlib %s\n",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
780 zlib != NULL ? "available": "UNAVAILABLE");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
781 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
782 return decompress;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
783 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
784
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
785 /* Given a path to a Zip file and a toc_entry, return the (uncompressed)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
786 data as a new reference. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
787 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
788 get_data(char *archive, PyObject *toc_entry)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
789 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
790 PyObject *raw_data, *data = NULL, *decompress;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
791 char *buf;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
792 FILE *fp;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
793 int err, bytes_read = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
794 long l;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
795 char *datapath;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
796 long compress, data_size, file_size, file_offset;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
797 long time, date, crc;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
798
27929
3da31778ecd7 Fix 64-bit problem, ParseTuple("i") needs C ints; ("l") needs C longs.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27915
diff changeset
799 if (!PyArg_ParseTuple(toc_entry, "slllllll", &datapath, &compress,
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
800 &data_size, &file_size, &file_offset, &time,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
801 &date, &crc)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
802 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
803 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
804
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
805 fp = fopen(archive, "rb");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
806 if (!fp) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
807 PyErr_Format(PyExc_IOError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
808 "zipimport: can not open file %s", archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
809 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
810 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
811
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
812 /* Check to make sure the local file header is correct */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
813 fseek(fp, file_offset, 0);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
814 l = PyMarshal_ReadLongFromFile(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
815 if (l != 0x04034B50) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
816 /* Bad: Local File Header */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
817 PyErr_Format(ZipImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
818 "bad local file header in %s",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
819 archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
820 fclose(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
821 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
822 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
823 fseek(fp, file_offset + 26, 0);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
824 l = 30 + PyMarshal_ReadShortFromFile(fp) +
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
825 PyMarshal_ReadShortFromFile(fp); /* local header size */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
826 file_offset += l; /* Start of file data */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
827
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
828 raw_data = PyString_FromStringAndSize((char *)NULL, compress == 0 ?
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
829 data_size : data_size + 1);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
830 if (raw_data == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
831 fclose(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
832 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
833 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
834 buf = PyString_AsString(raw_data);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
835
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
836 err = fseek(fp, file_offset, 0);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
837 if (err == 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
838 bytes_read = fread(buf, 1, data_size, fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
839 fclose(fp);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
840 if (err || bytes_read != data_size) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
841 PyErr_SetString(PyExc_IOError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
842 "zipimport: can't read data");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
843 Py_DECREF(raw_data);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
844 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
845 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
846
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
847 if (compress != 0) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
848 buf[data_size] = 'Z'; /* saw this in zipfile.py */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
849 data_size++;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
850 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
851 buf[data_size] = '\0';
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
852
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
853 if (compress == 0) /* data is not compressed */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
854 return raw_data;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
855
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
856 /* Decompress with zlib */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
857 decompress = get_decompress_func();
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
858 if (decompress == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
859 PyErr_SetString(ZipImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
860 "can't decompress data; "
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
861 "zlib not available");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
862 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
863 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
864 data = PyObject_CallFunction(decompress, "Ol", raw_data, -15);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
865 error:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
866 Py_DECREF(raw_data);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
867 return data;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
868 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
869
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
870 /* Lenient date/time comparison function. The precision of the mtime
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
871 in the archive is lower than the mtime stored in a .pyc: we
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
872 must allow a difference of at most one second. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
873 static int
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
874 eq_mtime(time_t t1, time_t t2)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
875 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
876 time_t d = t1 - t2;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
877 if (d < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
878 d = -d;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
879 /* dostime only stores even seconds, so be lenient */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
880 return d <= 1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
881 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
882
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
883 /* Given the contents of a .py[co] file in a buffer, unmarshal the data
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
884 and return the code object. Return None if it the magic word doesn't
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
885 match (we do this instead of raising an exception as we fall back
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
886 to .py if available and we don't want to mask other errors).
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
887 Returns a new reference. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
888 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
889 unmarshal_code(char *pathname, PyObject *data, time_t mtime)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
890 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
891 PyObject *code;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
892 char *buf = PyString_AsString(data);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
893 int size = PyString_Size(data);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
894
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
895 if (size <= 9) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
896 PyErr_SetString(ZipImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
897 "bad pyc data");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
898 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
899 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
900
26901
d5ad29a473e1 Added casts to forestall warnings with MetroWerks.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26895
diff changeset
901 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
902 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
903 PySys_WriteStderr("# %s has bad magic\n",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
904 pathname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
905 Py_INCREF(Py_None);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
906 return Py_None; /* signal caller to try alternative */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
907 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
908
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
909 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
910 mtime)) {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
911 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
912 PySys_WriteStderr("# %s has bad mtime\n",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
913 pathname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
914 Py_INCREF(Py_None);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
915 return Py_None; /* signal caller to try alternative */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
916 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
917
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
918 code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
919 if (code == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
920 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
921 if (!PyCode_Check(code)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
922 Py_DECREF(code);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
923 PyErr_Format(PyExc_TypeError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
924 "compiled module %.200s is not a code object",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
925 pathname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
926 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
927 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
928 return code;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
929 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
930
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
931 /* Replace any occurances of "\r\n?" in the input string with "\n".
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
932 This converts DOS and Mac line endings to Unix line endings.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
933 Also append a trailing "\n" to be compatible with
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
934 PyParser_SimpleParseFile(). Returns a new reference. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
935 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
936 normalize_line_endings(PyObject *source)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
937 {
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
938 char *buf, *q, *p = PyString_AsString(source);
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
939 PyObject *fixed_source;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
940
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
941 /* one char extra for trailing \n and one for terminating \0 */
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
942 buf = PyMem_Malloc(PyString_Size(source) + 2);
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
943 if (buf == NULL) {
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
944 PyErr_SetString(PyExc_MemoryError,
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
945 "zipimport: no memory to allocate "
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
946 "source buffer");
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
947 return NULL;
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
948 }
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
949 /* replace "\r\n?" by "\n" */
27915
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27047
diff changeset
950 for (q = buf; *p != '\0'; p++) {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
951 if (*p == '\r') {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
952 *q++ = '\n';
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
953 if (*(p + 1) == '\n')
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
954 p++;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
955 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
956 else
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
957 *q++ = *p;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
958 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
959 *q++ = '\n'; /* add trailing \n */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
960 *q = '\0';
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
961 fixed_source = PyString_FromString(buf);
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
962 PyMem_Free(buf);
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
963 return fixed_source;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
964 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
965
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
966 /* Given a string buffer containing Python source code, compile it
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
967 return and return a code object as a new reference. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
968 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
969 compile_source(char *pathname, PyObject *source)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
970 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
971 PyObject *code, *fixed_source;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
972
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
973 fixed_source = normalize_line_endings(source);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
974 if (fixed_source == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
975 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
976
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
977 code = Py_CompileString(PyString_AsString(fixed_source), pathname,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
978 Py_file_input);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
979 Py_DECREF(fixed_source);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
980 return code;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
981 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
982
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
983 /* Convert the date/time values found in the Zip archive to a value
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
984 that's compatible with the time stamp stored in .pyc files. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
985 time_t parse_dostime(int dostime, int dosdate)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
986 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
987 struct tm stm;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
988
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
989 stm.tm_sec = (dostime & 0x1f) * 2;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
990 stm.tm_min = (dostime >> 5) & 0x3f;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
991 stm.tm_hour = (dostime >> 11) & 0x1f;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
992 stm.tm_mday = dosdate & 0x1f;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
993 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
994 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
995 stm.tm_isdst = 0; /* wday/yday is ignored */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
996
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
997 return mktime(&stm);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
998 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
999
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1000 /* Given a path to a .pyc or .pyo file in the archive, return the
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1001 modifictaion time of the matching .py file, or 0 if no source
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1002 is available. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1003 static time_t
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1004 get_mtime_of_source(ZipImporter *self, char *path)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1005 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1006 PyObject *toc_entry;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1007 time_t mtime = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1008 int lastchar = strlen(path) - 1;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1009 char savechar = path[lastchar];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1010 path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1011 toc_entry = PyDict_GetItemString(self->files, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1012 if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1013 PyTuple_Size(toc_entry) == 8) {
26931
6c535dcedad3 - added missing decref
Just van Rossum <just@letterror.com>
parents: 26901
diff changeset
1014 /* fetch the time stamp of the .py file for comparison
6c535dcedad3 - added missing decref
Just van Rossum <just@letterror.com>
parents: 26901
diff changeset
1015 with an embedded pyc time stamp */
6c535dcedad3 - added missing decref
Just van Rossum <just@letterror.com>
parents: 26901
diff changeset
1016 int time, date;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1017 time = PyInt_AsLong(PyTuple_GetItem(toc_entry, 5));
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1018 date = PyInt_AsLong(PyTuple_GetItem(toc_entry, 6));
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1019 mtime = parse_dostime(time, date);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1020 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1021 path[lastchar] = savechar;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1022 return mtime;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1023 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1024
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1025 /* Return the code object for the module named by 'fullname' from the
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1026 Zip archive as a new reference. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1027 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1028 get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1029 time_t mtime, PyObject *toc_entry)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1030 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1031 PyObject *data, *code;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1032 char *modpath;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1033 char *archive = PyString_AsString(self->archive);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1034
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1035 if (archive == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1036 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1037
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1038 data = get_data(archive, toc_entry);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1039 if (data == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1040 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1041
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1042 modpath = PyString_AsString(PyTuple_GetItem(toc_entry, 0));
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1043
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1044 if (isbytecode) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1045 code = unmarshal_code(modpath, data, mtime);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1046 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1047 else {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1048 code = compile_source(modpath, data);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1049 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1050 Py_DECREF(data);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1051 return code;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1052 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1053
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1054 /* Get the code object assoiciated with the module specified by
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1055 'fullname'. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1056 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1057 get_module_code(ZipImporter *self, char *fullname,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1058 int *p_ispackage, char **p_modpath)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1059 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1060 PyObject *toc_entry;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1061 char *subname, path[MAXPATHLEN + 1];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1062 int len;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1063 struct st_zip_searchorder *zso;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1064
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1065 subname = get_subname(fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1066
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1067 len = make_filename(PyString_AsString(self->prefix), subname, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1068 if (len < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1069 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1070
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1071 for (zso = zip_searchorder; *zso->suffix; zso++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1072 PyObject *code = NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1073
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1074 strcpy(path + len, zso->suffix);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1075 if (Py_VerboseFlag > 1)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1076 PySys_WriteStderr("# trying %s%c%s\n",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1077 PyString_AsString(self->archive),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1078 SEP, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1079 toc_entry = PyDict_GetItemString(self->files, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1080 if (toc_entry != NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1081 time_t mtime = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1082 int ispackage = zso->type & IS_PACKAGE;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1083 int isbytecode = zso->type & IS_BYTECODE;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1084
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1085 if (isbytecode)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1086 mtime = get_mtime_of_source(self, path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1087 if (p_ispackage != NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1088 *p_ispackage = ispackage;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1089 code = get_code_from_data(self, ispackage,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1090 isbytecode, mtime,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1091 toc_entry);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1092 if (code == Py_None) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1093 /* bad magic number or non-matching mtime
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1094 in byte code, try next */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1095 Py_DECREF(code);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1096 continue;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1097 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1098 if (code != NULL && p_modpath != NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1099 *p_modpath = PyString_AsString(
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1100 PyTuple_GetItem(toc_entry, 0));
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1101 return code;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1102 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1103 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1104 PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1105 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1106 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1107
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1108
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1109 /* Module init */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1110
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1111 PyDoc_STRVAR(zipimport_doc,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1112 "zipimport provides support for importing Python modules from Zip archives.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1113 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1114 This module exports three objects:\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1115 - zipimporter: a class; its constructor takes a path to a Zip archive.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1116 - ZipImporterError: exception raised by zipimporter objects. It's a\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1117 subclass of ImportError, so it can be caught as ImportError, too.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1118 - _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1119 info dicts, as used in zipimporter._files.\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1120 \n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1121 It is usually not needed to use the zipimport module explicitly; it is\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1122 used by the builtin import mechanism for sys.path items that are paths\n\
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1123 to Zip archives.");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1124
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1125 PyMODINIT_FUNC
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1126 initzipimport(void)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1127 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1128 PyObject *mod;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1129
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1130 if (PyType_Ready(&ZipImporter_Type) < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1131 return;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1132
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1133 /* Correct directory separator */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1134 zip_searchorder[0].suffix[0] = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1135 zip_searchorder[1].suffix[0] = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1136 zip_searchorder[2].suffix[0] = SEP;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1137 if (Py_OptimizeFlag) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1138 /* Reverse *.pyc and *.pyo */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1139 struct st_zip_searchorder tmp;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1140 tmp = zip_searchorder[0];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1141 zip_searchorder[0] = zip_searchorder[1];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1142 zip_searchorder[1] = tmp;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1143 tmp = zip_searchorder[3];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1144 zip_searchorder[3] = zip_searchorder[4];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1145 zip_searchorder[4] = tmp;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1146 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1147
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1148 mod = Py_InitModule4("zipimport", NULL, zipimport_doc,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1149 NULL, PYTHON_API_VERSION);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1150
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1151 ZipImportError = PyErr_NewException("zipimport.ZipImportError",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1152 PyExc_ImportError, NULL);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1153 if (ZipImportError == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1154 return;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1155
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1156 Py_INCREF(ZipImportError);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1157 if (PyModule_AddObject(mod, "ZipImportError",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1158 ZipImportError) < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1159 return;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1160
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1161 Py_INCREF(&ZipImporter_Type);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1162 if (PyModule_AddObject(mod, "zipimporter",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1163 (PyObject *)&ZipImporter_Type) < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1164 return;
26931
6c535dcedad3 - added missing decref
Just van Rossum <just@letterror.com>
parents: 26901
diff changeset
1165
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1166 zip_directory_cache = PyDict_New();
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1167 if (zip_directory_cache == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1168 return;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1169 Py_INCREF(zip_directory_cache);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1170 if (PyModule_AddObject(mod, "_zip_directory_cache",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1171 zip_directory_cache) < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1172 return;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1173 }