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