From 7221a371b361672907d9f80be772f1610b65505e Mon Sep 17 00:00:00 2001 From: Gulshan Singh Date: Mon, 26 Dec 2022 13:52:25 -0800 Subject: [PATCH] More type hints (#1472) --- pwndbg/commands/comments.py | 2 +- pwndbg/commands/context.py | 5 +++-- pwndbg/commands/heap.py | 2 +- pwndbg/commands/leakfind.py | 3 ++- pwndbg/commands/start.py | 6 ++---- pwndbg/commands/telescope.py | 3 ++- pwndbg/gdblib/elf.py | 2 +- pwndbg/gdblib/events.py | 2 +- pwndbg/gdblib/regs.py | 3 ++- pwndbg/lib/config.py | 3 ++- pwndbg/ui.py | 2 +- pyproject.toml | 4 ++-- 12 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pwndbg/commands/comments.py b/pwndbg/commands/comments.py index 8cb74df28..e87031ef1 100644 --- a/pwndbg/commands/comments.py +++ b/pwndbg/commands/comments.py @@ -10,7 +10,7 @@ parser.add_argument( ) parser.add_argument("comment", type=str, default=None, help="The text you want to comment") -file_lists = {} # type:Dict[str,str] #This saves all comments. +file_lists: Dict[str, Dict[str, str]] = {} # This saves all comments. @pwndbg.commands.ArgparsedCommand(parser) diff --git a/pwndbg/commands/context.py b/pwndbg/commands/context.py index bfd9b680d..ed327c118 100644 --- a/pwndbg/commands/context.py +++ b/pwndbg/commands/context.py @@ -4,6 +4,7 @@ import os import sys from collections import defaultdict from io import open +from typing import DefaultDict from typing import Dict import gdb @@ -308,7 +309,7 @@ def context_expressions(target=sys.stdout, with_banner=True, width=None): except gdb.error as err: value = str(err) value = value.split("\n") - lines = [] + lines: List[str] = [] for line in value: if width and len(line) + len(exp) + 3 > width: n = width - (len(exp) + 3) - 1 # 1 Padding... @@ -388,7 +389,7 @@ def context(subcontext=None) -> None: sections += [(arg, context_sections.get(arg[0], None)) for arg in args] result = defaultdict(list) - result_settings = defaultdict(dict) + result_settings: DefaultDict[str, Dict] = defaultdict(dict) for section, func in sections: if func: target = output(section) diff --git a/pwndbg/commands/heap.py b/pwndbg/commands/heap.py index e8ae8d08d..26988f495 100644 --- a/pwndbg/commands/heap.py +++ b/pwndbg/commands/heap.py @@ -552,7 +552,7 @@ def find_fake_fast(addr, size=None, align=False) -> None: if size is None: size = max_fast elif size > addr: - print(message.warn("Size of 0x%x is greater than the target address 0x%x", (size, addr))) + print(message.warn("Size of 0x%x is greater than the target address 0x%x" % (size, addr))) size = addr elif size > max_fast: print( diff --git a/pwndbg/commands/leakfind.py b/pwndbg/commands/leakfind.py index 8a289b810..01a5695d3 100644 --- a/pwndbg/commands/leakfind.py +++ b/pwndbg/commands/leakfind.py @@ -4,6 +4,7 @@ Find a chain of leaks given some starting address. import argparse import queue +from typing import Dict import gdb @@ -162,7 +163,7 @@ def leakfind( break # A map of length->list of lines. Used to let us print in a somewhat nice manner. - output_map = {} + output_map: Dict[int, List[str]] = {} arrow_right = C.arrow(" %s " % pwndbg.gdblib.config.chain_arrow_right) for child in visited_map: diff --git a/pwndbg/commands/start.py b/pwndbg/commands/start.py index 7bf796b23..1e68b248b 100644 --- a/pwndbg/commands/start.py +++ b/pwndbg/commands/start.py @@ -98,15 +98,13 @@ To start the inferior without using a shell, use "set startup-with-shell off". """, ) parser.add_argument( - "args", nargs="*", type=str, default=None, help="The arguments to run the binary with." + "args", nargs="*", type=str, default=[], help="The arguments to run the binary with." ) @pwndbg.commands.ArgparsedCommand(parser) @pwndbg.commands.OnlyWithFile -def entry(args=None) -> None: - if args is None: - arg = [] +def entry(args=[]) -> None: global break_on_first_instruction break_on_first_instruction = True run = "run " + " ".join(map(quote, args)) diff --git a/pwndbg/commands/telescope.py b/pwndbg/commands/telescope.py index 1175eaabc..bfaac9e0b 100644 --- a/pwndbg/commands/telescope.py +++ b/pwndbg/commands/telescope.py @@ -7,6 +7,7 @@ Generally used to print out the stack or register values. import argparse import collections import math +from typing import List import pwndbg.chain import pwndbg.color.telescope as T @@ -126,7 +127,7 @@ def telescope(address=None, count=telescope_lines, to_string=False, reverse=Fals # Print everything out result = [] last = None - collapse_buffer = [] + collapse_buffer: List[str] = [] skipped_padding = ( 2 + len(offset_delimiter) diff --git a/pwndbg/gdblib/elf.py b/pwndbg/gdblib/elf.py index 726b8245d..3d4f174b4 100644 --- a/pwndbg/gdblib/elf.py +++ b/pwndbg/gdblib/elf.py @@ -351,7 +351,7 @@ def map_inner(ei_class, ehdr, objfile): # Entries are processed in-order so that later entries # which change page permissions (e.g. PT_GNU_RELRO) will # override their small subset of address space. - pages = [] + pages: List[pwndbg.lib.memory.Page] = [] for phdr in iter_phdrs(ehdr): memsz = int(phdr.p_memsz) diff --git a/pwndbg/gdblib/events.py b/pwndbg/gdblib/events.py index 07281ea9f..45f24d8a7 100644 --- a/pwndbg/gdblib/events.py +++ b/pwndbg/gdblib/events.py @@ -35,7 +35,7 @@ debug = config.add_param("debug-events", False, "display internal event debuggin # capture this so that we can fire off all of the 'start' events first. class StartEvent: def __init__(self) -> None: - self.registered = list() + self.registered: List[Callable] = [] self.running = False def connect(self, function) -> None: diff --git a/pwndbg/gdblib/regs.py b/pwndbg/gdblib/regs.py index 8443ad30c..af7393095 100644 --- a/pwndbg/gdblib/regs.py +++ b/pwndbg/gdblib/regs.py @@ -7,6 +7,7 @@ import re import sys from types import ModuleType from typing import Dict +from typing import List import gdb @@ -134,7 +135,7 @@ class module(ModuleType): @property def all(self): regs = reg_sets[pwndbg.gdblib.arch.current] - retval = [] + retval: List[str] = [] for regset in ( regs.pc, regs.stack, diff --git a/pwndbg/lib/config.py b/pwndbg/lib/config.py index d07de8fab..5abd5467f 100644 --- a/pwndbg/lib/config.py +++ b/pwndbg/lib/config.py @@ -1,5 +1,6 @@ import collections from functools import total_ordering +from typing import DefaultDict from typing import List import gdb @@ -126,7 +127,7 @@ class Parameter: class Config: def __init__(self) -> None: self.params: Dict[str, Parameter] = {} - self.triggers = collections.defaultdict(lambda: []) + self.triggers: DefaultDict[str, Callable] = collections.defaultdict(lambda: []) def add_param( self, diff --git a/pwndbg/ui.py b/pwndbg/ui.py index 10ba2f97b..22ffce9da 100644 --- a/pwndbg/ui.py +++ b/pwndbg/ui.py @@ -70,7 +70,7 @@ def get_window_size(target=sys.stdin): return fallback try: # get terminal size and force ret buffer len of 4 bytes for safe unpacking by passing equally long arg - rows, cols = struct.unpack("hh", fcntl.ioctl(target.fileno(), termios.TIOCGWINSZ, "1234")) + rows, cols = struct.unpack("hh", fcntl.ioctl(target.fileno(), termios.TIOCGWINSZ, b"1234")) except Exception: rows, cols = fallback return rows, cols diff --git a/pyproject.toml b/pyproject.toml index 79fc0161f..8abb24cfd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,12 +25,12 @@ disable_error_code = [ "name-defined", "attr-defined", # "var-annotated", - "index", + # "index", # "arg-type", "assignment", # "operator", "override", - "call-overload", + # "call-overload", "union-attr", # "str-format", # "call-arg",