@@ -1667,45 +1667,52 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
1667
1667
}
1668
1668
}
1669
1669
else {
1670
- static int ximporttime = 0 ;
1670
+ /* 1 -- true, 0 -- false, -1 -- not initialized */
1671
+ static int ximporttime = -1 ;
1671
1672
static int import_level ;
1672
1673
static _PyTime_t accumulated ;
1673
1674
_Py_IDENTIFIER (importtime );
1674
1675
1675
1676
_PyTime_t t1 = 0 , accumulated_copy = accumulated ;
1676
1677
1677
- Py_XDECREF (mod );
1678
-
1679
1678
/* 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().
1683
1682
*/
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' ) {
1687
1686
ximporttime = 1 ;
1688
1687
}
1689
1688
else {
1690
1689
PyObject * xoptions = PySys_GetXOptions ();
1690
+ PyObject * value = NULL ;
1691
1691
if (xoptions ) {
1692
- PyObject * value = _PyDict_GetItemId (
1692
+ value = _PyDict_GetItemIdWithError (
1693
1693
xoptions , & PyId_importtime );
1694
+ }
1695
+ if (value == NULL && PyErr_Occurred ()) {
1696
+ goto error ;
1697
+ }
1698
+ if (value != NULL || Py_IsInitialized ()) {
1694
1699
ximporttime = (value == Py_True );
1695
1700
}
1696
1701
}
1697
- if (ximporttime ) {
1702
+ if (ximporttime > 0 ) {
1698
1703
fputs ("import time: self [us] | cumulative | imported package\n" ,
1699
1704
stderr );
1700
1705
}
1701
1706
}
1702
1707
1703
- if (ximporttime ) {
1708
+ if (ximporttime > 0 ) {
1704
1709
import_level ++ ;
1705
1710
t1 = _PyTime_GetPerfCounter ();
1706
1711
accumulated = 0 ;
1707
1712
}
1708
1713
1714
+ Py_XDECREF (mod );
1715
+
1709
1716
if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED ())
1710
1717
PyDTrace_IMPORT_FIND_LOAD_START (PyUnicode_AsUTF8 (abs_name ));
1711
1718
@@ -1717,7 +1724,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
1717
1724
PyDTrace_IMPORT_FIND_LOAD_DONE (PyUnicode_AsUTF8 (abs_name ),
1718
1725
mod != NULL );
1719
1726
1720
- if (ximporttime ) {
1727
+ if (ximporttime > 0 ) {
1721
1728
_PyTime_t cum = _PyTime_GetPerfCounter () - t1 ;
1722
1729
1723
1730
import_level -- ;
0 commit comments