Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c8ed0a

Browse files
author
Victor Stinner
committed
zipimport: fix "can't find module ..." error message
I cannot use %U: fullname is a bytes object, not an unicode object. %A format cannot be used, it adds 'b' (bytes) prefix. So create cant_find_module() function to decode the filename and raise the error message.
1 parent 9a90900 commit 8c8ed0a

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

Modules/zipimport.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,20 @@ zipimporter_get_filename(PyObject *obj, PyObject *args)
399399
return modpath;
400400
}
401401

402+
static void
403+
cant_find_module(PyObject *bytes)
404+
{
405+
PyObject *unicode = PyUnicode_DecodeFSDefaultAndSize(
406+
PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
407+
if (unicode != NULL) {
408+
PyErr_Format(ZipImportError, "can't find module %U",
409+
unicode);
410+
Py_DECREF(unicode);
411+
}
412+
else
413+
PyErr_Format(ZipImportError, "can't find module");
414+
}
415+
402416
/* Return a bool signifying whether the module is a package or not. */
403417
static PyObject *
404418
zipimporter_is_package(PyObject *obj, PyObject *args)
@@ -415,8 +429,7 @@ zipimporter_is_package(PyObject *obj, PyObject *args)
415429
if (mi == MI_ERROR)
416430
goto error;
417431
if (mi == MI_NOT_FOUND) {
418-
PyErr_Format(ZipImportError, "can't find module '%.200U'",
419-
fullname);
432+
cant_find_module(fullname);
420433
goto error;
421434
}
422435
Py_DECREF(fullname);
@@ -511,8 +524,7 @@ zipimporter_get_source(PyObject *obj, PyObject *args)
511524
return NULL;
512525
}
513526
if (mi == MI_NOT_FOUND) {
514-
PyErr_Format(ZipImportError, "can't find module '%.200U'",
515-
fullname);
527+
cant_find_module(fullname);
516528
Py_DECREF(fullname);
517529
return NULL;
518530
}

0 commit comments

Comments
 (0)