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