[Python-checkins] bpo-31415: Support PYTHONPROFILEIMPORTTIME envvar equivalent to -X importtime (#4240)

Barry Warsaw webhook-mailer at python.org
Thu Nov 2 19:13:40 EDT 2017


https://github.com/python/cpython/commit/700d2e4755921d6c339ff20dacecde1aea64de34
commit: 700d2e4755921d6c339ff20dacecde1aea64de34
branch: master
author: Barry Warsaw <barry at python.org>
committer: GitHub <noreply at github.com>
date: 2017年11月02日T16:13:36-07:00
summary:
bpo-31415: Support PYTHONPROFILEIMPORTTIME envvar equivalent to -X importtime (#4240)
Support PYTHONPROFILEIMPORTTIME envvar equivalent to -X importtime
files:
M Doc/using/cmdline.rst
M Misc/NEWS.d/3.7.0a2.rst
M Python/import.c
diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst
index 8ab26f59557..310891d6d0a 100644
--- a/Doc/using/cmdline.rst
+++ b/Doc/using/cmdline.rst
@@ -411,7 +411,8 @@ Miscellaneous options
 * ``-X importtime`` to show how long each import takes. It shows module name,
 cumulative time (including nested imports) and self time (exluding nested
 imports). Note that its output may be broken in multi threaded application.
- Typical usage is ``python3 -X importtime -c 'import asyncio'``.
+ Typical usage is ``python3 -X importtime -c 'import asyncio'``. See also
+ :envvar:`PYTHONPROFILEIMPORTTIME`.
 
 It also allows passing arbitrary values and retrieving them through the
 :data:`sys._xoptions` dictionary.
@@ -429,7 +430,7 @@ Miscellaneous options
 The ``-X showalloccount`` option.
 
 .. versionadded:: 3.7
- The ``-X importtime`` option.
+ The ``-X importtime`` and :envvar:`PYTHONPROFILEIMPORTTIME` options.
 
 
 Options you shouldn't use
@@ -650,6 +651,15 @@ conflict.
 .. versionadded:: 3.4
 
 
+.. envvar:: PYTHONPROFILEIMPORTTIME
+
+ If this environment variable is set to a non-empty string, Python will
+ show how long each import takes. This is exactly equivalent to setting
+ ``-X importtime`` on the command line.
+
+ .. versionadded:: 3.7
+
+
 .. envvar:: PYTHONASYNCIODEBUG
 
 If this environment variable is set to a non-empty string, enable the
diff --git a/Misc/NEWS.d/3.7.0a2.rst b/Misc/NEWS.d/3.7.0a2.rst
index 6df77153a09..7784bb77be3 100644
--- a/Misc/NEWS.d/3.7.0a2.rst
+++ b/Misc/NEWS.d/3.7.0a2.rst
@@ -166,7 +166,8 @@ special method lookups. Patch by Stefan Behnel.
 .. section: Core and Builtins
 
 Add ``-X importtime`` option to show how long each import takes. It can be
-used to optimize application's startup time.
+used to optimize application's startup time. Support the
+:envvar:`PYTHONPROFILEIMPORTTIME` as an equivalent way to enable this.
 
 ..
 
diff --git a/Python/import.c b/Python/import.c
index d396b4de793..7ba1842e751 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1682,10 +1682,17 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
 * _PyDict_GetItemId()
 */
 if (ximporttime == 0) {
- PyObject *xoptions = PySys_GetXOptions();
- if (xoptions) {
- PyObject *value = _PyDict_GetItemId(xoptions, &PyId_importtime);
- ximporttime = (value == Py_True);
+ char *envoption = Py_GETENV("PYTHONPROFILEIMPORTTIME");
+ if (envoption != NULL && strlen(envoption) > 0) {
+ ximporttime = 1;
+ }
+ else {
+ PyObject *xoptions = PySys_GetXOptions();
+ if (xoptions) {
+ PyObject *value = _PyDict_GetItemId(
+ xoptions, &PyId_importtime);
+ ximporttime = (value == Py_True);
+ }
 }
 if (ximporttime) {
 fputs("import time: self [us] | cumulative | imported package\n",


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /