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