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