diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-11-07 06:55:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-07 06:55:38 (GMT) |
commit | 088929cf62fa22c06f6a44e25915abce9048a545 (patch) | |
tree | 7c929329696bb15051c4448503b647d1c13a1cff /Python/import.c | |
parent | 31af650ee25f65794b75d4dfefed6fe4758781c1 (diff) | |
download | cpython-088929cf62fa22c06f6a44e25915abce9048a545.zip cpython-088929cf62fa22c06f6a44e25915abce9048a545.tar.gz cpython-088929cf62fa22c06f6a44e25915abce9048a545.tar.bz2 |
bpo-31415: Improve error handling and caching of the importtime option. (#4138)
Diffstat (limited to 'Python/import.c')
-rw-r--r-- | Python/import.c | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/Python/import.c b/Python/import.c index 7ba1842..950c872 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1667,45 +1667,52 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, } } else { - static int ximporttime = 0; + /* 1 -- true, 0 -- false, -1 -- not initialized */ + static int ximporttime = -1; static int import_level; static _PyTime_t accumulated; _Py_IDENTIFIER(importtime); _PyTime_t t1 = 0, accumulated_copy = accumulated; - Py_XDECREF(mod); - /* XOptions is initialized after first some imports. - * So we can't have negative cache. - * Anyway, importlib.__find_and_load is much slower than - * _PyDict_GetItemId() + * So we can't have negative cache before completed initialization. + * Anyway, importlib._find_and_load is much slower than + * _PyDict_GetItemIdWithError(). */ - if (ximporttime == 0) { - char *envoption = Py_GETENV("PYTHONPROFILEIMPORTTIME"); - if (envoption != NULL && strlen(envoption) > 0) { + if (ximporttime < 0) { + const char *envoption = Py_GETENV("PYTHONPROFILEIMPORTTIME"); + if (envoption != NULL && *envoption != '\0') { ximporttime = 1; } else { PyObject *xoptions = PySys_GetXOptions(); + PyObject *value = NULL; if (xoptions) { - PyObject *value = _PyDict_GetItemId( + value = _PyDict_GetItemIdWithError( xoptions, &PyId_importtime); + } + if (value == NULL && PyErr_Occurred()) { + goto error; + } + if (value != NULL || Py_IsInitialized()) { ximporttime = (value == Py_True); } } - if (ximporttime) { + if (ximporttime > 0) { fputs("import time: self [us] | cumulative | imported package\n", stderr); } } - if (ximporttime) { + if (ximporttime > 0) { import_level++; t1 = _PyTime_GetPerfCounter(); accumulated = 0; } + Py_XDECREF(mod); + if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()) PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name)); @@ -1717,7 +1724,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name), mod != NULL); - if (ximporttime) { + if (ximporttime > 0) { _PyTime_t cum = _PyTime_GetPerfCounter() - t1; import_level--; |