From dfe844c200fd41232e20ef4e8fde83899beeb8dd Mon Sep 17 00:00:00 2001 From: bzoracler <50305397+bzoracler@users.noreply.github.com> Date: Fri, 6 Jan 2023 05:07:36 +1300 Subject: [PATCH] `gdb-stubs` fixes (#9439) * fix: Union subprinters with `None` See https://github.com/bminor/binutils-gdb/blob/a4418a9c6f99fd31c51698b1f6a6f8dbc1b81b6f/gdb/python/lib/gdb/printing.py#L52-L55 * fix: Allow callables as argument to `printer` See https://github.com/bminor/binutils-gdb/blob/a4418a9c6f99fd31c51698b1f6a6f8dbc1b81b6f/gdb/python/lib/gdb/printing.py#L77 and the description of "function / old way" in the body of `register_pretty_printer`. The new union's signature is equivalent to `gdb.printing.PrettyPrinter(...).__call__`. * fix: make `gdb.Block` iterable over `gdb.Symbol` See https://sourceware.org/gdb/onlinedocs/gdb/Blocks-In-Python.html#Blocks-In-Python: > A gdb.Block is iterable. The iterator returns the symbols (see [Symbols In Python](https://sourceware.org/gdb/onlinedocs/gdb/Symbols-In-Python.html#Symbols-In-Python)) local to the block. Implementation of `gdb.BlockIterator` is given in https://github.com/bminor/binutils-gdb/blob/gdb-12-branch/gdb/python/py-block.c. As with many of the other classes, `BlockIterator` is actually imported from the built-in `_gdb` module (https://github.com/bminor/binutils-gdb/blob/a4418a9c6f99fd31c51698b1f6a6f8dbc1b81b6f/gdb/python/lib/gdb/__init__.py#L28). Co-authored-by: Alex Waygood --- stubs/gdb/gdb/__init__.pyi | 6 ++++++ stubs/gdb/gdb/printing.pyi | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/stubs/gdb/gdb/__init__.pyi b/stubs/gdb/gdb/__init__.pyi index 7cf1c35d6..d96547f80 100644 --- a/stubs/gdb/gdb/__init__.pyi +++ b/stubs/gdb/gdb/__init__.pyi @@ -492,6 +492,12 @@ class Block: is_static: bool def is_valid(self) -> bool: ... + def __iter__(self) -> BlockIterator: ... + +class BlockIterator: + def is_valid(self) -> bool: ... + def __iter__(self: _typeshed.Self) -> _typeshed.Self: ... + def __next__(self) -> Symbol: ... # Symbols diff --git a/stubs/gdb/gdb/printing.pyi b/stubs/gdb/gdb/printing.pyi index 97cb7bea1..7187d014d 100644 --- a/stubs/gdb/gdb/printing.pyi +++ b/stubs/gdb/gdb/printing.pyi @@ -1,4 +1,4 @@ -from collections.abc import Iterable +from collections.abc import Callable, Iterable import gdb from gdb import _PrettyPrinterLookupFunction @@ -6,7 +6,7 @@ from gdb import _PrettyPrinterLookupFunction class PrettyPrinter: name: str - subprinters: list[SubPrettyPrinter] + subprinters: list[SubPrettyPrinter] | None enabled: bool def __init__(self, name: str, subprinters: Iterable[SubPrettyPrinter] | None = ...) -> None: ... @@ -26,4 +26,8 @@ class RegexpCollectionPrettyPrinter(PrettyPrinter): class FlagEnumerationPrinter(PrettyPrinter): def __init__(self, enum_type: str) -> None: ... -def register_pretty_printer(obj: gdb.Objfile | gdb.Progspace | None, printer: PrettyPrinter, replace: bool = ...) -> None: ... +def register_pretty_printer( + obj: gdb.Objfile | gdb.Progspace | None, + printer: PrettyPrinter | Callable[[gdb.Value], gdb._PrettyPrinter | None], + replace: bool = ..., +) -> None: ...