diff options
author | Tom de Vries <tdevries@suse.de> | 2024-12-03 22:58:47 +0100 |
---|---|---|
committer | Tom de Vries <tdevries@suse.de> | 2024-12-03 22:58:47 +0100 |
commit | 6a02aa77d80e4040bea699a88cf0dd208df639b4 (patch) | |
tree | af5de6f27dfce87c95bc4cacadd9bf02bdf1ae6d /gdb/python | |
parent | 922ab963e1c48c364b7f6363cb8e19f8a7175a20 (diff) | |
download | binutils-6a02aa77d80e4040bea699a88cf0dd208df639b4.zip binutils-6a02aa77d80e4040bea699a88cf0dd208df639b4.tar.gz binutils-6a02aa77d80e4040bea699a88cf0dd208df639b4.tar.bz2 |
[gdb/python] Issue warning if python fails to initialize
A common problem is that python may fail to initialize if PYTHONHOME is
set incorrectly, or points to incompatible default libraries.
Likewise if PYTHONPATH points to incompatible modules.
For instance, say PYTHONHOME is foo, then we get:
...
$ gdb -q
Python path configuration:
PYTHONHOME = 'foo'
PYTHONPATH = (not set)
program name = '/usr/bin/python'
isolated = 0
environment = 1
user site = 1
safe_path = 0
import site = 1
is in build tree = 0
stdlib dir = 'foo/lib64/python3.12'
sys._base_executable = '/usr/bin/python'
sys.base_prefix = 'foo'
sys.base_exec_prefix = 'foo'
sys.platlibdir = 'lib64'
sys.executable = '/usr/bin/python'
sys.prefix = 'foo'
sys.exec_prefix = 'foo'
sys.path = [
'foo/lib64/python312.zip',
'foo/lib64/python3.12',
'foo/lib64/python3.12/lib-dynload',
]
Python Exception <class 'ModuleNotFoundError'>: No module named 'encodings'
Python not initialized
$
...
In this case, it might be easy to figure out what went wrong because of the
obviously incorrect pathnames, but that might not be the case if PYTHONHOME
points to an incompatible python installation.
Fix this by adding a warning with a description of the possible cause and what
to do about it:
...
Python initialization failed: \
failed to get the Python codec of the filesystem encoding
gdb: warning: Python failed to initialize with PYTHONHOME set. Maybe because \
it is set incorrectly? Maybe because it points to incompatible standard \
libraries? Consider changing or unsetting it, or ignoring it using "set \
python ignore-environment on" at early initialization.
...
Likewise for PYTHONPATH:
...
Python initialization failed: \
failed to get the Python codec of the filesystem encoding
gdb: warning: Python failed to initialize with PYTHONPATH set. Maybe because \
it points to incompatible modules? Consider changing or unsetting it, or \
ignoring it using "set python ignore-environment on" at early \
initialization.
...
Tested on aarch64-linux.
Approved-By: Tom Tromey <tom@tromey.com>
PR python/32379
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32379
Diffstat (limited to 'gdb/python')
-rw-r--r-- | gdb/python/python.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/gdb/python/python.c b/gdb/python/python.c index 8d61b9c..acd80e5 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2819,6 +2819,39 @@ do_initialize (const struct extension_language_defn *extlang) return gdb_pymodule_addobject (m, "gdb", gdb_python_module) >= 0; } +/* Emit warnings in case python initialization has failed. */ + +static void +python_initialization_failed_warnings () +{ + const char *pythonhome = nullptr; + const char *pythonpath = nullptr; + + if (!python_ignore_environment) + { + pythonhome = getenv ("PYTHONHOME"); + pythonpath = getenv ("PYTHONPATH"); + } + + bool have_pythonhome + = pythonhome != nullptr && pythonhome[0] != '\0'; + bool have_pythonpath + = pythonpath != nullptr && pythonpath[0] != '\0'; + + if (have_pythonhome) + warning (_("Python failed to initialize with PYTHONHOME set. Maybe" + " because it is set incorrectly? Maybe because it points to" + " incompatible standard libraries? Consider changing or" + " unsetting it, or ignoring it using \"set python" + " ignore-environment on\" at early initialization.")); + + if (have_pythonpath) + warning (_("Python failed to initialize with PYTHONPATH set. Maybe because" + " it points to incompatible modules? Consider changing or" + " unsetting it, or ignoring it using \"set python" + " ignore-environment on\" at early initialization.")); +} + /* Perform Python initialization. This will be called after GDB has performed all of its own initialization. This is the extension_language_ops.initialize "method". */ @@ -2837,6 +2870,8 @@ gdbpy_initialize (const struct extension_language_defn *extlang) ASAP. */ Py_Finalize (); } + else + python_initialization_failed_warnings (); /* Continue with python disabled. */ return; |