[Python-checkins] bpo-34228: Allow PYTHONTRACEMALLOC=0 (GH-8467)

Victor Stinner webhook-mailer at python.org
Wed Jul 25 13:23:57 EDT 2018


https://github.com/python/cpython/commit/60b04c9f6fb87522a62ab6b95db9f8a09aef42d4
commit: 60b04c9f6fb87522a62ab6b95db9f8a09aef42d4
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-07-25T19:23:53+02:00
summary:

bpo-34228: Allow PYTHONTRACEMALLOC=0 (GH-8467)

PYTHONTRACEMALLOC=0 environment variable and -X tracemalloc=0 command
line option are now allowed to disable explicitly tracemalloc at
startup.

files:
A Misc/NEWS.d/next/Library/2018-07-25-19-02-39.bpo-34228.0Ibztw.rst
M Include/pystate.h
M Lib/test/test_tracemalloc.py
M Modules/main.c

diff --git a/Include/pystate.h b/Include/pystate.h
index fe14832fcff3..2c705052aad5 100644
--- a/Include/pystate.h
+++ b/Include/pystate.h
@@ -232,6 +232,7 @@ typedef struct {
         .install_signal_handlers = -1, \
         .ignore_environment = -1, \
         .use_hash_seed = -1, \
+        .tracemalloc = -1, \
         .coerce_c_locale = -1, \
         .utf8_mode = -1, \
         .argc = -1, \
diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py
index b0a0e1b2d786..491cd057ef87 100644
--- a/Lib/test/test_tracemalloc.py
+++ b/Lib/test/test_tracemalloc.py
@@ -15,6 +15,7 @@
 
 
 EMPTY_STRING_SIZE = sys.getsizeof(b'')
+INVALID_NFRAME = (-1, 2**30)
 
 
 def get_frames(nframe, lineno_delta):
@@ -833,6 +834,13 @@ def test_env_var_ignored_with_E(self):
         stdout = stdout.rstrip()
         self.assertEqual(stdout, b'False')
 
+    def test_env_var_disabled(self):
+        # tracing at startup
+        code = 'import tracemalloc; print(tracemalloc.is_tracing())'
+        ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='0')
+        stdout = stdout.rstrip()
+        self.assertEqual(stdout, b'False')
+
     def test_env_var_enabled_at_startup(self):
         # tracing at startup
         code = 'import tracemalloc; print(tracemalloc.is_tracing())'
@@ -861,7 +869,7 @@ def check_env_var_invalid(self, nframe):
 
 
     def test_env_var_invalid(self):
-        for nframe in (-1, 0, 2**30):
+        for nframe in INVALID_NFRAME:
             with self.subTest(nframe=nframe):
                 self.check_env_var_invalid(nframe)
 
@@ -889,7 +897,7 @@ def check_sys_xoptions_invalid(self, nframe):
         self.fail(f"unexpeced output: {stderr!a}")
 
     def test_sys_xoptions_invalid(self):
-        for nframe in (-1, 0, 2**30):
+        for nframe in INVALID_NFRAME:
             with self.subTest(nframe=nframe):
                 self.check_sys_xoptions_invalid(nframe)
 
diff --git a/Misc/NEWS.d/next/Library/2018-07-25-19-02-39.bpo-34228.0Ibztw.rst b/Misc/NEWS.d/next/Library/2018-07-25-19-02-39.bpo-34228.0Ibztw.rst
new file mode 100644
index 000000000000..729c0c4fa810
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-25-19-02-39.bpo-34228.0Ibztw.rst
@@ -0,0 +1,3 @@
+tracemalloc: PYTHONTRACEMALLOC=0 environment variable and -X tracemalloc=0
+command line option are now allowed to disable explicitly tracemalloc at
+startup.
diff --git a/Modules/main.c b/Modules/main.c
index a16c34019146..e116dd076538 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -1726,10 +1726,14 @@ pymain_init_tracemalloc(_PyCoreConfig *config)
     int nframe;
     int valid;
 
+    if (config->tracemalloc >= 0) {
+        return _Py_INIT_OK();
+    }
+
     const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC");
     if (env) {
         if (!pymain_str_to_int(env, &nframe)) {
-            valid = (nframe >= 1);
+            valid = (nframe >= 0);
         }
         else {
             valid = 0;
@@ -1746,7 +1750,7 @@ pymain_init_tracemalloc(_PyCoreConfig *config)
         const wchar_t *sep = wcschr(xoption, L'=');
         if (sep) {
             if (!pymain_wstr_to_int(sep + 1, &nframe)) {
-                valid = (nframe >= 1);
+                valid = (nframe >= 0);
             }
             else {
                 valid = 0;
@@ -2249,17 +2253,22 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
 
     config_init_locale(config);
 
-    /* Signal handlers are installed by default */
-    if (config->install_signal_handlers < 0) {
-        config->install_signal_handlers = 1;
-    }
-
     if (config->_install_importlib) {
         err = _PyCoreConfig_InitPathConfig(config);
         if (_Py_INIT_FAILED(err)) {
             return err;
         }
     }
+
+    /* default values */
+    if (config->tracemalloc < 0) {
+        config->tracemalloc = 0;
+    }
+    if (config->install_signal_handlers < 0) {
+        /* Signal handlers are installed by default */
+        config->install_signal_handlers = 1;
+    }
+
     return _Py_INIT_OK();
 }
 



More information about the Python-checkins mailing list