Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcdb027

Browse files
authored
bpo-38236: Dump path config at first import error (GH-16300)
Python now dumps path configuration if it fails to import the Python codecs of the filesystem and stdio encodings.
1 parent b4d0b39 commit fcdb027

6 files changed

Lines changed: 123 additions & 15 deletions

File tree

Doc/c-api/init_config.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,11 @@ PyConfig
425425
426426
:data:`sys.base_exec_prefix`.
427427
428+
.. c:member:: wchar_t* base_executable
429+
430+
:data:`sys._base_executable`: ``__PYVENV_LAUNCHER__`` environment
431+
variable value, or copy of :c:member:`PyConfig.executable`.
432+
428433
.. c:member:: wchar_t* base_prefix
429434
430435
:data:`sys.base_prefix`.
@@ -862,11 +867,13 @@ Path Configuration
862867
* Path configuration input fields:
863868
864869
* :c:member:`PyConfig.home`
865-
* :c:member:`PyConfig.pythonpath_env`
866870
* :c:member:`PyConfig.pathconfig_warnings`
871+
* :c:member:`PyConfig.program_name`
872+
* :c:member:`PyConfig.pythonpath_env`
867873
868874
* Path configuration output fields:
869875
876+
* :c:member:`PyConfig.base_executable`
870877
* :c:member:`PyConfig.exec_prefix`
871878
* :c:member:`PyConfig.executable`
872879
* :c:member:`PyConfig.prefix`
@@ -918,6 +925,9 @@ The following configuration files are used by the path configuration:
918925
* ``python._pth`` (Windows only)
919926
* ``pybuilddir.txt`` (Unix only)
920927
928+
The ``__PYVENV_LAUNCHER__`` environment variable is used to set
929+
:c:member:`PyConfig.base_executable`
930+
921931
922932
Py_RunMain()
923933
------------

Include/internal/pycore_pathconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extern wchar_t* _Py_GetDLLPath(void);
6060
#endif
6161

6262
extern PyStatus _PyPathConfig_Init(void);
63+
extern void _Py_DumpPathConfig(PyThreadState *tstate);
6364

6465
#ifdef __cplusplus
6566
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Python now dumps path configuration if it fails to import the Python codecs
2+
of the filesystem and stdio encodings.

Objects/unicodeobject.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15804,13 +15804,13 @@ config_get_codec_name(wchar_t **config_encoding)
1580415804

1580515805

1580615806
static PyStatus
15807-
init_stdio_encoding(PyInterpreterState *interp)
15807+
init_stdio_encoding(PyThreadState *tstate)
1580815808
{
1580915809
/* Update the stdio encoding to the normalized Python codec name. */
15810-
PyConfig *config = &interp->config;
15810+
PyConfig *config = &tstate->interp->config;
1581115811
if (config_get_codec_name(&config->stdio_encoding) < 0) {
1581215812
return _PyStatus_ERR("failed to get the Python codec name "
15813-
"of the stdio encoding");
15813+
"of the stdio encoding");
1581415814
}
1581515815
return _PyStatus_OK();
1581615816
}
@@ -15864,15 +15864,18 @@ init_fs_codec(PyInterpreterState *interp)
1586415864

1586515865

1586615866
static PyStatus
15867-
init_fs_encoding(PyInterpreterState *interp)
15867+
init_fs_encoding(PyThreadState *tstate)
1586815868
{
15869+
PyInterpreterState *interp = tstate->interp;
15870+
1586915871
/* Update the filesystem encoding to the normalized Python codec name.
1587015872
For example, replace "ANSI_X3.4-1968" (locale encoding) with "ascii"
1587115873
(Python codec name). */
1587215874
PyConfig *config = &interp->config;
1587315875
if (config_get_codec_name(&config->filesystem_encoding) < 0) {
15876+
_Py_DumpPathConfig(tstate);
1587415877
return _PyStatus_ERR("failed to get the Python codec "
15875-
"of the filesystem encoding");
15878+
"of the filesystem encoding");
1587615879
}
1587715880

1587815881
if (init_fs_codec(interp) < 0) {
@@ -15885,14 +15888,12 @@ init_fs_encoding(PyInterpreterState *interp)
1588515888
PyStatus
1588615889
_PyUnicode_InitEncodings(PyThreadState *tstate)
1588715890
{
15888-
PyInterpreterState *interp = tstate->interp;
15889-
15890-
PyStatus status = init_fs_encoding(interp);
15891+
PyStatus status = init_fs_encoding(tstate);
1589115892
if (_PyStatus_EXCEPTION(status)) {
1589215893
return status;
1589315894
}
1589415895

15895-
return init_stdio_encoding(interp);
15896+
return init_stdio_encoding(tstate);
1589615897
}
1589715898

1589815899

PC/getpathp.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,7 @@ _Py_GetDLLPath(void)
532532

533533

534534
static PyStatus
535-
get_program_full_path(const PyConfig *config,
536-
PyCalculatePath *calculate, _PyPathConfig *pathconfig)
535+
get_program_full_path(_PyPathConfig *pathconfig)
537536
{
538537
const wchar_t *pyvenv_launcher;
539538
wchar_t program_full_path[MAXPATHLEN+1];
@@ -977,7 +976,7 @@ calculate_path_impl(const PyConfig *config,
977976
{
978977
PyStatus status;
979978

980-
status = get_program_full_path(config, calculate, pathconfig);
979+
status = get_program_full_path(pathconfig);
981980
if (_PyStatus_EXCEPTION(status)) {
982981
return status;
983982
}

Python/initconfig.c

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "Python.h"
22
#include "osdefs.h" /* DELIM */
3-
#include "pycore_initconfig.h"
43
#include "pycore_fileutils.h"
54
#include "pycore_getopt.h"
5+
#include "pycore_initconfig.h"
6+
#include "pycore_pathconfig.h"
7+
#include "pycore_pyerrors.h"
68
#include "pycore_pylifecycle.h"
79
#include "pycore_pymem.h"
810
#include "pycore_pystate.h" /* _PyRuntime */
9-
#include "pycore_pathconfig.h"
1011
#include <locale.h> /* setlocale() */
1112
#ifdef HAVE_LANGINFO_H
1213
# include <langinfo.h> /* nl_langinfo(CODESET) */
@@ -2539,3 +2540,97 @@ _Py_GetConfigsAsDict(void)
25392540
Py_XDECREF(dict);
25402541
return NULL;
25412542
}
2543+
2544+
2545+
static void
2546+
init_dump_ascii_wstr(const wchar_t *str)
2547+
{
2548+
if (str == NULL) {
2549+
PySys_WriteStderr("(not set)");
2550+
}
2551+
2552+
PySys_WriteStderr("'");
2553+
for (; *str != L'\0'; str++) {
2554+
wchar_t ch = *str;
2555+
if (ch == L'\'') {
2556+
PySys_WriteStderr("\\'");
2557+
} else if (0x20 <= ch && ch < 0x7f) {
2558+
PySys_WriteStderr("%lc", ch);
2559+
}
2560+
else if (ch <= 0xff) {
2561+
PySys_WriteStderr("\\x%02x", ch);
2562+
}
2563+
#if SIZEOF_WCHAR_T > 2
2564+
else if (ch > 0xffff) {
2565+
PySys_WriteStderr("\\U%08x", ch);
2566+
}
2567+
#endif
2568+
else {
2569+
PySys_WriteStderr("\\u%04x", ch);
2570+
}
2571+
}
2572+
PySys_WriteStderr("'");
2573+
}
2574+
2575+
2576+
/* Dump the Python path configuration into sys.stderr */
2577+
void
2578+
_Py_DumpPathConfig(PyThreadState *tstate)
2579+
{
2580+
PyObject *exc_type, *exc_value, *exc_tb;
2581+
_PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
2582+
2583+
PySys_WriteStderr("Python path configuration:\n");
2584+
2585+
#define DUMP_CONFIG(NAME, FIELD) \
2586+
do { \
2587+
PySys_WriteStderr(" " NAME " = "); \
2588+
init_dump_ascii_wstr(config->FIELD); \
2589+
PySys_WriteStderr("\n"); \
2590+
} while (0)
2591+
2592+
PyConfig *config = &tstate->interp->config;
2593+
DUMP_CONFIG("PYTHONHOME", home);
2594+
DUMP_CONFIG("PYTHONPATH", pythonpath_env);
2595+
DUMP_CONFIG("program name", program_name);
2596+
PySys_WriteStderr(" isolated = %i\n", config->isolated);
2597+
PySys_WriteStderr(" environment = %i\n", config->use_environment);
2598+
PySys_WriteStderr(" user site = %i\n", config->user_site_directory);
2599+
PySys_WriteStderr(" import site = %i\n", config->site_import);
2600+
#undef DUMP_CONFIG
2601+
2602+
#define DUMP_SYS(NAME) \
2603+
do { \
2604+
obj = PySys_GetObject(#NAME); \
2605+
PySys_FormatStderr(" sys.%s = ", #NAME); \
2606+
if (obj != NULL) { \
2607+
PySys_FormatStderr("%A", obj); \
2608+
} \
2609+
else { \
2610+
PySys_WriteStderr("(not set)"); \
2611+
} \
2612+
PySys_FormatStderr("\n"); \
2613+
} while (0)
2614+
2615+
PyObject *obj;
2616+
DUMP_SYS(_base_executable);
2617+
DUMP_SYS(base_prefix);
2618+
DUMP_SYS(base_exec_prefix);
2619+
DUMP_SYS(executable);
2620+
DUMP_SYS(prefix);
2621+
DUMP_SYS(exec_prefix);
2622+
#undef DUMP_SYS
2623+
2624+
PyObject *sys_path = PySys_GetObject("path"); /* borrowed reference */
2625+
if (sys_path != NULL && PyList_Check(sys_path)) {
2626+
PySys_WriteStderr(" sys.path = [\n");
2627+
Py_ssize_t len = PyList_GET_SIZE(sys_path);
2628+
for (Py_ssize_t i=0; i < len; i++) {
2629+
PyObject *path = PyList_GET_ITEM(sys_path, i);
2630+
PySys_FormatStderr(" %A,\n", path);
2631+
}
2632+
PySys_WriteStderr(" ]\n");
2633+
}
2634+
2635+
_PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
2636+
}

0 commit comments

Comments
 (0)