]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-145275: add -X pathconfig_warnings and PYTHON_PATHCONFIG_WARNINGS (#145277)
authorFilipe Laíns <lains@riseup.net>
Mon, 2 Mar 2026 19:19:05 +0000 (19:19 +0000)
committerGitHub <noreply@github.com>
Mon, 2 Mar 2026 19:19:05 +0000 (19:19 +0000)
Doc/using/cmdline.rst
Misc/NEWS.d/next/Core_and_Builtins/2026-02-26-21-07-38.gh-issue-145275.qE-3O1.rst [new file with mode: 0644]
Python/initconfig.c

index f4085ff842aea98d88206fbbbac34e14a6ce7943..84b8575284b79349108d2829954bff4a9429f5c4 100644 (file)
@@ -687,6 +687,13 @@ Miscellaneous options
 
      .. versionadded:: 3.14
 
+   * :samp:`-X pathconfig_warnings={0,1}` if true (``1``) then
+     :ref:`sys-path-init` is allowed to log warnings into stderr.
+     If false (``0``) suppress these warnings. Set to true by default.
+     See also :envvar:`PYTHON_PATHCONFIG_WARNINGS`.
+
+     .. versionadded:: next
+
    * :samp:`-X tlbc={0,1}` enables (1, the default) or disables (0) thread-local
      bytecode in builds configured with :option:`--disable-gil`.  When disabled,
      this also disables the specializing interpreter.  See also
@@ -1354,6 +1361,14 @@ conflict.
 
    .. versionadded:: 3.14
 
+.. envvar:: PYTHON_PATHCONFIG_WARNINGS
+
+   If true (``1``) then :ref:`sys-path-init` is allowed to log warnings into
+   stderr. If false (``0``) suppress these warnings. Set to true by default.
+   See also :option:`-X pathconfig_warnings<-X>`.
+
+   .. versionadded:: next
+
 .. envvar:: PYTHON_JIT
 
    On builds where experimental just-in-time compilation is available, this
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-26-21-07-38.gh-issue-145275.qE-3O1.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-26-21-07-38.gh-issue-145275.qE-3O1.rst
new file mode 100644 (file)
index 0000000..1723a7c
--- /dev/null
@@ -0,0 +1,3 @@
+Added the :option:`-X pathconfig_warnings<-X>` and
+:envvar:`PYTHON_PATHCONFIG_WARNINGS` options, allowing to disable warnings
+from :ref:`sys-path-init`.
index 5ffee9eaf9f550b42e7f12c052a40acef52c18e5..57629ff8c5738049105846691a336ae4c6a59b18 100644 (file)
@@ -357,6 +357,9 @@ The following implementation-specific options are available:\n\
          use module globals, which is not concurrent-safe; set to true for\n\
          free-threaded builds and false otherwise; also\n\
          PYTHON_CONTEXT_AWARE_WARNINGS\n\
+-X pathconfig_warnings=[0|1]: if true (1) then path configuration is allowed\n\
+         to log warnings into stderr; if false (0) suppress these warnings;\n\
+         set to true by default; also PYTHON_PATHCONFIG_WARNINGS\n\
 -X tracemalloc[=N]: trace Python memory allocations; N sets a traceback limit\n \
          of N frames (default: 1); also PYTHONTRACEMALLOC=N\n\
 -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)
     return _PyStatus_OK();
 }
 
+static PyStatus
+config_init_pathconfig_warnings(PyConfig *config)
+{
+    const char *env = config_get_env(config, "PYTHON_PATHCONFIG_WARNINGS");
+    if (env) {
+        int enabled;
+        if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) {
+            return _PyStatus_ERR(
+                "PYTHON_PATHCONFIG_WARNINGS=N: N is missing or invalid");
+        }
+        config->pathconfig_warnings = enabled;
+    }
+
+    const wchar_t *xoption = config_get_xoption(config, L"pathconfig_warnings");
+    if (xoption) {
+        int enabled;
+        const wchar_t *sep = wcschr(xoption, L'=');
+        if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) {
+            return _PyStatus_ERR(
+                "-X pathconfig_warnings=n: n is missing or invalid");
+        }
+        config->pathconfig_warnings = enabled;
+    }
+    return _PyStatus_OK();
+}
+
 static PyStatus
 config_read_complex_options(PyConfig *config)
 {
@@ -2446,6 +2475,11 @@ config_read_complex_options(PyConfig *config)
         return status;
     }
 
+    status = config_init_pathconfig_warnings(config);
+    if (_PyStatus_EXCEPTION(status)) {
+        return status;
+    }
+
     return _PyStatus_OK();
 }