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