From b4f767797a7c6613516e0da3b1e3d0398ba7d176 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 29 Oct 2025 13:49:21 -0400 Subject: [PATCH] gdb/testsuite: remove unused but set variables in Python files Fix flake8 errors like this one: gdb/testsuite/gdb.python/py-unwind-inline.py:61:17: F841 local variable 'v' is assigned to but never used For this one, there is a slight possibility that removing a variable changes some behavior, and perhaps renders some test uneffective (where the test would no longer exercises what it meant to). Removing a variable means that the object it pointed to likely gets de-allocated earlier (its tp_dealloc method gets called). So if the intent of the variable was to ensure the variable outlives the statements that come after, then this change would be wrong. But I didn't see any evidence of that being the intent in all the occurences. Change-Id: Ic57bc68ad225a43ae6771c47b7f443956e8029a6 Approved-By: Tom Tromey --- .../gdb.multi/multi-target-info-inferiors.py | 5 --- .../gdb.python/pretty-print-call-by-hand.py | 2 +- gdb/testsuite/gdb.python/py-bad-printers.py | 2 - gdb/testsuite/gdb.python/py-corefile.py | 1 - gdb/testsuite/gdb.python/py-disasm.py | 38 ++++++++----------- gdb/testsuite/gdb.python/py-frame-args.py | 1 - .../gdb.python/py-pp-registration.py | 4 -- gdb/testsuite/gdb.python/py-recurse-unwind.py | 13 ++----- gdb/testsuite/gdb.python/py-unwind-inline.py | 2 +- .../gdb.python/tui-window-disabled.py | 1 - 10 files changed, 22 insertions(+), 47 deletions(-) diff --git a/gdb/testsuite/gdb.multi/multi-target-info-inferiors.py b/gdb/testsuite/gdb.multi/multi-target-info-inferiors.py index 59557aee028..3d37c724e3b 100644 --- a/gdb/testsuite/gdb.multi/multi-target-info-inferiors.py +++ b/gdb/testsuite/gdb.multi/multi-target-info-inferiors.py @@ -54,14 +54,9 @@ def info_connections(): print(fmt % (prefix, c.num, make_target_connection_string(c), c.description)) -def inf_num(i): - return i.num - - # Print information about each inferior, and the connection it is # using. def info_inferiors(): - all_inferiors = sorted(gdb.inferiors(), key=inf_num) for i in gdb.inferiors(): print( "Inferior %d, Connection #%d: %s" 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 48464ed72bd..57af869fdcf 100644 --- a/gdb/testsuite/gdb.python/pretty-print-call-by-hand.py +++ b/gdb/testsuite/gdb.python/pretty-print-call-by-hand.py @@ -24,7 +24,7 @@ class MytypePrinter: self.val = val def to_string(self): - calls = gdb.parse_and_eval("f()") + gdb.parse_and_eval("f()") return "mytype is %s" % self.val["x"] diff --git a/gdb/testsuite/gdb.python/py-bad-printers.py b/gdb/testsuite/gdb.python/py-bad-printers.py index 23973ebcb4d..b4f84bc695a 100644 --- a/gdb/testsuite/gdb.python/py-bad-printers.py +++ b/gdb/testsuite/gdb.python/py-bad-printers.py @@ -33,7 +33,6 @@ class BadChildrenContainerPrinter1(object): @staticmethod def _bad_iterator(pointer, len): - start = pointer end = pointer + len while pointer != end: yield "intentional violation of children iterator protocol" @@ -54,7 +53,6 @@ class BadChildrenContainerPrinter2(object): @staticmethod def _bad_iterator(pointer, len): - start = pointer end = pointer + len while pointer != end: # The first argument is supposed to be a string. diff --git a/gdb/testsuite/gdb.python/py-corefile.py b/gdb/testsuite/gdb.python/py-corefile.py index 0ea6d71f96a..ab3d1b74d75 100644 --- a/gdb/testsuite/gdb.python/py-corefile.py +++ b/gdb/testsuite/gdb.python/py-corefile.py @@ -116,7 +116,6 @@ class ShowBuildIds(gdb.Command): if len(b) > longest_build_id: longest_build_id = len(b) - count = 0 core_mapped_files = inf.corefile.mapped_files() for m in core_mapped_files: p = pathlib.Path(m.filename).resolve() diff --git a/gdb/testsuite/gdb.python/py-disasm.py b/gdb/testsuite/gdb.python/py-disasm.py index 95fe9ff4117..d88c7a24f29 100644 --- a/gdb/testsuite/gdb.python/py-disasm.py +++ b/gdb/testsuite/gdb.python/py-disasm.py @@ -39,9 +39,7 @@ def builtin_disassemble_wrapper(info): def check_building_disassemble_result(): """Check that we can create DisassembleResult objects correctly.""" - - result = gdb.disassembler.DisassemblerResult() - + gdb.disassembler.DisassemblerResult() print("PASS") @@ -230,7 +228,7 @@ class GdbErrorLateDisassembler(TestDisassembler): """Raise a GdbError after calling the builtin disassembler.""" def disassemble(self, info): - result = builtin_disassemble_wrapper(info) + builtin_disassemble_wrapper(info) raise gdb.GdbError("GdbError after builtin disassembler") @@ -238,7 +236,7 @@ class RuntimeErrorLateDisassembler(TestDisassembler): """Raise a RuntimeError after calling the builtin disassembler.""" def disassemble(self, info): - result = builtin_disassemble_wrapper(info) + builtin_disassemble_wrapper(info) raise RuntimeError("RuntimeError after builtin disassembler") @@ -261,7 +259,7 @@ class MemoryErrorLateDisassembler(TestDisassembler): before we return a result.""" def disassemble(self, info): - result = builtin_disassemble_wrapper(info) + builtin_disassemble_wrapper(info) # The following read will throw an error. info.read_memory(1, -info.address - 1) return DisassemblerResult(1, "BAD") @@ -273,7 +271,7 @@ class RethrowMemoryErrorDisassembler(TestDisassembler): def disassemble(self, info): try: info.read_memory(1, -info.address - 1) - except gdb.MemoryError as e: + except gdb.MemoryError: raise gdb.MemoryError("cannot read code at address -1") return DisassemblerResult(1, "BAD") @@ -353,7 +351,7 @@ class GlobalCachingDisassembler(TestDisassembler): assert isinstance(info, gdb.disassembler.DisassembleInfo) assert not info.is_valid() try: - val = info.address + info.address raise gdb.GdbError("DisassembleInfo.address is still valid") except RuntimeError as e: assert str(e) == "DisassembleInfo is no longer valid." @@ -363,7 +361,7 @@ class GlobalCachingDisassembler(TestDisassembler): ) try: - val = info.architecture + info.architecture raise gdb.GdbError("DisassembleInfo.architecture is still valid") except RuntimeError as e: assert str(e) == "DisassembleInfo is no longer valid." @@ -373,7 +371,7 @@ class GlobalCachingDisassembler(TestDisassembler): ) try: - val = info.read_memory(1, 0) + info.read_memory(1, 0) raise gdb.GdbError("DisassembleInfo.read is still valid") except RuntimeError as e: assert str(e) == "DisassembleInfo is no longer valid." @@ -542,32 +540,28 @@ class ErrorCreatingTextPart_NoArgs(TestDisassembler): """Try to create a DisassemblerTextPart with no arguments.""" def disassemble(self, info): - part = info.text_part() - return None + info.text_part() class ErrorCreatingAddressPart_NoArgs(TestDisassembler): """Try to create a DisassemblerAddressPart with no arguments.""" def disassemble(self, info): - part = info.address_part() - return None + info.address_part() class ErrorCreatingTextPart_NoString(TestDisassembler): """Try to create a DisassemblerTextPart with no string argument.""" def disassemble(self, info): - part = info.text_part(gdb.disassembler.STYLE_TEXT) - return None + info.text_part(gdb.disassembler.STYLE_TEXT) class ErrorCreatingTextPart_NoStyle(TestDisassembler): """Try to create a DisassemblerTextPart with no string argument.""" def disassemble(self, info): - part = info.text_part(string="abc") - return None + info.text_part(string="abc") class ErrorCreatingTextPart_StringAndParts(TestDisassembler): @@ -771,8 +765,8 @@ class AnalyzingDisassembler(Disassembler): idx > 0 and idx != nop_idx and not is_nop(self._pass_1_insn[idx]) - and self._pass_1_length[idx] > self._pass_1_length[nop_idx] - and self._pass_1_length[idx] % self._pass_1_length[nop_idx] == 0 + and self._pass_1_length[idx] > nop_length + and self._pass_1_length[idx] % nop_length == 0 ): replace_idx = idx break @@ -786,7 +780,7 @@ class AnalyzingDisassembler(Disassembler): idx > 0 and idx != nop_idx and not is_nop(self._pass_1_insn[idx]) - and self._pass_1_length[idx] == self._pass_1_length[nop_idx] + and self._pass_1_length[idx] == nop_length ): replace_idx = idx break @@ -807,7 +801,7 @@ class AnalyzingDisassembler(Disassembler): # is a copy of _pass_1_insn, but replace the instruction we # identified above with a series of 'nop' instructions. self._check = list(self._pass_1_insn) - nop_count = int(self._pass_1_length[replace_idx] / self._pass_1_length[nop_idx]) + nop_count = int(self._pass_1_length[replace_idx] / nop_length) nop_insn = self._pass_1_insn[nop_idx] nops = [nop_insn] * nop_count self._check[replace_idx : (replace_idx + 1)] = nops diff --git a/gdb/testsuite/gdb.python/py-frame-args.py b/gdb/testsuite/gdb.python/py-frame-args.py index 45476b1ead4..b72b45349ae 100644 --- a/gdb/testsuite/gdb.python/py-frame-args.py +++ b/gdb/testsuite/gdb.python/py-frame-args.py @@ -23,7 +23,6 @@ class pp_s(object): self.val = val def to_string(self): - m = self.val["m"] return "m=<" + str(self.val["m"]) + ">" diff --git a/gdb/testsuite/gdb.python/py-pp-registration.py b/gdb/testsuite/gdb.python/py-pp-registration.py index 4f39e426507..02ef2d26d47 100644 --- a/gdb/testsuite/gdb.python/py-pp-registration.py +++ b/gdb/testsuite/gdb.python/py-pp-registration.py @@ -40,8 +40,6 @@ class pp_s1(object): self.val = val def to_string(self): - a = self.val["a"] - b = self.val["b"] return "s1 a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" @@ -50,8 +48,6 @@ class pp_s2(object): self.val = val def to_string(self): - a = self.val["a"] - b = self.val["b"] return "s2 a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" diff --git a/gdb/testsuite/gdb.python/py-recurse-unwind.py b/gdb/testsuite/gdb.python/py-recurse-unwind.py index 244c6939b20..51bc600de07 100644 --- a/gdb/testsuite/gdb.python/py-recurse-unwind.py +++ b/gdb/testsuite/gdb.python/py-recurse-unwind.py @@ -59,20 +59,15 @@ class TestUnwinder(Unwinder): TestUnwinder.inc_count() if TestUnwinder.test == "check_user_reg_pc": - pc = pending_frame.read_register("pc") - pc_as_int = int(pc.cast(gdb.lookup_type("int"))) - # gdb.write("In unwinder: pc=%x\n" % pc_as_int) + pending_frame.read_register("pc") elif TestUnwinder.test == "check_pae_pc": - pc = gdb.parse_and_eval("$pc") - pc_as_int = int(pc.cast(gdb.lookup_type("int"))) - # gdb.write("In unwinder: pc=%x\n" % pc_as_int) + gdb.parse_and_eval("$pc") elif TestUnwinder.test == "check_undefined_symbol": try: - val = gdb.parse_and_eval("undefined_symbol") - - except Exception as arg: + gdb.parse_and_eval("undefined_symbol") + except Exception: pass self.recurse_level -= 1 diff --git a/gdb/testsuite/gdb.python/py-unwind-inline.py b/gdb/testsuite/gdb.python/py-unwind-inline.py index 1bdfd93527c..95310b779bd 100644 --- a/gdb/testsuite/gdb.python/py-unwind-inline.py +++ b/gdb/testsuite/gdb.python/py-unwind-inline.py @@ -58,7 +58,7 @@ class dummy_unwinder(Unwinder): to see if the unwinder should claim it, which is never does.""" try: for r in self.get_regs(pending_frame): - v = pending_frame.read_register(r).cast(self.void_ptr_t) + pending_frame.read_register(r).cast(self.void_ptr_t) except Exception: print("Dummy unwinder, exception") raise diff --git a/gdb/testsuite/gdb.python/tui-window-disabled.py b/gdb/testsuite/gdb.python/tui-window-disabled.py index 2adde773ce8..e7195eaac04 100644 --- a/gdb/testsuite/gdb.python/tui-window-disabled.py +++ b/gdb/testsuite/gdb.python/tui-window-disabled.py @@ -77,7 +77,6 @@ class EventWindow: def render(self): self._win.erase() - w = self._win.width h = self._win.height for i in range(min(h, len(self._events))): self._win.write(self._events[i] + "\n") -- 2.47.3