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.
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index 170672e..bba2510 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -442,8 +442,8 @@
     config.use_hash_seed = 1;
     config.hash_seed = 123;
 
-    putenv("PYTHONMALLOC=malloc");
-    config.preconfig.allocator = "malloc_debug";
+    putenv("PYTHONMALLOC=malloc_debug");
+    config.preconfig.allocator = "malloc";
 
     /* dev_mode=1 is tested in test_init_dev_mode() */
 
@@ -570,7 +570,7 @@
 static void test_init_env_putenvs(void)
 {
     putenv("PYTHONHASHSEED=42");
-    putenv("PYTHONMALLOC=malloc_debug");
+    putenv("PYTHONMALLOC=malloc");
     putenv("PYTHONTRACEMALLOC=2");
     putenv("PYTHONPROFILEIMPORTTIME=1");
     putenv("PYTHONMALLOCSTATS=1");
@@ -594,15 +594,6 @@
 }
 
 
-static void test_init_env_dev_mode_putenvs(void)
-{
-    test_init_env_putenvs();
-    putenv("PYTHONMALLOC=malloc");
-    putenv("PYTHONFAULTHANDLER=");
-    putenv("PYTHONDEVMODE=1");
-}
-
-
 static int test_init_env(void)
 {
     /* Test initialization from environment variables */
@@ -615,6 +606,15 @@
 }
 
 
+static void test_init_env_dev_mode_putenvs(void)
+{
+    test_init_env_putenvs();
+    putenv("PYTHONMALLOC=");
+    putenv("PYTHONFAULTHANDLER=");
+    putenv("PYTHONDEVMODE=1");
+}
+
+
 static int test_init_env_dev_mode(void)
 {
     /* Test initialization from environment variables */
@@ -627,6 +627,19 @@
 }
 
 
+static int test_init_env_dev_mode_alloc(void)
+{
+    /* Test initialization from environment variables */
+    Py_IgnoreEnvironmentFlag = 0;
+    test_init_env_dev_mode_putenvs();
+    putenv("PYTHONMALLOC=malloc");
+    _testembed_Py_Initialize();
+    dump_config();
+    Py_Finalize();
+    return 0;
+}
+
+
 static int test_init_isolated(void)
 {
     /* Test _PyCoreConfig.isolated=1 */
@@ -700,6 +713,7 @@
     { "init_from_config", test_init_from_config },
     { "init_env", test_init_env },
     { "init_env_dev_mode", test_init_env_dev_mode },
+    { "init_env_dev_mode_alloc", test_init_env_dev_mode_alloc },
     { "init_dev_mode", test_init_dev_mode },
     { "init_isolated", test_init_isolated },
     { NULL, NULL }