Skip to content

Commit db9e755

Browse files
authored
[3.13] gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250) (#151269) (#151283) (#151287)
[3.14][3.15] gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250) (#151269) (#151283) [3.15] gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250) (#151269) gh-151253: Dump the Python path configuration on _PyCodec_InitRegistry() failure (#151250) If "import encodings" fails at Python startup, dump the Python path configuration to help users debugging their configuration. The encodings module is the first module imported during Python startup. (cherry picked from commit 7b6e989) (cherry picked from commit 10f616c) (cherry picked from commit b3a7758)
1 parent fd0839d commit db9e755

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,17 @@ def res2int(self, res):
10181018
out = res.out.strip().decode("utf-8")
10191019
return tuple(int(i) for i in out.split())
10201020

1021+
def test_dump_path_config(self):
1022+
# gh-151253: At the first import (import encodings) during Python
1023+
# startup, if the import fails, dump the Python path configuration.
1024+
nonexistent = '/nonexistent-python-path'
1025+
# Use -X frozen_modules=off to disable frozen encodings module
1026+
# on release build.
1027+
cmd = ["-X", "frozen_modules=off", "-c", "pass"]
1028+
proc = assert_python_failure(*cmd, PYTHONHOME=nonexistent)
1029+
self.assertIn(b'Python path configuration:', proc.err)
1030+
self.assertIn(f"PYTHONHOME = '{nonexistent}'".encode(), proc.err)
1031+
10211032

10221033
@unittest.skipIf(interpreter_requires_environment(),
10231034
'Cannot run -I tests when PYTHON env vars are required.')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
If ``import encodings`` (first import) fails at Python startup, dump the
2+
Python path configuration to help users debugging their configuration. Patch
3+
by Victor Stinner.

Python/codecs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Copyright (c) Corporation for National Research Initiatives.
1010

1111
#include "Python.h"
1212
#include "pycore_call.h" // _PyObject_CallNoArgs()
13+
#include "pycore_initconfig.h" // _Py_DumpPathConfig()
1314
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
1415
#include "pycore_lock.h" // PyMutex
1516
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
@@ -1504,6 +1505,8 @@ _PyCodec_InitRegistry(PyInterpreterState *interp)
15041505
// search functions, so this is done after everything else is initialized.
15051506
PyObject *mod = PyImport_ImportModule("encodings");
15061507
if (mod == NULL) {
1508+
PyThreadState *tstate = _PyThreadState_GET();
1509+
_Py_DumpPathConfig(tstate);
15071510
return PyStatus_Error("Failed to import encodings module");
15081511
}
15091512
Py_DECREF(mod);

0 commit comments

Comments
 (0)