Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,24 @@ def test_import_time(self):
assert_python_failure('-X', 'importtime=-1', '-c', code)
assert_python_failure('-X', 'importtime=3', '-c', code)

def test_import_time_unencodable_module_name(self):
code = textwrap.dedent("""
import sys, types
name = 'mod\\ud800'
sys.modules[name] = types.ModuleType(name)
__import__(name)
try:
__import__('nonexistent\\ud800')
except ModuleNotFoundError:
pass
""")
res = assert_python_ok('-X', 'importtime=2', '-c', code)
res_err = res.err.decode('utf-8')
self.assertRegex(res_err,
r'import time: cached\s* \| cached\s* \| mod\\ud800')
self.assertRegex(res_err,
r'import time: \s*\d+ \| \s*\d+ \| \s*nonexistent\\ud800')

def res2int(self, res):
out = res.out.strip().decode("utf-8")
return tuple(int(i) for i in out.split())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash when importing a module whose name contains characters that
cannot be encoded to UTF-8 (such as lone surrogates) while :option:`-X
importtime <-X>` is enabled.
23 changes: 21 additions & 2 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ _PyImport_ClearLazyModules(PyInterpreterState *interp)
Py_CLEAR(LAZY_PENDING_SUBMODULES(interp));
}

static PyObject *
get_importtime_name(PyObject *name)
{
PyObject *exc = PyErr_GetRaisedException();
PyObject *encoded = PyUnicode_AsEncodedString(name, "utf-8",
"backslashreplace");
if (encoded == NULL) {
PyErr_Clear();
}
PyErr_SetRaisedException(exc);
return encoded;
}

static int
import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name)
{
Expand Down Expand Up @@ -323,8 +336,11 @@ import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *n
if (_PyInterpreterState_GetConfig(interp)->import_time == 2) {
_IMPORT_TIME_HEADER(interp);
#define import_level FIND_AND_LOAD(interp).import_level
PyObject *encoded_name = get_importtime_name(name);
fprintf(stderr, "import time: cached | cached | %*s\n",
import_level*2, PyUnicode_AsUTF8(name));
import_level*2,
encoded_name != NULL ? PyBytes_AS_STRING(encoded_name) : "?");
Py_XDECREF(encoded_name);
#undef import_level
}

Expand Down Expand Up @@ -4121,10 +4137,13 @@ import_find_and_load_with_name(PyThreadState *tstate, PyObject *abs_name,
PyTime_t cum = t2 - t1;

import_level--;
PyObject *encoded_name = get_importtime_name(abs_name);
fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n",
(long)_PyTime_AsMicroseconds(cum - accumulated, _PyTime_ROUND_CEILING),
(long)_PyTime_AsMicroseconds(cum, _PyTime_ROUND_CEILING),
import_level*2, "", PyUnicode_AsUTF8(abs_name));
import_level*2, "",
encoded_name != NULL ? PyBytes_AS_STRING(encoded_name) : "?");
Py_XDECREF(encoded_name);

accumulated = accumulated_copy + cum;
}
Expand Down
Loading