Skip to content

Commit 25d13f3

Browse files
authored
bpo-36142: PYTHONMALLOC overrides PYTHONDEV (GH-12191)
bpo-34247, bpo-36142: The PYTHONMALLOC environment variable has the priority over PYTHONDEV env var and "-X dev" command line option. For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory allocators to "malloc" (and not to "debug"). Add an unit test.
1 parent 01e0f43 commit 25d13f3

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

Lib/test/test_embed.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def test_init_from_config(self):
524524
'install_signal_handlers': 0,
525525
'use_hash_seed': 1,
526526
'hash_seed': 123,
527-
'allocator': 'malloc_debug',
527+
'allocator': 'malloc',
528528
'tracemalloc': 2,
529529
'import_time': 1,
530530
'show_ref_count': 1,
@@ -564,7 +564,7 @@ def test_init_from_config(self):
564564
INIT_ENV_CONFIG = {
565565
'use_hash_seed': 1,
566566
'hash_seed': 42,
567-
'allocator': 'malloc_debug',
567+
'allocator': 'malloc',
568568
'tracemalloc': 2,
569569
'import_time': 1,
570570
'malloc_stats': 1,
@@ -592,6 +592,12 @@ def test_init_env_dev_mode(self):
592592
dev_mode=1)
593593
self.check_config("init_env_dev_mode", config)
594594

595+
def test_init_env_dev_mode(self):
596+
config = dict(self.INIT_ENV_CONFIG,
597+
allocator='malloc',
598+
dev_mode=1)
599+
self.check_config("init_env_dev_mode_alloc", config)
600+
595601
def test_init_dev_mode(self):
596602
config = {
597603
'dev_mode': 1,

Programs/_testembed.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ static int test_init_from_config(void)
442442
config.use_hash_seed = 1;
443443
config.hash_seed = 123;
444444

445-
putenv("PYTHONMALLOC=malloc");
446-
config.preconfig.allocator = "malloc_debug";
445+
putenv("PYTHONMALLOC=malloc_debug");
446+
config.preconfig.allocator = "malloc";
447447

448448
/* dev_mode=1 is tested in test_init_dev_mode() */
449449

@@ -570,7 +570,7 @@ static int test_init_from_config(void)
570570
static void test_init_env_putenvs(void)
571571
{
572572
putenv("PYTHONHASHSEED=42");
573-
putenv("PYTHONMALLOC=malloc_debug");
573+
putenv("PYTHONMALLOC=malloc");
574574
putenv("PYTHONTRACEMALLOC=2");
575575
putenv("PYTHONPROFILEIMPORTTIME=1");
576576
putenv("PYTHONMALLOCSTATS=1");
@@ -594,32 +594,45 @@ static void test_init_env_putenvs(void)
594594
}
595595

596596

597+
static int test_init_env(void)
598+
{
599+
/* Test initialization from environment variables */
600+
Py_IgnoreEnvironmentFlag = 0;
601+
test_init_env_putenvs();
602+
_testembed_Py_Initialize();
603+
dump_config();
604+
Py_Finalize();
605+
return 0;
606+
}
607+
608+
597609
static void test_init_env_dev_mode_putenvs(void)
598610
{
599611
test_init_env_putenvs();
600-
putenv("PYTHONMALLOC=malloc");
612+
putenv("PYTHONMALLOC=");
601613
putenv("PYTHONFAULTHANDLER=");
602614
putenv("PYTHONDEVMODE=1");
603615
}
604616

605617

606-
static int test_init_env(void)
618+
static int test_init_env_dev_mode(void)
607619
{
608620
/* Test initialization from environment variables */
609621
Py_IgnoreEnvironmentFlag = 0;
610-
test_init_env_putenvs();
622+
test_init_env_dev_mode_putenvs();
611623
_testembed_Py_Initialize();
612624
dump_config();
613625
Py_Finalize();
614626
return 0;
615627
}
616628

617629

618-
static int test_init_env_dev_mode(void)
630+
static int test_init_env_dev_mode_alloc(void)
619631
{
620632
/* Test initialization from environment variables */
621633
Py_IgnoreEnvironmentFlag = 0;
622634
test_init_env_dev_mode_putenvs();
635+
putenv("PYTHONMALLOC=malloc");
623636
_testembed_Py_Initialize();
624637
dump_config();
625638
Py_Finalize();
@@ -700,6 +713,7 @@ static struct TestCase TestCases[] = {
700713
{ "init_from_config", test_init_from_config },
701714
{ "init_env", test_init_env },
702715
{ "init_env_dev_mode", test_init_env_dev_mode },
716+
{ "init_env_dev_mode_alloc", test_init_env_dev_mode_alloc },
703717
{ "init_dev_mode", test_init_dev_mode },
704718
{ "init_isolated", test_init_isolated },
705719
{ NULL, NULL }

Python/preconfig.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,11 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)
445445
}
446446

447447
/* allocator */
448-
if (config->dev_mode && config->allocator == NULL) {
449-
config->allocator = _PyMem_RawStrdup("debug");
450-
if (config->allocator == NULL) {
451-
return _Py_INIT_NO_MEMORY();
452-
}
453-
}
454-
455448
if (config->allocator == NULL) {
449+
/* bpo-34247. The PYTHONMALLOC environment variable has the priority
450+
over PYTHONDEV env var and "-X dev" command line option.
451+
For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory
452+
allocators to "malloc" (and not to "debug"). */
456453
const char *allocator = _PyPreConfig_GetEnv(config, "PYTHONMALLOC");
457454
if (allocator) {
458455
config->allocator = _PyMem_RawStrdup(allocator);
@@ -462,6 +459,13 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)
462459
}
463460
}
464461

462+
if (config->dev_mode && config->allocator == NULL) {
463+
config->allocator = _PyMem_RawStrdup("debug");
464+
if (config->allocator == NULL) {
465+
return _Py_INIT_NO_MEMORY();
466+
}
467+
}
468+
465469
assert(config->coerce_c_locale >= 0);
466470
assert(config->utf8_mode >= 0);
467471
assert(config->isolated >= 0);

0 commit comments

Comments
 (0)