diff --git a/gdb/python/python.c b/gdb/python/python.c index 67f362b852d..f5ace2ff978 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1644,6 +1644,8 @@ do_start_initialization () program_name, and respect the requirement of Py_SetProgramName for Python versions that do not duplicate program_name. */ static wchar_t *progname_copy; + /* Similar treatment for the Python home directory. */ + static wchar_t *home_copy; #endif #ifdef WITH_PYTHON_PATH @@ -1667,6 +1669,18 @@ do_start_initialization () fprintf (stderr, "Could not convert python path to string\n"); return false; } + + std::string homedir = ldirname (python_libdir.c_str ()); + home_copy = XNEWVEC (wchar_t, homedir.size () + 1); + count = mbstowcs (home_copy, homedir.c_str (), homedir.size () + 1); + if (count == (size_t) -1) + { + fprintf (stderr, "Could not convert python home to wide string\n"); + return false; + } + /* Note that, as with the program name, we don't free HOME_COPY. */ + Py_SetPythonHome (home_copy); + setlocale (LC_ALL, oldloc.c_str ()); /* Note that Py_SetProgramName expects the string it is passed to @@ -1678,8 +1692,22 @@ do_start_initialization () PyImport_AppendInittab ("_gdb", init__gdb_module); #else Py_SetProgramName (progname.release ()); -#endif -#endif + + /* We override any value that the PYTHONHOME might have, as we want + to make sure that we use the Python library that comes with GDB. + + Note that the Python 2.7.17 documentation for Py_SetPythonHome + says that the "home" argument should be: + + [...] a zero-terminated character string in static storage + whose contents will not change for the duration of the program’s + execution. No code in the Python interpreter will change + the contents of this storage. + + That's why we have to xstrdup the dirname. */ + Py_SetPythonHome (xstrdup (ldirname (python_libdir.c_str ()).c_str ())); +#endif /* IS_PY3K */ +#endif /* WITH_PYTHON_PATH */ Py_Initialize (); #if PY_VERSION_HEX < 0x03090000