Skip to content

Commit 60b04c9

Browse files
authored
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.
1 parent 96d1e69 commit 60b04c9

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

Include/pystate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ typedef struct {
232232
.install_signal_handlers = -1, \
233233
.ignore_environment = -1, \
234234
.use_hash_seed = -1, \
235+
.tracemalloc = -1, \
235236
.coerce_c_locale = -1, \
236237
.utf8_mode = -1, \
237238
.argc = -1, \

Lib/test/test_tracemalloc.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
EMPTY_STRING_SIZE = sys.getsizeof(b'')
18+
INVALID_NFRAME = (-1, 2**30)
1819

1920

2021
def get_frames(nframe, lineno_delta):
@@ -833,6 +834,13 @@ def test_env_var_ignored_with_E(self):
833834
stdout = stdout.rstrip()
834835
self.assertEqual(stdout, b'False')
835836

837+
def test_env_var_disabled(self):
838+
# tracing at startup
839+
code = 'import tracemalloc; print(tracemalloc.is_tracing())'
840+
ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='0')
841+
stdout = stdout.rstrip()
842+
self.assertEqual(stdout, b'False')
843+
836844
def test_env_var_enabled_at_startup(self):
837845
# tracing at startup
838846
code = 'import tracemalloc; print(tracemalloc.is_tracing())'
@@ -861,7 +869,7 @@ def check_env_var_invalid(self, nframe):
861869

862870

863871
def test_env_var_invalid(self):
864-
for nframe in (-1, 0, 2**30):
872+
for nframe in INVALID_NFRAME:
865873
with self.subTest(nframe=nframe):
866874
self.check_env_var_invalid(nframe)
867875

@@ -889,7 +897,7 @@ def check_sys_xoptions_invalid(self, nframe):
889897
self.fail(f"unexpeced output: {stderr!a}")
890898

891899
def test_sys_xoptions_invalid(self):
892-
for nframe in (-1, 0, 2**30):
900+
for nframe in INVALID_NFRAME:
893901
with self.subTest(nframe=nframe):
894902
self.check_sys_xoptions_invalid(nframe)
895903

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tracemalloc: PYTHONTRACEMALLOC=0 environment variable and -X tracemalloc=0
2+
command line option are now allowed to disable explicitly tracemalloc at
3+
startup.

Modules/main.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,10 +1726,14 @@ pymain_init_tracemalloc(_PyCoreConfig *config)
17261726
int nframe;
17271727
int valid;
17281728

1729+
if (config->tracemalloc >= 0) {
1730+
return _Py_INIT_OK();
1731+
}
1732+
17291733
const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC");
17301734
if (env) {
17311735
if (!pymain_str_to_int(env, &nframe)) {
1732-
valid = (nframe >= 1);
1736+
valid = (nframe >= 0);
17331737
}
17341738
else {
17351739
valid = 0;
@@ -1746,7 +1750,7 @@ pymain_init_tracemalloc(_PyCoreConfig *config)
17461750
const wchar_t *sep = wcschr(xoption, L'=');
17471751
if (sep) {
17481752
if (!pymain_wstr_to_int(sep + 1, &nframe)) {
1749-
valid = (nframe >= 1);
1753+
valid = (nframe >= 0);
17501754
}
17511755
else {
17521756
valid = 0;
@@ -2249,17 +2253,22 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
22492253

22502254
config_init_locale(config);
22512255

2252-
/* Signal handlers are installed by default */
2253-
if (config->install_signal_handlers < 0) {
2254-
config->install_signal_handlers = 1;
2255-
}
2256-
22572256
if (config->_install_importlib) {
22582257
err = _PyCoreConfig_InitPathConfig(config);
22592258
if (_Py_INIT_FAILED(err)) {
22602259
return err;
22612260
}
22622261
}
2262+
2263+
/* default values */
2264+
if (config->tracemalloc < 0) {
2265+
config->tracemalloc = 0;
2266+
}
2267+
if (config->install_signal_handlers < 0) {
2268+
/* Signal handlers are installed by default */
2269+
config->install_signal_handlers = 1;
2270+
}
2271+
22632272
return _Py_INIT_OK();
22642273
}
22652274

0 commit comments

Comments
 (0)