From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1028) id E78ED3858D28; Wed, 20 Jul 2022 19:14:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E78ED3858D28 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Kevin Buettner To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Handle Python 3.11 deprecation of PySys_SetPath and Py_SetProgramName X-Act-Checkin: binutils-gdb X-Git-Author: Kevin Buettner X-Git-Refname: refs/heads/master X-Git-Oldrev: b0cf0a5b9df3a5896524b066f234b5cf5a382b22 X-Git-Newrev: fe587fc997af5486e94250a3dbe7e96f966b1d94 Message-Id: <20220720191427.E78ED3858D28@sourceware.org> Date: Wed, 20 Jul 2022 19:14:27 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jul 2022 19:14:28 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dfe587fc997af= 5486e94250a3dbe7e96f966b1d94 commit fe587fc997af5486e94250a3dbe7e96f966b1d94 Author: Kevin Buettner Date: Thu Jun 30 15:40:29 2022 -0700 Handle Python 3.11 deprecation of PySys_SetPath and Py_SetProgramName =20 Python 3.11 deprecates PySys_SetPath and Py_SetProgramName. The PyConfig API replaces these and other functions. This commit uses the PyConfig API to provide equivalent functionality while also preserving support for older versions of Python, i.e. those before Python 3.8. =20 A beta version of Python 3.11 is available in Fedora Rawhide. Both Fedora 35 and Fedora 36 use Python 3.10, while Fedora 34 still used Python 3.9. I've tested these changes on Fedora 34, Fedora 36, and rawhide, though complete testing was not possible on rawhide due to a kernel bug. That being the case, I decided to enable the newer PyConfig API by testing PY_VERSION_HEX against 0x030a0000. This corresponds to Python 3.10. =20 We could try to use the PyConfig API for Python versions as early as 3.= 8, but I'm reluctant to do this as there may have been PyConfig related bugs in earlier versions which have since been fixed. Recent linux distributions should have support for Python 3.10. This should be more than adequate for testing the new Python initialization code in GDB. =20 Information about the PyConfig API as well as the motivation behind deprecating the old interface can be found at these links: =20 https://github.com/python/cpython/issues/88279 https://peps.python.org/pep-0587/ https://docs.python.org/3.11/c-api/init_config.html =20 The v2 commit also addresses several problems that Simon found in the v1 version. =20 In v1, I had used Py_DontWriteBytecodeFlag in the new initialization code, but Simon pointed out that this global configuration variable will be deprecated in Python 3.12. This version of the patch no longer uses Py_DontWriteBytecodeFlag in the new initialization code. Additionally, both Py_DontWriteBytecodeFlag and Py_IgnoreEnvironmentFlag will no longer be used when building GDB against Python 3.10 or higher. While it's true that both of these global configuration variables are deprecated in Python 3.12, it makes sense to disable their use for gdb builds against 3.10 and higher since those are the versions for which the PyConfig API is now being used by GDB. (The PyConfig API includes different mechanisms for making the same settings afforded by use of the soon-to-be deprecated global configuration variables.) =20 Simon also noted that PyConfig_Clear() would not have be called for one of the failure paths. I've fixed that problem and also made the rest of the "bail out" code more direct. In particular, PyConfig_Clear() will always be called, both for success and failure. =20 The v3 patch addresses some rebase conflicts related to module initialization . Commit 3acd9a692dd ("Make 'import gdb.events' work") uses PyImport_ExtendInittab instead of PyImport_AppendInittab. That commit also initializes a struct for each module to import. Both the initialization and the call to were moved ahead of the ifdefs to avoid having to replicate (at least some of) the code three times in various portions of the ifdefs. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D28668 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29287 Diff: --- gdb/python/python-internal.h | 5 +++ gdb/python/python.c | 99 ++++++++++++++++++++++++++++++++++++----= ---- 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 551438c4709..08749d14200 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -177,6 +177,10 @@ gdb_PySys_GetObject (const char *name) =20 #define PySys_GetObject gdb_PySys_GetObject =20 +/* PySys_SetPath was deprecated in Python 3.11. Disable the deprecated + code for Python 3.10 and newer. */ +#if PY_VERSION_HEX < 0x030a0000 + /* PySys_SetPath's 'path' parameter was missing the 'const' qualifier before Python 3.6. Hence, we wrap it in a function to avoid errors when compiled with -Werror. */ @@ -190,6 +194,7 @@ gdb_PySys_SetPath (const GDB_PYSYS_SETPATH_CHAR *path) } =20 #define PySys_SetPath gdb_PySys_SetPath +#endif =20 /* Wrap PyGetSetDef to allow convenient construction with string literals. Unfortunately, PyGetSetDef's 'name' and 'doc' members diff --git a/gdb/python/python.c b/gdb/python/python.c index c75896dc324..c719e3dc90c 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1820,8 +1820,14 @@ set_python_ignore_environment (const char *args, int= from_tty, struct cmd_list_element *c) { #ifdef HAVE_PYTHON + /* Py_IgnoreEnvironmentFlag is deprecated in Python 3.12. Disable + its usage in Python 3.10 and above since the PyConfig mechanism + is now (also) used in 3.10 and higher. See do_start_initialization() + in this file. */ +#if PY_VERSION_HEX < 0x030a0000 Py_IgnoreEnvironmentFlag =3D python_ignore_environment ? 1 : 0; #endif +#endif } =20 /* When this is turned on before Python is initialised then Python will @@ -1849,6 +1855,24 @@ show_python_dont_write_bytecode (struct ui_file *fil= e, int from_tty, value); } =20 +/* Return value to assign to PyConfig.write_bytecode or, when + negated (via !), Py_DontWriteBytecodeFlag. Py_DontWriteBytecodeFlag + is deprecated in Python 3.12. */ + +static int +python_write_bytecode () +{ + int wbc =3D 0; + + if (python_dont_write_bytecode =3D=3D AUTO_BOOLEAN_AUTO) + wbc =3D (!python_ignore_environment + && getenv ("PYTHONDONTWRITEBYTECODE") !=3D nullptr) ? 0 : 1; + else + wbc =3D python_dont_write_bytecode =3D=3D AUTO_BOOLEAN_TRUE ? 0 : 1; + + return wbc; +} + /* Implement 'set python dont-write-bytecode'. This sets Python's internal flag no matter when the command is issued, however, if this is used after Py_Initialize has been called then many modules could already @@ -1859,13 +1883,13 @@ set_python_dont_write_bytecode (const char *args, i= nt from_tty, struct cmd_list_element *c) { #ifdef HAVE_PYTHON - if (python_dont_write_bytecode =3D=3D AUTO_BOOLEAN_AUTO) - Py_DontWriteBytecodeFlag - =3D (!python_ignore_environment - && getenv ("PYTHONDONTWRITEBYTECODE") !=3D nullptr) ? 1 : 0; - else - Py_DontWriteBytecodeFlag - =3D python_dont_write_bytecode =3D=3D AUTO_BOOLEAN_TRUE ? 1 : 0; + /* Py_DontWriteBytecodeFlag is deprecated in Python 3.12. Disable + its usage in Python 3.10 and above since the PyConfig mechanism + is now (also) used in 3.10 and higher. See do_start_initialization() + in this file. */ +#if PY_VERSION_HEX < 0x030a0000 + Py_DontWriteBytecodeFlag =3D !python_write_bytecode (); +#endif #endif /* HAVE_PYTHON */ } =20 @@ -1970,6 +1994,18 @@ gdbpy_gdb_exiting (int exit_code) static bool do_start_initialization () { + /* Define all internal modules. These are all imported (and thus + created) during initialization. */ + struct _inittab mods[] =3D + { + { "_gdb", init__gdb_module }, + { "_gdbevents", gdbpy_events_mod_func }, + { nullptr, nullptr } + }; + + if (PyImport_ExtendInittab (mods) < 0) + return false; + #ifdef WITH_PYTHON_PATH /* Work around problem where python gets confused about where it is, and then can't find its libraries, etc. @@ -2001,25 +2037,41 @@ do_start_initialization () } setlocale (LC_ALL, oldloc.c_str ()); =20 + /* Py_SetProgramName was deprecated in Python 3.11. Use PyConfig + mechanisms for Python 3.10 and newer. */ +#if PY_VERSION_HEX < 0x030a0000 /* Note that Py_SetProgramName expects the string it is passed to remain alive for the duration of the program's execution, so it is not freed after this call. */ Py_SetProgramName (progname_copy); -#endif + Py_Initialize (); +#else + PyConfig config; =20 - /* Define all internal modules. These are all imported (and thus - created) during initialization. */ - struct _inittab mods[3] =3D - { - { "_gdb", init__gdb_module }, - { "_gdbevents", gdbpy_events_mod_func }, - { nullptr, nullptr } - }; + PyConfig_InitPythonConfig (&config); + PyStatus status =3D PyConfig_SetString (&config, &config.program_name, + progname_copy); + if (PyStatus_Exception (status)) + goto init_done; =20 - if (PyImport_ExtendInittab (mods) < 0) - return false; + config.write_bytecode =3D python_write_bytecode (); + config.use_environment =3D !python_ignore_environment; =20 + status =3D PyConfig_Read (&config); + if (PyStatus_Exception (status)) + goto init_done; + + status =3D Py_InitializeFromConfig (&config); + +init_done: + PyConfig_Clear (&config); + if (PyStatus_Exception (status)) + return false; +#endif +#else Py_Initialize (); +#endif + #if PY_VERSION_HEX < 0x03090000 /* PyEval_InitThreads became deprecated in Python 3.9 and will be removed in Python 3.11. Prior to Python 3.7, this call was @@ -2325,12 +2377,23 @@ do_initialize (const struct extension_language_defn= *extlang) =20 sys_path =3D PySys_GetObject ("path"); =20 + /* PySys_SetPath was deprecated in Python 3.11. Disable this + deprecated code for Python 3.10 and newer. Also note that this + ifdef eliminates potential initialization of sys.path via + PySys_SetPath. My (kevinb's) understanding of PEP 587 suggests + that it's not necessary due to module_search_paths being + initialized to an empty list following any of the PyConfig + initialization functions. If it does turn out that some kind of + initialization is still needed, it should be added to the + PyConfig-based initialization in do_start_initialize(). */ +#if PY_VERSION_HEX < 0x030a0000 /* If sys.path is not defined yet, define it first. */ if (!(sys_path && PyList_Check (sys_path))) { PySys_SetPath (L""); sys_path =3D PySys_GetObject ("path"); } +#endif if (sys_path && PyList_Check (sys_path)) { gdbpy_ref<> pythondir (PyUnicode_FromString (gdb_pythondir.c_str ())= );