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