Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,17 @@ def test_presite(self):
proc = assert_python_ok("-X", f"presite={entrypoint}", "-c", "pass")
self.assertEqual(proc.out.rstrip(), b"presite func")

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


@unittest.skipIf(interpreter_requires_environment(),
'Cannot run -I tests when PYTHON env vars are required.')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
If ``import encodings`` (first import) fails at Python startup, dump the
Python path configuration to help users debugging their configuration. Patch
by Victor Stinner.
3 changes: 3 additions & 0 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Copyright (c) Corporation for National Research Initiatives.
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_codecs.h" // export _PyCodec_LookupTextEncoding()
#include "pycore_initconfig.h" // _Py_DumpPathConfig()
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
Expand Down Expand Up @@ -1686,6 +1687,8 @@ _PyCodec_InitRegistry(PyInterpreterState *interp)
// search functions, so this is done after everything else is initialized.
PyObject *mod = PyImport_ImportModule("encodings");
if (mod == NULL) {
PyThreadState *tstate = _PyThreadState_GET();
_Py_DumpPathConfig(tstate);
return PyStatus_Error("Failed to import encodings module");
}
Py_DECREF(mod);
Expand Down
Loading