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