@@ -125,6 +125,15 @@ precmdline_clear(_PyPreCmdline *cmdline)
125125void
126126_PyPreConfig_Clear (_PyPreConfig * config )
127127{
128+ #define CLEAR (ATTR ) \
129+ do { \
130+ PyMem_RawFree(ATTR); \
131+ ATTR = NULL; \
132+ } while (0)
133+
134+ CLEAR (config -> allocator );
135+
136+ #undef CLEAR
128137}
129138
130139
@@ -134,6 +143,15 @@ _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2)
134143 _PyPreConfig_Clear (config );
135144
136145#define COPY_ATTR (ATTR ) config->ATTR = config2->ATTR
146+ #define COPY_STR_ATTR (ATTR ) \
147+ do { \
148+ if (config2->ATTR != NULL) { \
149+ config->ATTR = _PyMem_RawStrdup(config2->ATTR); \
150+ if (config->ATTR == NULL) { \
151+ return -1; \
152+ } \
153+ } \
154+ } while (0)
137155
138156 COPY_ATTR (isolated );
139157 COPY_ATTR (use_environment );
@@ -143,8 +161,11 @@ _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2)
143161 COPY_ATTR (legacy_windows_fs_encoding );
144162#endif
145163 COPY_ATTR (utf8_mode );
164+ COPY_ATTR (dev_mode );
165+ COPY_STR_ATTR (allocator );
146166
147167#undef COPY_ATTR
168+ #undef COPY_STR_ATTR
148169 return 0 ;
149170}
150171
@@ -345,6 +366,7 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)
345366{
346367 _PyPreConfig_GetGlobalConfig (config );
347368
369+ /* isolated and use_environment */
348370 if (config -> isolated > 0 ) {
349371 config -> use_environment = 0 ;
350372 }
@@ -354,6 +376,7 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)
354376 config -> use_environment = 0 ;
355377 }
356378
379+ /* legacy_windows_fs_encoding, utf8_mode, coerce_c_locale */
357380 if (config -> use_environment ) {
358381#ifdef MS_WINDOWS
359382 _Py_get_env_flag (config , & config -> legacy_windows_fs_encoding ,
@@ -414,11 +437,43 @@ preconfig_read(_PyPreConfig *config, const _PyPreCmdline *cmdline)
414437 if (config -> utf8_mode < 0 ) {
415438 config -> utf8_mode = 0 ;
416439 }
440+ if (config -> coerce_c_locale < 0 ) {
441+ config -> coerce_c_locale = 0 ;
442+ }
443+
444+ /* dev_mode */
445+ if ((cmdline && _Py_get_xoption (cmdline -> nxoption , cmdline -> xoptions , L"dev" ))
446+ || _PyPreConfig_GetEnv (config , "PYTHONDEVMODE" ))
447+ {
448+ config -> dev_mode = 1 ;
449+ }
450+ if (config -> dev_mode < 0 ) {
451+ config -> dev_mode = 0 ;
452+ }
453+
454+ /* allocator */
455+ if (config -> dev_mode && config -> allocator == NULL ) {
456+ config -> allocator = _PyMem_RawStrdup ("debug" );
457+ if (config -> allocator == NULL ) {
458+ return _Py_INIT_NO_MEMORY ();
459+ }
460+ }
461+
462+ if (config -> allocator == NULL ) {
463+ const char * allocator = _PyPreConfig_GetEnv (config , "PYTHONMALLOC" );
464+ if (allocator ) {
465+ config -> allocator = _PyMem_RawStrdup (allocator );
466+ if (config -> allocator == NULL ) {
467+ return _Py_INIT_NO_MEMORY ();
468+ }
469+ }
470+ }
417471
418472 assert (config -> coerce_c_locale >= 0 );
419473 assert (config -> utf8_mode >= 0 );
420474 assert (config -> isolated >= 0 );
421475 assert (config -> use_environment >= 0 );
476+ assert (config -> dev_mode >= 0 );
422477
423478 return _Py_INIT_OK ();
424479}
@@ -448,6 +503,12 @@ _PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict)
448503 } while (0)
449504#define SET_ITEM_INT (ATTR ) \
450505 SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
506+ #define FROM_STRING (STR ) \
507+ ((STR != NULL) ? \
508+ PyUnicode_FromString(STR) \
509+ : (Py_INCREF(Py_None), Py_None))
510+ #define SET_ITEM_STR (ATTR ) \
511+ SET_ITEM(#ATTR, FROM_STRING(config->ATTR))
451512
452513 SET_ITEM_INT (isolated );
453514 SET_ITEM_INT (use_environment );
@@ -457,13 +518,17 @@ _PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict)
457518#ifdef MS_WINDOWS
458519 SET_ITEM_INT (legacy_windows_fs_encoding );
459520#endif
521+ SET_ITEM_INT (dev_mode );
522+ SET_ITEM_STR (allocator );
460523 return 0 ;
461524
462525fail :
463526 return -1 ;
464527
528+ #undef FROM_STRING
465529#undef SET_ITEM
466530#undef SET_ITEM_INT
531+ #undef SET_ITEM_STR
467532}
468533
469534
0 commit comments