Mercurial > cpython-lru_cache-weakref
annotate Modules/zipimport.c @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 4906d2eb55ef |
| children | 088a14031998 |
| rev | line source |
|---|---|
| 26885 | 1 #include "Python.h" |
| 2 #include "structmember.h" | |
| 3 #include "osdefs.h" | |
| 4 #include "marshal.h" | |
| 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 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
13 char suffix[14]; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
14 int type; |
| 26885 | 15 }; |
| 16 | |
| 17 /* zip_searchorder defines how we search for a module in the Zip | |
| 18 archive: we first search for a package __init__, then for | |
| 19 non-package .pyc, .pyo and .py entries. The .pyc and .pyo entries | |
| 20 are swapped by initzipimport() if we run in optimized mode. Also, | |
| 21 '/' is replaced by SEP there. */ | |
|
28391
07617d0904a6
Make private function and data static.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28103
diff
changeset
|
22 static struct st_zip_searchorder zip_searchorder[] = { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
23 {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
24 {"/__init__.pyo", IS_PACKAGE | IS_BYTECODE}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
25 {"/__init__.py", IS_PACKAGE | IS_SOURCE}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
26 {".pyc", IS_BYTECODE}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
27 {".pyo", IS_BYTECODE}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
28 {".py", IS_SOURCE}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
29 {"", 0} |
| 26885 | 30 }; |
| 31 | |
| 32 /* zipimporter object definition and support */ | |
| 33 | |
| 34 typedef struct _zipimporter ZipImporter; | |
| 35 | |
| 36 struct _zipimporter { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
37 PyObject_HEAD |
|
65533
1a905391e3ed
zipimport: document archive encoding; fix indentation
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65532
diff
changeset
|
38 PyObject *archive; /* pathname of the Zip archive, |
|
1a905391e3ed
zipimport: document archive encoding; fix indentation
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65532
diff
changeset
|
39 decoded from the filesystem encoding */ |
|
65516
752e4b5c64e9
zipimport: encode the prefix to the fileystem encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65515
diff
changeset
|
40 PyObject *prefix; /* file prefix: "a/sub/directory/", |
|
752e4b5c64e9
zipimport: encode the prefix to the fileystem encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65515
diff
changeset
|
41 encoded to the filesystem encoding */ |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
42 PyObject *files; /* dict with file info {path: toc_entry} */ |
| 26885 | 43 }; |
| 44 | |
| 45 static PyObject *ZipImportError; | |
|
65514
8f0625954ffa
zipimport: fix indentation
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65398
diff
changeset
|
46 /* read_directory() cache */ |
| 26885 | 47 static PyObject *zip_directory_cache = NULL; |
| 48 | |
| 49 /* forward decls */ | |
|
64065
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
50 static PyObject *read_directory(PyObject *archive); |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
51 static PyObject *get_data(PyObject *archive, PyObject *toc_entry); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
52 static PyObject *get_module_code(ZipImporter *self, PyObject *fullname, |
|
65518
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
53 int *p_ispackage, PyObject **p_modpath); |
| 26885 | 54 |
| 55 | |
| 56 #define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type) | |
| 57 | |
| 58 | |
| 59 /* zipimporter.__init__ | |
| 60 Split the "subdirectory" from the Zip archive path, lookup a matching | |
| 61 entry in sys.path_importer_cache, fetch the file directory from there | |
| 62 if found, or else read it from the archive. */ | |
| 63 static int | |
| 64 zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) | |
| 65 { | |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
66 PyObject *path, *files, *tmp; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
67 PyObject *filename = NULL; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
68 Py_ssize_t len, flen; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
69 #ifdef ALTSEP |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
70 _Py_IDENTIFIER(replace); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
71 #endif |
| 26885 | 72 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
73 if (!_PyArg_NoKeywords("zipimporter()", kwds)) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
74 return -1; |
|
35450
97c97194e64b
Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents:
33955
diff
changeset
|
75 |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
76 if (!PyArg_ParseTuple(args, "O&:zipimporter", |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
77 PyUnicode_FSDecoder, &path)) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
78 return -1; |
| 26885 | 79 |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
80 if (PyUnicode_READY(path) == -1) |
| 72475 | 81 return -1; |
| 82 | |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
83 len = PyUnicode_GET_LENGTH(path); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
84 if (len == 0) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
85 PyErr_SetString(ZipImportError, "archive path is empty"); |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
86 goto error; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
87 } |
| 26885 | 88 |
| 89 #ifdef ALTSEP | |
| 73237 | 90 tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
91 if (!tmp) |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
92 goto error; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
93 Py_DECREF(path); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
94 path = tmp; |
| 26885 | 95 #endif |
| 96 | |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
97 filename = path; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
98 Py_INCREF(filename); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
99 flen = len; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
100 for (;;) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
101 struct stat statbuf; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
102 int rv; |
| 26885 | 103 |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
104 rv = _Py_stat(filename, &statbuf); |
|
74058
2a140b01d343
import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73238
diff
changeset
|
105 if (rv == -2) |
|
2a140b01d343
import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73238
diff
changeset
|
106 goto error; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
107 if (rv == 0) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
108 /* it exists */ |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
109 if (!S_ISREG(statbuf.st_mode)) |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
110 /* it's a not file */ |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
111 Py_CLEAR(filename); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
112 break; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
113 } |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
114 Py_CLEAR(filename); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
115 /* back up one path element */ |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
116 flen = PyUnicode_FindChar(path, SEP, 0, flen, -1); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
117 if (flen == -1) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
118 break; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
119 filename = PyUnicode_Substring(path, 0, flen); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
120 } |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
121 if (filename == NULL) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
122 PyErr_SetString(ZipImportError, "not a Zip file"); |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
123 goto error; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
124 } |
| 26885 | 125 |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
126 if (PyUnicode_READY(filename) < 0) |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
127 goto error; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
128 |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
129 files = PyDict_GetItem(zip_directory_cache, filename); |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
130 if (files == NULL) { |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
131 files = read_directory(filename); |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
132 if (files == NULL) |
|
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
133 goto error; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
134 if (PyDict_SetItem(zip_directory_cache, filename, files) != 0) |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
135 goto error; |
|
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
136 } |
|
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
137 else |
|
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
138 Py_INCREF(files); |
|
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
139 self->files = files; |
|
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
140 |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
141 /* Transfer reference */ |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
142 self->archive = filename; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
143 filename = NULL; |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
144 |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
145 /* Check if there is a prefix directory following the filename. */ |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
146 if (flen != len) { |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
147 tmp = PyUnicode_Substring(path, flen+1, |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
148 PyUnicode_GET_LENGTH(path)); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
149 if (tmp == NULL) |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
150 goto error; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
151 self->prefix = tmp; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
152 if (PyUnicode_READ_CHAR(path, len-1) != SEP) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
153 /* add trailing SEP */ |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
154 tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
155 if (tmp == NULL) |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
156 goto error; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
157 Py_DECREF(self->prefix); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
158 self->prefix = tmp; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
159 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
160 } |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
161 else |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
162 self->prefix = PyUnicode_New(0, 0); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
163 Py_DECREF(path); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
164 return 0; |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
165 |
|
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
166 error: |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
167 Py_DECREF(path); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
168 Py_XDECREF(filename); |
|
63986
a6592fcf75e7
Issue #9425: zipimporter_init() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63117
diff
changeset
|
169 return -1; |
| 26885 | 170 } |
| 171 | |
| 172 /* GC support. */ | |
| 173 static int | |
| 174 zipimporter_traverse(PyObject *obj, visitproc visit, void *arg) | |
| 175 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
176 ZipImporter *self = (ZipImporter *)obj; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
177 Py_VISIT(self->files); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
178 return 0; |
| 26885 | 179 } |
| 180 | |
| 181 static void | |
| 182 zipimporter_dealloc(ZipImporter *self) | |
| 183 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
184 PyObject_GC_UnTrack(self); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
185 Py_XDECREF(self->archive); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
186 Py_XDECREF(self->prefix); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
187 Py_XDECREF(self->files); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
188 Py_TYPE(self)->tp_free((PyObject *)self); |
| 26885 | 189 } |
| 190 | |
| 191 static PyObject * | |
| 192 zipimporter_repr(ZipImporter *self) | |
| 193 { | |
|
64091
0c8bfd285ef9
Issue #9425: zipimporter_repr() uses unicode
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64090
diff
changeset
|
194 if (self->archive == NULL) |
|
0c8bfd285ef9
Issue #9425: zipimporter_repr() uses unicode
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64090
diff
changeset
|
195 return PyUnicode_FromString("<zipimporter object \"???\">"); |
| 72475 | 196 else if (self->prefix != NULL && PyUnicode_GET_LENGTH(self->prefix) != 0) |
|
65534
8912192982cc
zipimport: remove arbitrary length limit from message formats
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65533
diff
changeset
|
197 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">", |
|
64091
0c8bfd285ef9
Issue #9425: zipimporter_repr() uses unicode
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64090
diff
changeset
|
198 self->archive, SEP, self->prefix); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
199 else |
|
65534
8912192982cc
zipimport: remove arbitrary length limit from message formats
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65533
diff
changeset
|
200 return PyUnicode_FromFormat("<zipimporter object \"%U\">", |
|
64091
0c8bfd285ef9
Issue #9425: zipimporter_repr() uses unicode
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64090
diff
changeset
|
201 self->archive); |
| 26885 | 202 } |
| 203 | |
| 204 /* return fullname.split(".")[-1] */ | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
205 static PyObject * |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
206 get_subname(PyObject *fullname) |
| 26885 | 207 { |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
208 Py_ssize_t len, dot; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
209 if (PyUnicode_READY(fullname) < 0) |
| 72475 | 210 return NULL; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
211 len = PyUnicode_GET_LENGTH(fullname); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
212 dot = PyUnicode_FindChar(fullname, '.', 0, len, -1); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
213 if (dot == -1) { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
214 Py_INCREF(fullname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
215 return fullname; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
216 } else |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
217 return PyUnicode_Substring(fullname, dot+1, len); |
| 26885 | 218 } |
| 219 | |
| 220 /* Given a (sub)modulename, write the potential file path in the | |
| 221 archive (without extension) to the path buffer. Return the | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
222 length of the resulting string. |
| 26885 | 223 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
224 return self.prefix + name.replace('.', os.sep) */ |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
225 static PyObject* |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
226 make_filename(PyObject *prefix, PyObject *name) |
| 26885 | 227 { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
228 PyObject *pathobj; |
| 72475 | 229 Py_UCS4 *p, *buf; |
| 230 Py_ssize_t len; | |
| 26885 | 231 |
| 72475 | 232 len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1; |
| 233 p = buf = PyMem_Malloc(sizeof(Py_UCS4) * len); | |
| 234 if (buf == NULL) { | |
| 235 PyErr_NoMemory(); | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
236 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
237 } |
| 26885 | 238 |
|
78933
6bd9626b0cc6
Fixed two memory leaks in make_filename() in zipimport.c. The allocated buffer wasn't cleaned up in two error cases. CID 486832
Christian Heimes <christian@cheimes.de>
parents:
78669
diff
changeset
|
239 if (!PyUnicode_AsUCS4(prefix, p, len, 0)) { |
|
6bd9626b0cc6
Fixed two memory leaks in make_filename() in zipimport.c. The allocated buffer wasn't cleaned up in two error cases. CID 486832
Christian Heimes <christian@cheimes.de>
parents:
78669
diff
changeset
|
240 PyMem_Free(buf); |
| 72475 | 241 return NULL; |
|
78933
6bd9626b0cc6
Fixed two memory leaks in make_filename() in zipimport.c. The allocated buffer wasn't cleaned up in two error cases. CID 486832
Christian Heimes <christian@cheimes.de>
parents:
78669
diff
changeset
|
242 } |
| 72475 | 243 p += PyUnicode_GET_LENGTH(prefix); |
| 244 len -= PyUnicode_GET_LENGTH(prefix); | |
|
78933
6bd9626b0cc6
Fixed two memory leaks in make_filename() in zipimport.c. The allocated buffer wasn't cleaned up in two error cases. CID 486832
Christian Heimes <christian@cheimes.de>
parents:
78669
diff
changeset
|
245 if (!PyUnicode_AsUCS4(name, p, len, 1)) { |
|
6bd9626b0cc6
Fixed two memory leaks in make_filename() in zipimport.c. The allocated buffer wasn't cleaned up in two error cases. CID 486832
Christian Heimes <christian@cheimes.de>
parents:
78669
diff
changeset
|
246 PyMem_Free(buf); |
| 72475 | 247 return NULL; |
|
78933
6bd9626b0cc6
Fixed two memory leaks in make_filename() in zipimport.c. The allocated buffer wasn't cleaned up in two error cases. CID 486832
Christian Heimes <christian@cheimes.de>
parents:
78669
diff
changeset
|
248 } |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
249 for (; *p; p++) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
250 if (*p == '.') |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
251 *p = SEP; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
252 } |
| 72475 | 253 pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, |
| 254 buf, p-buf); | |
| 255 PyMem_Free(buf); | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
256 return pathobj; |
| 26885 | 257 } |
| 258 | |
|
33955
77b1c15174b4
SF patch 1062495: Modules/zipimport.c does not compile on solaris
Raymond Hettinger <python@rcn.com>
parents:
30365
diff
changeset
|
259 enum zi_module_info { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
260 MI_ERROR, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
261 MI_NOT_FOUND, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
262 MI_MODULE, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
263 MI_PACKAGE |
| 26885 | 264 }; |
| 265 | |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
266 /* Does this path represent a directory? |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
267 on error, return < 0 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
268 if not a dir, return 0 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
269 if a dir, return 1 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
270 */ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
271 static int |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
272 check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path) |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
273 { |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
274 PyObject *dirpath; |
|
77133
17f48832e749
use PyDict_Contains
Benjamin Peterson <benjamin@python.org>
parents:
77132
diff
changeset
|
275 int res; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
276 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
277 /* See if this is a "directory". If so, it's eligible to be part |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
278 of a namespace package. We test by seeing if the name, with an |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
279 appended path separator, exists. */ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
280 dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP); |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
281 if (dirpath == NULL) |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
282 return -1; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
283 /* If dirpath is present in self->files, we have a directory. */ |
|
77133
17f48832e749
use PyDict_Contains
Benjamin Peterson <benjamin@python.org>
parents:
77132
diff
changeset
|
284 res = PyDict_Contains(self->files, dirpath); |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
285 Py_DECREF(dirpath); |
|
77133
17f48832e749
use PyDict_Contains
Benjamin Peterson <benjamin@python.org>
parents:
77132
diff
changeset
|
286 return res; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
287 } |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
288 |
| 26885 | 289 /* Return some information about a module. */ |
|
33955
77b1c15174b4
SF patch 1062495: Modules/zipimport.c does not compile on solaris
Raymond Hettinger <python@rcn.com>
parents:
30365
diff
changeset
|
290 static enum zi_module_info |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
291 get_module_info(ZipImporter *self, PyObject *fullname) |
| 26885 | 292 { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
293 PyObject *subname; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
294 PyObject *path, *fullpath, *item; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
295 struct st_zip_searchorder *zso; |
| 26885 | 296 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
297 subname = get_subname(fullname); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
298 if (subname == NULL) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
299 return MI_ERROR; |
| 26885 | 300 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
301 path = make_filename(self->prefix, subname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
302 Py_DECREF(subname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
303 if (path == NULL) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
304 return MI_ERROR; |
| 26885 | 305 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
306 for (zso = zip_searchorder; *zso->suffix; zso++) { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
307 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
308 if (fullpath == NULL) { |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
309 Py_DECREF(path); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
310 return MI_ERROR; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
311 } |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
312 item = PyDict_GetItem(self->files, fullpath); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
313 Py_DECREF(fullpath); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
314 if (item != NULL) { |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
315 Py_DECREF(path); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
316 if (zso->type & IS_PACKAGE) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
317 return MI_PACKAGE; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
318 else |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
319 return MI_MODULE; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
320 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
321 } |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
322 Py_DECREF(path); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
323 return MI_NOT_FOUND; |
| 26885 | 324 } |
| 325 | |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
326 typedef enum { |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
327 FL_ERROR, |
|
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
328 FL_NOT_FOUND, |
|
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
329 FL_MODULE_FOUND, |
|
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
330 FL_NS_FOUND |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
331 } find_loader_result; |
|
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
332 |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
333 /* The guts of "find_loader" and "find_module". Return values: |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
334 -1: error |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
335 0: no loader or namespace portions found |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
336 1: module/package found |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
337 2: namespace portion found: *namespace_portion will point to the name |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
338 */ |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
339 static find_loader_result |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
340 find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion) |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
341 { |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
342 enum zi_module_info mi; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
343 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
344 *namespace_portion = NULL; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
345 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
346 mi = get_module_info(self, fullname); |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
347 if (mi == MI_ERROR) |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
348 return FL_ERROR; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
349 if (mi == MI_NOT_FOUND) { |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
350 /* Not a module or regular package. See if this is a directory, and |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
351 therefore possibly a portion of a namespace package. */ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
352 int is_dir = check_is_directory(self, self->prefix, fullname); |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
353 if (is_dir < 0) |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
354 return -1; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
355 if (is_dir) { |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
356 /* This is possibly a portion of a namespace |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
357 package. Return the string representing its path, |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
358 without a trailing separator. */ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
359 *namespace_portion = PyUnicode_FromFormat("%U%c%U%U", |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
360 self->archive, SEP, |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
361 self->prefix, fullname); |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
362 if (*namespace_portion == NULL) |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
363 return FL_ERROR; |
|
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
364 return FL_NS_FOUND; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
365 } |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
366 return FL_NOT_FOUND; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
367 } |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
368 /* This is a module or package. */ |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
369 return FL_MODULE_FOUND; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
370 } |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
371 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
372 |
| 26885 | 373 /* Check whether we can satisfy the import of the module named by |
| 374 'fullname'. Return self if we can, None if we can't. */ | |
| 375 static PyObject * | |
| 376 zipimporter_find_module(PyObject *obj, PyObject *args) | |
| 377 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
378 ZipImporter *self = (ZipImporter *)obj; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
379 PyObject *path = NULL; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
380 PyObject *fullname; |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
381 PyObject *namespace_portion = NULL; |
|
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
382 PyObject *result = NULL; |
| 26885 | 383 |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
384 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
385 return NULL; |
| 26885 | 386 |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
387 switch (find_loader(self, fullname, &namespace_portion)) { |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
388 case FL_ERROR: |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
389 return NULL; |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
390 case FL_NS_FOUND: |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
391 /* A namespace portion is not allowed via find_module, so return None. */ |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
392 Py_DECREF(namespace_portion); |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
393 /* FALL THROUGH */ |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
394 case FL_NOT_FOUND: |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
395 result = Py_None; |
|
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
396 break; |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
397 case FL_MODULE_FOUND: |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
398 result = (PyObject *)self; |
|
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
399 break; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
400 } |
|
77132
9554a27b5b19
return NULL on error
Benjamin Peterson <benjamin@python.org>
parents:
77131
diff
changeset
|
401 Py_INCREF(result); |
|
77131
828e9d559c55
actually return the result
Benjamin Peterson <benjamin@python.org>
parents:
77129
diff
changeset
|
402 return result; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
403 } |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
404 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
405 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
406 /* Check whether we can satisfy the import of the module named by |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
407 'fullname', or whether it could be a portion of a namespace |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
408 package. Return self if we can load it, a string containing the |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
409 full path if it's a possible namespace portion, None if we |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
410 can't load it. */ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
411 static PyObject * |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
412 zipimporter_find_loader(PyObject *obj, PyObject *args) |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
413 { |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
414 ZipImporter *self = (ZipImporter *)obj; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
415 PyObject *path = NULL; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
416 PyObject *fullname; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
417 PyObject *result = NULL; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
418 PyObject *namespace_portion = NULL; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
419 |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
420 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) |
|
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
421 return NULL; |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
422 |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
423 switch (find_loader(self, fullname, &namespace_portion)) { |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
424 case FL_ERROR: |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
425 return NULL; |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
426 case FL_NOT_FOUND: /* Not found, return (None, []) */ |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
427 result = Py_BuildValue("O[]", Py_None); |
|
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
428 break; |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
429 case FL_MODULE_FOUND: /* Return (self, []) */ |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
430 result = Py_BuildValue("O[]", self); |
|
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
431 break; |
|
77144
4a6df7a2dd9d
capitialize enum members
Benjamin Peterson <benjamin@python.org>
parents:
77133
diff
changeset
|
432 case FL_NS_FOUND: /* Return (None, [namespace_portion]) */ |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
433 result = Py_BuildValue("O[O]", Py_None, namespace_portion); |
| 77128 | 434 Py_DECREF(namespace_portion); |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
435 return result; |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
436 } |
|
77129
a47d32a28662
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
Benjamin Peterson <benjamin@python.org>
parents:
77128
diff
changeset
|
437 return result; |
| 26885 | 438 } |
| 439 | |
| 440 /* Load and return the module named by 'fullname'. */ | |
| 441 static PyObject * | |
| 442 zipimporter_load_module(PyObject *obj, PyObject *args) | |
| 443 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
444 ZipImporter *self = (ZipImporter *)obj; |
|
65517
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
445 PyObject *code = NULL, *mod, *dict; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
446 PyObject *fullname; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
447 PyObject *modpath = NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
448 int ispackage; |
| 26885 | 449 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
450 if (!PyArg_ParseTuple(args, "U:zipimporter.load_module", |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
451 &fullname)) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
452 return NULL; |
| 72475 | 453 if (PyUnicode_READY(fullname) == -1) |
| 454 return NULL; | |
| 26885 | 455 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
456 code = get_module_code(self, fullname, &ispackage, &modpath); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
457 if (code == NULL) |
|
65517
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
458 goto error; |
| 26885 | 459 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
460 mod = PyImport_AddModuleObject(fullname); |
|
65517
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
461 if (mod == NULL) |
|
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
462 goto error; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
463 dict = PyModule_GetDict(mod); |
| 26885 | 464 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
465 /* mod.__loader__ = self */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
466 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
467 goto error; |
| 26885 | 468 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
469 if (ispackage) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
470 /* add __path__ to the module *before* the code gets |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
471 executed */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
472 PyObject *pkgpath, *fullpath; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
473 PyObject *subname = get_subname(fullname); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
474 int err; |
| 26885 | 475 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
476 fullpath = PyUnicode_FromFormat("%U%c%U%U", |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
477 self->archive, SEP, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
478 self->prefix, subname); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
479 Py_DECREF(subname); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
480 if (fullpath == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
481 goto error; |
| 26885 | 482 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
483 pkgpath = Py_BuildValue("[N]", fullpath); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
484 if (pkgpath == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
485 goto error; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
486 err = PyDict_SetItemString(dict, "__path__", pkgpath); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
487 Py_DECREF(pkgpath); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
488 if (err != 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
489 goto error; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
490 } |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
491 mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL); |
|
65517
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
492 Py_CLEAR(code); |
|
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
493 if (mod == NULL) |
|
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
494 goto error; |
|
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
495 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
496 if (Py_VerboseFlag) |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
497 PySys_FormatStderr("import %U # loaded from Zip %U\n", |
|
65518
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
498 fullname, modpath); |
|
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
499 Py_DECREF(modpath); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
500 return mod; |
| 26885 | 501 error: |
|
65517
c72757033d44
zipimporter_load_module() doesn't destroy mod on error
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65516
diff
changeset
|
502 Py_XDECREF(code); |
|
65518
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
503 Py_XDECREF(modpath); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
504 return NULL; |
| 26885 | 505 } |
| 506 | |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
507 /* Return a string matching __file__ for the named module */ |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
508 static PyObject * |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
509 zipimporter_get_filename(PyObject *obj, PyObject *args) |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
510 { |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
511 ZipImporter *self = (ZipImporter *)obj; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
512 PyObject *fullname, *code, *modpath; |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
513 int ispackage; |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
514 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
515 if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename", |
|
65533
1a905391e3ed
zipimport: document archive encoding; fix indentation
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65532
diff
changeset
|
516 &fullname)) |
|
65514
8f0625954ffa
zipimport: fix indentation
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65398
diff
changeset
|
517 return NULL; |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
518 |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
519 /* Deciding the filename requires working out where the code |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
520 would come from if the module was actually loaded */ |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
521 code = get_module_code(self, fullname, &ispackage, &modpath); |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
522 if (code == NULL) |
|
65514
8f0625954ffa
zipimport: fix indentation
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65398
diff
changeset
|
523 return NULL; |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
524 Py_DECREF(code); /* Only need the path info */ |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
525 |
|
65518
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
526 return modpath; |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
527 } |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
528 |
| 26885 | 529 /* Return a bool signifying whether the module is a package or not. */ |
| 530 static PyObject * | |
| 531 zipimporter_is_package(PyObject *obj, PyObject *args) | |
| 532 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
533 ZipImporter *self = (ZipImporter *)obj; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
534 PyObject *fullname; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
535 enum zi_module_info mi; |
| 26885 | 536 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
537 if (!PyArg_ParseTuple(args, "U:zipimporter.is_package", |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
538 &fullname)) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
539 return NULL; |
| 26885 | 540 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
541 mi = get_module_info(self, fullname); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
542 if (mi == MI_ERROR) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
543 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
544 if (mi == MI_NOT_FOUND) { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
545 PyErr_Format(ZipImportError, "can't find module %R", fullname); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
546 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
547 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
548 return PyBool_FromLong(mi == MI_PACKAGE); |
| 26885 | 549 } |
| 550 | |
| 72475 | 551 |
| 26885 | 552 static PyObject * |
| 553 zipimporter_get_data(PyObject *obj, PyObject *args) | |
| 554 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
555 ZipImporter *self = (ZipImporter *)obj; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
556 PyObject *path, *key; |
| 26885 | 557 #ifdef ALTSEP |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
558 _Py_IDENTIFIER(replace); |
| 26885 | 559 #endif |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
560 PyObject *toc_entry; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
561 Py_ssize_t path_start, path_len, len; |
| 26885 | 562 |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
563 if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path)) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
564 return NULL; |
| 26885 | 565 |
| 566 #ifdef ALTSEP | |
| 73237 | 567 path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
568 if (!path) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
569 return NULL; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
570 #else |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
571 Py_INCREF(path); |
| 26885 | 572 #endif |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
573 if (PyUnicode_READY(path) == -1) |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
574 goto error; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
575 |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
576 path_len = PyUnicode_GET_LENGTH(path); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
577 |
| 72475 | 578 len = PyUnicode_GET_LENGTH(self->archive); |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
579 path_start = 0; |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
580 if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1) |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
581 && PyUnicode_READ_CHAR(path, len) == SEP) { |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
582 path_start = len + 1; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
583 } |
| 26885 | 584 |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
585 key = PyUnicode_Substring(path, path_start, path_len); |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
586 if (key == NULL) |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
587 goto error; |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
588 toc_entry = PyDict_GetItem(self->files, key); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
589 if (toc_entry == NULL) { |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
590 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key); |
|
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
591 Py_DECREF(key); |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
592 goto error; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
593 } |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
594 Py_DECREF(key); |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
595 Py_DECREF(path); |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
596 return get_data(self->archive, toc_entry); |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
597 error: |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
598 Py_DECREF(path); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
599 return NULL; |
| 26885 | 600 } |
| 601 | |
| 602 static PyObject * | |
| 603 zipimporter_get_code(PyObject *obj, PyObject *args) | |
| 604 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
605 ZipImporter *self = (ZipImporter *)obj; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
606 PyObject *fullname; |
| 26885 | 607 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
608 if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname)) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
609 return NULL; |
| 26885 | 610 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
611 return get_module_code(self, fullname, NULL, NULL); |
| 26885 | 612 } |
| 613 | |
| 614 static PyObject * | |
| 615 zipimporter_get_source(PyObject *obj, PyObject *args) | |
| 616 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
617 ZipImporter *self = (ZipImporter *)obj; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
618 PyObject *toc_entry; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
619 PyObject *fullname, *subname, *path, *fullpath; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
620 enum zi_module_info mi; |
| 26885 | 621 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
622 if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname)) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
623 return NULL; |
| 26885 | 624 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
625 mi = get_module_info(self, fullname); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
626 if (mi == MI_ERROR) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
627 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
628 if (mi == MI_NOT_FOUND) { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
629 PyErr_Format(ZipImportError, "can't find module %R", fullname); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
630 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
631 } |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
632 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
633 subname = get_subname(fullname); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
634 if (subname == NULL) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
635 return NULL; |
| 26885 | 636 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
637 path = make_filename(self->prefix, subname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
638 Py_DECREF(subname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
639 if (path == NULL) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
640 return NULL; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
641 |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
642 if (mi == MI_PACKAGE) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
643 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
644 else |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
645 fullpath = PyUnicode_FromFormat("%U.py", path); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
646 Py_DECREF(path); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
647 if (fullpath == NULL) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
648 return NULL; |
| 26885 | 649 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
650 toc_entry = PyDict_GetItem(self->files, fullpath); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
651 Py_DECREF(fullpath); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
652 if (toc_entry != NULL) { |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
653 PyObject *res, *bytes; |
|
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
654 bytes = get_data(self->archive, toc_entry); |
|
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
655 if (bytes == NULL) |
|
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
656 return NULL; |
|
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
657 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes), |
|
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
658 PyBytes_GET_SIZE(bytes)); |
|
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
659 Py_DECREF(bytes); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
660 return res; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
661 } |
| 26885 | 662 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
663 /* we have the module, but no source */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
664 Py_INCREF(Py_None); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
665 return Py_None; |
| 26885 | 666 } |
| 667 | |
| 668 PyDoc_STRVAR(doc_find_module, | |
| 669 "find_module(fullname, path=None) -> self or None.\n\ | |
| 670 \n\ | |
| 671 Search for a module specified by 'fullname'. 'fullname' must be the\n\ | |
| 672 fully qualified (dotted) module name. It returns the zipimporter\n\ | |
| 673 instance itself if the module was found, or None if it wasn't.\n\ | |
| 674 The optional 'path' argument is ignored -- it's there for compatibility\n\ | |
| 675 with the importer protocol."); | |
| 676 | |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
677 PyDoc_STRVAR(doc_find_loader, |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
678 "find_loader(fullname, path=None) -> self, str or None.\n\ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
679 \n\ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
680 Search for a module specified by 'fullname'. 'fullname' must be the\n\ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
681 fully qualified (dotted) module name. It returns the zipimporter\n\ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
682 instance itself if the module was found, a string containing the\n\ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
683 full path name if it's possibly a portion of a namespace package,\n\ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
684 or None otherwise. The optional 'path' argument is ignored -- it's\n\ |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
685 there for compatibility with the importer protocol."); |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
686 |
| 26885 | 687 PyDoc_STRVAR(doc_load_module, |
| 688 "load_module(fullname) -> module.\n\ | |
| 689 \n\ | |
| 690 Load the module specified by 'fullname'. 'fullname' must be the\n\ | |
| 691 fully qualified (dotted) module name. It returns the imported\n\ | |
| 692 module, or raises ZipImportError if it wasn't found."); | |
| 693 | |
| 694 PyDoc_STRVAR(doc_get_data, | |
| 695 "get_data(pathname) -> string with file data.\n\ | |
| 696 \n\ | |
| 697 Return the data associated with 'pathname'. Raise IOError if\n\ | |
| 698 the file wasn't found."); | |
| 699 | |
| 700 PyDoc_STRVAR(doc_is_package, | |
| 701 "is_package(fullname) -> bool.\n\ | |
| 702 \n\ | |
| 703 Return True if the module specified by fullname is a package.\n\ | |
|
63117
bfdb5f08c0f8
Fix #9316. if/is grammar corrections.
Brian Curtin <brian.curtin@gmail.com>
parents:
61466
diff
changeset
|
704 Raise ZipImportError if the module couldn't be found."); |
| 26885 | 705 |
| 706 PyDoc_STRVAR(doc_get_code, | |
| 707 "get_code(fullname) -> code object.\n\ | |
| 708 \n\ | |
| 709 Return the code object for the specified module. Raise ZipImportError\n\ | |
|
63117
bfdb5f08c0f8
Fix #9316. if/is grammar corrections.
Brian Curtin <brian.curtin@gmail.com>
parents:
61466
diff
changeset
|
710 if the module couldn't be found."); |
| 26885 | 711 |
| 712 PyDoc_STRVAR(doc_get_source, | |
| 713 "get_source(fullname) -> source string.\n\ | |
| 714 \n\ | |
| 715 Return the source code for the specified module. Raise ZipImportError\n\ | |
|
63117
bfdb5f08c0f8
Fix #9316. if/is grammar corrections.
Brian Curtin <brian.curtin@gmail.com>
parents:
61466
diff
changeset
|
716 if the module couldn't be found, return None if the archive does\n\ |
| 26885 | 717 contain the module, but has no source for it."); |
| 718 | |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
719 |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
720 PyDoc_STRVAR(doc_get_filename, |
|
51456
74d196c542f7
Merged revisions 69425 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
51271
diff
changeset
|
721 "get_filename(fullname) -> filename string.\n\ |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
722 \n\ |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
723 Return the filename for the specified module."); |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
49051
diff
changeset
|
724 |
| 26885 | 725 static PyMethodDef zipimporter_methods[] = { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
726 {"find_module", zipimporter_find_module, METH_VARARGS, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
727 doc_find_module}, |
|
77126
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
728 {"find_loader", zipimporter_find_loader, METH_VARARGS, |
|
702009f3c0b1
issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents:
74691
diff
changeset
|
729 doc_find_loader}, |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
730 {"load_module", zipimporter_load_module, METH_VARARGS, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
731 doc_load_module}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
732 {"get_data", zipimporter_get_data, METH_VARARGS, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
733 doc_get_data}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
734 {"get_code", zipimporter_get_code, METH_VARARGS, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
735 doc_get_code}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
736 {"get_source", zipimporter_get_source, METH_VARARGS, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
737 doc_get_source}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
738 {"get_filename", zipimporter_get_filename, METH_VARARGS, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
739 doc_get_filename}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
740 {"is_package", zipimporter_is_package, METH_VARARGS, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
741 doc_is_package}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
742 {NULL, NULL} /* sentinel */ |
| 26885 | 743 }; |
| 744 | |
| 745 static PyMemberDef zipimporter_members[] = { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
746 {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
747 {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
748 {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY}, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
749 {NULL} |
| 26885 | 750 }; |
| 751 | |
| 752 PyDoc_STRVAR(zipimporter_doc, | |
| 753 "zipimporter(archivepath) -> zipimporter object\n\ | |
| 754 \n\ | |
| 755 Create a new zipimporter instance. 'archivepath' must be a path to\n\ | |
|
47072
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
756 a zipfile, or to a specific path inside a zipfile. For example, it can be\n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
757 '/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
758 valid directory inside the archive.\n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
759 \n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
760 'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
761 archive.\n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
762 \n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
763 The 'archive' attribute of zipimporter objects contains the name of the\n\ |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46718
diff
changeset
|
764 zipfile targeted."); |
| 26885 | 765 |
| 766 #define DEFERRED_ADDRESS(ADDR) 0 | |
| 767 | |
| 768 static PyTypeObject ZipImporter_Type = { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
769 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
770 "zipimport.zipimporter", |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
771 sizeof(ZipImporter), |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
772 0, /* tp_itemsize */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
773 (destructor)zipimporter_dealloc, /* tp_dealloc */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
774 0, /* tp_print */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
775 0, /* tp_getattr */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
776 0, /* tp_setattr */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
777 0, /* tp_reserved */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
778 (reprfunc)zipimporter_repr, /* tp_repr */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
779 0, /* tp_as_number */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
780 0, /* tp_as_sequence */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
781 0, /* tp_as_mapping */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
782 0, /* tp_hash */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
783 0, /* tp_call */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
784 0, /* tp_str */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
785 PyObject_GenericGetAttr, /* tp_getattro */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
786 0, /* tp_setattro */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
787 0, /* tp_as_buffer */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
788 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
789 Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
790 zipimporter_doc, /* tp_doc */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
791 zipimporter_traverse, /* tp_traverse */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
792 0, /* tp_clear */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
793 0, /* tp_richcompare */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
794 0, /* tp_weaklistoffset */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
795 0, /* tp_iter */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
796 0, /* tp_iternext */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
797 zipimporter_methods, /* tp_methods */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
798 zipimporter_members, /* tp_members */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
799 0, /* tp_getset */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
800 0, /* tp_base */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
801 0, /* tp_dict */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
802 0, /* tp_descr_get */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
803 0, /* tp_descr_set */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
804 0, /* tp_dictoffset */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
805 (initproc)zipimporter_init, /* tp_init */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
806 PyType_GenericAlloc, /* tp_alloc */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
807 PyType_GenericNew, /* tp_new */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
808 PyObject_GC_Del, /* tp_free */ |
| 26885 | 809 }; |
| 810 | |
| 811 | |
| 812 /* implementation */ | |
| 813 | |
| 814 /* Given a buffer, return the long that is represented by the first | |
| 815 4 bytes, encoded as little endian. This partially reimplements | |
| 816 marshal.c:r_long() */ | |
| 817 static long | |
| 818 get_long(unsigned char *buf) { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
819 long x; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
820 x = buf[0]; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
821 x |= (long)buf[1] << 8; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
822 x |= (long)buf[2] << 16; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
823 x |= (long)buf[3] << 24; |
| 26885 | 824 #if SIZEOF_LONG > 4 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
825 /* Sign extension for 64-bit machines */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
826 x |= -(x & 0x80000000L); |
| 26885 | 827 #endif |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
828 return x; |
| 26885 | 829 } |
| 830 | |
| 831 /* | |
| 832 read_directory(archive) -> files dict (new reference) | |
| 833 | |
| 834 Given a path to a Zip archive, build a dict, mapping file names | |
| 835 (local to the archive, using SEP as a separator) to toc entries. | |
| 836 | |
| 837 A toc_entry is a tuple: | |
| 838 | |
|
65518
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
839 (__file__, # value to use for __file__, available for all files, |
|
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
840 # encoded to the filesystem encoding |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
841 compress, # compression kind; 0 for uncompressed |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
842 data_size, # size of compressed data on disk |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
843 file_size, # size of decompressed data |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
844 file_offset, # offset of file header from start of archive |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
845 time, # mod time of file (in dos format) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
846 date, # mod data of file (in dos format) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
847 crc, # crc checksum of the data |
|
65514
8f0625954ffa
zipimport: fix indentation
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65398
diff
changeset
|
848 ) |
| 26885 | 849 |
| 850 Directories can be recognized by the trailing SEP in the name, | |
| 851 data_size and file_offset are 0. | |
| 852 */ | |
| 853 static PyObject * | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
854 read_directory(PyObject *archive) |
| 26885 | 855 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
856 PyObject *files = NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
857 FILE *fp; |
|
65520
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
858 unsigned short flags; |
|
74690
c5a09a96f533
Fix zip_import.c's read_directory() to use appropriate types for the values
Gregory P. Smith <greg@krypto.org>
parents:
74059
diff
changeset
|
859 short compress, time, date, name_size; |
|
c5a09a96f533
Fix zip_import.c's read_directory() to use appropriate types for the values
Gregory P. Smith <greg@krypto.org>
parents:
74059
diff
changeset
|
860 long crc, data_size, file_size, header_size; |
|
c5a09a96f533
Fix zip_import.c's read_directory() to use appropriate types for the values
Gregory P. Smith <greg@krypto.org>
parents:
74059
diff
changeset
|
861 Py_ssize_t file_offset, header_position, header_offset; |
| 72475 | 862 long l, count; |
| 863 Py_ssize_t i; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
864 char name[MAXPATHLEN + 5]; |
|
64065
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
865 PyObject *nameobj = NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
866 char *p, endof_central_dir[22]; |
|
74690
c5a09a96f533
Fix zip_import.c's read_directory() to use appropriate types for the values
Gregory P. Smith <greg@krypto.org>
parents:
74059
diff
changeset
|
867 Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */ |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
868 PyObject *path; |
|
65520
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
869 const char *charset; |
|
67717
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
870 int bootstrap; |
| 26885 | 871 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
872 fp = _Py_fopen(archive, "rb"); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
873 if (fp == NULL) { |
|
74059
586bcc05f88f
Handle correctly _Py_fopen() error: don't replace the exception
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70215
diff
changeset
|
874 if (!PyErr_Occurred()) |
|
74060
f44ab94cd28b
(Merge 3.2) Handle correctly _Py_fopen() error: don't replace the exception
Victor Stinner <victor.stinner@haypocalc.com>
diff
changeset
|
875 PyErr_Format(ZipImportError, "can't open Zip file: %R", archive); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
876 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
877 } |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
878 |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
879 if (fseek(fp, -22, SEEK_END) == -1) { |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
880 fclose(fp); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
881 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
882 return NULL; |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
883 } |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
884 header_position = ftell(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
885 if (fread(endof_central_dir, 1, 22, fp) != 22) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
886 fclose(fp); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
887 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
888 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
889 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
890 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
891 /* Bad: End of Central Dir signature */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
892 fclose(fp); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
893 PyErr_Format(ZipImportError, "not a Zip file: %R", archive); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
894 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
895 } |
| 26885 | 896 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
897 header_size = get_long((unsigned char *)endof_central_dir + 12); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
898 header_offset = get_long((unsigned char *)endof_central_dir + 16); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
899 arc_offset = header_position - header_offset - header_size; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
900 header_offset += arc_offset; |
| 26885 | 901 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
902 files = PyDict_New(); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
903 if (files == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
904 goto error; |
| 26885 | 905 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
906 /* Start of Central Directory */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
907 count = 0; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
908 for (;;) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
909 PyObject *t; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
910 int err; |
| 26885 | 911 |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
912 if (fseek(fp, header_offset, 0) == -1) /* Start of file header */ |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
913 goto fseek_error; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
914 l = PyMarshal_ReadLongFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
915 if (l != 0x02014B50) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
916 break; /* Bad: Central Dir File Header */ |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
917 if (fseek(fp, header_offset + 8, 0) == -1) |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
918 goto fseek_error; |
|
65520
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
919 flags = (unsigned short)PyMarshal_ReadShortFromFile(fp); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
920 compress = PyMarshal_ReadShortFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
921 time = PyMarshal_ReadShortFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
922 date = PyMarshal_ReadShortFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
923 crc = PyMarshal_ReadLongFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
924 data_size = PyMarshal_ReadLongFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
925 file_size = PyMarshal_ReadLongFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
926 name_size = PyMarshal_ReadShortFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
927 header_size = 46 + name_size + |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
928 PyMarshal_ReadShortFromFile(fp) + |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
929 PyMarshal_ReadShortFromFile(fp); |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
930 if (fseek(fp, header_offset + 42, 0) == -1) |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
931 goto fseek_error; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
932 file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
933 if (name_size > MAXPATHLEN) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
934 name_size = MAXPATHLEN; |
| 26885 | 935 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
936 p = name; |
| 72475 | 937 for (i = 0; i < (Py_ssize_t)name_size; i++) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
938 *p = (char)getc(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
939 if (*p == '/') |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
940 *p = SEP; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
941 p++; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
942 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
943 *p = 0; /* Add terminating null byte */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
944 header_offset += header_size; |
| 26885 | 945 |
|
67717
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
946 bootstrap = 0; |
|
65520
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
947 if (flags & 0x0800) |
|
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
948 charset = "utf-8"; |
|
67717
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
949 else if (!PyThreadState_GET()->interp->codecs_initialized) { |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
950 /* During bootstrap, we may need to load the encodings |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
951 package from a ZIP file. But the cp437 encoding is implemented |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
952 in Python in the encodings package. |
| 26885 | 953 |
|
67717
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
954 Break out of this dependency by assuming that the path to |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
955 the encodings module is ASCII-only. */ |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
956 charset = "ascii"; |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
957 bootstrap = 1; |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
958 } |
|
65520
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
959 else |
|
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
960 charset = "cp437"; |
|
372c9807d75d
zipimport: read_directory() uses cp437 or utf-8 (in strict mode), depending on
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65518
diff
changeset
|
961 nameobj = PyUnicode_Decode(name, name_size, charset, NULL); |
|
67717
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
962 if (nameobj == NULL) { |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
963 if (bootstrap) |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
964 PyErr_Format(PyExc_NotImplementedError, |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
965 "bootstrap issue: python%i%i.zip contains non-ASCII " |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
966 "filenames without the unicode flag", |
|
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
967 PY_MAJOR_VERSION, PY_MINOR_VERSION); |
|
64065
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
968 goto error; |
|
67717
b968053afad0
Issue #10955: zipimport uses ASCII at bootstrap
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65534
diff
changeset
|
969 } |
|
78669
c89fe7dc22ee
Closes #15737: Fix potential NULL dereference in zipimport.c.
Stefan Krah <skrah@bytereef.org>
parents:
77144
diff
changeset
|
970 if (PyUnicode_READY(nameobj) == -1) |
|
64065
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
971 goto error; |
|
73235
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
972 path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj); |
|
6d19a7c713a8
Replace Py_UCS4_ API with Unicode API.
Martin v. Löwis <martin@v.loewis.de>
parents:
72925
diff
changeset
|
973 if (path == NULL) |
|
64065
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
974 goto error; |
|
74691
2db416b953e8
Fix zipimport.c's read_directory() to use appropriate types for the values
Gregory P. Smith <greg@krypto.org>
diff
changeset
|
975 t = Py_BuildValue("Nhllnhhl", path, compress, data_size, |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
976 file_size, file_offset, time, date, crc); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
977 if (t == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
978 goto error; |
|
64065
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
979 err = PyDict_SetItem(files, nameobj, t); |
|
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
980 Py_CLEAR(nameobj); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
981 Py_DECREF(t); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
982 if (err != 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
983 goto error; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
984 count++; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
985 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
986 fclose(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
987 if (Py_VerboseFlag) |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
988 PySys_FormatStderr("# zipimport: found %ld names in %R\n", |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
989 count, archive); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
990 return files; |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
991 fseek_error: |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
992 fclose(fp); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
993 Py_XDECREF(files); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
994 Py_XDECREF(nameobj); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
995 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
996 return NULL; |
| 26885 | 997 error: |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
998 fclose(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
999 Py_XDECREF(files); |
|
64065
fc89b8deff1e
Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63986
diff
changeset
|
1000 Py_XDECREF(nameobj); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1001 return NULL; |
| 26885 | 1002 } |
| 1003 | |
| 1004 /* Return the zlib.decompress function object, or NULL if zlib couldn't | |
| 1005 be imported. The function is cached when found, so subsequent calls | |
|
70214
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1006 don't import zlib again. */ |
| 26885 | 1007 static PyObject * |
| 1008 get_decompress_func(void) | |
| 1009 { | |
|
70214
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1010 static int importing_zlib = 0; |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1011 PyObject *zlib; |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1012 PyObject *decompress; |
|
72925
7109f31300fb
Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents:
72867
diff
changeset
|
1013 _Py_IDENTIFIER(decompress); |
| 26885 | 1014 |
|
70214
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1015 if (importing_zlib != 0) |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1016 /* Someone has a zlib.py[co] in their Zip file; |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1017 let's avoid a stack overflow. */ |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1018 return NULL; |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1019 importing_zlib = 1; |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1020 zlib = PyImport_ImportModuleNoBlock("zlib"); |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1021 importing_zlib = 0; |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1022 if (zlib != NULL) { |
|
72848
81380082d216
Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents:
72489
diff
changeset
|
1023 decompress = _PyObject_GetAttrId(zlib, |
|
81380082d216
Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents:
72489
diff
changeset
|
1024 &PyId_decompress); |
|
70214
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1025 Py_DECREF(zlib); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1026 } |
|
70214
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1027 else { |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1028 PyErr_Clear(); |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1029 decompress = NULL; |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1030 } |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1031 if (Py_VerboseFlag) |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1032 PySys_WriteStderr("# zipimport: zlib %s\n", |
|
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1033 zlib != NULL ? "available": "UNAVAILABLE"); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1034 return decompress; |
| 26885 | 1035 } |
| 1036 | |
| 1037 /* Given a path to a Zip file and a toc_entry, return the (uncompressed) | |
| 1038 data as a new reference. */ | |
| 1039 static PyObject * | |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
1040 get_data(PyObject *archive, PyObject *toc_entry) |
| 26885 | 1041 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1042 PyObject *raw_data, *data = NULL, *decompress; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1043 char *buf; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1044 FILE *fp; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1045 int err; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1046 Py_ssize_t bytes_read = 0; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1047 long l; |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
1048 PyObject *datapath; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1049 long compress, data_size, file_size, file_offset, bytes_size; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1050 long time, date, crc; |
| 26885 | 1051 |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
1052 if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress, |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1053 &data_size, &file_size, &file_offset, &time, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1054 &date, &crc)) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1055 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1056 } |
| 26885 | 1057 |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
1058 fp = _Py_fopen(archive, "rb"); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1059 if (!fp) { |
|
74059
586bcc05f88f
Handle correctly _Py_fopen() error: don't replace the exception
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70215
diff
changeset
|
1060 if (!PyErr_Occurred()) |
|
586bcc05f88f
Handle correctly _Py_fopen() error: don't replace the exception
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70215
diff
changeset
|
1061 PyErr_Format(PyExc_IOError, |
|
586bcc05f88f
Handle correctly _Py_fopen() error: don't replace the exception
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70215
diff
changeset
|
1062 "zipimport: can not open file %U", archive); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1063 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1064 } |
| 26885 | 1065 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1066 /* Check to make sure the local file header is correct */ |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1067 if (fseek(fp, file_offset, 0) == -1) { |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1068 fclose(fp); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1069 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1070 return NULL; |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1071 } |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1072 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1073 l = PyMarshal_ReadLongFromFile(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1074 if (l != 0x04034B50) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1075 /* Bad: Local File Header */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1076 PyErr_Format(ZipImportError, |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
1077 "bad local file header in %U", |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1078 archive); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1079 fclose(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1080 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1081 } |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1082 if (fseek(fp, file_offset + 26, 0) == -1) { |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1083 fclose(fp); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1084 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1085 return NULL; |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1086 } |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1087 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1088 l = 30 + PyMarshal_ReadShortFromFile(fp) + |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1089 PyMarshal_ReadShortFromFile(fp); /* local header size */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1090 file_offset += l; /* Start of file data */ |
| 26885 | 1091 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1092 bytes_size = compress == 0 ? data_size : data_size + 1; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1093 if (bytes_size == 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1094 bytes_size++; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1095 raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1096 |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1097 if (raw_data == NULL) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1098 fclose(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1099 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1100 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1101 buf = PyBytes_AsString(raw_data); |
| 26885 | 1102 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1103 err = fseek(fp, file_offset, 0); |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1104 if (err == 0) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1105 bytes_read = fread(buf, 1, data_size, fp); |
|
79420
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1106 } else { |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1107 fclose(fp); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1108 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1109 return NULL; |
|
4da48083aaab
Closes #15897: zipimport.c doesn't check return value of fseek()
Jesus Cea <jcea@jcea.es>
parents:
74690
diff
changeset
|
1110 } |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1111 fclose(fp); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1112 if (err || bytes_read != data_size) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1113 PyErr_SetString(PyExc_IOError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1114 "zipimport: can't read data"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1115 Py_DECREF(raw_data); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1116 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1117 } |
| 26885 | 1118 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1119 if (compress != 0) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1120 buf[data_size] = 'Z'; /* saw this in zipfile.py */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1121 data_size++; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1122 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1123 buf[data_size] = '\0'; |
| 26885 | 1124 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1125 if (compress == 0) { /* data is not compressed */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1126 data = PyBytes_FromStringAndSize(buf, data_size); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1127 Py_DECREF(raw_data); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1128 return data; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1129 } |
| 26885 | 1130 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1131 /* Decompress with zlib */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1132 decompress = get_decompress_func(); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1133 if (decompress == NULL) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1134 PyErr_SetString(ZipImportError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1135 "can't decompress data; " |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1136 "zlib not available"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1137 goto error; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1138 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1139 data = PyObject_CallFunction(decompress, "Oi", raw_data, -15); |
|
70214
52d9183b34f8
Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68585
diff
changeset
|
1140 Py_DECREF(decompress); |
| 26885 | 1141 error: |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1142 Py_DECREF(raw_data); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1143 return data; |
| 26885 | 1144 } |
| 1145 | |
| 1146 /* Lenient date/time comparison function. The precision of the mtime | |
| 1147 in the archive is lower than the mtime stored in a .pyc: we | |
| 1148 must allow a difference of at most one second. */ | |
| 1149 static int | |
| 1150 eq_mtime(time_t t1, time_t t2) | |
| 1151 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1152 time_t d = t1 - t2; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1153 if (d < 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1154 d = -d; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1155 /* dostime only stores even seconds, so be lenient */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1156 return d <= 1; |
| 26885 | 1157 } |
| 1158 | |
| 1159 /* Given the contents of a .py[co] file in a buffer, unmarshal the data | |
| 1160 and return the code object. Return None if it the magic word doesn't | |
| 1161 match (we do this instead of raising an exception as we fall back | |
| 1162 to .py if available and we don't want to mask other errors). | |
| 1163 Returns a new reference. */ | |
| 1164 static PyObject * | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1165 unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime) |
| 26885 | 1166 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1167 PyObject *code; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1168 char *buf = PyBytes_AsString(data); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1169 Py_ssize_t size = PyBytes_Size(data); |
| 26885 | 1170 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1171 if (size <= 9) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1172 PyErr_SetString(ZipImportError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1173 "bad pyc data"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1174 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1175 } |
| 26885 | 1176 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1177 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1178 if (Py_VerboseFlag) |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1179 PySys_FormatStderr("# %R has bad magic\n", |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1180 pathname); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1181 Py_INCREF(Py_None); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1182 return Py_None; /* signal caller to try alternative */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1183 } |
| 26885 | 1184 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1185 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4), |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1186 mtime)) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1187 if (Py_VerboseFlag) |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1188 PySys_FormatStderr("# %R has bad mtime\n", |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1189 pathname); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1190 Py_INCREF(Py_None); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1191 return Py_None; /* signal caller to try alternative */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1192 } |
| 26885 | 1193 |
|
74358
87331661042b
Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents:
74060
diff
changeset
|
1194 /* XXX the pyc's size field is ignored; timestamp collisions are probably |
|
87331661042b
Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents:
74060
diff
changeset
|
1195 unimportant with zip files. */ |
|
87331661042b
Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents:
74060
diff
changeset
|
1196 code = PyMarshal_ReadObjectFromString(buf + 12, size - 12); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1197 if (code == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1198 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1199 if (!PyCode_Check(code)) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1200 Py_DECREF(code); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1201 PyErr_Format(PyExc_TypeError, |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1202 "compiled module %R is not a code object", |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1203 pathname); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1204 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1205 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1206 return code; |
| 26885 | 1207 } |
| 1208 | |
| 1209 /* Replace any occurances of "\r\n?" in the input string with "\n". | |
| 1210 This converts DOS and Mac line endings to Unix line endings. | |
| 1211 Also append a trailing "\n" to be compatible with | |
| 1212 PyParser_SimpleParseFile(). Returns a new reference. */ | |
| 1213 static PyObject * | |
| 1214 normalize_line_endings(PyObject *source) | |
| 1215 { | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1216 char *buf, *q, *p; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1217 PyObject *fixed_source; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1218 int len = 0; |
| 26885 | 1219 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1220 p = PyBytes_AsString(source); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1221 if (p == NULL) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1222 return PyBytes_FromStringAndSize("\n\0", 2); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1223 } |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1224 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1225 /* one char extra for trailing \n and one for terminating \0 */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1226 buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1227 if (buf == NULL) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1228 PyErr_SetString(PyExc_MemoryError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1229 "zipimport: no memory to allocate " |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1230 "source buffer"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1231 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1232 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1233 /* replace "\r\n?" by "\n" */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1234 for (q = buf; *p != '\0'; p++) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1235 if (*p == '\r') { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1236 *q++ = '\n'; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1237 if (*(p + 1) == '\n') |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1238 p++; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1239 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1240 else |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1241 *q++ = *p; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1242 len++; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1243 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1244 *q++ = '\n'; /* add trailing \n */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1245 *q = '\0'; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1246 fixed_source = PyBytes_FromStringAndSize(buf, len + 2); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1247 PyMem_Free(buf); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1248 return fixed_source; |
| 26885 | 1249 } |
| 1250 | |
| 1251 /* Given a string buffer containing Python source code, compile it | |
| 1252 return and return a code object as a new reference. */ | |
| 1253 static PyObject * | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1254 compile_source(PyObject *pathname, PyObject *source) |
| 26885 | 1255 { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1256 PyObject *code, *fixed_source, *pathbytes; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1257 |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1258 pathbytes = PyUnicode_EncodeFSDefault(pathname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1259 if (pathbytes == NULL) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1260 return NULL; |
| 26885 | 1261 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1262 fixed_source = normalize_line_endings(source); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1263 if (fixed_source == NULL) { |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1264 Py_DECREF(pathbytes); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1265 return NULL; |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1266 } |
| 26885 | 1267 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1268 code = Py_CompileString(PyBytes_AsString(fixed_source), |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1269 PyBytes_AsString(pathbytes), |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1270 Py_file_input); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1271 Py_DECREF(pathbytes); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1272 Py_DECREF(fixed_source); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1273 return code; |
| 26885 | 1274 } |
| 1275 | |
| 1276 /* Convert the date/time values found in the Zip archive to a value | |
| 1277 that's compatible with the time stamp stored in .pyc files. */ | |
|
28391
07617d0904a6
Make private function and data static.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28103
diff
changeset
|
1278 static time_t |
|
07617d0904a6
Make private function and data static.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28103
diff
changeset
|
1279 parse_dostime(int dostime, int dosdate) |
| 26885 | 1280 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1281 struct tm stm; |
| 26885 | 1282 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1283 memset((void *) &stm, '\0', sizeof(stm)); |
|
44742
14d4104976fc
Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44441
diff
changeset
|
1284 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1285 stm.tm_sec = (dostime & 0x1f) * 2; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1286 stm.tm_min = (dostime >> 5) & 0x3f; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1287 stm.tm_hour = (dostime >> 11) & 0x1f; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1288 stm.tm_mday = dosdate & 0x1f; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1289 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1290 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1291 stm.tm_isdst = -1; /* wday/yday is ignored */ |
| 26885 | 1292 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1293 return mktime(&stm); |
| 26885 | 1294 } |
| 1295 | |
| 1296 /* Given a path to a .pyc or .pyo file in the archive, return the | |
|
68585
bf94b6a73fc8
#11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68488
diff
changeset
|
1297 modification time of the matching .py file, or 0 if no source |
| 26885 | 1298 is available. */ |
| 1299 static time_t | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1300 get_mtime_of_source(ZipImporter *self, PyObject *path) |
| 26885 | 1301 { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1302 PyObject *toc_entry, *stripped; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1303 time_t mtime; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1304 |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1305 /* strip 'c' or 'o' from *.py[co] */ |
| 72475 | 1306 if (PyUnicode_READY(path) == -1) |
| 1307 return (time_t)-1; | |
| 1308 stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path), | |
| 1309 PyUnicode_DATA(path), | |
| 1310 PyUnicode_GET_LENGTH(path) - 1); | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1311 if (stripped == NULL) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1312 return (time_t)-1; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1313 |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1314 toc_entry = PyDict_GetItem(self->files, stripped); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1315 Py_DECREF(stripped); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1316 if (toc_entry != NULL && PyTuple_Check(toc_entry) && |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1317 PyTuple_Size(toc_entry) == 8) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1318 /* fetch the time stamp of the .py file for comparison |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1319 with an embedded pyc time stamp */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1320 int time, date; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1321 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5)); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1322 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6)); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1323 mtime = parse_dostime(time, date); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1324 } else |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1325 mtime = 0; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1326 return mtime; |
| 26885 | 1327 } |
| 1328 | |
| 1329 /* Return the code object for the module named by 'fullname' from the | |
| 1330 Zip archive as a new reference. */ | |
| 1331 static PyObject * | |
| 1332 get_code_from_data(ZipImporter *self, int ispackage, int isbytecode, | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1333 time_t mtime, PyObject *toc_entry) |
| 26885 | 1334 { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1335 PyObject *data, *modpath, *code; |
| 26885 | 1336 |
|
64090
b662a39f0823
Issue #9425: get_data() uses an unicode path
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64065
diff
changeset
|
1337 data = get_data(self->archive, toc_entry); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1338 if (data == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1339 return NULL; |
| 26885 | 1340 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1341 modpath = PyTuple_GetItem(toc_entry, 0); |
|
65521
77c13c9c6e59
get_code_from_data() uses the filesystem encoding to encode the module path,
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65520
diff
changeset
|
1342 if (isbytecode) |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1343 code = unmarshal_code(modpath, data, mtime); |
|
65521
77c13c9c6e59
get_code_from_data() uses the filesystem encoding to encode the module path,
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65520
diff
changeset
|
1344 else |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1345 code = compile_source(modpath, data); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1346 Py_DECREF(data); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1347 return code; |
| 26885 | 1348 } |
| 1349 | |
|
68488
0991b40e895d
#11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63117
diff
changeset
|
1350 /* Get the code object associated with the module specified by |
| 26885 | 1351 'fullname'. */ |
| 1352 static PyObject * | |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1353 get_module_code(ZipImporter *self, PyObject *fullname, |
|
65518
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
1354 int *p_ispackage, PyObject **p_modpath) |
| 26885 | 1355 { |
|
70245
812b2326bf47
avoid a compiler warning. the compiler doesn't know that the static
Gregory P. Smith <greg@krypto.org>
parents:
70216
diff
changeset
|
1356 PyObject *code = NULL, *toc_entry, *subname; |
|
70398
01f84c4e581e
zipimport: initialize fullpath to NULL
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70245
diff
changeset
|
1357 PyObject *path, *fullpath = NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1358 struct st_zip_searchorder *zso; |
| 26885 | 1359 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1360 subname = get_subname(fullname); |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1361 if (subname == NULL) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1362 return NULL; |
| 26885 | 1363 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1364 path = make_filename(self->prefix, subname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1365 Py_DECREF(subname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1366 if (path == NULL) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1367 return NULL; |
| 26885 | 1368 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1369 for (zso = zip_searchorder; *zso->suffix; zso++) { |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1370 code = NULL; |
| 26885 | 1371 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1372 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1373 if (fullpath == NULL) |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1374 goto exit; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1375 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1376 if (Py_VerboseFlag > 1) |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1377 PySys_FormatStderr("# trying %U%c%U\n", |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1378 self->archive, (int)SEP, fullpath); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1379 toc_entry = PyDict_GetItem(self->files, fullpath); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1380 if (toc_entry != NULL) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1381 time_t mtime = 0; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1382 int ispackage = zso->type & IS_PACKAGE; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1383 int isbytecode = zso->type & IS_BYTECODE; |
| 26885 | 1384 |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1385 if (isbytecode) { |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1386 mtime = get_mtime_of_source(self, fullpath); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1387 if (mtime == (time_t)-1 && PyErr_Occurred()) { |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1388 goto exit; |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1389 } |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1390 } |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1391 Py_CLEAR(fullpath); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1392 if (p_ispackage != NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1393 *p_ispackage = ispackage; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1394 code = get_code_from_data(self, ispackage, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1395 isbytecode, mtime, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1396 toc_entry); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1397 if (code == Py_None) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1398 /* bad magic number or non-matching mtime |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1399 in byte code, try next */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1400 Py_DECREF(code); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1401 continue; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1402 } |
|
65518
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
1403 if (code != NULL && p_modpath != NULL) { |
|
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
1404 *p_modpath = PyTuple_GetItem(toc_entry, 0); |
|
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
1405 Py_INCREF(*p_modpath); |
|
68bd75c69db1
zipimport: get_module_code() returns modpath as a Unicode object
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65517
diff
changeset
|
1406 } |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1407 goto exit; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1408 } |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1409 else |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1410 Py_CLEAR(fullpath); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1411 } |
|
68709
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1412 PyErr_Format(ZipImportError, "can't find module %R", fullname); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1413 exit: |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1414 Py_DECREF(path); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1415 Py_XDECREF(fullpath); |
|
6c80ac44ae9c
Issue #3080: zipimport has a full unicode suppport
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68586
diff
changeset
|
1416 return code; |
| 26885 | 1417 } |
| 1418 | |
| 1419 | |
| 1420 /* Module init */ | |
| 1421 | |
| 1422 PyDoc_STRVAR(zipimport_doc, | |
| 1423 "zipimport provides support for importing Python modules from Zip archives.\n\ | |
| 1424 \n\ | |
| 1425 This module exports three objects:\n\ | |
| 1426 - zipimporter: a class; its constructor takes a path to a Zip archive.\n\ | |
|
36296
c7cfea1ebb32
the implementation uses ZipImportError, not ZipImporterError...
Fredrik Lundh <fredrik@pythonware.com>
parents:
36230
diff
changeset
|
1427 - ZipImportError: exception raised by zipimporter objects. It's a\n\ |
| 26885 | 1428 subclass of ImportError, so it can be caught as ImportError, too.\n\ |
| 1429 - _zip_directory_cache: a dict, mapping archive paths to zip directory\n\ | |
| 1430 info dicts, as used in zipimporter._files.\n\ | |
| 1431 \n\ | |
| 1432 It is usually not needed to use the zipimport module explicitly; it is\n\ | |
| 1433 used by the builtin import mechanism for sys.path items that are paths\n\ | |
| 1434 to Zip archives."); | |
| 1435 | |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1436 static struct PyModuleDef zipimportmodule = { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1437 PyModuleDef_HEAD_INIT, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1438 "zipimport", |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1439 zipimport_doc, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1440 -1, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1441 NULL, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1442 NULL, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1443 NULL, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1444 NULL, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1445 NULL |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1446 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1447 |
| 26885 | 1448 PyMODINIT_FUNC |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47300
diff
changeset
|
1449 PyInit_zipimport(void) |
| 26885 | 1450 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1451 PyObject *mod; |
| 26885 | 1452 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1453 if (PyType_Ready(&ZipImporter_Type) < 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1454 return NULL; |
| 26885 | 1455 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1456 /* Correct directory separator */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1457 zip_searchorder[0].suffix[0] = SEP; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1458 zip_searchorder[1].suffix[0] = SEP; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1459 zip_searchorder[2].suffix[0] = SEP; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1460 if (Py_OptimizeFlag) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1461 /* Reverse *.pyc and *.pyo */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1462 struct st_zip_searchorder tmp; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1463 tmp = zip_searchorder[0]; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1464 zip_searchorder[0] = zip_searchorder[1]; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1465 zip_searchorder[1] = tmp; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1466 tmp = zip_searchorder[3]; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1467 zip_searchorder[3] = zip_searchorder[4]; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1468 zip_searchorder[4] = tmp; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1469 } |
| 26885 | 1470 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1471 mod = PyModule_Create(&zipimportmodule); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1472 if (mod == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1473 return NULL; |
| 26885 | 1474 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1475 ZipImportError = PyErr_NewException("zipimport.ZipImportError", |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1476 PyExc_ImportError, NULL); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1477 if (ZipImportError == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1478 return NULL; |
| 26885 | 1479 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1480 Py_INCREF(ZipImportError); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1481 if (PyModule_AddObject(mod, "ZipImportError", |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1482 ZipImportError) < 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1483 return NULL; |
| 26885 | 1484 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1485 Py_INCREF(&ZipImporter_Type); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1486 if (PyModule_AddObject(mod, "zipimporter", |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1487 (PyObject *)&ZipImporter_Type) < 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1488 return NULL; |
|
26931
6c535dcedad3
- added missing decref
Just van Rossum <just@letterror.com>
parents:
26901
diff
changeset
|
1489 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1490 zip_directory_cache = PyDict_New(); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1491 if (zip_directory_cache == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1492 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1493 Py_INCREF(zip_directory_cache); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1494 if (PyModule_AddObject(mod, "_zip_directory_cache", |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1495 zip_directory_cache) < 0) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1496 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59538
diff
changeset
|
1497 return mod; |
| 26885 | 1498 } |
