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