Skip to content

Commit 430a77d

Browse files
committed
gnatdbg: add type hints
Change-Id: Ib0f0cafe4927fc3c581f58d2e90a502b5b3a1544 TN: V707-013
1 parent 446eef6 commit 430a77d

16 files changed

+377
-302
lines changed

gnatdbg/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
_printers = None
3030

3131

32-
def create_printers(name="gnat-runtime"):
32+
def create_printers(name: str = "gnat-runtime") -> GDBPrettyPrinters:
3333
"""
3434
Instantiate GDBPrettyPrinters with the given name and register all
3535
pretty-printers for the GNAT runtime in it. Return this instance.
@@ -60,7 +60,7 @@ def create_printers(name="gnat-runtime"):
6060
return printers
6161

6262

63-
def setup(name="gnat-runtime", globally=True):
63+
def setup(name: str = "gnat-runtime", globally: bool = True) -> None:
6464
"""
6565
Instantiate pretty-printers for the GNAT runtime with the given name and
6666
register them.

gnatdbg/big_numbers.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ class BigInteger:
1313
Helper class to inspect Big_Integer values.
1414
"""
1515

16-
def __init__(self, value):
16+
def __init__(self, value: gdb.Value):
1717
self.value = value
1818

1919
@property
20-
def _bignum_address(self):
20+
def _bignum_address(self) -> gdb.Value:
2121
return self.value["value"]["c"]
2222

2323
@property
24-
def is_valid(self):
24+
def is_valid(self) -> bool:
2525
"""
2626
Return whether this big integer is valid.
2727
"""
2828
return bool(self._bignum_address)
2929

30-
def get(self):
30+
def get(self) -> int:
3131
# The data structure used to store big numbers is
3232
# System.Shared_Bignums.Bignum_Data. Unfortunately, in most cases we
3333
# don't have debug info for this type, so we have to poke blindly at
@@ -54,7 +54,7 @@ def get(self):
5454
data_array = data_ptr.cast(array_ptr_type).dereference()
5555

5656
result = 0
57-
for i in range(length):
57+
for i in range(int(length)):
5858
result = result << 32 | int(data_array[i + 1])
5959

6060
if neg:
@@ -68,19 +68,19 @@ class BigReal:
6868
Helper class to inspect Big_Real values.
6969
"""
7070

71-
def __init__(self, value):
71+
def __init__(self, value: gdb.Value):
7272
self.value = value
7373
self.numerator = BigInteger(value["num"])
7474
self.denominator = BigInteger(value["den"])
7575

7676
@property
77-
def is_valid(self):
77+
def is_valid(self) -> bool:
7878
return self.numerator.is_valid and self.denominator.is_valid
7979

80-
def get_numerator(self):
80+
def get_numerator(self) -> int:
8181
return self.numerator.get()
8282

83-
def get_denominator(self):
83+
def get_denominator(self) -> int:
8484
return self.denominator.get()
8585

8686

@@ -90,7 +90,7 @@ class BigIntegerPrinter(PrettyPrinter):
9090
name = "Big_Integer"
9191
type_pretty_name = "ada.numerics.big_numbers.big_integers.big_integer"
9292

93-
def to_string(self):
93+
def to_string(self) -> str:
9494
val = BigInteger(self.value)
9595
value = None
9696
try:
@@ -110,7 +110,7 @@ class BigRealPrinter(PrettyPrinter):
110110
name = "Big_Real"
111111
type_pretty_name = "ada.numerics.big_numbers.big_reals.big_real"
112112

113-
def to_string(self):
113+
def to_string(self) -> str:
114114
val = BigReal(self.value)
115115
try:
116116
value_repr = (

gnatdbg/debug.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
Helpers to ease the discovery/debugging of GDB's Python API.
33
"""
44

5+
from __future__ import annotations
6+
57
import itertools
8+
from typing import Dict, Tuple
69

710
import gdb
811

912
from gnatdbg.utils import gdb_code_names
1013

1114

12-
def print_type_tree(typeobj):
15+
def print_type_tree(typeobj: gdb.Type) -> None:
1316
"""
1417
Print a GDB type as a tree on the standard output.
1518
1619
:param gdb.Type typeobj: Root type for the tree to print.
1720
"""
1821
counter = iter(itertools.count(0))
19-
visited = {}
22+
visited: Dict[Tuple[int, str, str], int] = {}
2023

21-
def helper(t, indent=1):
24+
def helper(t: gdb.Type, indent: int = 1) -> str:
2225
indent_str = " " * indent
2326
code_name = gdb_code_names[t.code]
2427

@@ -65,10 +68,10 @@ class PrintGDBTypeTreeCommand(gdb.Command):
6568
GDB view of user types, i.e. trees of gdb.Type instances.
6669
"""
6770

68-
def __init__(self):
71+
def __init__(self) -> None:
6972
super().__init__("dbgtype", gdb.COMMAND_NONE, gdb.COMPLETE_SYMBOL)
7073

71-
def invoke(self, arg, from_tty):
74+
def invoke(self, arg: str, from_tty: bool) -> None:
7275
argv = gdb.string_to_argv(arg)
7376
typeobj = gdb.parse_and_eval(argv[0]).type
7477
print_type_tree(typeobj)

0 commit comments

Comments
 (0)