From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id AC0463814FD1 for ; Tue, 7 Jun 2022 13:20:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AC0463814FD1 Received: by mail-io1-xd34.google.com with SMTP id i201so9822684ioa.6 for ; Tue, 07 Jun 2022 06:20:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=L8zDkLcocLK0JGUZqm6quR9bfPtr3HKkB609ocuWAWg=; b=zfUX7xKcY2b8GEzGq6ih+z9BYRUUAqCbAiPhFnadLu3MHlMG1eaiCnTM82rZx/U3ZZ 6Wa/g1mqsPnnTtLFCL7berCgkXOFh7IHcrv3eeZDnLX18dd6CeeglACyyPeizYB29y2u iXs8l49Mhc3EXRr5heuOTxi/UFi5liWRhy0PB/gaoh1wk90Lri8XIVsgbmpOcJXMIkJB 6joAI7FMfFHGPicw06a0UA9XihLy42/zH0HQ3gVDeuTH/W88ZZlghwGFvQ0zbf+Igt3I hRX0TwlryZMKuckdS3d+xUk1MJ2fMajhUAJKAma2I1MawX4dILIwLmLdFPwvnZcft85y HEwQ== X-Gm-Message-State: AOAM533ObsSFUtsmCtlceR90rqYn/9Q0UBnKi3nxhk8uHWnq/leWWYcn hvFdGhFitVYS20McB0eRZ+0Q0Jms7+jWYQ== X-Google-Smtp-Source: ABdhPJza+64V//TNgBCH0asjoGa7JDuJ3S7sgmJEt3k5wqj14ra7jt/tgDllKqjioUfYMXtX2GAO1g== X-Received: by 2002:a05:6602:2b02:b0:65a:fea9:c715 with SMTP id p2-20020a0566022b0200b0065afea9c715mr14196782iov.142.1654608032855; Tue, 07 Jun 2022 06:20:32 -0700 (PDT) Received: from murgatroyd.Home (71-211-171-143.hlrn.qwest.net. [71.211.171.143]) by smtp.gmail.com with ESMTPSA id t25-20020a02ab99000000b00331c1e117absm1154218jan.29.2022.06.07.06.20.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 06:20:30 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Make 'import gdb.events' work Date: Tue, 7 Jun 2022 07:20:25 -0600 Message-Id: <20220607132025.2243948-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Jun 2022 13:20:36 -0000 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. 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 https://sourceware.org/pipermail/gdb-patches/2022-June/189796.html Note that this patch obsoletes https://sourceware.org/pipermail/gdb-patches/2022-June/189797.html --- 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: from _gdb import * +# Historically, gdb.events was always available, so ensure it's +# still available without an explicit import. +import _gdbevents as events +sys.modules['gdb.events'] = events + 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) == 0; + /* REGISTRY can be nullptr if gdb failed to find the data directory + at startup. */ + return registry == nullptr || PyList_Size (registry->callbacks) == 0; } static PyMethodDef eventregistry_object_methods[] = 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 = { PyModuleDef_HEAD_INIT, - "gdb.events", + "_gdbevents", NULL, -1, NULL, @@ -33,7 +33,8 @@ static struct PyModuleDef EventModuleDef = NULL }; -/* Initialize python events. */ +/* Helper function to add a single event registry to the events + module. */ 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, const char *name) (PyObject *)(*registryp)); } -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 = PyModule_Create (&EventModuleDef); + if (gdb_py_events.module == nullptr) + return nullptr; - 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 - 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 217bc15bb28..2883bb1bb8d 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 () @@ -541,6 +539,8 @@ int gdbpy_initialize_micommands (void) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; void gdbpy_finalize_micommands (); +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 9bef2252e88..d67fcc60df3 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2000,11 +2000,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 + /* Define all internal modules. These are all imported (and thus + created) during initialization. */ + struct _inittab mods[3] = + { + { "_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 @@ -2071,7 +2080,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.python/py-events.exp index 2fdd2160a44..4feeb029951 100644 --- a/gdb/testsuite/gdb.python/py-events.exp +++ b/gdb/testsuite/gdb.python/py-events.exp @@ -42,6 +42,8 @@ clean_restart ${testfile} if { [skip_python_tests] } { continue } +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" -- 2.34.1