Skip to content

Commit 700d2e4

Browse files
authored
bpo-31415: Support PYTHONPROFILEIMPORTTIME envvar equivalent to -X importtime (#4240)
Support PYTHONPROFILEIMPORTTIME envvar equivalent to -X importtime
1 parent 9e33973 commit 700d2e4

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

Doc/using/cmdline.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ Miscellaneous options
411411
* ``-X importtime`` to show how long each import takes. It shows module name,
412412
cumulative time (including nested imports) and self time (exluding nested
413413
imports). Note that its output may be broken in multi threaded application.
414-
Typical usage is ``python3 -X importtime -c 'import asyncio'``.
414+
Typical usage is ``python3 -X importtime -c 'import asyncio'``. See also
415+
:envvar:`PYTHONPROFILEIMPORTTIME`.
415416

416417
It also allows passing arbitrary values and retrieving them through the
417418
:data:`sys._xoptions` dictionary.
@@ -429,7 +430,7 @@ Miscellaneous options
429430
The ``-X showalloccount`` option.
430431

431432
.. versionadded:: 3.7
432-
The ``-X importtime`` option.
433+
The ``-X importtime`` and :envvar:`PYTHONPROFILEIMPORTTIME` options.
433434

434435

435436
Options you shouldn't use
@@ -650,6 +651,15 @@ conflict.
650651
.. versionadded:: 3.4
651652

652653

654+
.. envvar:: PYTHONPROFILEIMPORTTIME
655+
656+
If this environment variable is set to a non-empty string, Python will
657+
show how long each import takes. This is exactly equivalent to setting
658+
``-X importtime`` on the command line.
659+
660+
.. versionadded:: 3.7
661+
662+
653663
.. envvar:: PYTHONASYNCIODEBUG
654664

655665
If this environment variable is set to a non-empty string, enable the

Misc/NEWS.d/3.7.0a2.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ special method lookups. Patch by Stefan Behnel.
166166
.. section: Core and Builtins
167167
168168
Add ``-X importtime`` option to show how long each import takes. It can be
169-
used to optimize application's startup time.
169+
used to optimize application's startup time. Support the
170+
:envvar:`PYTHONPROFILEIMPORTTIME` as an equivalent way to enable this.
170171

171172
..
172173

Python/import.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,10 +1682,17 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
16821682
* _PyDict_GetItemId()
16831683
*/
16841684
if (ximporttime == 0) {
1685-
PyObject *xoptions = PySys_GetXOptions();
1686-
if (xoptions) {
1687-
PyObject *value = _PyDict_GetItemId(xoptions, &PyId_importtime);
1688-
ximporttime = (value == Py_True);
1685+
char *envoption = Py_GETENV("PYTHONPROFILEIMPORTTIME");
1686+
if (envoption != NULL && strlen(envoption) > 0) {
1687+
ximporttime = 1;
1688+
}
1689+
else {
1690+
PyObject *xoptions = PySys_GetXOptions();
1691+
if (xoptions) {
1692+
PyObject *value = _PyDict_GetItemId(
1693+
xoptions, &PyId_importtime);
1694+
ximporttime = (value == Py_True);
1695+
}
16891696
}
16901697
if (ximporttime) {
16911698
fputs("import time: self [us] | cumulative | imported package\n",

0 commit comments

Comments
 (0)