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