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); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 52 | static PyObject *get_module_code(ZipImporter *self, PyObject *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 | { |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 66 | PyObject *path, *files, *tmp; |
| 67 | PyObject *filename = NULL; |
| 68 | Py_ssize_t len, flen; |
| 69 | #ifdef ALTSEP |
| 70 | _Py_IDENTIFIER(replace); |
| 71 | #endif |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 72 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | if (!_PyArg_NoKeywords("zipimporter()", kwds)) |
| 74 | return -1; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 75 | |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 76 | if (!PyArg_ParseTuple(args, "O&:zipimporter", |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 77 | PyUnicode_FSDecoder, &path)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | return -1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 79 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 80 | if (PyUnicode_READY(path) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 81 | return -1; |
| 82 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 83 | len = PyUnicode_GET_LENGTH(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | if (len == 0) { |
| 85 | PyErr_SetString(ZipImportError, "archive path is empty"); |
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 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 88 | |
| 89 | #ifdef ALTSEP |
Martin v. Löwis | cfa6129 | 2011-10-31 09:01:22 +0100 | [diff] [blame] | 90 | tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 91 | if (!tmp) |
| 92 | goto error; |
| 93 | Py_DECREF(path); |
| 94 | path = tmp; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 95 | #endif |
| 96 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 97 | filename = path; |
| 98 | Py_INCREF(filename); |
| 99 | flen = len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | for (;;) { |
| 101 | struct stat statbuf; |
| 102 | int rv; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 103 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 104 | rv = _Py_stat(filename, &statbuf); |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 105 | if (rv == -2) |
| 106 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | if (rv == 0) { |
| 108 | /* it exists */ |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 109 | if (!S_ISREG(statbuf.st_mode)) |
| 110 | /* it's a not file */ |
| 111 | Py_CLEAR(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | break; |
| 113 | } |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 114 | Py_CLEAR(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | /* back up one path element */ |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 116 | flen = PyUnicode_FindChar(path, SEP, 0, flen, -1); |
| 117 | if (flen == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | break; |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 119 | filename = PyUnicode_Substring(path, 0, flen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | } |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 121 | if (filename == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | PyErr_SetString(ZipImportError, "not a Zip file"); |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 123 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 125 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 126 | if (PyUnicode_READY(filename) < 0) |
| 127 | goto error; |
| 128 | |
| 129 | files = PyDict_GetItem(zip_directory_cache, filename); |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 130 | if (files == NULL) { |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 131 | files = read_directory(filename); |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 132 | if (files == NULL) |
| 133 | goto error; |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 134 | if (PyDict_SetItem(zip_directory_cache, filename, files) != 0) |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 135 | goto error; |
| 136 | } |
| 137 | else |
| 138 | Py_INCREF(files); |
| 139 | self->files = files; |
| 140 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 141 | /* Transfer reference */ |
| 142 | self->archive = filename; |
| 143 | filename = NULL; |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 144 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 145 | /* Check if there is a prefix directory following the filename. */ |
| 146 | if (flen != len) { |
| 147 | tmp = PyUnicode_Substring(path, flen+1, |
| 148 | PyUnicode_GET_LENGTH(path)); |
| 149 | if (tmp == NULL) |
| 150 | goto error; |
| 151 | self->prefix = tmp; |
| 152 | if (PyUnicode_READ_CHAR(path, len-1) != SEP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | /* add trailing SEP */ |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 154 | tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP); |
| 155 | if (tmp == NULL) |
| 156 | goto error; |
| 157 | Py_DECREF(self->prefix); |
| 158 | self->prefix = tmp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 161 | else |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 162 | self->prefix = PyUnicode_New(0, 0); |
| 163 | Py_DECREF(path); |
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: |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 167 | Py_DECREF(path); |
| 168 | Py_XDECREF(filename); |
Victor Stinner | 2b8dab7 | 2010-08-14 14:54:10 +0000 | [diff] [blame] | 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 \"???\">"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 196 | else if (self->prefix != NULL && PyUnicode_GET_LENGTH(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] */ |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 205 | static PyObject * |
| 206 | get_subname(PyObject *fullname) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 207 | { |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 208 | Py_ssize_t len, dot; |
| 209 | if (PyUnicode_READY(fullname) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 210 | return NULL; |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 211 | len = PyUnicode_GET_LENGTH(fullname); |
| 212 | dot = PyUnicode_FindChar(fullname, '.', 0, len, -1); |
| 213 | if (dot == -1) { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 214 | Py_INCREF(fullname); |
| 215 | return fullname; |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 216 | } else |
| 217 | return PyUnicode_Substring(fullname, dot+1, len); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* Given a (sub)modulename, write the potential file path in the |
| 221 | archive (without extension) to the path buffer. Return the |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 222 | length of the resulting string. |
| 223 | |
| 224 | return self.prefix + name.replace('.', os.sep) */ |
| 225 | static PyObject* |
| 226 | make_filename(PyObject *prefix, PyObject *name) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 227 | { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 228 | PyObject *pathobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 229 | Py_UCS4 *p, *buf; |
| 230 | Py_ssize_t len; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 231 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 232 | len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1; |
| 233 | p = buf = PyMem_Malloc(sizeof(Py_UCS4) * len); |
| 234 | if (buf == NULL) { |
| 235 | PyErr_NoMemory(); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 236 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 237 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 238 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 239 | if (!PyUnicode_AsUCS4(prefix, p, len, 0)) |
| 240 | return NULL; |
| 241 | p += PyUnicode_GET_LENGTH(prefix); |
| 242 | len -= PyUnicode_GET_LENGTH(prefix); |
| 243 | if (!PyUnicode_AsUCS4(name, p, len, 1)) |
| 244 | return NULL; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 245 | for (; *p; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | if (*p == '.') |
| 247 | *p = SEP; |
| 248 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 249 | pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, |
| 250 | buf, p-buf); |
| 251 | PyMem_Free(buf); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 252 | return pathobj; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Raymond Hettinger | 2c45c9a | 2004-11-10 13:08:35 +0000 | [diff] [blame] | 255 | enum zi_module_info { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | MI_ERROR, |
| 257 | MI_NOT_FOUND, |
| 258 | MI_MODULE, |
| 259 | MI_PACKAGE |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 262 | /* Does this path represent a directory? |
| 263 | on error, return < 0 |
| 264 | if not a dir, return 0 |
| 265 | if a dir, return 1 |
| 266 | */ |
| 267 | static int |
| 268 | check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path) |
| 269 | { |
| 270 | PyObject *dirpath; |
Benjamin Peterson | 18eac4a | 2012-05-25 00:24:42 -0700 | [diff] [blame] | 271 | int res; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 272 | |
| 273 | /* See if this is a "directory". If so, it's eligible to be part |
| 274 | of a namespace package. We test by seeing if the name, with an |
| 275 | appended path separator, exists. */ |
| 276 | dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP); |
| 277 | if (dirpath == NULL) |
| 278 | return -1; |
| 279 | /* If dirpath is present in self->files, we have a directory. */ |
Benjamin Peterson | 18eac4a | 2012-05-25 00:24:42 -0700 | [diff] [blame] | 280 | res = PyDict_Contains(self->files, dirpath); |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 281 | Py_DECREF(dirpath); |
Benjamin Peterson | 18eac4a | 2012-05-25 00:24:42 -0700 | [diff] [blame] | 282 | return res; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 285 | /* Return some information about a module. */ |
Raymond Hettinger | 2c45c9a | 2004-11-10 13:08:35 +0000 | [diff] [blame] | 286 | static enum zi_module_info |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 287 | get_module_info(ZipImporter *self, PyObject *fullname) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 288 | { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 289 | PyObject *subname; |
| 290 | PyObject *path, *fullpath, *item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | struct st_zip_searchorder *zso; |
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 | subname = get_subname(fullname); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 294 | if (subname == NULL) |
| 295 | return MI_ERROR; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 296 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 297 | path = make_filename(self->prefix, subname); |
| 298 | Py_DECREF(subname); |
| 299 | if (path == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | return MI_ERROR; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | for (zso = zip_searchorder; *zso->suffix; zso++) { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 303 | fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
| 304 | if (fullpath == NULL) { |
| 305 | Py_DECREF(path); |
| 306 | return MI_ERROR; |
| 307 | } |
| 308 | item = PyDict_GetItem(self->files, fullpath); |
| 309 | Py_DECREF(fullpath); |
| 310 | if (item != NULL) { |
| 311 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (zso->type & IS_PACKAGE) |
| 313 | return MI_PACKAGE; |
| 314 | else |
| 315 | return MI_MODULE; |
| 316 | } |
| 317 | } |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 318 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | return MI_NOT_FOUND; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 322 | typedef enum { |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 323 | FL_ERROR, |
| 324 | FL_NOT_FOUND, |
| 325 | FL_MODULE_FOUND, |
| 326 | FL_NS_FOUND |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 327 | } find_loader_result; |
| 328 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 329 | /* The guts of "find_loader" and "find_module". Return values: |
| 330 | -1: error |
| 331 | 0: no loader or namespace portions found |
| 332 | 1: module/package found |
| 333 | 2: namespace portion found: *namespace_portion will point to the name |
| 334 | */ |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 335 | static find_loader_result |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 336 | find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion) |
| 337 | { |
| 338 | enum zi_module_info mi; |
| 339 | |
| 340 | *namespace_portion = NULL; |
| 341 | |
| 342 | mi = get_module_info(self, fullname); |
| 343 | if (mi == MI_ERROR) |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 344 | return FL_ERROR; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 345 | if (mi == MI_NOT_FOUND) { |
| 346 | /* Not a module or regular package. See if this is a directory, and |
| 347 | therefore possibly a portion of a namespace package. */ |
| 348 | int is_dir = check_is_directory(self, self->prefix, fullname); |
| 349 | if (is_dir < 0) |
| 350 | return -1; |
| 351 | if (is_dir) { |
| 352 | /* This is possibly a portion of a namespace |
| 353 | package. Return the string representing its path, |
| 354 | without a trailing separator. */ |
| 355 | *namespace_portion = PyUnicode_FromFormat("%U%c%U%U", |
| 356 | self->archive, SEP, |
| 357 | self->prefix, fullname); |
| 358 | if (*namespace_portion == NULL) |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 359 | return FL_ERROR; |
| 360 | return FL_NS_FOUND; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 361 | } |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 362 | return FL_NOT_FOUND; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 363 | } |
| 364 | /* This is a module or package. */ |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 365 | return FL_MODULE_FOUND; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 369 | /* Check whether we can satisfy the import of the module named by |
| 370 | 'fullname'. Return self if we can, None if we can't. */ |
| 371 | static PyObject * |
| 372 | zipimporter_find_module(PyObject *obj, PyObject *args) |
| 373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | ZipImporter *self = (ZipImporter *)obj; |
| 375 | PyObject *path = NULL; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 376 | PyObject *fullname; |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 377 | PyObject *namespace_portion = NULL; |
| 378 | PyObject *result = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 379 | |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 380 | if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) |
| 381 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 382 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 383 | switch (find_loader(self, fullname, &namespace_portion)) { |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 384 | case FL_ERROR: |
Benjamin Peterson | a6a7a1a | 2012-05-25 00:22:04 -0700 | [diff] [blame] | 385 | return NULL; |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 386 | case FL_NS_FOUND: |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 387 | /* A namespace portion is not allowed via find_module, so return None. */ |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 388 | Py_DECREF(namespace_portion); |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 389 | /* FALL THROUGH */ |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 390 | case FL_NOT_FOUND: |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 391 | result = Py_None; |
| 392 | break; |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 393 | case FL_MODULE_FOUND: |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 394 | result = (PyObject *)self; |
| 395 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | } |
Benjamin Peterson | a6a7a1a | 2012-05-25 00:22:04 -0700 | [diff] [blame] | 397 | Py_INCREF(result); |
Benjamin Peterson | 2d12e14 | 2012-05-25 00:19:40 -0700 | [diff] [blame] | 398 | return result; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
| 402 | /* Check whether we can satisfy the import of the module named by |
| 403 | 'fullname', or whether it could be a portion of a namespace |
| 404 | package. Return self if we can load it, a string containing the |
| 405 | full path if it's a possible namespace portion, None if we |
| 406 | can't load it. */ |
| 407 | static PyObject * |
| 408 | zipimporter_find_loader(PyObject *obj, PyObject *args) |
| 409 | { |
| 410 | ZipImporter *self = (ZipImporter *)obj; |
| 411 | PyObject *path = NULL; |
| 412 | PyObject *fullname; |
| 413 | PyObject *result = NULL; |
| 414 | PyObject *namespace_portion = NULL; |
| 415 | |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 416 | if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) |
| 417 | return NULL; |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 418 | |
| 419 | switch (find_loader(self, fullname, &namespace_portion)) { |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 420 | case FL_ERROR: |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 421 | return NULL; |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 422 | case FL_NOT_FOUND: /* Not found, return (None, []) */ |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 423 | result = Py_BuildValue("O[]", Py_None); |
| 424 | break; |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 425 | case FL_MODULE_FOUND: /* Return (self, []) */ |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 426 | result = Py_BuildValue("O[]", self); |
| 427 | break; |
Benjamin Peterson | 46c214d | 2012-05-25 10:22:29 -0700 | [diff] [blame] | 428 | case FL_NS_FOUND: /* Return (None, [namespace_portion]) */ |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 429 | result = Py_BuildValue("O[O]", Py_None, namespace_portion); |
Benjamin Peterson | 209e04c | 2012-05-24 22:35:39 -0700 | [diff] [blame] | 430 | Py_DECREF(namespace_portion); |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 431 | return result; |
| 432 | } |
Benjamin Peterson | 5ed7bd7 | 2012-05-24 22:54:15 -0700 | [diff] [blame] | 433 | return result; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Load and return the module named by 'fullname'. */ |
| 437 | static PyObject * |
| 438 | zipimporter_load_module(PyObject *obj, PyObject *args) |
| 439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | ZipImporter *self = (ZipImporter *)obj; |
Victor Stinner | 26fabe1 | 2010-10-18 12:03:25 +0000 | [diff] [blame] | 441 | PyObject *code = NULL, *mod, *dict; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 442 | PyObject *fullname; |
| 443 | PyObject *modpath = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | int ispackage; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 445 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 446 | if (!PyArg_ParseTuple(args, "U:zipimporter.load_module", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | &fullname)) |
| 448 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 449 | if (PyUnicode_READY(fullname) == -1) |
| 450 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | code = get_module_code(self, fullname, &ispackage, &modpath); |
| 453 | if (code == NULL) |
Victor Stinner | 26fabe1 | 2010-10-18 12:03:25 +0000 | [diff] [blame] | 454 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 455 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 456 | mod = PyImport_AddModuleObject(fullname); |
Victor Stinner | 26fabe1 | 2010-10-18 12:03:25 +0000 | [diff] [blame] | 457 | if (mod == NULL) |
| 458 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | dict = PyModule_GetDict(mod); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | /* mod.__loader__ = self */ |
| 462 | if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) |
| 463 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | if (ispackage) { |
| 466 | /* add __path__ to the module *before* the code gets |
| 467 | executed */ |
| 468 | PyObject *pkgpath, *fullpath; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 469 | PyObject *subname = get_subname(fullname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | int err; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 471 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 472 | fullpath = PyUnicode_FromFormat("%U%c%U%U", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | self->archive, SEP, |
| 474 | self->prefix, subname); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 475 | Py_DECREF(subname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | if (fullpath == NULL) |
| 477 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 478 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 479 | pkgpath = Py_BuildValue("[N]", fullpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | if (pkgpath == NULL) |
| 481 | goto error; |
| 482 | err = PyDict_SetItemString(dict, "__path__", pkgpath); |
| 483 | Py_DECREF(pkgpath); |
| 484 | if (err != 0) |
| 485 | goto error; |
| 486 | } |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 487 | mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL); |
Victor Stinner | 26fabe1 | 2010-10-18 12:03:25 +0000 | [diff] [blame] | 488 | Py_CLEAR(code); |
| 489 | if (mod == NULL) |
| 490 | goto error; |
| 491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | if (Py_VerboseFlag) |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 493 | PySys_FormatStderr("import %U # loaded from Zip %U\n", |
Victor Stinner | 08654e1 | 2010-10-18 12:09:02 +0000 | [diff] [blame] | 494 | fullname, modpath); |
| 495 | Py_DECREF(modpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | return mod; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 497 | error: |
Victor Stinner | 26fabe1 | 2010-10-18 12:03:25 +0000 | [diff] [blame] | 498 | Py_XDECREF(code); |
Victor Stinner | 08654e1 | 2010-10-18 12:09:02 +0000 | [diff] [blame] | 499 | Py_XDECREF(modpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 503 | /* Return a string matching __file__ for the named module */ |
| 504 | static PyObject * |
| 505 | zipimporter_get_filename(PyObject *obj, PyObject *args) |
| 506 | { |
| 507 | ZipImporter *self = (ZipImporter *)obj; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 508 | PyObject *fullname, *code, *modpath; |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 509 | int ispackage; |
| 510 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 511 | if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename", |
Victor Stinner | 9e40fad | 2010-10-18 22:34:46 +0000 | [diff] [blame] | 512 | &fullname)) |
Victor Stinner | c342fca | 2010-10-18 11:39:05 +0000 | [diff] [blame] | 513 | return NULL; |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 514 | |
| 515 | /* Deciding the filename requires working out where the code |
| 516 | would come from if the module was actually loaded */ |
| 517 | code = get_module_code(self, fullname, &ispackage, &modpath); |
| 518 | if (code == NULL) |
Victor Stinner | c342fca | 2010-10-18 11:39:05 +0000 | [diff] [blame] | 519 | return NULL; |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 520 | Py_DECREF(code); /* Only need the path info */ |
| 521 | |
Victor Stinner | 08654e1 | 2010-10-18 12:09:02 +0000 | [diff] [blame] | 522 | return modpath; |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 525 | /* Return a bool signifying whether the module is a package or not. */ |
| 526 | static PyObject * |
| 527 | zipimporter_is_package(PyObject *obj, PyObject *args) |
| 528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | ZipImporter *self = (ZipImporter *)obj; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 530 | PyObject *fullname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | enum zi_module_info mi; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 532 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 533 | if (!PyArg_ParseTuple(args, "U:zipimporter.is_package", |
Victor Stinner | 965a8a1 | 2010-10-18 21:44:33 +0000 | [diff] [blame] | 534 | &fullname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | mi = get_module_info(self, fullname); |
| 538 | if (mi == MI_ERROR) |
Victor Stinner | 965a8a1 | 2010-10-18 21:44:33 +0000 | [diff] [blame] | 539 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | if (mi == MI_NOT_FOUND) { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 541 | PyErr_Format(ZipImportError, "can't find module %R", fullname); |
Victor Stinner | 965a8a1 | 2010-10-18 21:44:33 +0000 | [diff] [blame] | 542 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | } |
| 544 | return PyBool_FromLong(mi == MI_PACKAGE); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 547 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 548 | static PyObject * |
| 549 | zipimporter_get_data(PyObject *obj, PyObject *args) |
| 550 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | ZipImporter *self = (ZipImporter *)obj; |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 552 | PyObject *path, *key; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 553 | #ifdef ALTSEP |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 554 | _Py_IDENTIFIER(replace); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 555 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | PyObject *toc_entry; |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 557 | Py_ssize_t path_start, path_len, len; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 558 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 559 | if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 561 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 562 | #ifdef ALTSEP |
Martin v. Löwis | cfa6129 | 2011-10-31 09:01:22 +0100 | [diff] [blame] | 563 | path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 564 | if (!path) |
| 565 | return NULL; |
| 566 | #else |
| 567 | Py_INCREF(path); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 568 | #endif |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 569 | if (PyUnicode_READY(path) == -1) |
| 570 | goto error; |
| 571 | |
| 572 | path_len = PyUnicode_GET_LENGTH(path); |
| 573 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 574 | len = PyUnicode_GET_LENGTH(self->archive); |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 575 | path_start = 0; |
| 576 | if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1) |
| 577 | && PyUnicode_READ_CHAR(path, len) == SEP) { |
| 578 | path_start = len + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 580 | |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 581 | key = PyUnicode_Substring(path, path_start, path_len); |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 582 | if (key == NULL) |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 583 | goto error; |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 584 | toc_entry = PyDict_GetItem(self->files, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | if (toc_entry == NULL) { |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 586 | PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key); |
| 587 | Py_DECREF(key); |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 588 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | } |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 590 | Py_DECREF(key); |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 591 | Py_DECREF(path); |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 592 | return get_data(self->archive, toc_entry); |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 593 | error: |
| 594 | Py_DECREF(path); |
| 595 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static PyObject * |
| 599 | zipimporter_get_code(PyObject *obj, PyObject *args) |
| 600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | ZipImporter *self = (ZipImporter *)obj; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 602 | PyObject *fullname; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 603 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 604 | if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | return get_module_code(self, fullname, NULL, NULL); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static PyObject * |
| 611 | zipimporter_get_source(PyObject *obj, PyObject *args) |
| 612 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | ZipImporter *self = (ZipImporter *)obj; |
| 614 | PyObject *toc_entry; |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 615 | PyObject *fullname, *subname, *path, *fullpath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | enum zi_module_info mi; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 617 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 618 | if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | mi = get_module_info(self, fullname); |
Victor Stinner | 965a8a1 | 2010-10-18 21:44:33 +0000 | [diff] [blame] | 622 | if (mi == MI_ERROR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | return NULL; |
Victor Stinner | 0410656 | 2010-10-18 20:44:08 +0000 | [diff] [blame] | 624 | if (mi == MI_NOT_FOUND) { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 625 | PyErr_Format(ZipImportError, "can't find module %R", fullname); |
Victor Stinner | 0410656 | 2010-10-18 20:44:08 +0000 | [diff] [blame] | 626 | return NULL; |
| 627 | } |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 628 | |
Victor Stinner | 965a8a1 | 2010-10-18 21:44:33 +0000 | [diff] [blame] | 629 | subname = get_subname(fullname); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 630 | if (subname == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 632 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 633 | path = make_filename(self->prefix, subname); |
| 634 | Py_DECREF(subname); |
| 635 | if (path == NULL) |
| 636 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 637 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 638 | if (mi == MI_PACKAGE) |
| 639 | fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP); |
| 640 | else |
| 641 | fullpath = PyUnicode_FromFormat("%U.py", path); |
| 642 | Py_DECREF(path); |
| 643 | if (fullpath == NULL) |
| 644 | return NULL; |
| 645 | |
| 646 | toc_entry = PyDict_GetItem(self->files, fullpath); |
| 647 | Py_DECREF(fullpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | if (toc_entry != NULL) { |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 649 | PyObject *res, *bytes; |
| 650 | bytes = get_data(self->archive, toc_entry); |
| 651 | if (bytes == NULL) |
| 652 | return NULL; |
| 653 | res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes), |
| 654 | PyBytes_GET_SIZE(bytes)); |
| 655 | Py_DECREF(bytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | return res; |
| 657 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | /* we have the module, but no source */ |
| 660 | Py_INCREF(Py_None); |
| 661 | return Py_None; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | PyDoc_STRVAR(doc_find_module, |
| 665 | "find_module(fullname, path=None) -> self or None.\n\ |
| 666 | \n\ |
| 667 | Search for a module specified by 'fullname'. 'fullname' must be the\n\ |
| 668 | fully qualified (dotted) module name. It returns the zipimporter\n\ |
| 669 | instance itself if the module was found, or None if it wasn't.\n\ |
| 670 | The optional 'path' argument is ignored -- it's there for compatibility\n\ |
| 671 | with the importer protocol."); |
| 672 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 673 | PyDoc_STRVAR(doc_find_loader, |
| 674 | "find_loader(fullname, path=None) -> self, str or None.\n\ |
| 675 | \n\ |
| 676 | Search for a module specified by 'fullname'. 'fullname' must be the\n\ |
| 677 | fully qualified (dotted) module name. It returns the zipimporter\n\ |
| 678 | instance itself if the module was found, a string containing the\n\ |
| 679 | full path name if it's possibly a portion of a namespace package,\n\ |
| 680 | or None otherwise. The optional 'path' argument is ignored -- it's\n\ |
| 681 | there for compatibility with the importer protocol."); |
| 682 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 683 | PyDoc_STRVAR(doc_load_module, |
| 684 | "load_module(fullname) -> module.\n\ |
| 685 | \n\ |
| 686 | Load the module specified by 'fullname'. 'fullname' must be the\n\ |
| 687 | fully qualified (dotted) module name. It returns the imported\n\ |
| 688 | module, or raises ZipImportError if it wasn't found."); |
| 689 | |
| 690 | PyDoc_STRVAR(doc_get_data, |
| 691 | "get_data(pathname) -> string with file data.\n\ |
| 692 | \n\ |
| 693 | Return the data associated with 'pathname'. Raise IOError if\n\ |
| 694 | the file wasn't found."); |
| 695 | |
| 696 | PyDoc_STRVAR(doc_is_package, |
| 697 | "is_package(fullname) -> bool.\n\ |
| 698 | \n\ |
| 699 | Return True if the module specified by fullname is a package.\n\ |
Brian Curtin | 3283973 | 2010-07-21 01:44:19 +0000 | [diff] [blame] | 700 | Raise ZipImportError if the module couldn't be found."); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 701 | |
| 702 | PyDoc_STRVAR(doc_get_code, |
| 703 | "get_code(fullname) -> code object.\n\ |
| 704 | \n\ |
| 705 | Return the code object for the specified module. Raise ZipImportError\n\ |
Brian Curtin | 3283973 | 2010-07-21 01:44:19 +0000 | [diff] [blame] | 706 | if the module couldn't be found."); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 707 | |
| 708 | PyDoc_STRVAR(doc_get_source, |
| 709 | "get_source(fullname) -> source string.\n\ |
| 710 | \n\ |
| 711 | Return the source code for the specified module. Raise ZipImportError\n\ |
Brian Curtin | 3283973 | 2010-07-21 01:44:19 +0000 | [diff] [blame] | 712 | 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] | 713 | contain the module, but has no source for it."); |
| 714 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 715 | |
| 716 | PyDoc_STRVAR(doc_get_filename, |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 717 | "get_filename(fullname) -> filename string.\n\ |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 718 | \n\ |
| 719 | Return the filename for the specified module."); |
| 720 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 721 | static PyMethodDef zipimporter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | {"find_module", zipimporter_find_module, METH_VARARGS, |
| 723 | doc_find_module}, |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 724 | {"find_loader", zipimporter_find_loader, METH_VARARGS, |
| 725 | doc_find_loader}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | {"load_module", zipimporter_load_module, METH_VARARGS, |
| 727 | doc_load_module}, |
| 728 | {"get_data", zipimporter_get_data, METH_VARARGS, |
| 729 | doc_get_data}, |
| 730 | {"get_code", zipimporter_get_code, METH_VARARGS, |
| 731 | doc_get_code}, |
| 732 | {"get_source", zipimporter_get_source, METH_VARARGS, |
| 733 | doc_get_source}, |
| 734 | {"get_filename", zipimporter_get_filename, METH_VARARGS, |
| 735 | doc_get_filename}, |
| 736 | {"is_package", zipimporter_is_package, METH_VARARGS, |
| 737 | doc_is_package}, |
| 738 | {NULL, NULL} /* sentinel */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 739 | }; |
| 740 | |
| 741 | static PyMemberDef zipimporter_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY}, |
| 743 | {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY}, |
| 744 | {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY}, |
| 745 | {NULL} |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 746 | }; |
| 747 | |
| 748 | PyDoc_STRVAR(zipimporter_doc, |
| 749 | "zipimporter(archivepath) -> zipimporter object\n\ |
| 750 | \n\ |
| 751 | Create a new zipimporter instance. 'archivepath' must be a path to\n\ |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 752 | a zipfile, or to a specific path inside a zipfile. For example, it can be\n\ |
| 753 | '/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\ |
| 754 | valid directory inside the archive.\n\ |
| 755 | \n\ |
| 756 | 'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\ |
| 757 | archive.\n\ |
| 758 | \n\ |
| 759 | The 'archive' attribute of zipimporter objects contains the name of the\n\ |
| 760 | zipfile targeted."); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 761 | |
| 762 | #define DEFERRED_ADDRESS(ADDR) 0 |
| 763 | |
| 764 | static PyTypeObject ZipImporter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 766 | "zipimport.zipimporter", |
| 767 | sizeof(ZipImporter), |
| 768 | 0, /* tp_itemsize */ |
| 769 | (destructor)zipimporter_dealloc, /* tp_dealloc */ |
| 770 | 0, /* tp_print */ |
| 771 | 0, /* tp_getattr */ |
| 772 | 0, /* tp_setattr */ |
| 773 | 0, /* tp_reserved */ |
| 774 | (reprfunc)zipimporter_repr, /* tp_repr */ |
| 775 | 0, /* tp_as_number */ |
| 776 | 0, /* tp_as_sequence */ |
| 777 | 0, /* tp_as_mapping */ |
| 778 | 0, /* tp_hash */ |
| 779 | 0, /* tp_call */ |
| 780 | 0, /* tp_str */ |
| 781 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 782 | 0, /* tp_setattro */ |
| 783 | 0, /* tp_as_buffer */ |
| 784 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 785 | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 786 | zipimporter_doc, /* tp_doc */ |
| 787 | zipimporter_traverse, /* tp_traverse */ |
| 788 | 0, /* tp_clear */ |
| 789 | 0, /* tp_richcompare */ |
| 790 | 0, /* tp_weaklistoffset */ |
| 791 | 0, /* tp_iter */ |
| 792 | 0, /* tp_iternext */ |
| 793 | zipimporter_methods, /* tp_methods */ |
| 794 | zipimporter_members, /* tp_members */ |
| 795 | 0, /* tp_getset */ |
| 796 | 0, /* tp_base */ |
| 797 | 0, /* tp_dict */ |
| 798 | 0, /* tp_descr_get */ |
| 799 | 0, /* tp_descr_set */ |
| 800 | 0, /* tp_dictoffset */ |
| 801 | (initproc)zipimporter_init, /* tp_init */ |
| 802 | PyType_GenericAlloc, /* tp_alloc */ |
| 803 | PyType_GenericNew, /* tp_new */ |
| 804 | PyObject_GC_Del, /* tp_free */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 805 | }; |
| 806 | |
| 807 | |
| 808 | /* implementation */ |
| 809 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 810 | /* Given a buffer, return the long that is represented by the first |
| 811 | 4 bytes, encoded as little endian. This partially reimplements |
| 812 | marshal.c:r_long() */ |
| 813 | static long |
| 814 | get_long(unsigned char *buf) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | long x; |
| 816 | x = buf[0]; |
| 817 | x |= (long)buf[1] << 8; |
| 818 | x |= (long)buf[2] << 16; |
| 819 | x |= (long)buf[3] << 24; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 820 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | /* Sign extension for 64-bit machines */ |
| 822 | x |= -(x & 0x80000000L); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 823 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | return x; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* |
| 828 | read_directory(archive) -> files dict (new reference) |
| 829 | |
| 830 | Given a path to a Zip archive, build a dict, mapping file names |
| 831 | (local to the archive, using SEP as a separator) to toc entries. |
| 832 | |
| 833 | A toc_entry is a tuple: |
| 834 | |
Victor Stinner | 08654e1 | 2010-10-18 12:09:02 +0000 | [diff] [blame] | 835 | (__file__, # value to use for __file__, available for all files, |
| 836 | # encoded to the filesystem encoding |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | compress, # compression kind; 0 for uncompressed |
| 838 | data_size, # size of compressed data on disk |
| 839 | file_size, # size of decompressed data |
| 840 | file_offset, # offset of file header from start of archive |
| 841 | time, # mod time of file (in dos format) |
| 842 | date, # mod data of file (in dos format) |
| 843 | crc, # crc checksum of the data |
Victor Stinner | c342fca | 2010-10-18 11:39:05 +0000 | [diff] [blame] | 844 | ) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 845 | |
| 846 | Directories can be recognized by the trailing SEP in the name, |
| 847 | data_size and file_offset are 0. |
| 848 | */ |
| 849 | static PyObject * |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 850 | read_directory(PyObject *archive) |
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 | PyObject *files = NULL; |
| 853 | FILE *fp; |
Victor Stinner | d36c821 | 2010-10-18 12:13:46 +0000 | [diff] [blame] | 854 | unsigned short flags; |
Gregory P. Smith | ab32066 | 2012-01-30 15:17:33 -0800 | [diff] [blame] | 855 | short compress, time, date, name_size; |
| 856 | long crc, data_size, file_size, header_size; |
| 857 | Py_ssize_t file_offset, header_position, header_offset; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 858 | long l, count; |
| 859 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | char name[MAXPATHLEN + 5]; |
Victor Stinner | 2460a43 | 2010-08-16 17:54:28 +0000 | [diff] [blame] | 861 | PyObject *nameobj = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | char *p, endof_central_dir[22]; |
Gregory P. Smith | ab32066 | 2012-01-30 15:17:33 -0800 | [diff] [blame] | 863 | Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */ |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 864 | PyObject *path; |
Victor Stinner | d36c821 | 2010-10-18 12:13:46 +0000 | [diff] [blame] | 865 | const char *charset; |
Victor Stinner | 4ee65a9 | 2011-01-22 10:30:29 +0000 | [diff] [blame] | 866 | int bootstrap; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 867 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 868 | fp = _Py_fopen(archive, "rb"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | if (fp == NULL) { |
Victor Stinner | bd206e2 | 2011-12-18 21:04:17 +0100 | [diff] [blame] | 870 | if (!PyErr_Occurred()) |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 871 | PyErr_Format(ZipImportError, "can't open Zip file: %R", archive); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | return NULL; |
| 873 | } |
| 874 | fseek(fp, -22, SEEK_END); |
| 875 | header_position = ftell(fp); |
| 876 | if (fread(endof_central_dir, 1, 22, fp) != 22) { |
| 877 | fclose(fp); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 878 | PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
| 881 | if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) { |
| 882 | /* Bad: End of Central Dir signature */ |
| 883 | fclose(fp); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 884 | PyErr_Format(ZipImportError, "not a Zip file: %R", archive); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | return NULL; |
| 886 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | header_size = get_long((unsigned char *)endof_central_dir + 12); |
| 889 | header_offset = get_long((unsigned char *)endof_central_dir + 16); |
| 890 | arc_offset = header_position - header_offset - header_size; |
| 891 | header_offset += arc_offset; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | files = PyDict_New(); |
| 894 | if (files == NULL) |
| 895 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | /* Start of Central Directory */ |
| 898 | count = 0; |
| 899 | for (;;) { |
| 900 | PyObject *t; |
| 901 | int err; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | fseek(fp, header_offset, 0); /* Start of file header */ |
| 904 | l = PyMarshal_ReadLongFromFile(fp); |
| 905 | if (l != 0x02014B50) |
| 906 | break; /* Bad: Central Dir File Header */ |
Victor Stinner | d36c821 | 2010-10-18 12:13:46 +0000 | [diff] [blame] | 907 | fseek(fp, header_offset + 8, 0); |
| 908 | flags = (unsigned short)PyMarshal_ReadShortFromFile(fp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | compress = PyMarshal_ReadShortFromFile(fp); |
| 910 | time = PyMarshal_ReadShortFromFile(fp); |
| 911 | date = PyMarshal_ReadShortFromFile(fp); |
| 912 | crc = PyMarshal_ReadLongFromFile(fp); |
| 913 | data_size = PyMarshal_ReadLongFromFile(fp); |
| 914 | file_size = PyMarshal_ReadLongFromFile(fp); |
| 915 | name_size = PyMarshal_ReadShortFromFile(fp); |
| 916 | header_size = 46 + name_size + |
| 917 | PyMarshal_ReadShortFromFile(fp) + |
| 918 | PyMarshal_ReadShortFromFile(fp); |
| 919 | fseek(fp, header_offset + 42, 0); |
| 920 | file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset; |
| 921 | if (name_size > MAXPATHLEN) |
| 922 | name_size = MAXPATHLEN; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | p = name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 925 | for (i = 0; i < (Py_ssize_t)name_size; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | *p = (char)getc(fp); |
| 927 | if (*p == '/') |
| 928 | *p = SEP; |
| 929 | p++; |
| 930 | } |
| 931 | *p = 0; /* Add terminating null byte */ |
| 932 | header_offset += header_size; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 933 | |
Victor Stinner | 4ee65a9 | 2011-01-22 10:30:29 +0000 | [diff] [blame] | 934 | bootstrap = 0; |
Victor Stinner | d36c821 | 2010-10-18 12:13:46 +0000 | [diff] [blame] | 935 | if (flags & 0x0800) |
| 936 | charset = "utf-8"; |
Victor Stinner | 4ee65a9 | 2011-01-22 10:30:29 +0000 | [diff] [blame] | 937 | else if (!PyThreadState_GET()->interp->codecs_initialized) { |
| 938 | /* During bootstrap, we may need to load the encodings |
| 939 | package from a ZIP file. But the cp437 encoding is implemented |
| 940 | in Python in the encodings package. |
| 941 | |
| 942 | Break out of this dependency by assuming that the path to |
| 943 | the encodings module is ASCII-only. */ |
| 944 | charset = "ascii"; |
| 945 | bootstrap = 1; |
| 946 | } |
Victor Stinner | d36c821 | 2010-10-18 12:13:46 +0000 | [diff] [blame] | 947 | else |
| 948 | charset = "cp437"; |
| 949 | nameobj = PyUnicode_Decode(name, name_size, charset, NULL); |
Victor Stinner | 4ee65a9 | 2011-01-22 10:30:29 +0000 | [diff] [blame] | 950 | if (nameobj == NULL) { |
| 951 | if (bootstrap) |
| 952 | PyErr_Format(PyExc_NotImplementedError, |
| 953 | "bootstrap issue: python%i%i.zip contains non-ASCII " |
| 954 | "filenames without the unicode flag", |
| 955 | PY_MAJOR_VERSION, PY_MINOR_VERSION); |
Victor Stinner | 2460a43 | 2010-08-16 17:54:28 +0000 | [diff] [blame] | 956 | goto error; |
Victor Stinner | 4ee65a9 | 2011-01-22 10:30:29 +0000 | [diff] [blame] | 957 | } |
Stefan Krah | 000fde9 | 2012-08-20 14:14:49 +0200 | [diff] [blame] | 958 | if (PyUnicode_READY(nameobj) == -1) |
| 959 | goto error; |
Martin v. Löwis | a72e78b | 2011-10-31 08:33:37 +0100 | [diff] [blame] | 960 | path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj); |
| 961 | if (path == NULL) |
Victor Stinner | 2460a43 | 2010-08-16 17:54:28 +0000 | [diff] [blame] | 962 | goto error; |
Gregory P. Smith | cc6abd5 | 2012-01-30 15:55:29 -0800 | [diff] [blame] | 963 | t = Py_BuildValue("Nhllnhhl", path, compress, data_size, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | file_size, file_offset, time, date, crc); |
| 965 | if (t == NULL) |
| 966 | goto error; |
Victor Stinner | 2460a43 | 2010-08-16 17:54:28 +0000 | [diff] [blame] | 967 | err = PyDict_SetItem(files, nameobj, t); |
| 968 | Py_CLEAR(nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | Py_DECREF(t); |
| 970 | if (err != 0) |
| 971 | goto error; |
| 972 | count++; |
| 973 | } |
| 974 | fclose(fp); |
| 975 | if (Py_VerboseFlag) |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 976 | PySys_FormatStderr("# zipimport: found %ld names in %R\n", |
| 977 | count, archive); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | return files; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 979 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | fclose(fp); |
| 981 | Py_XDECREF(files); |
Victor Stinner | 2460a43 | 2010-08-16 17:54:28 +0000 | [diff] [blame] | 982 | Py_XDECREF(nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | /* Return the zlib.decompress function object, or NULL if zlib couldn't |
| 987 | be imported. The function is cached when found, so subsequent calls |
Victor Stinner | 4925cde | 2011-05-20 00:16:09 +0200 | [diff] [blame] | 988 | don't import zlib again. */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 989 | static PyObject * |
| 990 | get_decompress_func(void) |
| 991 | { |
Victor Stinner | 4925cde | 2011-05-20 00:16:09 +0200 | [diff] [blame] | 992 | static int importing_zlib = 0; |
| 993 | PyObject *zlib; |
| 994 | PyObject *decompress; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 995 | _Py_IDENTIFIER(decompress); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 996 | |
Victor Stinner | 4925cde | 2011-05-20 00:16:09 +0200 | [diff] [blame] | 997 | if (importing_zlib != 0) |
| 998 | /* Someone has a zlib.py[co] in their Zip file; |
| 999 | let's avoid a stack overflow. */ |
| 1000 | return NULL; |
| 1001 | importing_zlib = 1; |
| 1002 | zlib = PyImport_ImportModuleNoBlock("zlib"); |
| 1003 | importing_zlib = 0; |
| 1004 | if (zlib != NULL) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1005 | decompress = _PyObject_GetAttrId(zlib, |
| 1006 | &PyId_decompress); |
Victor Stinner | 4925cde | 2011-05-20 00:16:09 +0200 | [diff] [blame] | 1007 | Py_DECREF(zlib); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | } |
Victor Stinner | 4925cde | 2011-05-20 00:16:09 +0200 | [diff] [blame] | 1009 | else { |
| 1010 | PyErr_Clear(); |
| 1011 | decompress = NULL; |
| 1012 | } |
| 1013 | if (Py_VerboseFlag) |
| 1014 | PySys_WriteStderr("# zipimport: zlib %s\n", |
| 1015 | zlib != NULL ? "available": "UNAVAILABLE"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | return decompress; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /* Given a path to a Zip file and a toc_entry, return the (uncompressed) |
| 1020 | data as a new reference. */ |
| 1021 | static PyObject * |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 1022 | get_data(PyObject *archive, PyObject *toc_entry) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1023 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | PyObject *raw_data, *data = NULL, *decompress; |
| 1025 | char *buf; |
| 1026 | FILE *fp; |
| 1027 | int err; |
| 1028 | Py_ssize_t bytes_read = 0; |
| 1029 | long l; |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 1030 | PyObject *datapath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | long compress, data_size, file_size, file_offset, bytes_size; |
| 1032 | long time, date, crc; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1033 | |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 1034 | if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | &data_size, &file_size, &file_offset, &time, |
| 1036 | &date, &crc)) { |
| 1037 | return NULL; |
| 1038 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1039 | |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 1040 | fp = _Py_fopen(archive, "rb"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | if (!fp) { |
Victor Stinner | bd206e2 | 2011-12-18 21:04:17 +0100 | [diff] [blame] | 1042 | if (!PyErr_Occurred()) |
| 1043 | PyErr_Format(PyExc_IOError, |
| 1044 | "zipimport: can not open file %U", archive); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | return NULL; |
| 1046 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | /* Check to make sure the local file header is correct */ |
| 1049 | fseek(fp, file_offset, 0); |
| 1050 | l = PyMarshal_ReadLongFromFile(fp); |
| 1051 | if (l != 0x04034B50) { |
| 1052 | /* Bad: Local File Header */ |
| 1053 | PyErr_Format(ZipImportError, |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 1054 | "bad local file header in %U", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | archive); |
| 1056 | fclose(fp); |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | fseek(fp, file_offset + 26, 0); |
| 1060 | l = 30 + PyMarshal_ReadShortFromFile(fp) + |
| 1061 | PyMarshal_ReadShortFromFile(fp); /* local header size */ |
| 1062 | file_offset += l; /* Start of file data */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | bytes_size = compress == 0 ? data_size : data_size + 1; |
| 1065 | if (bytes_size == 0) |
| 1066 | bytes_size++; |
| 1067 | raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | if (raw_data == NULL) { |
| 1070 | fclose(fp); |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | buf = PyBytes_AsString(raw_data); |
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 | err = fseek(fp, file_offset, 0); |
| 1076 | if (err == 0) |
| 1077 | bytes_read = fread(buf, 1, data_size, fp); |
| 1078 | fclose(fp); |
| 1079 | if (err || bytes_read != data_size) { |
| 1080 | PyErr_SetString(PyExc_IOError, |
| 1081 | "zipimport: can't read data"); |
| 1082 | Py_DECREF(raw_data); |
| 1083 | return NULL; |
| 1084 | } |
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 | if (compress != 0) { |
| 1087 | buf[data_size] = 'Z'; /* saw this in zipfile.py */ |
| 1088 | data_size++; |
| 1089 | } |
| 1090 | buf[data_size] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | if (compress == 0) { /* data is not compressed */ |
| 1093 | data = PyBytes_FromStringAndSize(buf, data_size); |
| 1094 | Py_DECREF(raw_data); |
| 1095 | return data; |
| 1096 | } |
| 1097 | |
| 1098 | /* Decompress with zlib */ |
| 1099 | decompress = get_decompress_func(); |
| 1100 | if (decompress == NULL) { |
| 1101 | PyErr_SetString(ZipImportError, |
| 1102 | "can't decompress data; " |
| 1103 | "zlib not available"); |
| 1104 | goto error; |
| 1105 | } |
| 1106 | data = PyObject_CallFunction(decompress, "Oi", raw_data, -15); |
Victor Stinner | 4925cde | 2011-05-20 00:16:09 +0200 | [diff] [blame] | 1107 | Py_DECREF(decompress); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1108 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | Py_DECREF(raw_data); |
| 1110 | return data; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* Lenient date/time comparison function. The precision of the mtime |
| 1114 | in the archive is lower than the mtime stored in a .pyc: we |
| 1115 | must allow a difference of at most one second. */ |
| 1116 | static int |
| 1117 | eq_mtime(time_t t1, time_t t2) |
| 1118 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | time_t d = t1 - t2; |
| 1120 | if (d < 0) |
| 1121 | d = -d; |
| 1122 | /* dostime only stores even seconds, so be lenient */ |
| 1123 | return d <= 1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* Given the contents of a .py[co] file in a buffer, unmarshal the data |
| 1127 | and return the code object. Return None if it the magic word doesn't |
| 1128 | match (we do this instead of raising an exception as we fall back |
| 1129 | to .py if available and we don't want to mask other errors). |
| 1130 | Returns a new reference. */ |
| 1131 | static PyObject * |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1132 | unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | PyObject *code; |
| 1135 | char *buf = PyBytes_AsString(data); |
| 1136 | Py_ssize_t size = PyBytes_Size(data); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | if (size <= 9) { |
| 1139 | PyErr_SetString(ZipImportError, |
| 1140 | "bad pyc data"); |
| 1141 | return NULL; |
| 1142 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) { |
| 1145 | if (Py_VerboseFlag) |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1146 | PySys_FormatStderr("# %R has bad magic\n", |
| 1147 | pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | Py_INCREF(Py_None); |
| 1149 | return Py_None; /* signal caller to try alternative */ |
| 1150 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4), |
| 1153 | mtime)) { |
| 1154 | if (Py_VerboseFlag) |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1155 | PySys_FormatStderr("# %R has bad mtime\n", |
| 1156 | pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | Py_INCREF(Py_None); |
| 1158 | return Py_None; /* signal caller to try alternative */ |
| 1159 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 1161 | /* XXX the pyc's size field is ignored; timestamp collisions are probably |
| 1162 | unimportant with zip files. */ |
| 1163 | code = PyMarshal_ReadObjectFromString(buf + 12, size - 12); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | if (code == NULL) |
| 1165 | return NULL; |
| 1166 | if (!PyCode_Check(code)) { |
| 1167 | Py_DECREF(code); |
| 1168 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1169 | "compiled module %R is not a code object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | pathname); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | return code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* Replace any occurances of "\r\n?" in the input string with "\n". |
| 1177 | This converts DOS and Mac line endings to Unix line endings. |
| 1178 | Also append a trailing "\n" to be compatible with |
| 1179 | PyParser_SimpleParseFile(). Returns a new reference. */ |
| 1180 | static PyObject * |
| 1181 | normalize_line_endings(PyObject *source) |
| 1182 | { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1183 | char *buf, *q, *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | PyObject *fixed_source; |
| 1185 | int len = 0; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1186 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1187 | p = PyBytes_AsString(source); |
| 1188 | if (p == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | return PyBytes_FromStringAndSize("\n\0", 2); |
| 1190 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | /* one char extra for trailing \n and one for terminating \0 */ |
| 1193 | buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2); |
| 1194 | if (buf == NULL) { |
| 1195 | PyErr_SetString(PyExc_MemoryError, |
| 1196 | "zipimport: no memory to allocate " |
| 1197 | "source buffer"); |
| 1198 | return NULL; |
| 1199 | } |
| 1200 | /* replace "\r\n?" by "\n" */ |
| 1201 | for (q = buf; *p != '\0'; p++) { |
| 1202 | if (*p == '\r') { |
| 1203 | *q++ = '\n'; |
| 1204 | if (*(p + 1) == '\n') |
| 1205 | p++; |
| 1206 | } |
| 1207 | else |
| 1208 | *q++ = *p; |
| 1209 | len++; |
| 1210 | } |
| 1211 | *q++ = '\n'; /* add trailing \n */ |
| 1212 | *q = '\0'; |
| 1213 | fixed_source = PyBytes_FromStringAndSize(buf, len + 2); |
| 1214 | PyMem_Free(buf); |
| 1215 | return fixed_source; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* Given a string buffer containing Python source code, compile it |
| 1219 | return and return a code object as a new reference. */ |
| 1220 | static PyObject * |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1221 | compile_source(PyObject *pathname, PyObject *source) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1222 | { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1223 | PyObject *code, *fixed_source, *pathbytes; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1224 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1225 | pathbytes = PyUnicode_EncodeFSDefault(pathname); |
| 1226 | if (pathbytes == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1228 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1229 | fixed_source = normalize_line_endings(source); |
| 1230 | if (fixed_source == NULL) { |
| 1231 | Py_DECREF(pathbytes); |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
| 1235 | code = Py_CompileString(PyBytes_AsString(fixed_source), |
| 1236 | PyBytes_AsString(pathbytes), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | Py_file_input); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1238 | Py_DECREF(pathbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | Py_DECREF(fixed_source); |
| 1240 | return code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* Convert the date/time values found in the Zip archive to a value |
| 1244 | that's compatible with the time stamp stored in .pyc files. */ |
Neal Norwitz | 29fd2ba | 2003-03-23 13:21:03 +0000 | [diff] [blame] | 1245 | static time_t |
| 1246 | parse_dostime(int dostime, int dosdate) |
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 | struct tm stm; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | memset((void *) &stm, '\0', sizeof(stm)); |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | stm.tm_sec = (dostime & 0x1f) * 2; |
| 1253 | stm.tm_min = (dostime >> 5) & 0x3f; |
| 1254 | stm.tm_hour = (dostime >> 11) & 0x1f; |
| 1255 | stm.tm_mday = dosdate & 0x1f; |
| 1256 | stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1; |
| 1257 | stm.tm_year = ((dosdate >> 9) & 0x7f) + 80; |
| 1258 | stm.tm_isdst = -1; /* wday/yday is ignored */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | return mktime(&stm); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | /* Given a path to a .pyc or .pyo file in the archive, return the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1264 | modification time of the matching .py file, or 0 if no source |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1265 | is available. */ |
| 1266 | static time_t |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1267 | get_mtime_of_source(ZipImporter *self, PyObject *path) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1268 | { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1269 | PyObject *toc_entry, *stripped; |
| 1270 | time_t mtime; |
| 1271 | |
| 1272 | /* strip 'c' or 'o' from *.py[co] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1273 | if (PyUnicode_READY(path) == -1) |
| 1274 | return (time_t)-1; |
| 1275 | stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path), |
| 1276 | PyUnicode_DATA(path), |
| 1277 | PyUnicode_GET_LENGTH(path) - 1); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1278 | if (stripped == NULL) |
| 1279 | return (time_t)-1; |
| 1280 | |
| 1281 | toc_entry = PyDict_GetItem(self->files, stripped); |
| 1282 | Py_DECREF(stripped); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | if (toc_entry != NULL && PyTuple_Check(toc_entry) && |
| 1284 | PyTuple_Size(toc_entry) == 8) { |
| 1285 | /* fetch the time stamp of the .py file for comparison |
| 1286 | with an embedded pyc time stamp */ |
| 1287 | int time, date; |
| 1288 | time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5)); |
| 1289 | date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6)); |
| 1290 | mtime = parse_dostime(time, date); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1291 | } else |
| 1292 | mtime = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | return mtime; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /* Return the code object for the module named by 'fullname' from the |
| 1297 | Zip archive as a new reference. */ |
| 1298 | static PyObject * |
| 1299 | get_code_from_data(ZipImporter *self, int ispackage, int isbytecode, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | time_t mtime, PyObject *toc_entry) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1301 | { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1302 | PyObject *data, *modpath, *code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1303 | |
Victor Stinner | 60fe8d9 | 2010-08-16 23:48:11 +0000 | [diff] [blame] | 1304 | data = get_data(self->archive, toc_entry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | if (data == NULL) |
| 1306 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1307 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1308 | modpath = PyTuple_GetItem(toc_entry, 0); |
Victor Stinner | 2a94f4c | 2010-10-18 12:15:34 +0000 | [diff] [blame] | 1309 | if (isbytecode) |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1310 | code = unmarshal_code(modpath, data, mtime); |
Victor Stinner | 2a94f4c | 2010-10-18 12:15:34 +0000 | [diff] [blame] | 1311 | else |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1312 | code = compile_source(modpath, data); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | Py_DECREF(data); |
| 1314 | return code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1317 | /* Get the code object associated with the module specified by |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1318 | 'fullname'. */ |
| 1319 | static PyObject * |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1320 | get_module_code(ZipImporter *self, PyObject *fullname, |
Victor Stinner | 08654e1 | 2010-10-18 12:09:02 +0000 | [diff] [blame] | 1321 | int *p_ispackage, PyObject **p_modpath) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1322 | { |
Gregory P. Smith | 95c7c46 | 2011-05-21 05:19:42 -0700 | [diff] [blame] | 1323 | PyObject *code = NULL, *toc_entry, *subname; |
Victor Stinner | 9a2261a | 2011-05-26 13:59:41 +0200 | [diff] [blame] | 1324 | PyObject *path, *fullpath = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | struct st_zip_searchorder *zso; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | subname = get_subname(fullname); |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1328 | if (subname == NULL) |
| 1329 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1330 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1331 | path = make_filename(self->prefix, subname); |
| 1332 | Py_DECREF(subname); |
| 1333 | if (path == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | for (zso = zip_searchorder; *zso->suffix; zso++) { |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1337 | code = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1338 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1339 | fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
| 1340 | if (fullpath == NULL) |
| 1341 | goto exit; |
| 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | if (Py_VerboseFlag > 1) |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1344 | PySys_FormatStderr("# trying %U%c%U\n", |
| 1345 | self->archive, (int)SEP, fullpath); |
| 1346 | toc_entry = PyDict_GetItem(self->files, fullpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | if (toc_entry != NULL) { |
| 1348 | time_t mtime = 0; |
| 1349 | int ispackage = zso->type & IS_PACKAGE; |
| 1350 | int isbytecode = zso->type & IS_BYTECODE; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1351 | |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1352 | if (isbytecode) { |
| 1353 | mtime = get_mtime_of_source(self, fullpath); |
| 1354 | if (mtime == (time_t)-1 && PyErr_Occurred()) { |
| 1355 | goto exit; |
| 1356 | } |
| 1357 | } |
| 1358 | Py_CLEAR(fullpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | if (p_ispackage != NULL) |
| 1360 | *p_ispackage = ispackage; |
| 1361 | code = get_code_from_data(self, ispackage, |
| 1362 | isbytecode, mtime, |
| 1363 | toc_entry); |
| 1364 | if (code == Py_None) { |
| 1365 | /* bad magic number or non-matching mtime |
| 1366 | in byte code, try next */ |
| 1367 | Py_DECREF(code); |
| 1368 | continue; |
| 1369 | } |
Victor Stinner | 08654e1 | 2010-10-18 12:09:02 +0000 | [diff] [blame] | 1370 | if (code != NULL && p_modpath != NULL) { |
| 1371 | *p_modpath = PyTuple_GetItem(toc_entry, 0); |
| 1372 | Py_INCREF(*p_modpath); |
| 1373 | } |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1374 | goto exit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | } |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1376 | else |
| 1377 | Py_CLEAR(fullpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | } |
Victor Stinner | f6b563a | 2011-03-14 20:46:50 -0400 | [diff] [blame] | 1379 | PyErr_Format(ZipImportError, "can't find module %R", fullname); |
| 1380 | exit: |
| 1381 | Py_DECREF(path); |
| 1382 | Py_XDECREF(fullpath); |
| 1383 | return code; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | /* Module init */ |
| 1388 | |
| 1389 | PyDoc_STRVAR(zipimport_doc, |
| 1390 | "zipimport provides support for importing Python modules from Zip archives.\n\ |
| 1391 | \n\ |
| 1392 | This module exports three objects:\n\ |
| 1393 | - 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] | 1394 | - ZipImportError: exception raised by zipimporter objects. It's a\n\ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1395 | subclass of ImportError, so it can be caught as ImportError, too.\n\ |
| 1396 | - _zip_directory_cache: a dict, mapping archive paths to zip directory\n\ |
| 1397 | info dicts, as used in zipimporter._files.\n\ |
| 1398 | \n\ |
| 1399 | It is usually not needed to use the zipimport module explicitly; it is\n\ |
| 1400 | used by the builtin import mechanism for sys.path items that are paths\n\ |
| 1401 | to Zip archives."); |
| 1402 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1403 | static struct PyModuleDef zipimportmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | PyModuleDef_HEAD_INIT, |
| 1405 | "zipimport", |
| 1406 | zipimport_doc, |
| 1407 | -1, |
| 1408 | NULL, |
| 1409 | NULL, |
| 1410 | NULL, |
| 1411 | NULL, |
| 1412 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1413 | }; |
| 1414 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1415 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1416 | PyInit_zipimport(void) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | PyObject *mod; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | if (PyType_Ready(&ZipImporter_Type) < 0) |
| 1421 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | /* Correct directory separator */ |
| 1424 | zip_searchorder[0].suffix[0] = SEP; |
| 1425 | zip_searchorder[1].suffix[0] = SEP; |
| 1426 | zip_searchorder[2].suffix[0] = SEP; |
| 1427 | if (Py_OptimizeFlag) { |
| 1428 | /* Reverse *.pyc and *.pyo */ |
| 1429 | struct st_zip_searchorder tmp; |
| 1430 | tmp = zip_searchorder[0]; |
| 1431 | zip_searchorder[0] = zip_searchorder[1]; |
| 1432 | zip_searchorder[1] = tmp; |
| 1433 | tmp = zip_searchorder[3]; |
| 1434 | zip_searchorder[3] = zip_searchorder[4]; |
| 1435 | zip_searchorder[4] = tmp; |
| 1436 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | mod = PyModule_Create(&zipimportmodule); |
| 1439 | if (mod == NULL) |
| 1440 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | ZipImportError = PyErr_NewException("zipimport.ZipImportError", |
| 1443 | PyExc_ImportError, NULL); |
| 1444 | if (ZipImportError == NULL) |
| 1445 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | Py_INCREF(ZipImportError); |
| 1448 | if (PyModule_AddObject(mod, "ZipImportError", |
| 1449 | ZipImportError) < 0) |
| 1450 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | Py_INCREF(&ZipImporter_Type); |
| 1453 | if (PyModule_AddObject(mod, "zipimporter", |
| 1454 | (PyObject *)&ZipImporter_Type) < 0) |
| 1455 | return NULL; |
Just van Rossum | f8b6de1 | 2002-12-31 09:51:59 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | zip_directory_cache = PyDict_New(); |
| 1458 | if (zip_directory_cache == NULL) |
| 1459 | return NULL; |
| 1460 | Py_INCREF(zip_directory_cache); |
| 1461 | if (PyModule_AddObject(mod, "_zip_directory_cache", |
| 1462 | zip_directory_cache) < 0) |
| 1463 | return NULL; |
| 1464 | return mod; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1465 | } |