Skip to content

Commit 86d84ca

Browse files
committed
GH-145275: add -X pathconfig_warnings and PYTHON_PATHCONFIG_WARNINGS
Signed-off-by: Filipe Laíns <lains@riseup.net>
1 parent 4401f23 commit 86d84ca

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added the :option:`-X pathconfig_warnings<-X>` and
2+
:envvar:`PYTHON_PATHCONFIG_WARNINGS` options, allowing to disable warnings
3+
from :ref:`sys_path_init`.

Python/initconfig.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ The following implementation-specific options are available:\n\
357357
use module globals, which is not concurrent-safe; set to true for\n\
358358
free-threaded builds and false otherwise; also\n\
359359
PYTHON_CONTEXT_AWARE_WARNINGS\n\
360+
-X pathconfig_warnings=[0|1]: if true (1) then path configuration is allowed\n\
361+
to log warnings into stderr; if false (0) suppress these warnings;\n\
362+
set to true by default; also PYTHON_PATHCONFIG_WARNINGS\n\
360363
-X tracemalloc[=N]: trace Python memory allocations; N sets a traceback limit\n \
361364
of N frames (default: 1); also PYTHONTRACEMALLOC=N\n\
362365
-X utf8[=0|1]: enable (1) or disable (0) UTF-8 mode; also PYTHONUTF8\n\
@@ -2350,6 +2353,32 @@ config_init_lazy_imports(PyConfig *config)
23502353
return _PyStatus_OK();
23512354
}
23522355

2356+
static PyStatus
2357+
config_init_pathconfig_warnings(PyConfig *config)
2358+
{
2359+
const char *env = config_get_env(config, "PYTHON_PATHCONFIG_WARNINGS");
2360+
if (env) {
2361+
int enabled;
2362+
if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) {
2363+
return _PyStatus_ERR(
2364+
"PYTHON_PATHCONFIG_WARNINGS=N: N is missing or invalid");
2365+
}
2366+
config->pathconfig_warnings = enabled;
2367+
}
2368+
2369+
const wchar_t *xoption = config_get_xoption(config, L"pathconfig_warnings");
2370+
if (xoption) {
2371+
int enabled;
2372+
const wchar_t *sep = wcschr(xoption, L'=');
2373+
if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) {
2374+
return _PyStatus_ERR(
2375+
"-X pathconfig_warnings=n: n is missing or invalid");
2376+
}
2377+
config->pathconfig_warnings = enabled;
2378+
}
2379+
return _PyStatus_OK();
2380+
}
2381+
23532382
static PyStatus
23542383
config_read_complex_options(PyConfig *config)
23552384
{
@@ -2446,6 +2475,11 @@ config_read_complex_options(PyConfig *config)
24462475
return status;
24472476
}
24482477

2478+
status = config_init_pathconfig_warnings(config);
2479+
if (_PyStatus_EXCEPTION(status)) {
2480+
return status;
2481+
}
2482+
24492483
return _PyStatus_OK();
24502484
}
24512485

0 commit comments

Comments
 (0)