From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 330B7385AE43; Tue, 5 Jul 2022 16:36:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 330B7385AE43 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Make 'import gdb.events' work X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 736918239b16cc2ff57bfc64a982f2f0afc8c0f6 X-Git-Newrev: 3acd9a692ddaf8f24d6d34cb5ccb7c26d057e9b3 Message-Id: <20220705163602.330B7385AE43@sourceware.org> Date: Tue, 5 Jul 2022 16:36:02 +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: Tue, 05 Jul 2022 16:36:02 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D3acd9a692dda= f8f24d6d34cb5ccb7c26d057e9b3 commit 3acd9a692ddaf8f24d6d34cb5ccb7c26d057e9b3 Author: Tom Tromey Date: Fri Jun 3 07:59:49 2022 -0600 Make 'import gdb.events' work =20 Pierre-Marie noticed that, while gdb.events is a Python module, it can't be imported. This patch changes how this module is created, so that it can be imported, while also ensuring that the module is always visible, just as it was in the past. =20 This new approach required one non-obvious change -- when running gdb.base/warning.exp, where --data-directory is intentionally not found, the event registries can now be nullptr. Consequently, this patch probably also requires =20 https://sourceware.org/pipermail/gdb-patches/2022-June/189796.html =20 Note that this patch obsoletes =20 https://sourceware.org/pipermail/gdb-patches/2022-June/189797.html Diff: --- gdb/python/lib/gdb/__init__.py | 5 +++++ gdb/python/py-evtregistry.c | 4 +++- gdb/python/py-evts.c | 28 +++++++++++++--------------- gdb/python/python-internal.h | 4 ++-- gdb/python/python.c | 16 ++++++++++++---- gdb/testsuite/gdb.python/py-events.exp | 2 ++ 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index a52f6b46413..17ee6a19e14 100644 --- a/gdb/python/lib/gdb/__init__.py +++ b/gdb/python/lib/gdb/__init__.py @@ -27,6 +27,11 @@ else: =20 from _gdb import * =20 +# Historically, gdb.events was always available, so ensure it's +# still available without an explicit import. +import _gdbevents as events +sys.modules['gdb.events'] =3D events + =20 class _GdbFile(object): # These two are needed in Python 3 diff --git a/gdb/python/py-evtregistry.c b/gdb/python/py-evtregistry.c index ef96c483f47..f3a7f0ca244 100644 --- a/gdb/python/py-evtregistry.c +++ b/gdb/python/py-evtregistry.c @@ -118,7 +118,9 @@ gdbpy_initialize_eventregistry (void) bool evregpy_no_listeners_p (eventregistry_object *registry) { - return PyList_Size (registry->callbacks) =3D=3D 0; + /* REGISTRY can be nullptr if gdb failed to find the data directory + at startup. */ + return registry =3D=3D nullptr || PyList_Size (registry->callbacks) =3D= =3D 0; } =20 static PyMethodDef eventregistry_object_methods[] =3D diff --git a/gdb/python/py-evts.c b/gdb/python/py-evts.c index 23a5d75ae21..ca9326a1469 100644 --- a/gdb/python/py-evts.c +++ b/gdb/python/py-evts.c @@ -23,7 +23,7 @@ static struct PyModuleDef EventModuleDef =3D { PyModuleDef_HEAD_INIT, - "gdb.events", + "_gdbevents", NULL, -1, NULL, @@ -33,7 +33,8 @@ static struct PyModuleDef EventModuleDef =3D NULL }; =20 -/* Initialize python events. */ +/* Helper function to add a single event registry to the events + module. */ =20 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION add_new_registry (eventregistry_object **registryp, const char *name) @@ -48,24 +49,21 @@ add_new_registry (eventregistry_object **registryp, con= st char *name) (PyObject *)(*registryp)); } =20 -int -gdbpy_initialize_py_events (void) +/* Create and populate the _gdbevents module. Note that this is + always created, see the base gdb __init__.py. */ + +PyMODINIT_FUNC +gdbpy_events_mod_func () { gdb_py_events.module =3D PyModule_Create (&EventModuleDef); + if (gdb_py_events.module =3D=3D nullptr) + return nullptr; =20 - if (!gdb_py_events.module) - return -1; - -#define GDB_PY_DEFINE_EVENT(name) \ +#define GDB_PY_DEFINE_EVENT(name) \ if (add_new_registry (&gdb_py_events.name, #name) < 0) \ - return -1; + return nullptr; #include "py-all-events.def" #undef GDB_PY_DEFINE_EVENT =20 - if (gdb_pymodule_addobject (gdb_module, - "events", - (PyObject *) gdb_py_events.module) < 0) - return -1; - - return 0; + return gdb_py_events.module; } diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 5ff9989af83..5d296a73e9a 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -521,8 +521,6 @@ int gdbpy_initialize_eventregistry (void) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; int gdbpy_initialize_event (void) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; -int gdbpy_initialize_py_events (void) - CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; int gdbpy_initialize_arch (void) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; int gdbpy_initialize_registers () @@ -543,6 +541,8 @@ void gdbpy_finalize_micommands (); int gdbpy_initialize_disasm () CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; =20 +PyMODINIT_FUNC gdbpy_events_mod_func (); + /* A wrapper for PyErr_Fetch that handles reference counting for the caller. */ class gdbpy_err_fetch diff --git a/gdb/python/python.c b/gdb/python/python.c index 8f526bba84e..2f1a00e629c 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2005,11 +2005,20 @@ do_start_initialization () remain alive for the duration of the program's execution, so it is not freed after this call. */ Py_SetProgramName (progname_copy); - - /* Define _gdb as a built-in module. */ - PyImport_AppendInittab ("_gdb", init__gdb_module); #endif =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 } + }; + + if (PyImport_ExtendInittab (mods) < 0) + return false; + Py_Initialize (); #if PY_VERSION_HEX < 0x03090000 /* PyEval_InitThreads became deprecated in Python 3.9 and will @@ -2077,7 +2086,6 @@ do_start_initialization () || gdbpy_initialize_thread () < 0 || gdbpy_initialize_inferior () < 0 || gdbpy_initialize_eventregistry () < 0 - || gdbpy_initialize_py_events () < 0 || gdbpy_initialize_event () < 0 || gdbpy_initialize_arch () < 0 || gdbpy_initialize_registers () < 0 diff --git a/gdb/testsuite/gdb.python/py-events.exp b/gdb/testsuite/gdb.pyt= hon/py-events.exp index a9ababbc40c..94762739cd4 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -38,6 +38,8 @@ clean_restart ${testfile} =20 if { [skip_python_tests] } { continue } =20 +gdb_test_no_output "python import gdb.events" + set pyfile [gdb_remote_download host ${srcdir}/${subdir}/py-events.py] gdb_test_no_output "source ${pyfile}" "load python file"