Left: | ||
Right: |
OLD | NEW |
---|---|
1 #include "Python.h" | 1 #include "Python.h" |
2 #include "structmember.h" | 2 #include "structmember.h" |
3 #include "osdefs.h" | 3 #include "osdefs.h" |
4 #include "marshal.h" | 4 #include "marshal.h" |
5 #include <time.h> | 5 #include <time.h> |
6 | 6 |
7 | 7 |
8 #define IS_SOURCE 0x0 | 8 #define IS_SOURCE 0x0 |
9 #define IS_BYTECODE 0x1 | 9 #define IS_BYTECODE 0x1 |
10 #define IS_PACKAGE 0x2 | 10 #define IS_PACKAGE 0x2 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 PyObject *files; /* dict with file info {path: toc_entry} */ | 42 PyObject *files; /* dict with file info {path: toc_entry} */ |
43 }; | 43 }; |
44 | 44 |
45 static PyObject *ZipImportError; | 45 static PyObject *ZipImportError; |
46 /* read_directory() cache */ | 46 /* read_directory() cache */ |
47 static PyObject *zip_directory_cache = NULL; | 47 static PyObject *zip_directory_cache = NULL; |
48 | 48 |
49 /* forward decls */ | 49 /* forward decls */ |
50 static PyObject *read_directory(PyObject *archive); | 50 static PyObject *read_directory(PyObject *archive); |
51 static PyObject *get_data(PyObject *archive, PyObject *toc_entry); | 51 static PyObject *get_data(PyObject *archive, PyObject *toc_entry); |
52 static PyObject *get_module_code(ZipImporter *self, char *fullname, | 52 static PyObject *get_module_code(ZipImporter *self, PyObject *fullname, |
53 int *p_ispackage, PyObject **p_modpath); | 53 int *p_ispackage, PyObject **p_modpath); |
54 | 54 |
55 | 55 |
56 #define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type) | 56 #define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type) |
57 | 57 |
58 | 58 |
59 /* zipimporter.__init__ | 59 /* zipimporter.__init__ |
60 Split the "subdirectory" from the Zip archive path, lookup a matching | 60 Split the "subdirectory" from the Zip archive path, lookup a matching |
61 entry in sys.path_importer_cache, fetch the file directory from there | 61 entry in sys.path_importer_cache, fetch the file directory from there |
62 if found, or else read it from the archive. */ | 62 if found, or else read it from the archive. */ |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 return PyUnicode_FromString("<zipimporter object \"???\">"); | 195 return PyUnicode_FromString("<zipimporter object \"???\">"); |
196 else if (self->prefix != NULL && PyUnicode_GET_SIZE(self->prefix) != 0) | 196 else if (self->prefix != NULL && PyUnicode_GET_SIZE(self->prefix) != 0) |
197 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">", | 197 return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">", |
198 self->archive, SEP, self->prefix); | 198 self->archive, SEP, self->prefix); |
199 else | 199 else |
200 return PyUnicode_FromFormat("<zipimporter object \"%U\">", | 200 return PyUnicode_FromFormat("<zipimporter object \"%U\">", |
201 self->archive); | 201 self->archive); |
202 } | 202 } |
203 | 203 |
204 /* return fullname.split(".")[-1] */ | 204 /* return fullname.split(".")[-1] */ |
205 static char * | 205 static PyObject * |
206 get_subname(char *fullname) | 206 get_subname(PyObject *fullname) |
207 { | 207 { |
208 char *subname = strrchr(fullname, '.'); | 208 Py_ssize_t len; |
209 if (subname == NULL) | 209 Py_UNICODE *subname; |
210 subname = fullname; | 210 subname = Py_UNICODE_strrchr(PyUnicode_AS_UNICODE(fullname), '.'); |
sasha
2011/01/21 19:27:49
This won't work if Py_UNICODE buffer in fullname i
| |
211 else | 211 if (subname == NULL) { |
212 Py_INCREF(fullname); | |
213 return fullname; | |
214 } else { | |
212 subname++; | 215 subname++; |
sasha
2011/01/21 19:27:49
The code below is just impenetrable. Please don'
| |
213 return subname; | 216 len = subname - PyUnicode_AS_UNICODE(fullname); |
217 len = PyUnicode_GET_SIZE(fullname) - len; | |
218 return PyUnicode_FromUnicode(subname, len); | |
219 } | |
214 } | 220 } |
215 | 221 |
216 /* Given a (sub)modulename, write the potential file path in the | 222 /* Given a (sub)modulename, write the potential file path in the |
217 archive (without extension) to the path buffer. Return the | 223 archive (without extension) to the path buffer. Return the |
218 length of the resulting string. */ | 224 length of the resulting string. */ |
219 static int | 225 static PyObject* |
220 make_filename(PyObject *prefix_obj, char *name, char *path, size_t pathsize) | 226 make_filename(PyObject *prefix, PyObject *name) |
221 { | 227 { |
222 size_t len; | 228 Py_UNICODE path[MAXPATHLEN + 1], *p; |
sasha
2011/01/21 19:27:49
Rather than using a fixed size buffer and low leve
| |
223 char *p; | 229 size_t pathsize = MAXPATHLEN + 1; |
224 PyObject *prefix; | 230 Py_ssize_t len; |
225 | 231 |
226 prefix = PyUnicode_EncodeFSDefault(prefix_obj); | 232 len = PyUnicode_GET_SIZE(prefix); |
227 if (prefix == NULL) | |
228 return -1; | |
229 len = PyBytes_GET_SIZE(prefix); | |
230 | 233 |
231 /* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */ | 234 /* self.prefix + name */ |
232 if (len + strlen(name) + 13 >= pathsize - 1) { | 235 if (len + PyUnicode_GET_SIZE(name) >= pathsize) { |
233 PyErr_SetString(ZipImportError, "path too long"); | 236 PyErr_SetString(ZipImportError, "path too long"); |
234 Py_DECREF(prefix); | 237 return NULL; |
235 return -1; | |
236 } | 238 } |
237 | 239 |
238 strcpy(path, PyBytes_AS_STRING(prefix)); | 240 Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(prefix)); |
239 Py_DECREF(prefix); | 241 Py_UNICODE_strcpy(path + len, PyUnicode_AS_UNICODE(name)); |
240 strcpy(path + len, name); | |
241 for (p = path + len; *p; p++) { | 242 for (p = path + len; *p; p++) { |
242 if (*p == '.') | 243 if (*p == '.') |
243 *p = SEP; | 244 *p = SEP; |
244 } | 245 } |
245 len += strlen(name); | 246 len += PyUnicode_GET_SIZE(name); |
246 assert(len < INT_MAX); | 247 return PyUnicode_FromUnicode(path, len); |
247 return (int)len; | |
248 } | 248 } |
249 | 249 |
250 enum zi_module_info { | 250 enum zi_module_info { |
251 MI_ERROR, | 251 MI_ERROR, |
252 MI_NOT_FOUND, | 252 MI_NOT_FOUND, |
253 MI_MODULE, | 253 MI_MODULE, |
254 MI_PACKAGE | 254 MI_PACKAGE |
255 }; | 255 }; |
256 | 256 |
257 /* Return some information about a module. */ | 257 /* Return some information about a module. */ |
258 static enum zi_module_info | 258 static enum zi_module_info |
259 get_module_info(ZipImporter *self, char *fullname) | 259 get_module_info(ZipImporter *self, PyObject *fullname) |
260 { | 260 { |
261 char *subname, path[MAXPATHLEN + 1]; | 261 PyObject *subname; |
262 int len; | 262 PyObject *path, *fullpath, *item; |
263 struct st_zip_searchorder *zso; | 263 struct st_zip_searchorder *zso; |
264 | 264 |
265 subname = get_subname(fullname); | 265 subname = get_subname(fullname); |
266 if (subname == NULL) | |
267 return MI_ERROR; | |
266 | 268 |
267 len = make_filename(self->prefix, subname, path, sizeof(path)); | 269 path = make_filename(self->prefix, subname); |
268 if (len < 0) | 270 Py_DECREF(subname); |
271 if (path == NULL) | |
269 return MI_ERROR; | 272 return MI_ERROR; |
270 | 273 |
271 for (zso = zip_searchorder; *zso->suffix; zso++) { | 274 for (zso = zip_searchorder; *zso->suffix; zso++) { |
272 strcpy(path + len, zso->suffix); | 275 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
273 if (PyDict_GetItemString(self->files, path) != NULL) { | 276 if (fullpath == NULL) { |
277 Py_DECREF(path); | |
278 return MI_ERROR; | |
279 } | |
280 item = PyDict_GetItem(self->files, fullpath); | |
281 Py_DECREF(fullpath); | |
282 if (item != NULL) { | |
283 Py_DECREF(path); | |
274 if (zso->type & IS_PACKAGE) | 284 if (zso->type & IS_PACKAGE) |
275 return MI_PACKAGE; | 285 return MI_PACKAGE; |
276 else | 286 else |
277 return MI_MODULE; | 287 return MI_MODULE; |
278 } | 288 } |
279 } | 289 } |
290 Py_DECREF(path); | |
280 return MI_NOT_FOUND; | 291 return MI_NOT_FOUND; |
281 } | 292 } |
282 | 293 |
283 /* Check whether we can satisfy the import of the module named by | 294 /* Check whether we can satisfy the import of the module named by |
284 'fullname'. Return self if we can, None if we can't. */ | 295 'fullname'. Return self if we can, None if we can't. */ |
285 static PyObject * | 296 static PyObject * |
286 zipimporter_find_module(PyObject *obj, PyObject *args) | 297 zipimporter_find_module(PyObject *obj, PyObject *args) |
287 { | 298 { |
288 ZipImporter *self = (ZipImporter *)obj; | 299 ZipImporter *self = (ZipImporter *)obj; |
289 PyObject *path = NULL; | 300 PyObject *path = NULL; |
290 char *fullname; | 301 PyObject *fullname; |
291 enum zi_module_info mi; | 302 enum zi_module_info mi; |
292 | 303 |
293 if (!PyArg_ParseTuple(args, "s|O:zipimporter.find_module", | 304 if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", |
294 &fullname, &path)) | 305 &fullname, &path)) |
295 return NULL; | 306 return NULL; |
296 | 307 |
297 mi = get_module_info(self, fullname); | 308 mi = get_module_info(self, fullname); |
298 if (mi == MI_ERROR) | 309 if (mi == MI_ERROR) |
299 return NULL; | 310 return NULL; |
300 if (mi == MI_NOT_FOUND) { | 311 if (mi == MI_NOT_FOUND) { |
301 Py_INCREF(Py_None); | 312 Py_INCREF(Py_None); |
302 return Py_None; | 313 return Py_None; |
303 } | 314 } |
304 Py_INCREF(self); | 315 Py_INCREF(self); |
305 return (PyObject *)self; | 316 return (PyObject *)self; |
306 } | 317 } |
307 | 318 |
308 /* Load and return the module named by 'fullname'. */ | 319 /* Load and return the module named by 'fullname'. */ |
309 static PyObject * | 320 static PyObject * |
310 zipimporter_load_module(PyObject *obj, PyObject *args) | 321 zipimporter_load_module(PyObject *obj, PyObject *args) |
311 { | 322 { |
312 ZipImporter *self = (ZipImporter *)obj; | 323 ZipImporter *self = (ZipImporter *)obj; |
313 PyObject *code = NULL, *mod, *dict; | 324 PyObject *code = NULL, *mod, *dict; |
314 char *fullname; | 325 PyObject *fullname; |
315 PyObject *modpath = NULL, *modpath_bytes; | 326 PyObject *modpath = NULL; |
316 int ispackage; | 327 int ispackage; |
317 | 328 |
318 if (!PyArg_ParseTuple(args, "s:zipimporter.load_module", | 329 if (!PyArg_ParseTuple(args, "U:zipimporter.load_module", |
319 &fullname)) | 330 &fullname)) |
320 return NULL; | 331 return NULL; |
321 | 332 |
322 code = get_module_code(self, fullname, &ispackage, &modpath); | 333 code = get_module_code(self, fullname, &ispackage, &modpath); |
323 if (code == NULL) | 334 if (code == NULL) |
324 goto error; | 335 goto error; |
325 | 336 |
326 mod = PyImport_AddModule(fullname); | 337 mod = PyImport_AddModuleUnicode(fullname); |
327 if (mod == NULL) | 338 if (mod == NULL) |
328 goto error; | 339 goto error; |
329 dict = PyModule_GetDict(mod); | 340 dict = PyModule_GetDict(mod); |
330 | 341 |
331 /* mod.__loader__ = self */ | 342 /* mod.__loader__ = self */ |
332 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) | 343 if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) |
333 goto error; | 344 goto error; |
334 | 345 |
335 if (ispackage) { | 346 if (ispackage) { |
336 /* add __path__ to the module *before* the code gets | 347 /* add __path__ to the module *before* the code gets |
337 executed */ | 348 executed */ |
338 PyObject *pkgpath, *fullpath; | 349 PyObject *pkgpath, *fullpath; |
339 char *subname = get_subname(fullname); | 350 PyObject *subname = get_subname(fullname); |
340 int err; | 351 int err; |
341 | 352 |
342 fullpath = PyUnicode_FromFormat("%U%c%U%s", | 353 fullpath = PyUnicode_FromFormat("%U%c%U%U", |
343 self->archive, SEP, | 354 self->archive, SEP, |
344 self->prefix, subname); | 355 self->prefix, subname); |
356 Py_DECREF(subname); | |
345 if (fullpath == NULL) | 357 if (fullpath == NULL) |
346 goto error; | 358 goto error; |
347 | 359 |
348 pkgpath = Py_BuildValue("[O]", fullpath); | 360 pkgpath = Py_BuildValue("[O]", fullpath); |
349 Py_DECREF(fullpath); | 361 Py_DECREF(fullpath); |
350 if (pkgpath == NULL) | 362 if (pkgpath == NULL) |
351 goto error; | 363 goto error; |
352 err = PyDict_SetItemString(dict, "__path__", pkgpath); | 364 err = PyDict_SetItemString(dict, "__path__", pkgpath); |
353 Py_DECREF(pkgpath); | 365 Py_DECREF(pkgpath); |
354 if (err != 0) | 366 if (err != 0) |
355 goto error; | 367 goto error; |
356 } | 368 } |
357 modpath_bytes = PyUnicode_EncodeFSDefault(modpath); | 369 mod = PyImport_ExecCodeModuleUnicode(fullname, code, modpath, NULL); |
358 if (modpath_bytes == NULL) | |
359 goto error; | |
360 mod = PyImport_ExecCodeModuleEx(fullname, code, | |
361 PyBytes_AS_STRING(modpath_bytes)); | |
362 Py_DECREF(modpath_bytes); | |
363 Py_CLEAR(code); | 370 Py_CLEAR(code); |
364 if (mod == NULL) | 371 if (mod == NULL) |
365 goto error; | 372 goto error; |
366 | 373 |
367 if (Py_VerboseFlag) | 374 if (Py_VerboseFlag) |
368 PySys_FormatStderr("import %s # loaded from Zip %U\n", | 375 PySys_FormatStderr("import %U # loaded from Zip %U\n", |
369 fullname, modpath); | 376 fullname, modpath); |
370 Py_DECREF(modpath); | 377 Py_DECREF(modpath); |
371 return mod; | 378 return mod; |
372 error: | 379 error: |
373 Py_XDECREF(code); | 380 Py_XDECREF(code); |
374 Py_XDECREF(modpath); | 381 Py_XDECREF(modpath); |
375 return NULL; | 382 return NULL; |
376 } | 383 } |
377 | 384 |
378 /* Return a string matching __file__ for the named module */ | 385 /* Return a string matching __file__ for the named module */ |
379 static PyObject * | 386 static PyObject * |
380 zipimporter_get_filename(PyObject *obj, PyObject *args) | 387 zipimporter_get_filename(PyObject *obj, PyObject *args) |
381 { | 388 { |
382 ZipImporter *self = (ZipImporter *)obj; | 389 ZipImporter *self = (ZipImporter *)obj; |
383 PyObject *code; | 390 PyObject *fullname, *code, *modpath; |
384 char *fullname; | |
385 PyObject *modpath; | |
386 int ispackage; | 391 int ispackage; |
387 | 392 |
388 if (!PyArg_ParseTuple(args, "s:zipimporter.get_filename", | 393 if (!PyArg_ParseTuple(args, "U:zipimporter.get_filename", |
389 &fullname)) | 394 &fullname)) |
390 return NULL; | 395 return NULL; |
391 | 396 |
392 /* Deciding the filename requires working out where the code | 397 /* Deciding the filename requires working out where the code |
393 would come from if the module was actually loaded */ | 398 would come from if the module was actually loaded */ |
394 code = get_module_code(self, fullname, &ispackage, &modpath); | 399 code = get_module_code(self, fullname, &ispackage, &modpath); |
395 if (code == NULL) | 400 if (code == NULL) |
396 return NULL; | 401 return NULL; |
397 Py_DECREF(code); /* Only need the path info */ | 402 Py_DECREF(code); /* Only need the path info */ |
398 | 403 |
399 return modpath; | 404 return modpath; |
400 } | 405 } |
401 | 406 |
402 /* Return a bool signifying whether the module is a package or not. */ | 407 /* Return a bool signifying whether the module is a package or not. */ |
403 static PyObject * | 408 static PyObject * |
404 zipimporter_is_package(PyObject *obj, PyObject *args) | 409 zipimporter_is_package(PyObject *obj, PyObject *args) |
405 { | 410 { |
406 ZipImporter *self = (ZipImporter *)obj; | 411 ZipImporter *self = (ZipImporter *)obj; |
407 char *fullname; | 412 PyObject *fullname; |
408 enum zi_module_info mi; | 413 enum zi_module_info mi; |
409 | 414 |
410 if (!PyArg_ParseTuple(args, "s:zipimporter.is_package", | 415 if (!PyArg_ParseTuple(args, "U:zipimporter.is_package", |
411 &fullname)) | 416 &fullname)) |
412 return NULL; | 417 return NULL; |
413 | 418 |
414 mi = get_module_info(self, fullname); | 419 mi = get_module_info(self, fullname); |
415 if (mi == MI_ERROR) | 420 if (mi == MI_ERROR) |
416 return NULL; | 421 return NULL; |
417 if (mi == MI_NOT_FOUND) { | 422 if (mi == MI_NOT_FOUND) { |
418 PyErr_Format(ZipImportError, "can't find module '%s'", fullname); | 423 PyErr_Format(ZipImportError, "can't find module %R", fullname); |
419 return NULL; | 424 return NULL; |
420 } | 425 } |
421 return PyBool_FromLong(mi == MI_PACKAGE); | 426 return PyBool_FromLong(mi == MI_PACKAGE); |
422 } | 427 } |
423 | 428 |
424 static PyObject * | 429 static PyObject * |
425 zipimporter_get_data(PyObject *obj, PyObject *args) | 430 zipimporter_get_data(PyObject *obj, PyObject *args) |
426 { | 431 { |
427 ZipImporter *self = (ZipImporter *)obj; | 432 ZipImporter *self = (ZipImporter *)obj; |
428 PyObject *pathobj, *key; | 433 PyObject *pathobj, *key; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
470 return NULL; | 475 return NULL; |
471 } | 476 } |
472 Py_DECREF(key); | 477 Py_DECREF(key); |
473 return get_data(self->archive, toc_entry); | 478 return get_data(self->archive, toc_entry); |
474 } | 479 } |
475 | 480 |
476 static PyObject * | 481 static PyObject * |
477 zipimporter_get_code(PyObject *obj, PyObject *args) | 482 zipimporter_get_code(PyObject *obj, PyObject *args) |
478 { | 483 { |
479 ZipImporter *self = (ZipImporter *)obj; | 484 ZipImporter *self = (ZipImporter *)obj; |
480 char *fullname; | 485 PyObject *fullname; |
481 | 486 |
482 if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname)) | 487 if (!PyArg_ParseTuple(args, "U:zipimporter.get_code", &fullname)) |
483 return NULL; | 488 return NULL; |
484 | 489 |
485 return get_module_code(self, fullname, NULL, NULL); | 490 return get_module_code(self, fullname, NULL, NULL); |
486 } | 491 } |
487 | 492 |
488 static PyObject * | 493 static PyObject * |
489 zipimporter_get_source(PyObject *obj, PyObject *args) | 494 zipimporter_get_source(PyObject *obj, PyObject *args) |
490 { | 495 { |
491 ZipImporter *self = (ZipImporter *)obj; | 496 ZipImporter *self = (ZipImporter *)obj; |
492 PyObject *toc_entry; | 497 PyObject *toc_entry; |
493 char *fullname, *subname, path[MAXPATHLEN+1]; | 498 PyObject *fullname, *subname, *path, *fullpath; |
494 int len; | |
495 enum zi_module_info mi; | 499 enum zi_module_info mi; |
496 | 500 |
497 if (!PyArg_ParseTuple(args, "s:zipimporter.get_source", &fullname)) | 501 if (!PyArg_ParseTuple(args, "U:zipimporter.get_source", &fullname)) |
498 return NULL; | 502 return NULL; |
499 | 503 |
500 mi = get_module_info(self, fullname); | 504 mi = get_module_info(self, fullname); |
501 if (mi == MI_ERROR) | 505 if (mi == MI_ERROR) |
502 return NULL; | 506 return NULL; |
503 if (mi == MI_NOT_FOUND) { | 507 if (mi == MI_NOT_FOUND) { |
504 PyErr_Format(ZipImportError, "can't find module '%s'", fullname); | 508 PyErr_Format(ZipImportError, "can't find module %R", fullname); |
505 return NULL; | 509 return NULL; |
506 } | 510 } |
511 | |
507 subname = get_subname(fullname); | 512 subname = get_subname(fullname); |
508 | 513 if (subname == NULL) |
509 len = make_filename(self->prefix, subname, path, sizeof(path)); | |
510 if (len < 0) | |
511 return NULL; | 514 return NULL; |
512 | 515 |
513 if (mi == MI_PACKAGE) { | 516 path = make_filename(self->prefix, subname); |
514 path[len] = SEP; | 517 Py_DECREF(subname); |
515 strcpy(path + len + 1, "__init__.py"); | 518 if (path == NULL) |
516 } | 519 return NULL; |
520 | |
521 if (mi == MI_PACKAGE) | |
522 fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP); | |
517 else | 523 else |
518 strcpy(path + len, ".py"); | 524 fullpath = PyUnicode_FromFormat("%U.py", path); |
525 Py_DECREF(path); | |
526 if (fullpath == NULL) | |
527 return NULL; | |
519 | 528 |
520 toc_entry = PyDict_GetItemString(self->files, path); | 529 toc_entry = PyDict_GetItem(self->files, fullpath); |
530 Py_DECREF(fullpath); | |
521 if (toc_entry != NULL) { | 531 if (toc_entry != NULL) { |
522 PyObject *res, *bytes; | 532 PyObject *res, *bytes; |
523 bytes = get_data(self->archive, toc_entry); | 533 bytes = get_data(self->archive, toc_entry); |
524 if (bytes == NULL) | 534 if (bytes == NULL) |
525 return NULL; | 535 return NULL; |
526 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes), | 536 res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes), |
527 PyBytes_GET_SIZE(bytes)); | 537 PyBytes_GET_SIZE(bytes)); |
528 Py_DECREF(bytes); | 538 Py_DECREF(bytes); |
529 return res; | 539 return res; |
530 } | 540 } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 file_offset, # offset of file header from start of archive | 711 file_offset, # offset of file header from start of archive |
702 time, # mod time of file (in dos format) | 712 time, # mod time of file (in dos format) |
703 date, # mod data of file (in dos format) | 713 date, # mod data of file (in dos format) |
704 crc, # crc checksum of the data | 714 crc, # crc checksum of the data |
705 ) | 715 ) |
706 | 716 |
707 Directories can be recognized by the trailing SEP in the name, | 717 Directories can be recognized by the trailing SEP in the name, |
708 data_size and file_offset are 0. | 718 data_size and file_offset are 0. |
709 */ | 719 */ |
710 static PyObject * | 720 static PyObject * |
711 read_directory(PyObject *archive_obj) | 721 read_directory(PyObject *archive) |
712 { | 722 { |
713 /* FIXME: work on Py_UNICODE* instead of char* */ | |
714 PyObject *files = NULL; | 723 PyObject *files = NULL; |
715 FILE *fp; | 724 FILE *fp; |
716 unsigned short flags; | 725 unsigned short flags; |
717 long compress, crc, data_size, file_size, file_offset, date, time; | 726 long compress, crc, data_size, file_size, file_offset, date, time; |
718 long header_offset, name_size, header_size, header_position; | 727 long header_offset, name_size, header_size, header_position; |
719 long i, l, count; | 728 long i, l, count; |
720 size_t length; | 729 size_t length; |
721 Py_UNICODE path[MAXPATHLEN + 5]; | 730 Py_UNICODE path[MAXPATHLEN + 5]; |
722 char name[MAXPATHLEN + 5]; | 731 char name[MAXPATHLEN + 5]; |
723 PyObject *nameobj = NULL; | 732 PyObject *nameobj = NULL; |
724 char *p, endof_central_dir[22]; | 733 char *p, endof_central_dir[22]; |
725 long arc_offset; /* offset from beginning of file to start of zip-archive */ | 734 long arc_offset; /* offset from beginning of file to start of zip-archive */ |
726 PyObject *pathobj; | 735 PyObject *pathobj; |
727 const char *charset; | 736 const char *charset; |
728 | 737 |
729 if (PyUnicode_GET_SIZE(archive_obj) > MAXPATHLEN) { | 738 if (PyUnicode_GET_SIZE(archive) > MAXPATHLEN) { |
730 PyErr_SetString(PyExc_OverflowError, | 739 PyErr_SetString(PyExc_OverflowError, |
731 "Zip path name is too long"); | 740 "Zip path name is too long"); |
732 return NULL; | 741 return NULL; |
733 } | 742 } |
734 Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(archive_obj)); | 743 Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(archive)); |
735 | 744 |
736 fp = _Py_fopen(archive_obj, "rb"); | 745 fp = _Py_fopen(archive, "rb"); |
737 if (fp == NULL) { | 746 if (fp == NULL) { |
738 PyErr_Format(ZipImportError, "can't open Zip file: '%U'", archive_obj); | 747 PyErr_Format(ZipImportError, "can't open Zip file: %R", archive); |
739 return NULL; | 748 return NULL; |
740 } | 749 } |
741 fseek(fp, -22, SEEK_END); | 750 fseek(fp, -22, SEEK_END); |
742 header_position = ftell(fp); | 751 header_position = ftell(fp); |
743 if (fread(endof_central_dir, 1, 22, fp) != 22) { | 752 if (fread(endof_central_dir, 1, 22, fp) != 22) { |
744 fclose(fp); | 753 fclose(fp); |
745 PyErr_Format(ZipImportError, "can't read Zip file: '%U'", archive_obj); | 754 PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); |
746 return NULL; | 755 return NULL; |
747 } | 756 } |
748 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) { | 757 if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) { |
749 /* Bad: End of Central Dir signature */ | 758 /* Bad: End of Central Dir signature */ |
750 fclose(fp); | 759 fclose(fp); |
751 PyErr_Format(ZipImportError, "not a Zip file: '%U'", archive_obj); | 760 PyErr_Format(ZipImportError, "not a Zip file: %R", archive); |
752 return NULL; | 761 return NULL; |
753 } | 762 } |
754 | 763 |
755 header_size = get_long((unsigned char *)endof_central_dir + 12); | 764 header_size = get_long((unsigned char *)endof_central_dir + 12); |
756 header_offset = get_long((unsigned char *)endof_central_dir + 16); | 765 header_offset = get_long((unsigned char *)endof_central_dir + 16); |
757 arc_offset = header_position - header_offset - header_size; | 766 arc_offset = header_position - header_offset - header_size; |
758 header_offset += arc_offset; | 767 header_offset += arc_offset; |
759 | 768 |
760 files = PyDict_New(); | 769 files = PyDict_New(); |
761 if (files == NULL) | 770 if (files == NULL) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
801 *p = 0; /* Add terminating null byte */ | 810 *p = 0; /* Add terminating null byte */ |
802 header_offset += header_size; | 811 header_offset += header_size; |
803 | 812 |
804 if (flags & 0x0800) | 813 if (flags & 0x0800) |
805 charset = "utf-8"; | 814 charset = "utf-8"; |
806 else | 815 else |
807 charset = "cp437"; | 816 charset = "cp437"; |
808 nameobj = PyUnicode_Decode(name, name_size, charset, NULL); | 817 nameobj = PyUnicode_Decode(name, name_size, charset, NULL); |
809 if (nameobj == NULL) | 818 if (nameobj == NULL) |
810 goto error; | 819 goto error; |
811 Py_UNICODE_strncpy(path + length + 1, PyUnicode_AS_UNICODE(nameobj), MAX PATHLEN - length - 1); | 820 Py_UNICODE_strncpy(path + length + 1, |
821 PyUnicode_AS_UNICODE(nameobj), | |
822 MAXPATHLEN - length - 1); | |
812 | 823 |
813 pathobj = PyUnicode_FromUnicode(path, Py_UNICODE_strlen(path)); | 824 pathobj = PyUnicode_FromUnicode(path, Py_UNICODE_strlen(path)); |
814 if (pathobj == NULL) | 825 if (pathobj == NULL) |
815 goto error; | 826 goto error; |
816 t = Py_BuildValue("Niiiiiii", pathobj, compress, data_size, | 827 t = Py_BuildValue("Niiiiiii", pathobj, compress, data_size, |
817 file_size, file_offset, time, date, crc); | 828 file_size, file_offset, time, date, crc); |
818 if (t == NULL) | 829 if (t == NULL) |
819 goto error; | 830 goto error; |
820 err = PyDict_SetItem(files, nameobj, t); | 831 err = PyDict_SetItem(files, nameobj, t); |
821 Py_CLEAR(nameobj); | 832 Py_CLEAR(nameobj); |
822 Py_DECREF(t); | 833 Py_DECREF(t); |
823 if (err != 0) | 834 if (err != 0) |
824 goto error; | 835 goto error; |
825 count++; | 836 count++; |
826 } | 837 } |
827 fclose(fp); | 838 fclose(fp); |
828 if (Py_VerboseFlag) | 839 if (Py_VerboseFlag) |
829 PySys_FormatStderr("# zipimport: found %ld names in %U\n", | 840 PySys_FormatStderr("# zipimport: found %ld names in %U\n", |
830 count, archive_obj); | 841 count, archive); |
831 return files; | 842 return files; |
832 error: | 843 error: |
833 fclose(fp); | 844 fclose(fp); |
834 Py_XDECREF(files); | 845 Py_XDECREF(files); |
835 Py_XDECREF(nameobj); | 846 Py_XDECREF(nameobj); |
836 return NULL; | 847 return NULL; |
837 } | 848 } |
838 | 849 |
839 /* Return the zlib.decompress function object, or NULL if zlib couldn't | 850 /* Return the zlib.decompress function object, or NULL if zlib couldn't |
840 be imported. The function is cached when found, so subsequent calls | 851 be imported. The function is cached when found, so subsequent calls |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
974 /* dostime only stores even seconds, so be lenient */ | 985 /* dostime only stores even seconds, so be lenient */ |
975 return d <= 1; | 986 return d <= 1; |
976 } | 987 } |
977 | 988 |
978 /* Given the contents of a .py[co] file in a buffer, unmarshal the data | 989 /* Given the contents of a .py[co] file in a buffer, unmarshal the data |
979 and return the code object. Return None if it the magic word doesn't | 990 and return the code object. Return None if it the magic word doesn't |
980 match (we do this instead of raising an exception as we fall back | 991 match (we do this instead of raising an exception as we fall back |
981 to .py if available and we don't want to mask other errors). | 992 to .py if available and we don't want to mask other errors). |
982 Returns a new reference. */ | 993 Returns a new reference. */ |
983 static PyObject * | 994 static PyObject * |
984 unmarshal_code(char *pathname, PyObject *data, time_t mtime) | 995 unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime) |
985 { | 996 { |
986 PyObject *code; | 997 PyObject *code; |
987 char *buf = PyBytes_AsString(data); | 998 char *buf = PyBytes_AsString(data); |
988 Py_ssize_t size = PyBytes_Size(data); | 999 Py_ssize_t size = PyBytes_Size(data); |
989 | 1000 |
990 if (size <= 9) { | 1001 if (size <= 9) { |
991 PyErr_SetString(ZipImportError, | 1002 PyErr_SetString(ZipImportError, |
992 "bad pyc data"); | 1003 "bad pyc data"); |
993 return NULL; | 1004 return NULL; |
994 } | 1005 } |
995 | 1006 |
996 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) { | 1007 if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) { |
997 if (Py_VerboseFlag) | 1008 if (Py_VerboseFlag) |
998 PySys_WriteStderr("# %s has bad magic\n", | 1009 PySys_FormatStderr("# %U has bad magic\n", |
999 pathname); | 1010 pathname); |
1000 Py_INCREF(Py_None); | 1011 Py_INCREF(Py_None); |
1001 return Py_None; /* signal caller to try alternative */ | 1012 return Py_None; /* signal caller to try alternative */ |
1002 } | 1013 } |
1003 | 1014 |
1004 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4), | 1015 if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4), |
1005 mtime)) { | 1016 mtime)) { |
1006 if (Py_VerboseFlag) | 1017 if (Py_VerboseFlag) |
1007 PySys_WriteStderr("# %s has bad mtime\n", | 1018 PySys_FormatStderr("# %U has bad mtime\n", |
1008 pathname); | 1019 pathname); |
1009 Py_INCREF(Py_None); | 1020 Py_INCREF(Py_None); |
1010 return Py_None; /* signal caller to try alternative */ | 1021 return Py_None; /* signal caller to try alternative */ |
1011 } | 1022 } |
1012 | 1023 |
1013 code = PyMarshal_ReadObjectFromString(buf + 8, size - 8); | 1024 code = PyMarshal_ReadObjectFromString(buf + 8, size - 8); |
1014 if (code == NULL) | 1025 if (code == NULL) |
1015 return NULL; | 1026 return NULL; |
1016 if (!PyCode_Check(code)) { | 1027 if (!PyCode_Check(code)) { |
1017 Py_DECREF(code); | 1028 Py_DECREF(code); |
1018 PyErr_Format(PyExc_TypeError, | 1029 PyErr_Format(PyExc_TypeError, |
1019 "compiled module %s is not a code object", | 1030 "compiled module %U is not a code object", |
1020 pathname); | 1031 pathname); |
1021 return NULL; | 1032 return NULL; |
1022 } | 1033 } |
1023 return code; | 1034 return code; |
1024 } | 1035 } |
1025 | 1036 |
1026 /* Replace any occurances of "\r\n?" in the input string with "\n". | 1037 /* Replace any occurances of "\r\n?" in the input string with "\n". |
1027 This converts DOS and Mac line endings to Unix line endings. | 1038 This converts DOS and Mac line endings to Unix line endings. |
1028 Also append a trailing "\n" to be compatible with | 1039 Also append a trailing "\n" to be compatible with |
1029 PyParser_SimpleParseFile(). Returns a new reference. */ | 1040 PyParser_SimpleParseFile(). Returns a new reference. */ |
(...skipping 30 matching lines...) Expand all Loading... | |
1060 *q++ = '\n'; /* add trailing \n */ | 1071 *q++ = '\n'; /* add trailing \n */ |
1061 *q = '\0'; | 1072 *q = '\0'; |
1062 fixed_source = PyBytes_FromStringAndSize(buf, len + 2); | 1073 fixed_source = PyBytes_FromStringAndSize(buf, len + 2); |
1063 PyMem_Free(buf); | 1074 PyMem_Free(buf); |
1064 return fixed_source; | 1075 return fixed_source; |
1065 } | 1076 } |
1066 | 1077 |
1067 /* Given a string buffer containing Python source code, compile it | 1078 /* Given a string buffer containing Python source code, compile it |
1068 return and return a code object as a new reference. */ | 1079 return and return a code object as a new reference. */ |
1069 static PyObject * | 1080 static PyObject * |
1070 compile_source(char *pathname, PyObject *source) | 1081 compile_source(PyObject *pathname, PyObject *source) |
1071 { | 1082 { |
1072 PyObject *code, *fixed_source; | 1083 PyObject *code, *fixed_source, *pathbytes; |
1084 | |
1085 pathbytes = PyUnicode_EncodeFSDefault(pathname); | |
1086 if (pathbytes == NULL) | |
1087 return NULL; | |
1073 | 1088 |
1074 fixed_source = normalize_line_endings(source); | 1089 fixed_source = normalize_line_endings(source); |
1075 if (fixed_source == NULL) | 1090 if (fixed_source == NULL) { |
1091 Py_DECREF(pathbytes); | |
1076 return NULL; | 1092 return NULL; |
1093 } | |
1077 | 1094 |
1078 code = Py_CompileString(PyBytes_AsString(fixed_source), pathname, | 1095 code = Py_CompileString(PyBytes_AsString(fixed_source), |
1096 PyBytes_AsString(pathbytes), | |
1079 Py_file_input); | 1097 Py_file_input); |
1098 Py_DECREF(pathbytes); | |
1080 Py_DECREF(fixed_source); | 1099 Py_DECREF(fixed_source); |
1081 return code; | 1100 return code; |
1082 } | 1101 } |
1083 | 1102 |
1084 /* Convert the date/time values found in the Zip archive to a value | 1103 /* Convert the date/time values found in the Zip archive to a value |
1085 that's compatible with the time stamp stored in .pyc files. */ | 1104 that's compatible with the time stamp stored in .pyc files. */ |
1086 static time_t | 1105 static time_t |
1087 parse_dostime(int dostime, int dosdate) | 1106 parse_dostime(int dostime, int dosdate) |
1088 { | 1107 { |
1089 struct tm stm; | 1108 struct tm stm; |
1090 | 1109 |
1091 memset((void *) &stm, '\0', sizeof(stm)); | 1110 memset((void *) &stm, '\0', sizeof(stm)); |
1092 | 1111 |
1093 stm.tm_sec = (dostime & 0x1f) * 2; | 1112 stm.tm_sec = (dostime & 0x1f) * 2; |
1094 stm.tm_min = (dostime >> 5) & 0x3f; | 1113 stm.tm_min = (dostime >> 5) & 0x3f; |
1095 stm.tm_hour = (dostime >> 11) & 0x1f; | 1114 stm.tm_hour = (dostime >> 11) & 0x1f; |
1096 stm.tm_mday = dosdate & 0x1f; | 1115 stm.tm_mday = dosdate & 0x1f; |
1097 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1; | 1116 stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1; |
1098 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80; | 1117 stm.tm_year = ((dosdate >> 9) & 0x7f) + 80; |
1099 stm.tm_isdst = -1; /* wday/yday is ignored */ | 1118 stm.tm_isdst = -1; /* wday/yday is ignored */ |
1100 | 1119 |
1101 return mktime(&stm); | 1120 return mktime(&stm); |
1102 } | 1121 } |
1103 | 1122 |
1104 /* Given a path to a .pyc or .pyo file in the archive, return the | 1123 /* Given a path to a .pyc or .pyo file in the archive, return the |
1105 modifictaion time of the matching .py file, or 0 if no source | 1124 modifictaion time of the matching .py file, or 0 if no source |
1106 is available. */ | 1125 is available. */ |
1107 static time_t | 1126 static time_t |
1108 get_mtime_of_source(ZipImporter *self, char *path) | 1127 get_mtime_of_source(ZipImporter *self, PyObject *path) |
1109 { | 1128 { |
1110 PyObject *toc_entry; | 1129 PyObject *toc_entry, *stripped; |
1111 time_t mtime = 0; | 1130 time_t mtime; |
1112 Py_ssize_t lastchar = strlen(path) - 1; | 1131 |
1113 char savechar = path[lastchar]; | 1132 /* strip 'c' or 'o' from *.py[co] */ |
1114 path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */ | 1133 stripped = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(path), |
1115 toc_entry = PyDict_GetItemString(self->files, path); | 1134 PyUnicode_GET_SIZE(path) - 1); |
1135 if (stripped == NULL) | |
1136 return (time_t)-1; | |
1137 | |
1138 toc_entry = PyDict_GetItem(self->files, stripped); | |
1139 Py_DECREF(stripped); | |
1116 if (toc_entry != NULL && PyTuple_Check(toc_entry) && | 1140 if (toc_entry != NULL && PyTuple_Check(toc_entry) && |
1117 PyTuple_Size(toc_entry) == 8) { | 1141 PyTuple_Size(toc_entry) == 8) { |
1118 /* fetch the time stamp of the .py file for comparison | 1142 /* fetch the time stamp of the .py file for comparison |
1119 with an embedded pyc time stamp */ | 1143 with an embedded pyc time stamp */ |
1120 int time, date; | 1144 int time, date; |
1121 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5)); | 1145 time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5)); |
1122 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6)); | 1146 date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6)); |
1123 mtime = parse_dostime(time, date); | 1147 mtime = parse_dostime(time, date); |
1124 } | 1148 } else |
1125 path[lastchar] = savechar; | 1149 mtime = 0; |
1126 return mtime; | 1150 return mtime; |
1127 } | 1151 } |
1128 | 1152 |
1129 /* Return the code object for the module named by 'fullname' from the | 1153 /* Return the code object for the module named by 'fullname' from the |
1130 Zip archive as a new reference. */ | 1154 Zip archive as a new reference. */ |
1131 static PyObject * | 1155 static PyObject * |
1132 get_code_from_data(ZipImporter *self, int ispackage, int isbytecode, | 1156 get_code_from_data(ZipImporter *self, int ispackage, int isbytecode, |
1133 time_t mtime, PyObject *toc_entry) | 1157 time_t mtime, PyObject *toc_entry) |
1134 { | 1158 { |
1135 PyObject *data, *code; | 1159 PyObject *data, *modpath, *code; |
1136 PyObject *modpath; | |
1137 | 1160 |
1138 data = get_data(self->archive, toc_entry); | 1161 data = get_data(self->archive, toc_entry); |
1139 if (data == NULL) | 1162 if (data == NULL) |
1140 return NULL; | 1163 return NULL; |
1141 | 1164 |
1142 modpath = PyUnicode_EncodeFSDefault(PyTuple_GetItem(toc_entry, 0)); | 1165 modpath = PyTuple_GetItem(toc_entry, 0); |
1143 if (modpath == NULL) { | |
1144 Py_DECREF(data); | |
1145 return NULL; | |
1146 } | |
1147 | |
1148 if (isbytecode) | 1166 if (isbytecode) |
1149 code = unmarshal_code(PyBytes_AS_STRING(modpath), data, mtime); | 1167 code = unmarshal_code(modpath, data, mtime); |
1150 else | 1168 else |
1151 code = compile_source(PyBytes_AS_STRING(modpath), data); | 1169 code = compile_source(modpath, data); |
1152 Py_DECREF(modpath); | |
1153 Py_DECREF(data); | 1170 Py_DECREF(data); |
1154 return code; | 1171 return code; |
1155 } | 1172 } |
1156 | 1173 |
1157 /* Get the code object assoiciated with the module specified by | 1174 /* Get the code object assoiciated with the module specified by |
1158 'fullname'. */ | 1175 'fullname'. */ |
1159 static PyObject * | 1176 static PyObject * |
1160 get_module_code(ZipImporter *self, char *fullname, | 1177 get_module_code(ZipImporter *self, PyObject *fullname, |
1161 int *p_ispackage, PyObject **p_modpath) | 1178 int *p_ispackage, PyObject **p_modpath) |
1162 { | 1179 { |
1163 PyObject *toc_entry; | 1180 PyObject *code, *toc_entry, *subname; |
1164 char *subname, path[MAXPATHLEN + 1]; | 1181 PyObject *path, *fullpath; |
1165 int len; | |
1166 struct st_zip_searchorder *zso; | 1182 struct st_zip_searchorder *zso; |
1167 | 1183 |
1168 subname = get_subname(fullname); | 1184 subname = get_subname(fullname); |
1185 if (subname == NULL) | |
1186 return NULL; | |
1169 | 1187 |
1170 len = make_filename(self->prefix, subname, path, sizeof(path)); | 1188 path = make_filename(self->prefix, subname); |
1171 if (len < 0) | 1189 Py_DECREF(subname); |
1190 if (path == NULL) | |
1172 return NULL; | 1191 return NULL; |
1173 | 1192 |
1174 for (zso = zip_searchorder; *zso->suffix; zso++) { | 1193 for (zso = zip_searchorder; *zso->suffix; zso++) { |
1175 PyObject *code = NULL; | 1194 code = NULL; |
1176 | 1195 |
1177 strcpy(path + len, zso->suffix); | 1196 fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix); |
1197 if (fullpath == NULL) | |
1198 goto error; | |
1199 | |
1178 if (Py_VerboseFlag > 1) | 1200 if (Py_VerboseFlag > 1) |
1179 PySys_FormatStderr("# trying %U%c%s\n", | 1201 PySys_FormatStderr("# trying %U%c%U\n", |
1180 self->archive, (int)SEP, path); | 1202 self->archive, (int)SEP, fullpath); |
1181 toc_entry = PyDict_GetItemString(self->files, path); | 1203 toc_entry = PyDict_GetItem(self->files, fullpath); |
1182 if (toc_entry != NULL) { | 1204 if (toc_entry != NULL) { |
1183 time_t mtime = 0; | 1205 time_t mtime = 0; |
1184 int ispackage = zso->type & IS_PACKAGE; | 1206 int ispackage = zso->type & IS_PACKAGE; |
1185 int isbytecode = zso->type & IS_BYTECODE; | 1207 int isbytecode = zso->type & IS_BYTECODE; |
1186 | 1208 |
1187 if (isbytecode) | 1209 if (isbytecode) { |
1188 mtime = get_mtime_of_source(self, path); | 1210 mtime = get_mtime_of_source(self, fullpath); |
1211 if (mtime == (time_t)-1 && PyErr_Occurred()) { | |
1212 goto error; | |
sasha
2011/01/21 19:27:49
Do you need two goto labels? It may be better to
| |
1213 } | |
1214 } | |
1215 Py_CLEAR(fullpath); | |
1189 if (p_ispackage != NULL) | 1216 if (p_ispackage != NULL) |
1190 *p_ispackage = ispackage; | 1217 *p_ispackage = ispackage; |
1191 code = get_code_from_data(self, ispackage, | 1218 code = get_code_from_data(self, ispackage, |
1192 isbytecode, mtime, | 1219 isbytecode, mtime, |
1193 toc_entry); | 1220 toc_entry); |
1194 if (code == Py_None) { | 1221 if (code == Py_None) { |
1195 /* bad magic number or non-matching mtime | 1222 /* bad magic number or non-matching mtime |
1196 in byte code, try next */ | 1223 in byte code, try next */ |
1197 Py_DECREF(code); | 1224 Py_DECREF(code); |
1198 continue; | 1225 continue; |
1199 } | 1226 } |
1200 if (code != NULL && p_modpath != NULL) { | 1227 if (code != NULL && p_modpath != NULL) { |
1201 *p_modpath = PyTuple_GetItem(toc_entry, 0); | 1228 *p_modpath = PyTuple_GetItem(toc_entry, 0); |
1202 Py_INCREF(*p_modpath); | 1229 Py_INCREF(*p_modpath); |
1203 } | 1230 } |
1204 return code; | 1231 goto finally; |
1205 } | 1232 } |
1233 else | |
1234 Py_CLEAR(fullpath); | |
1206 } | 1235 } |
1207 PyErr_Format(ZipImportError, "can't find module '%s'", fullname); | 1236 PyErr_Format(ZipImportError, "can't find module %R", fullname); |
1208 return NULL; | 1237 error: |
1238 code = NULL; | |
1239 finally: | |
1240 Py_DECREF(path); | |
1241 Py_XDECREF(fullpath); | |
1242 return code; | |
1209 } | 1243 } |
1210 | 1244 |
1211 | 1245 |
1212 /* Module init */ | 1246 /* Module init */ |
1213 | 1247 |
1214 PyDoc_STRVAR(zipimport_doc, | 1248 PyDoc_STRVAR(zipimport_doc, |
1215 "zipimport provides support for importing Python modules from Zip archives.\n\ | 1249 "zipimport provides support for importing Python modules from Zip archives.\n\ |
1216 \n\ | 1250 \n\ |
1217 This module exports three objects:\n\ | 1251 This module exports three objects:\n\ |
1218 - zipimporter: a class; its constructor takes a path to a Zip archive.\n\ | 1252 - zipimporter: a class; its constructor takes a path to a Zip archive.\n\ |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1281 | 1315 |
1282 zip_directory_cache = PyDict_New(); | 1316 zip_directory_cache = PyDict_New(); |
1283 if (zip_directory_cache == NULL) | 1317 if (zip_directory_cache == NULL) |
1284 return NULL; | 1318 return NULL; |
1285 Py_INCREF(zip_directory_cache); | 1319 Py_INCREF(zip_directory_cache); |
1286 if (PyModule_AddObject(mod, "_zip_directory_cache", | 1320 if (PyModule_AddObject(mod, "_zip_directory_cache", |
1287 zip_directory_cache) < 0) | 1321 zip_directory_cache) < 0) |
1288 return NULL; | 1322 return NULL; |
1289 return mod; | 1323 return mod; |
1290 } | 1324 } |
OLD | NEW |