Skip to content

Commit 9197e8e

Browse files
committed
[gdb/python] Reimplement F405 fix
At commit 34b0776^, flake8 reports the following F405 warnings: ... $ pre-commit run flake8 --file gdb/python/lib/gdb/__init__.py flake8...................................................................Failed - hook id: flake8 - exit code: 1 F405 'flush' may be undefined, or defined from star imports: _gdb F405 'write' may be undefined, or defined from star imports: _gdb F405 'STDOUT' may be undefined, or defined from star imports: _gdb F405 'STDERR' may be undefined, or defined from star imports: _gdb ... F405 'selected_inferior' may be undefined, or defined from star imports: _gdb F405 'execute' may be undefined, or defined from star imports: _gdb F405 'parameter' may be undefined, or defined from star imports: _gdb ... The F405s are addressed by commit 34b0776 ('Suppress some "undefined" warnings from flake8'). The problem indicated by the first F405 is that the use of flush here: ... class _GdbFile(object): ... def flush(self): flush(stream=self.stream) ... cannot be verified by flake8. It concludes that either, flush is undefined, or it is defined by this "star import": ... from _gdb import * # noqa: F401,F403 ... In this particular case, indeed flush is defined by the star import. This can be addressed by simply adding: ... flush(stream=self.stream) # noqa: F405 ... but that has only effect for flake8, so other analyzers may report the same problem. The commit 34b0776 addresses it instead by adding an "import _gdb" and adding a "_gdb." prefix: ... _gdb.flush(stream=self.stream) ... This introduces a second way to specify _gdb names, but the first one still remains, and occasionally someone will use the first one, which then requires fixing once flake8 is run [1]. While this works to silence the warnings, there is a problem: if a developer makes a typo: ... _gdb.flash(stream=self.stream) ... this is not detected by flake8. This matters because although the python import already complains: ... $ gdb -q -batch -ex "python import gdb" Exception ignored in: <gdb._GdbFile object at 0x7f6186d4d7f0> Traceback (most recent call last): File "__init__.py", line 63, in flush _gdb.flash(stream=self.stream) AttributeError: module '_gdb' has no attribute 'flash' ... that doesn't trigger if the code is hidden behind some control flow: ... if _var_mostly_false: flash(stream=self.stream) ... Instead, fix the F405s by reverting commit 34b0776 and adding a second import of _gdb alongside the star import which lists the names used locally: ... from _gdb import * # noqa: F401,F403 +from _gdb import ( + STDERR, + STDOUT, + Command, + execute, + flush, + parameter, + selected_inferior, + write, +) ... This gives the following warnings for the flash typo: ... 31:1: F401 '_gdb.flush' imported but unused 70:5: F811 redefinition of unused 'flush' from line 31 71:9: F405 'flash' may be undefined, or defined from star imports: _gdb ... The benefits of this approach compared to the previous one are that: - the typo is noticed, and - when using a new name, the F405 fix needs to be done once (by adding it to the explicit import list), while previously the fix had to be applied to each use (by adding the "_gdb." prefix). Tested on x86_64-linux. Approved-By: Tom Tromey <[email protected]> [1] Commit 475799b ("Fix some pre-commit nits in gdb/__init__.py")
1 parent ceec721 commit 9197e8e

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

gdb/python/lib/gdb/__init__.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@
2121
from contextlib import contextmanager
2222
from importlib import reload
2323

24-
import _gdb
25-
24+
# The star import imports _gdb names. When the names are used locally, they
25+
# trigger F405 warnings unless added to the explicit import list.
2626
# Note that two indicators are needed here to silence flake8.
2727
from _gdb import * # noqa: F401,F403
28+
from _gdb import (
29+
STDERR,
30+
STDOUT,
31+
Command,
32+
execute,
33+
flush,
34+
parameter,
35+
selected_inferior,
36+
write,
37+
)
2838

2939
# isort: split
3040

@@ -55,14 +65,14 @@ def writelines(self, iterable):
5565
self.write(line)
5666

5767
def flush(self):
58-
_gdb.flush(stream=self.stream)
68+
flush(stream=self.stream)
5969

6070
def write(self, s):
61-
_gdb.write(s, stream=self.stream)
71+
write(s, stream=self.stream)
6272

6373

64-
sys.stdout = _GdbFile(_gdb.STDOUT)
65-
sys.stderr = _GdbFile(_gdb.STDERR)
74+
sys.stdout = _GdbFile(STDOUT)
75+
sys.stderr = _GdbFile(STDERR)
6676

6777
# Default prompt hook does nothing.
6878
prompt_hook = None
@@ -184,7 +194,7 @@ def GdbSetPythonDirectory(dir):
184194

185195
def current_progspace():
186196
"Return the current Progspace."
187-
return _gdb.selected_inferior().progspace
197+
return selected_inferior().progspace
188198

189199

190200
def objfiles():
@@ -221,14 +231,14 @@ def set_parameter(name, value):
221231
value = "on"
222232
else:
223233
value = "off"
224-
_gdb.execute("set " + name + " " + str(value), to_string=True)
234+
execute("set " + name + " " + str(value), to_string=True)
225235

226236

227237
@contextmanager
228238
def with_parameter(name, value):
229239
"""Temporarily set the GDB parameter NAME to VALUE.
230240
Note that this is a context manager."""
231-
old_value = _gdb.parameter(name)
241+
old_value = parameter(name)
232242
set_parameter(name, value)
233243
try:
234244
# Nothing that useful to return.
@@ -406,7 +416,7 @@ class ParameterPrefix:
406416
# __doc__ string of its own, then sub-classes will inherit that __doc__
407417
# string, and GDB will not understand that it needs to generate one.
408418

409-
class _PrefixCommand(_gdb.Command):
419+
class _PrefixCommand(Command):
410420
"""A gdb.Command used to implement both the set and show prefixes.
411421
412422
This documentation string is not used as the prefix command

0 commit comments

Comments
 (0)