bpo-34170: Enhance _PyCoreConfig_Read() (GH-8468) · python/cpython@ecf411c (original) (raw)

`@@ -701,6 +701,7 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)

`

701

701

` config->LEN = config2->LEN; \

`

702

702

` } while (0)

`

703

703

``

``

704

`+

COPY_ATTR(install_signal_handlers);

`

704

705

`COPY_ATTR(ignore_environment);

`

705

706

`COPY_ATTR(use_hash_seed);

`

706

707

`COPY_ATTR(hash_seed);

`

`@@ -714,6 +715,9 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)

`

714

715

`COPY_ATTR(show_alloc_count);

`

715

716

`COPY_ATTR(dump_refs);

`

716

717

`COPY_ATTR(malloc_stats);

`

``

718

+

``

719

`+

COPY_ATTR(coerce_c_locale);

`

``

720

`+

COPY_ATTR(coerce_c_locale_warn);

`

717

721

`COPY_ATTR(utf8_mode);

`

718

722

``

719

723

`COPY_STR_ATTR(pycache_prefix);

`

`@@ -1244,9 +1248,7 @@ config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)

`

1244

1248

`static _PyInitError

`

1245

1249

`cmdline_init_env_warnoptions(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)

`

1246

1250

`{

`

1247

``

`-

if (config->ignore_environment) {

`

1248

``

`-

return _Py_INIT_OK();

`

1249

``

`-

}

`

``

1251

`+

assert(!config->ignore_environment);

`

1250

1252

``

1251

1253

`wchar_t *env;

`

1252

1254

`int res = config_get_env_var_dup(config, &env,

`

`@@ -1819,37 +1821,6 @@ get_env_flag(_PyCoreConfig *config, int *flag, const char *name)

`

1819

1821

`}

`

1820

1822

``

1821

1823

``

1822

``

`-

static void

`

1823

``

`-

cmdline_get_env_flags(_PyMain *pymain, _PyCoreConfig *config,

`

1824

``

`-

_PyCmdline *cmdline)

`

1825

``

`-

{

`

1826

``

`-

get_env_flag(config, &config->debug, "PYTHONDEBUG");

`

1827

``

`-

get_env_flag(config, &config->verbose, "PYTHONVERBOSE");

`

1828

``

`-

get_env_flag(config, &config->optimization_level, "PYTHONOPTIMIZE");

`

1829

``

`-

get_env_flag(config, &config->inspect, "PYTHONINSPECT");

`

1830

``

-

1831

``

`-

int dont_write_bytecode = 0;

`

1832

``

`-

get_env_flag(config, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");

`

1833

``

`-

if (dont_write_bytecode) {

`

1834

``

`-

config->write_bytecode = 0;

`

1835

``

`-

}

`

1836

``

-

1837

``

`-

int no_user_site_directory = 0;

`

1838

``

`-

get_env_flag(config, &no_user_site_directory, "PYTHONNOUSERSITE");

`

1839

``

`-

if (no_user_site_directory) {

`

1840

``

`-

config->user_site_directory = 0;

`

1841

``

`-

}

`

1842

``

-

1843

``

`-

get_env_flag(config, &config->unbuffered_stdio, "PYTHONUNBUFFERED");

`

1844

``

`-

#ifdef MS_WINDOWS

`

1845

``

`-

get_env_flag(config, &config->legacy_windows_fs_encoding,

`

1846

``

`-

"PYTHONLEGACYWINDOWSFSENCODING");

`

1847

``

`-

get_env_flag(config, &config->legacy_windows_stdio,

`

1848

``

`-

"PYTHONLEGACYWINDOWSSTDIO");

`

1849

``

`-

#endif

`

1850

``

`-

}

`

1851

``

-

1852

``

-

1853

1824

`static _PyInitError

`

1854

1825

`config_init_home(_PyCoreConfig *config)

`

1855

1826

`{

`

`@@ -1944,7 +1915,37 @@ config_init_utf8_mode(_PyCoreConfig *config)

`

1944

1915

`static _PyInitError

`

1945

1916

`config_read_env_vars(_PyCoreConfig *config)

`

1946

1917

`{

`

1947

``

`-

config->allocator = config_get_env_var(config, "PYTHONMALLOC");

`

``

1918

`+

assert(!config->ignore_environment);

`

``

1919

+

``

1920

`+

/* Get environment variables */

`

``

1921

`+

get_env_flag(config, &config->debug, "PYTHONDEBUG");

`

``

1922

`+

get_env_flag(config, &config->verbose, "PYTHONVERBOSE");

`

``

1923

`+

get_env_flag(config, &config->optimization_level, "PYTHONOPTIMIZE");

`

``

1924

`+

get_env_flag(config, &config->inspect, "PYTHONINSPECT");

`

``

1925

+

``

1926

`+

int dont_write_bytecode = 0;

`

``

1927

`+

get_env_flag(config, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");

`

``

1928

`+

if (dont_write_bytecode) {

`

``

1929

`+

config->write_bytecode = 0;

`

``

1930

`+

}

`

``

1931

+

``

1932

`+

int no_user_site_directory = 0;

`

``

1933

`+

get_env_flag(config, &no_user_site_directory, "PYTHONNOUSERSITE");

`

``

1934

`+

if (no_user_site_directory) {

`

``

1935

`+

config->user_site_directory = 0;

`

``

1936

`+

}

`

``

1937

+

``

1938

`+

get_env_flag(config, &config->unbuffered_stdio, "PYTHONUNBUFFERED");

`

``

1939

`+

#ifdef MS_WINDOWS

`

``

1940

`+

get_env_flag(config, &config->legacy_windows_fs_encoding,

`

``

1941

`+

"PYTHONLEGACYWINDOWSFSENCODING");

`

``

1942

`+

get_env_flag(config, &config->legacy_windows_stdio,

`

``

1943

`+

"PYTHONLEGACYWINDOWSSTDIO");

`

``

1944

`+

#endif

`

``

1945

+

``

1946

`+

if (config->allocator == NULL) {

`

``

1947

`+

config->allocator = config_get_env_var(config, "PYTHONMALLOC");

`

``

1948

`+

}

`

1948

1949

``

1949

1950

`if (config_get_env_var(config, "PYTHONDUMPREFS")) {

`

1950

1951

`config->dump_refs = 1;

`

`@@ -1998,8 +1999,6 @@ config_read_complex_options(_PyCoreConfig *config)

`

1998

1999

`config_get_env_var(config, "PYTHONDEVMODE"))

`

1999

2000

` {

`

2000

2001

`config->dev_mode = 1;

`

2001

``

`-

config->faulthandler = 1;

`

2002

``

`-

config->allocator = "debug";

`

2003

2002

` }

`

2004

2003

``

2005

2004

`_PyInitError err = pymain_init_tracemalloc(config);

`

`@@ -2033,23 +2032,17 @@ pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,

`

2033

2032

`return res;

`

2034

2033

` }

`

2035

2034

``

2036

``

`-

/* Get environment variables */

`

2037

``

`-

cmdline_get_env_flags(pymain, config, cmdline);

`

2038

``

-

2039

``

`-

err = cmdline_init_env_warnoptions(pymain, config, cmdline);

`

2040

``

`-

if (_Py_INIT_FAILED(err)) {

`

2041

``

`-

pymain->err = err;

`

``

2035

`+

if (pymain_init_core_argv(pymain, config, cmdline) < 0) {

`

2042

2036

`return -1;

`

2043

2037

` }

`

2044

2038

``

2045

``

`-

#ifdef MS_WINDOWS

`

2046

``

`-

if (config->legacy_windows_fs_encoding) {

`

2047

``

`-

config->utf8_mode = 0;

`

2048

``

`-

}

`

2049

``

`-

#endif

`

2050

``

-

2051

``

`-

if (pymain_init_core_argv(pymain, config, cmdline) < 0) {

`

2052

``

`-

return -1;

`

``

2039

`+

assert(config->ignore_environment >= 0);

`

``

2040

`+

if (!config->ignore_environment) {

`

``

2041

`+

err = cmdline_init_env_warnoptions(pymain, config, cmdline);

`

``

2042

`+

if (_Py_INIT_FAILED(err)) {

`

``

2043

`+

pymain->err = err;

`

``

2044

`+

return -1;

`

``

2045

`+

}

`

2053

2046

` }

`

2054

2047

``

2055

2048

`err = _PyCoreConfig_Read(config);

`

`@@ -2186,14 +2179,6 @@ config_init_locale(_PyCoreConfig *config)

`

2186

2179

` }

`

2187

2180

`return;

`

2188

2181

` }

`

2189

``

-

2190

``

`-

/* By default, C locale coercion and UTF-8 Mode are disabled */

`

2191

``

`-

if (config->coerce_c_locale < 0) {

`

2192

``

`-

config->coerce_c_locale = 0;

`

2193

``

`-

}

`

2194

``

`-

if (config->utf8_mode < 0) {

`

2195

``

`-

config->utf8_mode = 0;

`

2196

``

`-

}

`

2197

2182

`}

`

2198

2183

``

2199

2184

``

`@@ -2216,9 +2201,12 @@ _PyCoreConfig_Read(_PyCoreConfig *config)

`

2216

2201

``

2217

2202

`_PyCoreConfig_GetGlobalConfig(config);

`

2218

2203

``

2219

``

`-

err = config_read_env_vars(config);

`

2220

``

`-

if (_Py_INIT_FAILED(err)) {

`

2221

``

`-

return err;

`

``

2204

`+

assert(config->ignore_environment >= 0);

`

``

2205

`+

if (!config->ignore_environment) {

`

``

2206

`+

err = config_read_env_vars(config);

`

``

2207

`+

if (_Py_INIT_FAILED(err)) {

`

``

2208

`+

return err;

`

``

2209

`+

}

`

2222

2210

` }

`

2223

2211

``

2224

2212

`/* -X options */

`

`@@ -2260,13 +2248,38 @@ _PyCoreConfig_Read(_PyCoreConfig *config)

`

2260

2248

` }

`

2261

2249

` }

`

2262

2250

``

``

2251

`+

/* options side effects */

`

``

2252

`+

if (config->dev_mode) {

`

``

2253

`+

if (config->faulthandler < 0) {

`

``

2254

`+

config->faulthandler = 1;

`

``

2255

`+

}

`

``

2256

`+

if (config->allocator == NULL) {

`

``

2257

`+

config->allocator = "debug";

`

``

2258

`+

}

`

``

2259

`+

}

`

``

2260

+

``

2261

`+

#ifdef MS_WINDOWS

`

``

2262

`+

if (config->legacy_windows_fs_encoding) {

`

``

2263

`+

config->utf8_mode = 0;

`

``

2264

`+

}

`

``

2265

`+

#endif

`

``

2266

+

2263

2267

`/* default values */

`

``

2268

`+

if (config->use_hash_seed < 0) {

`

``

2269

`+

config->use_hash_seed = 0;

`

``

2270

`+

config->hash_seed = 0;

`

``

2271

`+

}

`

``

2272

`+

if (config->faulthandler < 0) {

`

``

2273

`+

config->faulthandler = 0;

`

``

2274

`+

}

`

2264

2275

`if (config->tracemalloc < 0) {

`

2265

2276

`config->tracemalloc = 0;

`

2266

2277

` }

`

2267

``

`-

if (config->install_signal_handlers < 0) {

`

2268

``

`-

/* Signal handlers are installed by default */

`

2269

``

`-

config->install_signal_handlers = 1;

`

``

2278

`+

if (config->coerce_c_locale < 0) {

`

``

2279

`+

config->coerce_c_locale = 0;

`

``

2280

`+

}

`

``

2281

`+

if (config->utf8_mode < 0) {

`

``

2282

`+

config->utf8_mode = 0;

`

2270

2283

` }

`

2271

2284

``

2272

2285

`return _Py_INIT_OK();

`

`@@ -2599,7 +2612,6 @@ pymain_init(_PyMain *pymain, PyInterpreterState **interp_p)

`

2599

2612

``

2600

2613

`_PyCoreConfig local_config = _PyCoreConfig_INIT;

`

2601

2614

`_PyCoreConfig *config = &local_config;

`

2602

``

`-

config->install_signal_handlers = 1;

`

2603

2615

``

2604

2616

`_PyCoreConfig_GetGlobalConfig(config);

`

2605

2617

``