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