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