Skip to content

Commit f4bf551

Browse files
serhiy-storchakaembray
authored andcommitted
bpo-31415: Improve error handling and caching of the importtime option. (python#4138)
1 parent 5dc225d commit f4bf551

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

Python/import.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,45 +1667,52 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
16671667
}
16681668
}
16691669
else {
1670-
static int ximporttime = 0;
1670+
/* 1 -- true, 0 -- false, -1 -- not initialized */
1671+
static int ximporttime = -1;
16711672
static int import_level;
16721673
static _PyTime_t accumulated;
16731674
_Py_IDENTIFIER(importtime);
16741675

16751676
_PyTime_t t1 = 0, accumulated_copy = accumulated;
16761677

1677-
Py_XDECREF(mod);
1678-
16791678
/* XOptions is initialized after first some imports.
1680-
* So we can't have negative cache.
1681-
* Anyway, importlib.__find_and_load is much slower than
1682-
* _PyDict_GetItemId()
1679+
* So we can't have negative cache before completed initialization.
1680+
* Anyway, importlib._find_and_load is much slower than
1681+
* _PyDict_GetItemIdWithError().
16831682
*/
1684-
if (ximporttime == 0) {
1685-
char *envoption = Py_GETENV("PYTHONPROFILEIMPORTTIME");
1686-
if (envoption != NULL && strlen(envoption) > 0) {
1683+
if (ximporttime < 0) {
1684+
const char *envoption = Py_GETENV("PYTHONPROFILEIMPORTTIME");
1685+
if (envoption != NULL && *envoption != '\0') {
16871686
ximporttime = 1;
16881687
}
16891688
else {
16901689
PyObject *xoptions = PySys_GetXOptions();
1690+
PyObject *value = NULL;
16911691
if (xoptions) {
1692-
PyObject *value = _PyDict_GetItemId(
1692+
value = _PyDict_GetItemIdWithError(
16931693
xoptions, &PyId_importtime);
1694+
}
1695+
if (value == NULL && PyErr_Occurred()) {
1696+
goto error;
1697+
}
1698+
if (value != NULL || Py_IsInitialized()) {
16941699
ximporttime = (value == Py_True);
16951700
}
16961701
}
1697-
if (ximporttime) {
1702+
if (ximporttime > 0) {
16981703
fputs("import time: self [us] | cumulative | imported package\n",
16991704
stderr);
17001705
}
17011706
}
17021707

1703-
if (ximporttime) {
1708+
if (ximporttime > 0) {
17041709
import_level++;
17051710
t1 = _PyTime_GetPerfCounter();
17061711
accumulated = 0;
17071712
}
17081713

1714+
Py_XDECREF(mod);
1715+
17091716
if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED())
17101717
PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name));
17111718

@@ -1717,7 +1724,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
17171724
PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name),
17181725
mod != NULL);
17191726

1720-
if (ximporttime) {
1727+
if (ximporttime > 0) {
17211728
_PyTime_t cum = _PyTime_GetPerfCounter() - t1;
17221729

17231730
import_level--;

0 commit comments

Comments
 (0)