[binutils-gdb] gdb/{testsuite, system-gdbinit}: import gdb module in Python scripts

Simon Marchi simark@sourceware.org
Thu Oct 30 16:26:36 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0c830fc5745dcc4b57765602f9138759c6d09729

commit 0c830fc5745dcc4b57765602f9138759c6d09729
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Wed Oct 29 13:49:14 2025 -0400

    gdb/{testsuite,system-gdbinit}: import gdb module in Python scripts
    
    Fix flake8 warnings like:
    
        gdb/system-gdbinit/wrs-linux.py:21:5: F821 undefined name 'gdb'
    
    These scripts get executed in a context where the gdb module is already
    loaded, so this is not strictly necessary.  However, adding these
    imports removes a lot of red lines when editing these files in an IDE.
    Without them, the code uses this `gdb` thing that appears to be
    undefined.  Pylance is able to pull the module definition from typeshed
    [1] and provide a good experience with typings.
    
    [1] https://github.com/python/typeshed/tree/main/stubs/gdb/gdb
    
    Change-Id: I09c6ae1866ef66f10d8270457771687343c84e32
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/system-gdbinit/wrs-linux.py                            | 2 ++
 gdb/testsuite/gdb.base/bp-disabled-by-cond.py              | 1 +
 gdb/testsuite/gdb.base/frame-view.py                       | 1 +
 gdb/testsuite/gdb.base/reset-catchpoint-cond.py            | 2 ++
 gdb/testsuite/gdb.base/shlib-unload.py                     | 2 ++
 gdb/testsuite/gdb.perf/backtrace.py                        | 2 ++
 gdb/testsuite/gdb.perf/disassemble.py                      | 2 ++
 gdb/testsuite/gdb.perf/single-step.py                      | 2 ++
 gdb/testsuite/gdb.perf/skip-command.py                     | 2 ++
 gdb/testsuite/gdb.perf/skip-prologue.py                    | 2 ++
 gdb/testsuite/gdb.perf/solib.py                            | 2 ++
 gdb/testsuite/gdb.perf/template-breakpoints.py             | 2 ++
 gdb/testsuite/gdb.python/pretty-print-call-by-hand.py      | 3 +++
 gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py | 2 ++
 gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py | 2 ++
 gdb/testsuite/gdb.python/py-color-leak.py                  | 2 ++
 gdb/testsuite/gdb.python/py-corefile.py                    | 2 ++
 gdb/testsuite/gdb.python/py-finish-breakpoint-deletion.py  | 3 +++
 gdb/testsuite/gdb.python/py-finish-breakpoint.py           | 3 +++
 gdb/testsuite/gdb.python/py-finish-breakpoint2.py          | 2 ++
 gdb/testsuite/gdb.python/py-inferior-leak.py               | 2 ++
 gdb/testsuite/gdb.python/py-objfile-script-gdb.py          | 2 ++
 gdb/testsuite/gdb.python/py-read-memory-leak.py            | 2 ++
 gdb/testsuite/gdb.python/py-section-script.py              | 2 ++
 gdb/testsuite/gdb.python/py-watchpoint.py                  | 3 +++
 gdb/testsuite/gdb.python/tui-window-factory.py             | 3 +++
 gdb/testsuite/gdb.server/fileio-packets.py                 | 2 ++
 27 files changed, 57 insertions(+)

diff --git a/gdb/system-gdbinit/wrs-linux.py b/gdb/system-gdbinit/wrs-linux.py
index 7db36bf64f1..7575240cb0e 100644
--- a/gdb/system-gdbinit/wrs-linux.py
+++ b/gdb/system-gdbinit/wrs-linux.py
@@ -17,6 +17,8 @@
 
 import os
 
+import gdb
+
 if "ENV_PREFIX" in os.environ:
     gdb.execute("set sysroot %s" % os.environ["ENV_PREFIX"])
 
diff --git a/gdb/testsuite/gdb.base/bp-disabled-by-cond.py b/gdb/testsuite/gdb.base/bp-disabled-by-cond.py
index 9a939201826..7e7bade237f 100644
--- a/gdb/testsuite/gdb.base/bp-disabled-by-cond.py
+++ b/gdb/testsuite/gdb.base/bp-disabled-by-cond.py
@@ -12,6 +12,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+import gdb
 
 bp_modified_list = []
 
diff --git a/gdb/testsuite/gdb.base/frame-view.py b/gdb/testsuite/gdb.base/frame-view.py
index 832f3485e69..322c2640772 100644
--- a/gdb/testsuite/gdb.base/frame-view.py
+++ b/gdb/testsuite/gdb.base/frame-view.py
@@ -12,6 +12,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+import gdb
 
 
 class Printer1:
diff --git a/gdb/testsuite/gdb.base/reset-catchpoint-cond.py b/gdb/testsuite/gdb.base/reset-catchpoint-cond.py
index 9a939201826..d7a4e8d3567 100644
--- a/gdb/testsuite/gdb.base/reset-catchpoint-cond.py
+++ b/gdb/testsuite/gdb.base/reset-catchpoint-cond.py
@@ -13,6 +13,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import gdb
+
 bp_modified_list = []
 
 
diff --git a/gdb/testsuite/gdb.base/shlib-unload.py b/gdb/testsuite/gdb.base/shlib-unload.py
index ec62caf586d..66ff7a594e3 100644
--- a/gdb/testsuite/gdb.base/shlib-unload.py
+++ b/gdb/testsuite/gdb.base/shlib-unload.py
@@ -13,6 +13,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import gdb
+
 # Breakpoint modification events will be recorded in this dictionary.
 # The keys are the b/p numbers, and the values are the number of
 # modification events seen.
diff --git a/gdb/testsuite/gdb.perf/backtrace.py b/gdb/testsuite/gdb.perf/backtrace.py
index 12f297a115a..e3d3e91418c 100644
--- a/gdb/testsuite/gdb.perf/backtrace.py
+++ b/gdb/testsuite/gdb.perf/backtrace.py
@@ -15,6 +15,8 @@
 
 from perftest import perftest
 
+import gdb
+
 
 class BackTrace(perftest.TestCaseWithBasicMeasurements):
     def __init__(self, depth):
diff --git a/gdb/testsuite/gdb.perf/disassemble.py b/gdb/testsuite/gdb.perf/disassemble.py
index 49050c1fc17..57054698077 100644
--- a/gdb/testsuite/gdb.perf/disassemble.py
+++ b/gdb/testsuite/gdb.perf/disassemble.py
@@ -15,6 +15,8 @@
 
 from perftest import perftest
 
+import gdb
+
 
 class Disassemble(perftest.TestCaseWithBasicMeasurements):
     def __init__(self):
diff --git a/gdb/testsuite/gdb.perf/single-step.py b/gdb/testsuite/gdb.perf/single-step.py
index c6b1443a727..eecf2f982a0 100644
--- a/gdb/testsuite/gdb.perf/single-step.py
+++ b/gdb/testsuite/gdb.perf/single-step.py
@@ -15,6 +15,8 @@
 
 from perftest import perftest
 
+import gdb
+
 
 class SingleStep(perftest.TestCaseWithBasicMeasurements):
     def __init__(self, step):
diff --git a/gdb/testsuite/gdb.perf/skip-command.py b/gdb/testsuite/gdb.perf/skip-command.py
index f0baf603040..b344c9c36a3 100644
--- a/gdb/testsuite/gdb.perf/skip-command.py
+++ b/gdb/testsuite/gdb.perf/skip-command.py
@@ -15,6 +15,8 @@
 
 from perftest import perftest
 
+import gdb
+
 
 class SkipCommand(perftest.TestCaseWithBasicMeasurements):
     def __init__(self, name, step):
diff --git a/gdb/testsuite/gdb.perf/skip-prologue.py b/gdb/testsuite/gdb.perf/skip-prologue.py
index a41b7a7a159..9dd0ca7c83a 100644
--- a/gdb/testsuite/gdb.perf/skip-prologue.py
+++ b/gdb/testsuite/gdb.perf/skip-prologue.py
@@ -18,6 +18,8 @@
 
 from perftest import perftest
 
+import gdb
+
 
 class SkipPrologue(perftest.TestCaseWithBasicMeasurements):
     def __init__(self, count):
diff --git a/gdb/testsuite/gdb.perf/solib.py b/gdb/testsuite/gdb.perf/solib.py
index 501eb6954e5..d7266bf55bf 100644
--- a/gdb/testsuite/gdb.perf/solib.py
+++ b/gdb/testsuite/gdb.perf/solib.py
@@ -18,6 +18,8 @@
 
 from perftest import measure, perftest
 
+import gdb
+
 
 class SolibLoadUnload1(perftest.TestCaseWithBasicMeasurements):
     def __init__(self, solib_count, measure_load):
diff --git a/gdb/testsuite/gdb.perf/template-breakpoints.py b/gdb/testsuite/gdb.perf/template-breakpoints.py
index 494d571761c..82dc5b511a1 100644
--- a/gdb/testsuite/gdb.perf/template-breakpoints.py
+++ b/gdb/testsuite/gdb.perf/template-breakpoints.py
@@ -15,6 +15,8 @@
 
 from perftest import perftest
 
+import gdb
+
 
 class TemplateBreakpoints(perftest.TestCaseWithBasicMeasurements):
     def __init__(self):
diff --git a/gdb/testsuite/gdb.python/pretty-print-call-by-hand.py b/gdb/testsuite/gdb.python/pretty-print-call-by-hand.py
index 3620354548a..48464ed72bd 100644
--- a/gdb/testsuite/gdb.python/pretty-print-call-by-hand.py
+++ b/gdb/testsuite/gdb.python/pretty-print-call-by-hand.py
@@ -14,6 +14,9 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import gdb
+
+
 class MytypePrinter:
     """pretty print my type"""
 
diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py b/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py
index 9641a86049e..7c107ec9842 100644
--- a/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py
+++ b/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py
@@ -18,6 +18,8 @@
 
 import re
 
+import gdb
+
 print("Entering f1.o auto-load script")
 
 print("Current objfile is: %s" % gdb.current_objfile().filename)
diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py b/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py
index f1abc2cbda6..6c3311810ca 100644
--- a/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py
+++ b/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py
@@ -16,6 +16,8 @@
 # This script is auto-loaded when the py-auto-load-chaining-f2.o
 # object is loaded.
 
+import gdb
+
 print("Entering f2.o auto-load script")
 
 print("Current objfile is: %s" % gdb.current_objfile().filename)
diff --git a/gdb/testsuite/gdb.python/py-color-leak.py b/gdb/testsuite/gdb.python/py-color-leak.py
index 28afd59bfcb..d903ee344e5 100644
--- a/gdb/testsuite/gdb.python/py-color-leak.py
+++ b/gdb/testsuite/gdb.python/py-color-leak.py
@@ -15,6 +15,8 @@
 
 import sys
 
+import gdb
+
 # Avoid generating
 # src/gdb/testsuite/gdb.python/__pycache__/gdb_leak_detector.cpython-<n>.pyc.
 sys.dont_write_bytecode = True
diff --git a/gdb/testsuite/gdb.python/py-corefile.py b/gdb/testsuite/gdb.python/py-corefile.py
index bfb1c820d5d..0ea6d71f96a 100644
--- a/gdb/testsuite/gdb.python/py-corefile.py
+++ b/gdb/testsuite/gdb.python/py-corefile.py
@@ -15,6 +15,8 @@
 
 import pathlib
 
+import gdb
+
 
 class Mapping:
     def __init__(self, mapping, region):
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint-deletion.py b/gdb/testsuite/gdb.python/py-finish-breakpoint-deletion.py
index 8d8ca53deaf..d8235e3ac7d 100644
--- a/gdb/testsuite/gdb.python/py-finish-breakpoint-deletion.py
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint-deletion.py
@@ -14,6 +14,9 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import gdb
+
+
 class MyFinishBreakpoint(gdb.FinishBreakpoint):
     def stop(self):
         print("stopped at MyFinishBreakpoint")
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint.py b/gdb/testsuite/gdb.python/py-finish-breakpoint.py
index 413489f68fd..dd2e158c56c 100644
--- a/gdb/testsuite/gdb.python/py-finish-breakpoint.py
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint.py
@@ -17,6 +17,9 @@
 # Breakpoints.
 
 
+import gdb
+
+
 class MyFinishBreakpoint(gdb.FinishBreakpoint):
     def __init__(self, val, frame):
         gdb.FinishBreakpoint.__init__(self, frame)
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint2.py b/gdb/testsuite/gdb.python/py-finish-breakpoint2.py
index ae75e518b1b..01d220c6970 100644
--- a/gdb/testsuite/gdb.python/py-finish-breakpoint2.py
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint2.py
@@ -16,6 +16,8 @@
 # This file is part of the GDB testsuite.  It tests python Finish
 # Breakpoints.
 
+import gdb
+
 
 class ExceptionFinishBreakpoint(gdb.FinishBreakpoint):
     def __init__(self, frame):
diff --git a/gdb/testsuite/gdb.python/py-inferior-leak.py b/gdb/testsuite/gdb.python/py-inferior-leak.py
index bf61668532b..4ee4dc232eb 100644
--- a/gdb/testsuite/gdb.python/py-inferior-leak.py
+++ b/gdb/testsuite/gdb.python/py-inferior-leak.py
@@ -15,6 +15,8 @@
 
 import sys
 
+import gdb
+
 # Avoid generating
 # src/gdb/testsuite/gdb.python/__pycache__/gdb_leak_detector.cpython-<n>.pyc.
 sys.dont_write_bytecode = True
diff --git a/gdb/testsuite/gdb.python/py-objfile-script-gdb.py b/gdb/testsuite/gdb.python/py-objfile-script-gdb.py
index 86660741419..6661700d62c 100644
--- a/gdb/testsuite/gdb.python/py-objfile-script-gdb.py
+++ b/gdb/testsuite/gdb.python/py-objfile-script-gdb.py
@@ -17,6 +17,8 @@
 
 import re
 
+import gdb
+
 
 class pp_ss:
     def __init__(self, val):
diff --git a/gdb/testsuite/gdb.python/py-read-memory-leak.py b/gdb/testsuite/gdb.python/py-read-memory-leak.py
index 89647cf2785..a58ebfc09f7 100644
--- a/gdb/testsuite/gdb.python/py-read-memory-leak.py
+++ b/gdb/testsuite/gdb.python/py-read-memory-leak.py
@@ -15,6 +15,8 @@
 
 import sys
 
+import gdb
+
 # Avoid generating
 # src/gdb/testsuite/gdb.python/__pycache__/gdb_leak_detector.cpython-<n>.pyc.
 sys.dont_write_bytecode = True
diff --git a/gdb/testsuite/gdb.python/py-section-script.py b/gdb/testsuite/gdb.python/py-section-script.py
index d85221e3a79..fb7bef3c5c8 100644
--- a/gdb/testsuite/gdb.python/py-section-script.py
+++ b/gdb/testsuite/gdb.python/py-section-script.py
@@ -17,6 +17,8 @@
 
 import re
 
+import gdb
+
 
 class pp_ss:
     def __init__(self, val):
diff --git a/gdb/testsuite/gdb.python/py-watchpoint.py b/gdb/testsuite/gdb.python/py-watchpoint.py
index d25826fad7c..9d1ea5e38b5 100644
--- a/gdb/testsuite/gdb.python/py-watchpoint.py
+++ b/gdb/testsuite/gdb.python/py-watchpoint.py
@@ -14,6 +14,9 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import gdb
+
+
 class MyBreakpoint(gdb.Breakpoint):
     def __init__(self):
         super().__init__("i", gdb.BP_WATCHPOINT)
diff --git a/gdb/testsuite/gdb.python/tui-window-factory.py b/gdb/testsuite/gdb.python/tui-window-factory.py
index 75932147895..9ad75f78f48 100644
--- a/gdb/testsuite/gdb.python/tui-window-factory.py
+++ b/gdb/testsuite/gdb.python/tui-window-factory.py
@@ -14,6 +14,9 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import gdb
+
+
 class TestWindow:
     def __init__(self, tui_win, msg):
         self.msg = msg
diff --git a/gdb/testsuite/gdb.server/fileio-packets.py b/gdb/testsuite/gdb.server/fileio-packets.py
index f132e91a65d..d76cb9a7440 100644
--- a/gdb/testsuite/gdb.server/fileio-packets.py
+++ b/gdb/testsuite/gdb.server/fileio-packets.py
@@ -16,6 +16,8 @@
 import os
 import stat
 
+import gdb
+
 
 # Hex encode INPUT_STRING in the same way that GDB does.  Each
 # character in INPUT_STRING is expanded to its two digit hex


More information about the Gdb-cvs mailing list