You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1510 lines
46 KiB

14 years ago
14 years ago
14 years ago
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63066 | georg.brandl | 2008-05-11 10:56:04 -0400 (Sun, 11 May 2008) | 2 lines #2709 followup: better description of Tk's pros and cons. ........ r63067 | georg.brandl | 2008-05-11 11:05:13 -0400 (Sun, 11 May 2008) | 2 lines #1326: document and test zipimporter.archive and zipimporter.prefix. ........ r63068 | georg.brandl | 2008-05-11 11:07:39 -0400 (Sun, 11 May 2008) | 2 lines #2816: clarify error messages for EOF while scanning strings. ........ r63069 | georg.brandl | 2008-05-11 11:17:41 -0400 (Sun, 11 May 2008) | 3 lines #2787: Flush stdout after writing test name, helpful when running hanging or long-running tests. Patch by Adam Olsen. ........ r63070 | georg.brandl | 2008-05-11 11:20:16 -0400 (Sun, 11 May 2008) | 3 lines #2803: fix wrong invocation of heappush in seldom-reached code. Thanks to Matt Harden. ........ r63073 | benjamin.peterson | 2008-05-11 12:38:07 -0400 (Sun, 11 May 2008) | 2 lines broaden .bzrignore ........ r63076 | andrew.kuchling | 2008-05-11 15:15:52 -0400 (Sun, 11 May 2008) | 1 line Add message to test assertion ........ r63083 | andrew.kuchling | 2008-05-11 16:08:33 -0400 (Sun, 11 May 2008) | 1 line Try setting HOME env.var to fix test on Win32 ........ r63092 | georg.brandl | 2008-05-11 16:53:55 -0400 (Sun, 11 May 2008) | 2 lines #2809 followup: even better split docstring. ........ r63094 | georg.brandl | 2008-05-11 17:03:42 -0400 (Sun, 11 May 2008) | 4 lines - #2250: Exceptions raised during evaluation of names in rlcompleter's ``Completer.complete()`` method are now caught and ignored. ........ r63095 | georg.brandl | 2008-05-11 17:16:37 -0400 (Sun, 11 May 2008) | 2 lines Clarify os.strerror()s exception behavior. ........ r63097 | georg.brandl | 2008-05-11 17:34:10 -0400 (Sun, 11 May 2008) | 2 lines #2535: remove duplicated method. ........ r63104 | alexandre.vassalotti | 2008-05-11 19:04:27 -0400 (Sun, 11 May 2008) | 2 lines Moved the Queue module stub in lib-old. ........
18 years ago
Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59987 | raymond.hettinger | 2008-01-15 21:52:42 +0100 (Tue, 15 Jan 2008) | 1 line Refactor if/elif chain for clarity and speed. Remove dependency on subclasses having to implement _empty and _full. ........ r59988 | raymond.hettinger | 2008-01-15 22:22:47 +0100 (Tue, 15 Jan 2008) | 1 line Fix-up half-written paragraph in the docs ........ r59989 | amaury.forgeotdarc | 2008-01-15 22:25:11 +0100 (Tue, 15 Jan 2008) | 3 lines test_doctest fails since r59984. Not sure if these are the correct values, but save_stdout has to be set before its usage... ........ r59992 | andrew.kuchling | 2008-01-16 01:32:03 +0100 (Wed, 16 Jan 2008) | 1 line Docstring typos ........ r59993 | andrew.kuchling | 2008-01-16 04:17:25 +0100 (Wed, 16 Jan 2008) | 1 line Add PEP 3141 section ........ r59998 | andrew.kuchling | 2008-01-16 14:01:51 +0100 (Wed, 16 Jan 2008) | 1 line Markup fix ........ r59999 | georg.brandl | 2008-01-16 17:56:29 +0100 (Wed, 16 Jan 2008) | 2 lines Fix MSDN library URL. (#1854) ........ r60006 | georg.brandl | 2008-01-16 21:27:56 +0100 (Wed, 16 Jan 2008) | 3 lines Add Python-specific content to Doc dir. Update configuration file to work with the newest Sphinx. ........ r60007 | georg.brandl | 2008-01-16 21:29:00 +0100 (Wed, 16 Jan 2008) | 2 lines Doc build should work with 2.4 now. ........ r60009 | raymond.hettinger | 2008-01-17 00:38:16 +0100 (Thu, 17 Jan 2008) | 1 line Minor wordsmithing. ........ r60010 | raymond.hettinger | 2008-01-17 00:40:45 +0100 (Thu, 17 Jan 2008) | 1 line Add queues will alternative fetch orders (priority based and stack based). ........ r60011 | raymond.hettinger | 2008-01-17 00:49:35 +0100 (Thu, 17 Jan 2008) | 1 line Add news entry. ........ r60013 | raymond.hettinger | 2008-01-17 04:02:14 +0100 (Thu, 17 Jan 2008) | 1 line Make starmap() match its pure python definition and accept any itertable input (not just tuples). ........ r60015 | gregory.p.smith | 2008-01-17 08:43:20 +0100 (Thu, 17 Jan 2008) | 3 lines Comply with RFC 3207. Fixes issue 829951 - http://bugs.python.org/issue829951 ........ r60018 | gregory.p.smith | 2008-01-17 09:03:17 +0100 (Thu, 17 Jan 2008) | 2 lines entry for r60015 ........ r60019 | raymond.hettinger | 2008-01-17 09:07:05 +0100 (Thu, 17 Jan 2008) | 1 line Note versionadded. ........ r60020 | gregory.p.smith | 2008-01-17 09:35:49 +0100 (Thu, 17 Jan 2008) | 8 lines Fixes (accepts patch) issue1339 - http://bugs.python.org/issue1339 - Factor out the duplication of EHLO/HELO in login() and sendmail() to a new function, ehlo_or_helo_if_needed(). - Use ehlo_or_helo_if_needed() in starttls() - Check for the starttls exception in starttls() in the same way as login() checks for the auth extension. Contributed by Bill Fenner. ........ r60021 | andrew.kuchling | 2008-01-17 13:00:15 +0100 (Thu, 17 Jan 2008) | 1 line Revise 3141 section a bit; add some Windows items ........ r60022 | brett.cannon | 2008-01-17 19:45:10 +0100 (Thu, 17 Jan 2008) | 2 lines Fix a function pointer declaration to silence the compiler. ........ r60024 | raymond.hettinger | 2008-01-17 20:31:38 +0100 (Thu, 17 Jan 2008) | 1 line Issue #1861: Add read-only attribute listing upcoming events in the order they will be run. ........ r60025 | andrew.kuchling | 2008-01-17 20:49:24 +0100 (Thu, 17 Jan 2008) | 1 line Correction from Jordan Lewis: halfdelay() uses tenths of a second, not milliseconds ........ r60026 | raymond.hettinger | 2008-01-17 23:27:49 +0100 (Thu, 17 Jan 2008) | 1 line Add advice on choosing between scheduler and threading.Timer(). ........ r60028 | christian.heimes | 2008-01-18 00:01:44 +0100 (Fri, 18 Jan 2008) | 2 lines Updated new property syntax. An elaborate example for subclassing and the getter was missing. Added comment about VS 2008 and PGO builds. ........ r60029 | raymond.hettinger | 2008-01-18 00:32:01 +0100 (Fri, 18 Jan 2008) | 1 line Fix-up Timer() example. ........ r60030 | raymond.hettinger | 2008-01-18 00:56:56 +0100 (Fri, 18 Jan 2008) | 1 line Fix markup ........ r60031 | raymond.hettinger | 2008-01-18 01:10:42 +0100 (Fri, 18 Jan 2008) | 1 line clearcache() needs to remove the dict as well as clear it. ........ r60033 | andrew.kuchling | 2008-01-18 03:26:16 +0100 (Fri, 18 Jan 2008) | 1 line Bump verson ........ r60034 | andrew.kuchling | 2008-01-18 03:42:52 +0100 (Fri, 18 Jan 2008) | 1 line Typo fix ........ r60035 | christian.heimes | 2008-01-18 08:30:20 +0100 (Fri, 18 Jan 2008) | 3 lines Coverity issue CID #197 var_decl: Declared variable "stm" without initializer ninit_use_in_call: Using uninitialized value "stm" (field "stm".tm_zone uninitialized) in call to function "mktime" ........ r60036 | christian.heimes | 2008-01-18 08:45:30 +0100 (Fri, 18 Jan 2008) | 11 lines Coverity issue CID #167 Event alloc_fn: Called allocation function "metacompile" [model] Event var_assign: Assigned variable "gr" to storage returned from "metacompile" gr = metacompile(n); Event pass_arg: Variable "gr" not freed or pointed-to in function "maketables" [model] g = maketables(gr); translatelabels(g); addfirstsets(g); Event leaked_storage: Returned without freeing storage "gr" return g; ........ r60038 | christian.heimes | 2008-01-18 09:04:57 +0100 (Fri, 18 Jan 2008) | 3 lines Coverity issue CID #182 size_error: Allocating 1 bytes to pointer "children", which needs at least 4 bytes ........ r60041 | christian.heimes | 2008-01-18 09:47:59 +0100 (Fri, 18 Jan 2008) | 4 lines Coverity issue CID #169 local_ptr_assign_local: Assigning address of stack variable "namebuf" to pointer "filename" out_of_scope: Variable "namebuf" goes out of scope use_invalid: Used "filename" pointing to out-of-scope variable "namebuf" ........ r60042 | christian.heimes | 2008-01-18 09:53:45 +0100 (Fri, 18 Jan 2008) | 2 lines Coverity CID #168 leaked_storage: Returned without freeing storage "fp" ........
18 years ago
  1. #include "Python.h"
  2. #include "structmember.h"
  3. #include "osdefs.h"
  4. #include "marshal.h"
  5. #include <time.h>
  6. #define IS_SOURCE 0x0
  7. #define IS_BYTECODE 0x1
  8. #define IS_PACKAGE 0x2
  9. struct st_zip_searchorder {
  10. char suffix[14];
  11. int type;
  12. };
  13. #ifdef ALTSEP
  14. _Py_IDENTIFIER(replace);
  15. #endif
  16. /* zip_searchorder defines how we search for a module in the Zip
  17. archive: we first search for a package __init__, then for
  18. non-package .pyc, and .py entries. The .pyc entries
  19. are swapped by initzipimport() if we run in optimized mode. Also,
  20. '/' is replaced by SEP there. */
  21. static struct st_zip_searchorder zip_searchorder[] = {
  22. {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
  23. {"/__init__.py", IS_PACKAGE | IS_SOURCE},
  24. {".pyc", IS_BYTECODE},
  25. {".py", IS_SOURCE},
  26. {"", 0}
  27. };
  28. /* zipimporter object definition and support */
  29. typedef struct _zipimporter ZipImporter;
  30. struct _zipimporter {
  31. PyObject_HEAD
  32. PyObject *archive; /* pathname of the Zip archive,
  33. decoded from the filesystem encoding */
  34. PyObject *prefix; /* file prefix: "a/sub/directory/",
  35. encoded to the filesystem encoding */
  36. PyObject *files; /* dict with file info {path: toc_entry} */
  37. };
  38. static PyObject *ZipImportError;
  39. /* read_directory() cache */
  40. static PyObject *zip_directory_cache = NULL;
  41. /* forward decls */
  42. static PyObject *read_directory(PyObject *archive);
  43. static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
  44. static PyObject *get_module_code(ZipImporter *self, PyObject *fullname,
  45. int *p_ispackage, PyObject **p_modpath);
  46. #define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
  47. /* zipimporter.__init__
  48. Split the "subdirectory" from the Zip archive path, lookup a matching
  49. entry in sys.path_importer_cache, fetch the file directory from there
  50. if found, or else read it from the archive. */
  51. static int
  52. zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
  53. {
  54. PyObject *path, *files, *tmp;
  55. PyObject *filename = NULL;
  56. Py_ssize_t len, flen;
  57. if (!_PyArg_NoKeywords("zipimporter()", kwds))
  58. return -1;
  59. if (!PyArg_ParseTuple(args, "O&:zipimporter",
  60. PyUnicode_FSDecoder, &path))
  61. return -1;
  62. if (PyUnicode_READY(path) == -1)
  63. return -1;
  64. len = PyUnicode_GET_LENGTH(path);
  65. if (len == 0) {
  66. PyErr_SetString(ZipImportError, "archive path is empty");
  67. goto error;
  68. }
  69. #ifdef ALTSEP
  70. tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
  71. if (!tmp)
  72. goto error;
  73. Py_DECREF(path);
  74. path = tmp;
  75. #endif
  76. filename = path;
  77. Py_INCREF(filename);
  78. flen = len;
  79. for (;;) {
  80. struct stat statbuf;
  81. int rv;
  82. rv = _Py_stat(filename, &statbuf);
  83. if (rv == -2)
  84. goto error;
  85. if (rv == 0) {
  86. /* it exists */
  87. if (!S_ISREG(statbuf.st_mode))
  88. /* it's a not file */
  89. Py_CLEAR(filename);
  90. break;
  91. }
  92. Py_CLEAR(filename);
  93. /* back up one path element */
  94. flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);
  95. if (flen == -1)
  96. break;
  97. filename = PyUnicode_Substring(path, 0, flen);
  98. if (filename == NULL)
  99. goto error;
  100. }
  101. if (filename == NULL) {
  102. PyErr_SetString(ZipImportError, "not a Zip file");
  103. goto error;
  104. }
  105. if (PyUnicode_READY(filename) < 0)
  106. goto error;
  107. files = PyDict_GetItem(zip_directory_cache, filename);
  108. if (files == NULL) {
  109. files = read_directory(filename);
  110. if (files == NULL)
  111. goto error;
  112. if (PyDict_SetItem(zip_directory_cache, filename, files) != 0)
  113. goto error;
  114. }
  115. else
  116. Py_INCREF(files);
  117. self->files = files;
  118. /* Transfer reference */
  119. self->archive = filename;
  120. filename = NULL;
  121. /* Check if there is a prefix directory following the filename. */
  122. if (flen != len) {
  123. tmp = PyUnicode_Substring(path, flen+1,
  124. PyUnicode_GET_LENGTH(path));
  125. if (tmp == NULL)
  126. goto error;
  127. self->prefix = tmp;
  128. if (PyUnicode_READ_CHAR(path, len-1) != SEP) {
  129. /* add trailing SEP */
  130. tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
  131. if (tmp == NULL)
  132. goto error;
  133. Py_DECREF(self->prefix);
  134. self->prefix = tmp;
  135. }
  136. }
  137. else
  138. self->prefix = PyUnicode_New(0, 0);
  139. Py_DECREF(path);
  140. return 0;
  141. error:
  142. Py_DECREF(path);
  143. Py_XDECREF(filename);
  144. return -1;
  145. }
  146. /* GC support. */
  147. static int
  148. zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
  149. {
  150. ZipImporter *self = (ZipImporter *)obj;
  151. Py_VISIT(self->files);
  152. return 0;
  153. }
  154. static void
  155. zipimporter_dealloc(ZipImporter *self)
  156. {
  157. PyObject_GC_UnTrack(self);
  158. Py_XDECREF(self->archive);
  159. Py_XDECREF(self->prefix);
  160. Py_XDECREF(self->files);
  161. Py_TYPE(self)->tp_free((PyObject *)self);
  162. }
  163. static PyObject *
  164. zipimporter_repr(ZipImporter *self)
  165. {
  166. if (self->archive == NULL)
  167. return PyUnicode_FromString("<zipimporter object \"???\">");
  168. else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0)
  169. return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
  170. self->archive, SEP, self->prefix);
  171. else
  172. return PyUnicode_FromFormat("<zipimporter object \"%U\">",
  173. self->archive);
  174. }
  175. /* return fullname.split(".")[-1] */
  176. static PyObject *
  177. get_subname(PyObject *fullname)
  178. {
  179. Py_ssize_t len, dot;
  180. if (PyUnicode_READY(fullname) < 0)
  181. return NULL;
  182. len = PyUnicode_GET_LENGTH(fullname);
  183. dot = PyUnicode_FindChar(fullname, '.', 0, len, -1);
  184. if (dot == -1) {
  185. Py_INCREF(fullname);
  186. return fullname;
  187. } else
  188. return PyUnicode_Substring(fullname, dot+1, len);
  189. }
  190. /* Given a (sub)modulename, write the potential file path in the
  191. archive (without extension) to the path buffer. Return the
  192. length of the resulting string.
  193. return self.prefix + name.replace('.', os.sep) */
  194. static PyObject*
  195. make_filename(PyObject *prefix, PyObject *name)
  196. {
  197. PyObject *pathobj;
  198. Py_UCS4 *p, *buf;
  199. Py_ssize_t len;
  200. len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1;
  201. p = buf = PyMem_New(Py_UCS4, len);
  202. if (buf == NULL) {
  203. PyErr_NoMemory();
  204. return NULL;
  205. }
  206. if (!PyUnicode_AsUCS4(prefix, p, len, 0)) {
  207. PyMem_Free(buf);
  208. return NULL;
  209. }
  210. p += PyUnicode_GET_LENGTH(prefix);
  211. len -= PyUnicode_GET_LENGTH(prefix);
  212. if (!PyUnicode_AsUCS4(name, p, len, 1)) {
  213. PyMem_Free(buf);
  214. return NULL;
  215. }
  216. for (; *p; p++) {
  217. if (*p == '.')
  218. *p = SEP;
  219. }
  220. pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
  221. buf, p-buf);
  222. PyMem_Free(buf);
  223. return pathobj;
  224. }
  225. enum zi_module_info {
  226. MI_ERROR,
  227. MI_NOT_FOUND,
  228. MI_MODULE,
  229. MI_PACKAGE
  230. };
  231. /* Does this path represent a directory?
  232. on error, return < 0
  233. if not a dir, return 0
  234. if a dir, return 1
  235. */
  236. static int
  237. check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path)
  238. {
  239. PyObject *dirpath;
  240. int res;
  241. /* See if this is a "directory". If so, it's eligible to be part
  242. of a namespace package. We test by seeing if the name, with an
  243. appended path separator, exists. */
  244. dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
  245. if (dirpath == NULL)
  246. return -1;
  247. /* If dirpath is present in self->files, we have a directory. */
  248. res = PyDict_Contains(self->files, dirpath);
  249. Py_DECREF(dirpath);
  250. return res;
  251. }
  252. /* Return some information about a module. */
  253. static enum zi_module_info
  254. get_module_info(ZipImporter *self, PyObject *fullname)
  255. {
  256. PyObject *subname;
  257. PyObject *path, *fullpath, *item;
  258. struct st_zip_searchorder *zso;
  259. subname = get_subname(fullname);
  260. if (subname == NULL)
  261. return MI_ERROR;
  262. path = make_filename(self->prefix, subname);
  263. Py_DECREF(subname);
  264. if (path == NULL)
  265. return MI_ERROR;
  266. for (zso = zip_searchorder; *zso->suffix; zso++) {
  267. fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
  268. if (fullpath == NULL) {
  269. Py_DECREF(path);
  270. return MI_ERROR;
  271. }
  272. item = PyDict_GetItem(self->files, fullpath);
  273. Py_DECREF(fullpath);
  274. if (item != NULL) {
  275. Py_DECREF(path);
  276. if (zso->type & IS_PACKAGE)
  277. return MI_PACKAGE;
  278. else
  279. return MI_MODULE;
  280. }
  281. }
  282. Py_DECREF(path);
  283. return MI_NOT_FOUND;
  284. }
  285. typedef enum {
  286. FL_ERROR,
  287. FL_NOT_FOUND,
  288. FL_MODULE_FOUND,
  289. FL_NS_FOUND
  290. } find_loader_result;
  291. /* The guts of "find_loader" and "find_module". Return values:
  292. -1: error
  293. 0: no loader or namespace portions found
  294. 1: module/package found
  295. 2: namespace portion found: *namespace_portion will point to the name
  296. */
  297. static find_loader_result
  298. find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
  299. {
  300. enum zi_module_info mi;
  301. *namespace_portion = NULL;
  302. mi = get_module_info(self, fullname);
  303. if (mi == MI_ERROR)
  304. return FL_ERROR;
  305. if (mi == MI_NOT_FOUND) {
  306. /* Not a module or regular package. See if this is a directory, and
  307. therefore possibly a portion of a namespace package. */
  308. int is_dir = check_is_directory(self, self->prefix, fullname);
  309. if (is_dir < 0)
  310. return -1;
  311. if (is_dir) {
  312. /* This is possibly a portion of a namespace
  313. package. Return the string representing its path,
  314. without a trailing separator. */
  315. *namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
  316. self->archive, SEP,
  317. self->prefix, fullname);
  318. if (*namespace_portion == NULL)
  319. return FL_ERROR;
  320. return FL_NS_FOUND;
  321. }
  322. return FL_NOT_FOUND;
  323. }
  324. /* This is a module or package. */
  325. return FL_MODULE_FOUND;
  326. }
  327. /* Check whether we can satisfy the import of the module named by
  328. 'fullname'. Return self if we can, None if we can't. */
  329. static PyObject *
  330. zipimporter_find_module(PyObject *obj, PyObject *args)
  331. {
  332. ZipImporter *self = (ZipImporter *)obj;
  333. PyObject *path = NULL;
  334. PyObject *fullname;
  335. PyObject *namespace_portion = NULL;
  336. PyObject *result = NULL;
  337. if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
  338. return NULL;
  339. switch (find_loader(self, fullname, &namespace_portion)) {
  340. case FL_ERROR:
  341. return NULL;
  342. case FL_NS_FOUND:
  343. /* A namespace portion is not allowed via find_module, so return None. */
  344. Py_DECREF(namespace_portion);
  345. /* FALL THROUGH */
  346. case FL_NOT_FOUND:
  347. result = Py_None;
  348. break;
  349. case FL_MODULE_FOUND:
  350. result = (PyObject *)self;
  351. break;
  352. }
  353. Py_INCREF(result);
  354. return result;
  355. }
  356. /* Check whether we can satisfy the import of the module named by
  357. 'fullname', or whether it could be a portion of a namespace
  358. package. Return self if we can load it, a string containing the
  359. full path if it's a possible namespace portion, None if we
  360. can't load it. */
  361. static PyObject *
  362. zipimporter_find_loader(PyObject *obj, PyObject *args)
  363. {
  364. ZipImporter *self = (ZipImporter *)obj;
  365. PyObject *path = NULL;
  366. PyObject *fullname;
  367. PyObject *result = NULL;
  368. PyObject *namespace_portion = NULL;
  369. if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
  370. return NULL;
  371. switch (find_loader(self, fullname, &namespace_portion)) {
  372. case FL_ERROR:
  373. return NULL;
  374. case FL_NOT_FOUND: /* Not found, return (None, []) */
  375. result = Py_BuildValue("O[]", Py_None);
  376. break;
  377. case FL_MODULE_FOUND: /* Return (self, []) */
  378. result = Py_BuildValue("O[]", self);
  379. break;
  380. case FL_NS_FOUND: /* Return (None, [namespace_portion]) */
  381. result = Py_BuildValue("O[O]", Py_None, namespace_portion);
  382. Py_DECREF(namespace_portion);
  383. return result;
  384. }
  385. return result;
  386. }
  387. /* Load and return the module named by 'fullname'. */
  388. static PyObject *
  389. zipimporter_load_module(PyObject *obj, PyObject *args)
  390. {
  391. ZipImporter *self = (ZipImporter *)obj;
  392. PyObject *code = NULL, *mod, *dict;
  393. PyObject *fullname;
  394. PyObject *modpath = NULL;
  395. int ispackage;
  396. if (!PyArg_ParseTuple(args, "U:zipimporter.load_module",
  397. &fullname))
  398. return NULL;
  399. if (PyUnicode_READY(fullname) == -1)
  400. return NULL;
  401. code = get_module_code(self, fullname, &ispackage, &modpath);
  402. if (code == NULL)
  403. goto error;
  404. mod = PyImport_AddModuleObject(fullname);
  405. if (mod == NULL)
  406. goto error;
  407. dict = PyModule_GetDict(mod);
  408. /* mod.__loader__ = self */
  409. if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
  410. goto error;
  411. if (ispackage) {
  412. /* add __path__ to the module *before* the code gets
  413. executed */
  414. PyObject *pkgpath, *fullpath, *subname;
  415. int err;
  416. subname = get_subname(fullname);
  417. if (subname == NULL)
  418. goto error;
  419. fullpath = PyUnicode_FromFormat("%U%c%U%U",
  420. self->archive, SEP,
  421. self->prefix, subname);
  422. Py_DECREF(subname);
  423. if (fullpath == NULL)
  424. goto error;
  425. pkgpath = Py_BuildValue("[N]", fullpath);
  426. if (pkgpath == NULL)
  427. goto error;
  428. err = PyDict_SetItemString(dict, "__path__", pkgpath);
  429. Py_DECREF(pkgpath);
  430. if (err != 0)
  431. goto error;
  432. }
  433. mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
  434. Py_CLEAR(code);
  435. if (mod == NULL)
  436. goto error;
  437. if (Py_VerboseFlag)
  438. PySys_FormatStderr("import %U # loaded from Zip %U\n",
  439. fullname, modpath);
  440. Py_DECREF(modpath);
  441. return mod;
  442. error:
  443. Py_XDECREF(code);
  444. Py_XDECREF(modpath);
  445. return NULL;
  446. }
  447. /* Return a string matching __file__ for the named module */
  448. static PyObject *
  449. zipimporter_get_filename(PyObject *obj, PyObject *args)
  450. {
  451. ZipImporter *self = (ZipImporter *)obj;
  452. PyObject *fullname, *code, *modpath;
  453. int ispackage;
  454. if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename",
  455. &fullname))
  456. return NULL;
  457. /* Deciding the filename requires working out where the code
  458. would come from if the module was actually loaded */
  459. code = get_module_code(self, fullname, &ispackage, &modpath);
  460. if (code == NULL)
  461. return NULL;
  462. Py_DECREF(code); /* Only need the path info */
  463. return modpath;
  464. }
  465. /* Return a bool signifying whether the module is a package or not. */
  466. static PyObject *
  467. zipimporter_is_package(PyObject *obj, PyObject *args)
  468. {
  469. ZipImporter *self = (ZipImporter *)obj;
  470. PyObject *fullname;
  471. enum zi_module_info mi;
  472. if (!PyArg_ParseTuple(args, "U:zipimporter.is_package",
  473. &fullname))
  474. return NULL;
  475. mi = get_module_info(self, fullname);
  476. if (mi == MI_ERROR)
  477. return NULL;
  478. if (mi == MI_NOT_FOUND) {
  479. PyErr_Format(ZipImportError, "can't find module %R", fullname);
  480. return NULL;
  481. }
  482. return PyBool_FromLong(mi == MI_PACKAGE);
  483. }
  484. static PyObject *
  485. zipimporter_get_data(PyObject *obj, PyObject *args)
  486. {
  487. ZipImporter *self = (ZipImporter *)obj;
  488. PyObject *path, *key;
  489. PyObject *toc_entry;
  490. Py_ssize_t path_start, path_len, len;
  491. if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path))
  492. return NULL;
  493. #ifdef ALTSEP
  494. path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
  495. if (!path)
  496. return NULL;
  497. #else
  498. Py_INCREF(path);
  499. #endif
  500. if (PyUnicode_READY(path) == -1)
  501. goto error;
  502. path_len = PyUnicode_GET_LENGTH(path);
  503. len = PyUnicode_GET_LENGTH(self->archive);
  504. path_start = 0;
  505. if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
  506. && PyUnicode_READ_CHAR(path, len) == SEP) {
  507. path_start = len + 1;
  508. }
  509. key = PyUnicode_Substring(path, path_start, path_len);
  510. if (key == NULL)
  511. goto error;
  512. toc_entry = PyDict_GetItem(self->files, key);
  513. if (toc_entry == NULL) {
  514. PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
  515. Py_DECREF(key);
  516. goto error;
  517. }
  518. Py_DECREF(key);
  519. Py_DECREF(path);
  520. return get_data(self->archive, toc_entry);
  521. error:
  522. Py_DECREF(path);
  523. return NULL;
  524. }
  525. static PyObject *
  526. zipimporter_get_code(PyObject *obj, PyObject *args)
  527. {
  528. ZipImporter *self = (ZipImporter *)obj;
  529. PyObject *fullname;
  530. if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname))
  531. return NULL;
  532. return get_module_code(self, fullname, NULL, NULL);
  533. }
  534. static PyObject *
  535. zipimporter_get_source(PyObject *obj, PyObject *args)
  536. {
  537. ZipImporter *self = (ZipImporter *)obj;
  538. PyObject *toc_entry;
  539. PyObject *fullname, *subname, *path, *fullpath;
  540. enum zi_module_info mi;
  541. if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname))
  542. return NULL;
  543. mi = get_module_info(self, fullname);
  544. if (mi == MI_ERROR)
  545. return NULL;
  546. if (mi == MI_NOT_FOUND) {
  547. PyErr_Format(ZipImportError, "can't find module %R", fullname);
  548. return NULL;
  549. }
  550. subname = get_subname(fullname);
  551. if (subname == NULL)
  552. return NULL;
  553. path = make_filename(self->prefix, subname);
  554. Py_DECREF(subname);
  555. if (path == NULL)
  556. return NULL;
  557. if (mi == MI_PACKAGE)
  558. fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
  559. else
  560. fullpath = PyUnicode_FromFormat("%U.py", path);
  561. Py_DECREF(path);
  562. if (fullpath == NULL)
  563. return NULL;
  564. toc_entry = PyDict_GetItem(self->files, fullpath);
  565. Py_DECREF(fullpath);
  566. if (toc_entry != NULL) {
  567. PyObject *res, *bytes;
  568. bytes = get_data(self->archive, toc_entry);
  569. if (bytes == NULL)
  570. return NULL;
  571. res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
  572. PyBytes_GET_SIZE(bytes));
  573. Py_DECREF(bytes);
  574. return res;
  575. }
  576. /* we have the module, but no source */
  577. Py_INCREF(Py_None);
  578. return Py_None;
  579. }
  580. PyDoc_STRVAR(doc_find_module,
  581. "find_module(fullname, path=None) -> self or None.\n\
  582. \n\
  583. Search for a module specified by 'fullname'. 'fullname' must be the\n\
  584. fully qualified (dotted) module name. It returns the zipimporter\n\
  585. instance itself if the module was found, or None if it wasn't.\n\
  586. The optional 'path' argument is ignored -- it's there for compatibility\n\
  587. with the importer protocol.");
  588. PyDoc_STRVAR(doc_find_loader,
  589. "find_loader(fullname, path=None) -> self, str or None.\n\
  590. \n\
  591. Search for a module specified by 'fullname'. 'fullname' must be the\n\
  592. fully qualified (dotted) module name. It returns the zipimporter\n\
  593. instance itself if the module was found, a string containing the\n\
  594. full path name if it's possibly a portion of a namespace package,\n\
  595. or None otherwise. The optional 'path' argument is ignored -- it's\n\
  596. there for compatibility with the importer protocol.");
  597. PyDoc_STRVAR(doc_load_module,
  598. "load_module(fullname) -> module.\n\
  599. \n\
  600. Load the module specified by 'fullname'. 'fullname' must be the\n\
  601. fully qualified (dotted) module name. It returns the imported\n\
  602. module, or raises ZipImportError if it wasn't found.");
  603. PyDoc_STRVAR(doc_get_data,
  604. "get_data(pathname) -> string with file data.\n\
  605. \n\
  606. Return the data associated with 'pathname'. Raise IOError if\n\
  607. the file wasn't found.");
  608. PyDoc_STRVAR(doc_is_package,
  609. "is_package(fullname) -> bool.\n\
  610. \n\
  611. Return True if the module specified by fullname is a package.\n\
  612. Raise ZipImportError if the module couldn't be found.");
  613. PyDoc_STRVAR(doc_get_code,
  614. "get_code(fullname) -> code object.\n\
  615. \n\
  616. Return the code object for the specified module. Raise ZipImportError\n\
  617. if the module couldn't be found.");
  618. PyDoc_STRVAR(doc_get_source,
  619. "get_source(fullname) -> source string.\n\
  620. \n\
  621. Return the source code for the specified module. Raise ZipImportError\n\
  622. if the module couldn't be found, return None if the archive does\n\
  623. contain the module, but has no source for it.");
  624. PyDoc_STRVAR(doc_get_filename,
  625. "get_filename(fullname) -> filename string.\n\
  626. \n\
  627. Return the filename for the specified module.");
  628. static PyMethodDef zipimporter_methods[] = {
  629. {"find_module", zipimporter_find_module, METH_VARARGS,
  630. doc_find_module},
  631. {"find_loader", zipimporter_find_loader, METH_VARARGS,
  632. doc_find_loader},
  633. {"load_module", zipimporter_load_module, METH_VARARGS,
  634. doc_load_module},
  635. {"get_data", zipimporter_get_data, METH_VARARGS,
  636. doc_get_data},
  637. {"get_code", zipimporter_get_code, METH_VARARGS,
  638. doc_get_code},
  639. {"get_source", zipimporter_get_source, METH_VARARGS,
  640. doc_get_source},
  641. {"get_filename", zipimporter_get_filename, METH_VARARGS,
  642. doc_get_filename},
  643. {"is_package", zipimporter_is_package, METH_VARARGS,
  644. doc_is_package},
  645. {NULL, NULL} /* sentinel */
  646. };
  647. static PyMemberDef zipimporter_members[] = {
  648. {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
  649. {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
  650. {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
  651. {NULL}
  652. };
  653. PyDoc_STRVAR(zipimporter_doc,
  654. "zipimporter(archivepath) -> zipimporter object\n\
  655. \n\
  656. Create a new zipimporter instance. 'archivepath' must be a path to\n\
  657. a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
  658. '/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
  659. valid directory inside the archive.\n\
  660. \n\
  661. 'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
  662. archive.\n\
  663. \n\
  664. The 'archive' attribute of zipimporter objects contains the name of the\n\
  665. zipfile targeted.");
  666. #define DEFERRED_ADDRESS(ADDR) 0
  667. static PyTypeObject ZipImporter_Type = {
  668. PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
  669. "zipimport.zipimporter",
  670. sizeof(ZipImporter),
  671. 0, /* tp_itemsize */
  672. (destructor)zipimporter_dealloc, /* tp_dealloc */
  673. 0, /* tp_print */
  674. 0, /* tp_getattr */
  675. 0, /* tp_setattr */
  676. 0, /* tp_reserved */
  677. (reprfunc)zipimporter_repr, /* tp_repr */
  678. 0, /* tp_as_number */
  679. 0, /* tp_as_sequence */
  680. 0, /* tp_as_mapping */
  681. 0, /* tp_hash */
  682. 0, /* tp_call */
  683. 0, /* tp_str */
  684. PyObject_GenericGetAttr, /* tp_getattro */
  685. 0, /* tp_setattro */
  686. 0, /* tp_as_buffer */
  687. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
  688. Py_TPFLAGS_HAVE_GC, /* tp_flags */
  689. zipimporter_doc, /* tp_doc */
  690. zipimporter_traverse, /* tp_traverse */
  691. 0, /* tp_clear */
  692. 0, /* tp_richcompare */
  693. 0, /* tp_weaklistoffset */
  694. 0, /* tp_iter */
  695. 0, /* tp_iternext */
  696. zipimporter_methods, /* tp_methods */
  697. zipimporter_members, /* tp_members */
  698. 0, /* tp_getset */
  699. 0, /* tp_base */
  700. 0, /* tp_dict */
  701. 0, /* tp_descr_get */
  702. 0, /* tp_descr_set */
  703. 0, /* tp_dictoffset */
  704. (initproc)zipimporter_init, /* tp_init */
  705. PyType_GenericAlloc, /* tp_alloc */
  706. PyType_GenericNew, /* tp_new */
  707. PyObject_GC_Del, /* tp_free */
  708. };
  709. /* implementation */
  710. /* Given a buffer, return the long that is represented by the first
  711. 4 bytes, encoded as little endian. This partially reimplements
  712. marshal.c:r_long() */
  713. static long
  714. get_long(unsigned char *buf) {
  715. long x;
  716. x = buf[0];
  717. x |= (long)buf[1] << 8;
  718. x |= (long)buf[2] << 16;
  719. x |= (long)buf[3] << 24;
  720. #if SIZEOF_LONG > 4
  721. /* Sign extension for 64-bit machines */
  722. x |= -(x & 0x80000000L);
  723. #endif
  724. return x;
  725. }
  726. /*
  727. read_directory(archive) -> files dict (new reference)
  728. Given a path to a Zip archive, build a dict, mapping file names
  729. (local to the archive, using SEP as a separator) to toc entries.
  730. A toc_entry is a tuple:
  731. (__file__, # value to use for __file__, available for all files,
  732. # encoded to the filesystem encoding
  733. compress, # compression kind; 0 for uncompressed
  734. data_size, # size of compressed data on disk
  735. file_size, # size of decompressed data
  736. file_offset, # offset of file header from start of archive
  737. time, # mod time of file (in dos format)
  738. date, # mod data of file (in dos format)
  739. crc, # crc checksum of the data
  740. )
  741. Directories can be recognized by the trailing SEP in the name,
  742. data_size and file_offset are 0.
  743. */
  744. static PyObject *
  745. read_directory(PyObject *archive)
  746. {
  747. PyObject *files = NULL;
  748. FILE *fp;
  749. unsigned short flags;
  750. short compress, time, date, name_size;
  751. long crc, data_size, file_size, header_size;
  752. Py_ssize_t file_offset, header_position, header_offset;
  753. long l, count;
  754. Py_ssize_t i;
  755. char name[MAXPATHLEN + 5];
  756. char dummy[8]; /* Buffer to read unused header values into */
  757. PyObject *nameobj = NULL;
  758. char *p, endof_central_dir[22];
  759. Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */
  760. PyObject *path;
  761. const char *charset;
  762. int bootstrap;
  763. fp = _Py_fopen_obj(archive, "rb");
  764. if (fp == NULL) {
  765. if (PyErr_ExceptionMatches(PyExc_OSError)) {
  766. PyObject *exc, *val, *tb;
  767. PyErr_Fetch(&exc, &val, &tb);
  768. PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
  769. _PyErr_ChainExceptions(exc, val, tb);
  770. }
  771. return NULL;
  772. }
  773. if (fseek(fp, -22, SEEK_END) == -1) {
  774. fclose(fp);
  775. PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
  776. return NULL;
  777. }
  778. header_position = ftell(fp);
  779. if (fread(endof_central_dir, 1, 22, fp) != 22) {
  780. fclose(fp);
  781. PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
  782. return NULL;
  783. }
  784. if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
  785. /* Bad: End of Central Dir signature */
  786. fclose(fp);
  787. PyErr_Format(ZipImportError, "not a Zip file: %R", archive);
  788. return NULL;
  789. }
  790. header_size = get_long((unsigned char *)endof_central_dir + 12);
  791. header_offset = get_long((unsigned char *)endof_central_dir + 16);
  792. arc_offset = header_position - header_offset - header_size;
  793. header_offset += arc_offset;
  794. files = PyDict_New();
  795. if (files == NULL)
  796. goto error;
  797. /* Start of Central Directory */
  798. count = 0;
  799. if (fseek(fp, header_offset, 0) == -1)
  800. goto file_error;
  801. for (;;) {
  802. PyObject *t;
  803. int err;
  804. /* Start of file header */
  805. l = PyMarshal_ReadLongFromFile(fp);
  806. if (l == -1 && PyErr_Occurred())
  807. goto error;
  808. if (l != 0x02014B50)
  809. break; /* Bad: Central Dir File Header */
  810. /* On Windows, calling fseek to skip over the fields we don't use is
  811. slower than reading the data into a dummy buffer because fseek flushes
  812. stdio's internal buffers. See issue #8745. */
  813. if (fread(dummy, 1, 4, fp) != 4) /* Skip unused fields, avoid fseek */
  814. goto file_error;
  815. flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
  816. compress = PyMarshal_ReadShortFromFile(fp);
  817. time = PyMarshal_ReadShortFromFile(fp);
  818. date = PyMarshal_ReadShortFromFile(fp);
  819. crc = PyMarshal_ReadLongFromFile(fp);
  820. data_size = PyMarshal_ReadLongFromFile(fp);
  821. file_size = PyMarshal_ReadLongFromFile(fp);
  822. name_size = PyMarshal_ReadShortFromFile(fp);
  823. header_size = name_size +
  824. PyMarshal_ReadShortFromFile(fp) +
  825. PyMarshal_ReadShortFromFile(fp);
  826. if (PyErr_Occurred())
  827. goto error;
  828. if (fread(dummy, 1, 8, fp) != 8) /* Skip unused fields, avoid fseek */
  829. goto file_error;
  830. file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
  831. if (PyErr_Occurred())
  832. goto error;
  833. if (name_size > MAXPATHLEN)
  834. name_size = MAXPATHLEN;
  835. p = name;
  836. for (i = 0; i < (Py_ssize_t)name_size; i++) {
  837. *p = (char)getc(fp);
  838. if (*p == '/')
  839. *p = SEP;
  840. p++;
  841. }
  842. *p = 0; /* Add terminating null byte */
  843. for (; i < header_size; i++) /* Skip the rest of the header */
  844. if(getc(fp) == EOF) /* Avoid fseek */
  845. goto file_error;
  846. bootstrap = 0;
  847. if (flags & 0x0800)
  848. charset = "utf-8";
  849. else if (!PyThreadState_GET()->interp->codecs_initialized) {
  850. /* During bootstrap, we may need to load the encodings
  851. package from a ZIP file. But the cp437 encoding is implemented
  852. in Python in the encodings package.
  853. Break out of this dependency by assuming that the path to
  854. the encodings module is ASCII-only. */
  855. charset = "ascii";
  856. bootstrap = 1;
  857. }
  858. else
  859. charset = "cp437";
  860. nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
  861. if (nameobj == NULL) {
  862. if (bootstrap)
  863. PyErr_Format(PyExc_NotImplementedError,
  864. "bootstrap issue: python%i%i.zip contains non-ASCII "
  865. "filenames without the unicode flag",
  866. PY_MAJOR_VERSION, PY_MINOR_VERSION);
  867. goto error;
  868. }
  869. if (PyUnicode_READY(nameobj) == -1)
  870. goto error;
  871. path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
  872. if (path == NULL)
  873. goto error;
  874. t = Py_BuildValue("Nhllnhhl", path, compress, data_size,
  875. file_size, file_offset, time, date, crc);
  876. if (t == NULL)
  877. goto error;
  878. err = PyDict_SetItem(files, nameobj, t);
  879. Py_CLEAR(nameobj);
  880. Py_DECREF(t);
  881. if (err != 0)
  882. goto error;
  883. count++;
  884. }
  885. fclose(fp);
  886. if (Py_VerboseFlag)
  887. PySys_FormatStderr("# zipimport: found %ld names in %R\n",
  888. count, archive);
  889. return files;
  890. file_error:
  891. fclose(fp);
  892. Py_XDECREF(files);
  893. Py_XDECREF(nameobj);
  894. PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
  895. return NULL;
  896. error:
  897. fclose(fp);
  898. Py_XDECREF(files);
  899. Py_XDECREF(nameobj);
  900. return NULL;
  901. }
  902. /* Return the zlib.decompress function object, or NULL if zlib couldn't
  903. be imported. The function is cached when found, so subsequent calls
  904. don't import zlib again. */
  905. static PyObject *
  906. get_decompress_func(void)
  907. {
  908. static int importing_zlib = 0;
  909. PyObject *zlib;
  910. PyObject *decompress;
  911. _Py_IDENTIFIER(decompress);
  912. if (importing_zlib != 0)
  913. /* Someone has a zlib.py[co] in their Zip file;
  914. let's avoid a stack overflow. */
  915. return NULL;
  916. importing_zlib = 1;
  917. zlib = PyImport_ImportModuleNoBlock("zlib");
  918. importing_zlib = 0;
  919. if (zlib != NULL) {
  920. decompress = _PyObject_GetAttrId(zlib,
  921. &PyId_decompress);
  922. Py_DECREF(zlib);
  923. }
  924. else {
  925. PyErr_Clear();
  926. decompress = NULL;
  927. }
  928. if (Py_VerboseFlag)
  929. PySys_WriteStderr("# zipimport: zlib %s\n",
  930. zlib != NULL ? "available": "UNAVAILABLE");
  931. return decompress;
  932. }
  933. /* Given a path to a Zip file and a toc_entry, return the (uncompressed)
  934. data as a new reference. */
  935. static PyObject *
  936. get_data(PyObject *archive, PyObject *toc_entry)
  937. {
  938. PyObject *raw_data, *data = NULL, *decompress;
  939. char *buf;
  940. FILE *fp;
  941. int err;
  942. Py_ssize_t bytes_read = 0;
  943. long l;
  944. PyObject *datapath;
  945. long compress, data_size, file_size, file_offset, bytes_size;
  946. long time, date, crc;
  947. if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
  948. &data_size, &file_size, &file_offset, &time,
  949. &date, &crc)) {
  950. return NULL;
  951. }
  952. fp = _Py_fopen_obj(archive, "rb");
  953. if (!fp)
  954. return NULL;
  955. /* Check to make sure the local file header is correct */
  956. if (fseek(fp, file_offset, 0) == -1) {
  957. fclose(fp);
  958. PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
  959. return NULL;
  960. }
  961. l = PyMarshal_ReadLongFromFile(fp);
  962. if (l != 0x04034B50) {
  963. /* Bad: Local File Header */
  964. if (!PyErr_Occurred())
  965. PyErr_Format(ZipImportError,
  966. "bad local file header in %U",
  967. archive);
  968. fclose(fp);
  969. return NULL;
  970. }
  971. if (fseek(fp, file_offset + 26, 0) == -1) {
  972. fclose(fp);
  973. PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
  974. return NULL;
  975. }
  976. l = 30 + PyMarshal_ReadShortFromFile(fp) +
  977. PyMarshal_ReadShortFromFile(fp); /* local header size */
  978. if (PyErr_Occurred()) {
  979. fclose(fp);
  980. return NULL;
  981. }
  982. file_offset += l; /* Start of file data */
  983. bytes_size = compress == 0 ? data_size : data_size + 1;
  984. if (bytes_size == 0)
  985. bytes_size++;
  986. raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
  987. if (raw_data == NULL) {
  988. fclose(fp);
  989. return NULL;
  990. }
  991. buf = PyBytes_AsString(raw_data);
  992. err = fseek(fp, file_offset, 0);
  993. if (err == 0) {
  994. bytes_read = fread(buf, 1, data_size, fp);
  995. } else {
  996. fclose(fp);
  997. PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
  998. return NULL;
  999. }
  1000. fclose(fp);
  1001. if (err || bytes_read != data_size) {
  1002. PyErr_SetString(PyExc_IOError,
  1003. "zipimport: can't read data");
  1004. Py_DECREF(raw_data);
  1005. return NULL;
  1006. }
  1007. if (compress != 0) {
  1008. buf[data_size] = 'Z'; /* saw this in zipfile.py */
  1009. data_size++;
  1010. }
  1011. buf[data_size] = '\0';
  1012. if (compress == 0) { /* data is not compressed */
  1013. data = PyBytes_FromStringAndSize(buf, data_size);
  1014. Py_DECREF(raw_data);
  1015. return data;
  1016. }
  1017. /* Decompress with zlib */
  1018. decompress = get_decompress_func();
  1019. if (decompress == NULL) {
  1020. PyErr_SetString(ZipImportError,
  1021. "can't decompress data; "
  1022. "zlib not available");
  1023. goto error;
  1024. }
  1025. data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
  1026. Py_DECREF(decompress);
  1027. error:
  1028. Py_DECREF(raw_data);
  1029. return data;
  1030. }
  1031. /* Lenient date/time comparison function. The precision of the mtime
  1032. in the archive is lower than the mtime stored in a .pyc: we
  1033. must allow a difference of at most one second. */
  1034. static int
  1035. eq_mtime(time_t t1, time_t t2)
  1036. {
  1037. time_t d = t1 - t2;
  1038. if (d < 0)
  1039. d = -d;
  1040. /* dostime only stores even seconds, so be lenient */
  1041. return d <= 1;
  1042. }
  1043. /* Given the contents of a .py[co] file in a buffer, unmarshal the data
  1044. and return the code object. Return None if it the magic word doesn't
  1045. match (we do this instead of raising an exception as we fall back
  1046. to .py if available and we don't want to mask other errors).
  1047. Returns a new reference. */
  1048. static PyObject *
  1049. unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
  1050. {
  1051. PyObject *code;
  1052. char *buf = PyBytes_AsString(data);
  1053. Py_ssize_t size = PyBytes_Size(data);
  1054. if (size <= 9) {
  1055. PyErr_SetString(ZipImportError,
  1056. "bad pyc data");
  1057. return NULL;
  1058. }
  1059. if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
  1060. if (Py_VerboseFlag)
  1061. PySys_FormatStderr("# %R has bad magic\n",
  1062. pathname);
  1063. Py_INCREF(Py_None);
  1064. return Py_None; /* signal caller to try alternative */
  1065. }
  1066. if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
  1067. mtime)) {
  1068. if (Py_VerboseFlag)
  1069. PySys_FormatStderr("# %R has bad mtime\n",
  1070. pathname);
  1071. Py_INCREF(Py_None);
  1072. return Py_None; /* signal caller to try alternative */
  1073. }
  1074. /* XXX the pyc's size field is ignored; timestamp collisions are probably
  1075. unimportant with zip files. */
  1076. code = PyMarshal_ReadObjectFromString(buf + 12, size - 12);
  1077. if (code == NULL)
  1078. return NULL;
  1079. if (!PyCode_Check(code)) {
  1080. Py_DECREF(code);
  1081. PyErr_Format(PyExc_TypeError,
  1082. "compiled module %R is not a code object",
  1083. pathname);
  1084. return NULL;
  1085. }
  1086. return code;
  1087. }
  1088. /* Replace any occurances of "\r\n?" in the input string with "\n".
  1089. This converts DOS and Mac line endings to Unix line endings.
  1090. Also append a trailing "\n" to be compatible with
  1091. PyParser_SimpleParseFile(). Returns a new reference. */
  1092. static PyObject *
  1093. normalize_line_endings(PyObject *source)
  1094. {
  1095. char *buf, *q, *p;
  1096. PyObject *fixed_source;
  1097. int len = 0;
  1098. p = PyBytes_AsString(source);
  1099. if (p == NULL) {
  1100. return PyBytes_FromStringAndSize("\n\0", 2);
  1101. }
  1102. /* one char extra for trailing \n and one for terminating \0 */
  1103. buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
  1104. if (buf == NULL) {
  1105. PyErr_SetString(PyExc_MemoryError,
  1106. "zipimport: no memory to allocate "
  1107. "source buffer");
  1108. return NULL;
  1109. }
  1110. /* replace "\r\n?" by "\n" */
  1111. for (q = buf; *p != '\0'; p++) {
  1112. if (*p == '\r') {
  1113. *q++ = '\n';
  1114. if (*(p + 1) == '\n')
  1115. p++;
  1116. }
  1117. else
  1118. *q++ = *p;
  1119. len++;
  1120. }
  1121. *q++ = '\n'; /* add trailing \n */
  1122. *q = '\0';
  1123. fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
  1124. PyMem_Free(buf);
  1125. return fixed_source;
  1126. }
  1127. /* Given a string buffer containing Python source code, compile it
  1128. and return a code object as a new reference. */
  1129. static PyObject *
  1130. compile_source(PyObject *pathname, PyObject *source)
  1131. {
  1132. PyObject *code, *fixed_source, *pathbytes;
  1133. pathbytes = PyUnicode_EncodeFSDefault(pathname);
  1134. if (pathbytes == NULL)
  1135. return NULL;
  1136. fixed_source = normalize_line_endings(source);
  1137. if (fixed_source == NULL) {
  1138. Py_DECREF(pathbytes);
  1139. return NULL;
  1140. }
  1141. code = Py_CompileString(PyBytes_AsString(fixed_source),
  1142. PyBytes_AsString(pathbytes),
  1143. Py_file_input);
  1144. Py_DECREF(pathbytes);
  1145. Py_DECREF(fixed_source);
  1146. return code;
  1147. }
  1148. /* Convert the date/time values found in the Zip archive to a value
  1149. that's compatible with the time stamp stored in .pyc files. */
  1150. static time_t
  1151. parse_dostime(int dostime, int dosdate)
  1152. {
  1153. struct tm stm;
  1154. memset((void *) &stm, '\0', sizeof(stm));
  1155. stm.tm_sec = (dostime & 0x1f) * 2;
  1156. stm.tm_min = (dostime >> 5) & 0x3f;
  1157. stm.tm_hour = (dostime >> 11) & 0x1f;
  1158. stm.tm_mday = dosdate & 0x1f;
  1159. stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
  1160. stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
  1161. stm.tm_isdst = -1; /* wday/yday is ignored */
  1162. return mktime(&stm);
  1163. }
  1164. /* Given a path to a .pyc file in the archive, return the
  1165. modification time of the matching .py file, or 0 if no source
  1166. is available. */
  1167. static time_t
  1168. get_mtime_of_source(ZipImporter *self, PyObject *path)
  1169. {
  1170. PyObject *toc_entry, *stripped;
  1171. time_t mtime;
  1172. /* strip 'c' or 'o' from *.py[co] */
  1173. if (PyUnicode_READY(path) == -1)
  1174. return (time_t)-1;
  1175. stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
  1176. PyUnicode_DATA(path),
  1177. PyUnicode_GET_LENGTH(path) - 1);
  1178. if (stripped == NULL)
  1179. return (time_t)-1;
  1180. toc_entry = PyDict_GetItem(self->files, stripped);
  1181. Py_DECREF(stripped);
  1182. if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
  1183. PyTuple_Size(toc_entry) == 8) {
  1184. /* fetch the time stamp of the .py file for comparison
  1185. with an embedded pyc time stamp */
  1186. int time, date;
  1187. time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
  1188. date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
  1189. mtime = parse_dostime(time, date);
  1190. } else
  1191. mtime = 0;
  1192. return mtime;
  1193. }
  1194. /* Return the code object for the module named by 'fullname' from the
  1195. Zip archive as a new reference. */
  1196. static PyObject *
  1197. get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
  1198. time_t mtime, PyObject *toc_entry)
  1199. {
  1200. PyObject *data, *modpath, *code;
  1201. data = get_data(self->archive, toc_entry);
  1202. if (data == NULL)
  1203. return NULL;
  1204. modpath = PyTuple_GetItem(toc_entry, 0);
  1205. if (isbytecode)
  1206. code = unmarshal_code(modpath, data, mtime);
  1207. else
  1208. code = compile_source(modpath, data);
  1209. Py_DECREF(data);
  1210. return code;
  1211. }
  1212. /* Get the code object associated with the module specified by
  1213. 'fullname'. */
  1214. static PyObject *
  1215. get_module_code(ZipImporter *self, PyObject *fullname,
  1216. int *p_ispackage, PyObject **p_modpath)
  1217. {
  1218. PyObject *code = NULL, *toc_entry, *subname;
  1219. PyObject *path, *fullpath = NULL;
  1220. struct st_zip_searchorder *zso;
  1221. subname = get_subname(fullname);
  1222. if (subname == NULL)
  1223. return NULL;
  1224. path = make_filename(self->prefix, subname);
  1225. Py_DECREF(subname);
  1226. if (path == NULL)
  1227. return NULL;
  1228. for (zso = zip_searchorder; *zso->suffix; zso++) {
  1229. code = NULL;
  1230. fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
  1231. if (fullpath == NULL)
  1232. goto exit;
  1233. if (Py_VerboseFlag > 1)
  1234. PySys_FormatStderr("# trying %U%c%U\n",
  1235. self->archive, (int)SEP, fullpath);
  1236. toc_entry = PyDict_GetItem(self->files, fullpath);
  1237. if (toc_entry != NULL) {
  1238. time_t mtime = 0;
  1239. int ispackage = zso->type & IS_PACKAGE;
  1240. int isbytecode = zso->type & IS_BYTECODE;
  1241. if (isbytecode) {
  1242. mtime = get_mtime_of_source(self, fullpath);
  1243. if (mtime == (time_t)-1 && PyErr_Occurred()) {
  1244. goto exit;
  1245. }
  1246. }
  1247. Py_CLEAR(fullpath);
  1248. if (p_ispackage != NULL)
  1249. *p_ispackage = ispackage;
  1250. code = get_code_from_data(self, ispackage,
  1251. isbytecode, mtime,
  1252. toc_entry);
  1253. if (code == Py_None) {
  1254. /* bad magic number or non-matching mtime
  1255. in byte code, try next */
  1256. Py_DECREF(code);
  1257. continue;
  1258. }
  1259. if (code != NULL && p_modpath != NULL) {
  1260. *p_modpath = PyTuple_GetItem(toc_entry, 0);
  1261. Py_INCREF(*p_modpath);
  1262. }
  1263. goto exit;
  1264. }
  1265. else
  1266. Py_CLEAR(fullpath);
  1267. }
  1268. PyErr_Format(ZipImportError, "can't find module %R", fullname);
  1269. exit:
  1270. Py_DECREF(path);
  1271. Py_XDECREF(fullpath);
  1272. return code;
  1273. }
  1274. /* Module init */
  1275. PyDoc_STRVAR(zipimport_doc,
  1276. "zipimport provides support for importing Python modules from Zip archives.\n\
  1277. \n\
  1278. This module exports three objects:\n\
  1279. - zipimporter: a class; its constructor takes a path to a Zip archive.\n\
  1280. - ZipImportError: exception raised by zipimporter objects. It's a\n\
  1281. subclass of ImportError, so it can be caught as ImportError, too.\n\
  1282. - _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
  1283. info dicts, as used in zipimporter._files.\n\
  1284. \n\
  1285. It is usually not needed to use the zipimport module explicitly; it is\n\
  1286. used by the builtin import mechanism for sys.path items that are paths\n\
  1287. to Zip archives.");
  1288. static struct PyModuleDef zipimportmodule = {
  1289. PyModuleDef_HEAD_INIT,
  1290. "zipimport",
  1291. zipimport_doc,
  1292. -1,
  1293. NULL,
  1294. NULL,
  1295. NULL,
  1296. NULL,
  1297. NULL
  1298. };
  1299. PyMODINIT_FUNC
  1300. PyInit_zipimport(void)
  1301. {
  1302. PyObject *mod;
  1303. if (PyType_Ready(&ZipImporter_Type) < 0)
  1304. return NULL;
  1305. /* Correct directory separator */
  1306. zip_searchorder[0].suffix[0] = SEP;
  1307. zip_searchorder[1].suffix[0] = SEP;
  1308. mod = PyModule_Create(&zipimportmodule);
  1309. if (mod == NULL)
  1310. return NULL;
  1311. ZipImportError = PyErr_NewException("zipimport.ZipImportError",
  1312. PyExc_ImportError, NULL);
  1313. if (ZipImportError == NULL)
  1314. return NULL;
  1315. Py_INCREF(ZipImportError);
  1316. if (PyModule_AddObject(mod, "ZipImportError",
  1317. ZipImportError) < 0)
  1318. return NULL;
  1319. Py_INCREF(&ZipImporter_Type);
  1320. if (PyModule_AddObject(mod, "zipimporter",
  1321. (PyObject *)&ZipImporter_Type) < 0)
  1322. return NULL;
  1323. zip_directory_cache = PyDict_New();
  1324. if (zip_directory_cache == NULL)
  1325. return NULL;
  1326. Py_INCREF(zip_directory_cache);
  1327. if (PyModule_AddObject(mod, "_zip_directory_cache",
  1328. zip_directory_cache) < 0)
  1329. return NULL;
  1330. return mod;
  1331. }