public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: add type annotations to ada-unicode.py
@ 2024-04-25 17:26 Simon Marchi
  2024-04-25 18:25 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2024-04-25 17:26 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Add type annotations to ada-unicode.py, just enough to make pyright
happy:

    $ pyright --version
    pyright 1.1.359
    $ pyright ada-unicode.py
    0 errors, 0 warnings, 0 informations

Introduce a `Range` class instead of using separate variables and
tuples, to make the code and type annotations a bit cleaner.

When running ada-unicode.py, I get a diff for ada-casefold.h, but I get
the same diff before and after this patch, so that is a separate issue.

Change-Id: I0d8975a57f9fb115703178ae197dc6b6b8b4eb7a
---
 gdb/ada-unicode.py | 98 +++++++++++++++++++++++++++++-----------------
 1 file changed, 61 insertions(+), 37 deletions(-)

diff --git a/gdb/ada-unicode.py b/gdb/ada-unicode.py
index 06c5de0f6e55..f128dee69f54 100755
--- a/gdb/ada-unicode.py
+++ b/gdb/ada-unicode.py
@@ -25,41 +25,59 @@
 
 import gdbcopyright
 
-# The start of the current range of case-conversions we are
-# processing.  If RANGE_START is None, then we're outside of a range.
-range_start = None
-# End of the current range.
-range_end = None
-# The delta between RANGE_START and the upper-case variant of that
-# character.
-upper_delta = None
-# The delta between RANGE_START and the lower-case variant of that
-# character.
-lower_delta = None
+
+class Range:
+    def __init__(self, range_start: int, upper_delta: int, lower_delta: int):
+        self._range_start = range_start
+        self._range_end = range_start
+        self._upper_delta = upper_delta
+        self._lower_delta = lower_delta
+
+    # The start of the range.
+    @property
+    def range_start(self):
+        return self._range_start
+
+    # The end of the range.
+    @property
+    def range_end(self):
+        return self._range_end
+
+    @range_end.setter
+    def range_end(self, val: int):
+        self._range_end = val
+
+    # The delta between RANGE_START and the upper-case variant of that
+    # character.
+    @property
+    def upper_delta(self):
+        return self._upper_delta
+
+    # The delta between RANGE_START and the lower-case variant of that
+    # character.
+    @property
+    def lower_delta(self):
+        return self._lower_delta
+
+
+# The current range we are processing.  If None,  then we're outside of a range.
+current_range: Range | None = None
 
 # All the ranges found and completed so far.
-# Each entry is a tuple of the form (START, END, UPPER_DELTA, LOWER_DELTA).
-all_ranges = []
+all_ranges: list[Range] = []
 
 
 def finish_range():
-    global range_start
-    global range_end
-    global upper_delta
-    global lower_delta
-    if range_start is not None:
-        all_ranges.append((range_start, range_end, upper_delta, lower_delta))
-        range_start = None
-        range_end = None
-        upper_delta = None
-        lower_delta = None
-
-
-def process_codepoint(val):
-    global range_start
-    global range_end
-    global upper_delta
-    global lower_delta
+    global current_range
+
+    if current_range is not None:
+        all_ranges.append(current_range)
+        current_range = None
+
+
+def process_codepoint(val: int):
+    global current_range
+
     c = chr(val)
     low = c.lower()
     up = c.upper()
@@ -74,13 +92,16 @@ def process_codepoint(val):
         return
     updelta = ord(up) - val
     lowdelta = ord(low) - val
-    if range_start is not None and (updelta != upper_delta or lowdelta != lower_delta):
+
+    if current_range is not None and (
+        updelta != current_range.upper_delta or lowdelta != current_range.lower_delta
+    ):
         finish_range()
-    if range_start is None:
-        range_start = val
-        upper_delta = updelta
-        lower_delta = lowdelta
-    range_end = val
+
+    if current_range is None:
+        current_range = Range(val, updelta, lowdelta)
+
+    current_range.range_end = val
 
 
 for c in range(0, 0x10FFFF):
@@ -93,4 +114,7 @@ with open("ada-casefold.h", "w") as f:
     )
     print("", file=f)
     for r in all_ranges:
-        print(f"   {{{r[0]}, {r[1]}, {r[2]}, {r[3]}}},", file=f)
+        print(
+            f"   {{{r.range_start}, {r.range_end}, {r.upper_delta}, {r.lower_delta}}},",
+            file=f,
+        )

base-commit: 5b9707eb872ad4cb50c98d396d16f110070a44ca
-- 
2.44.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: add type annotations to ada-unicode.py
  2024-04-25 17:26 [PATCH] gdb: add type annotations to ada-unicode.py Simon Marchi
@ 2024-04-25 18:25 ` Tom Tromey
  2024-04-25 18:37   ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2024-04-25 18:25 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:

Simon> Add type annotations to ada-unicode.py, just enough to make pyright
Simon> happy:

Ok.
Approved-By: Tom Tromey <tom@tromey.com>

Simon> +        print(
Simon> +            f"   {{{r.range_start}, {r.range_end}, {r.upper_delta}, {r.lower_delta}}},",
Simon> +            file=f,
Simon> +        )

This trailing comma seems weird.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: add type annotations to ada-unicode.py
  2024-04-25 18:25 ` Tom Tromey
@ 2024-04-25 18:37   ` Simon Marchi
  0 siblings, 0 replies; 3+ messages in thread
From: Simon Marchi @ 2024-04-25 18:37 UTC (permalink / raw)
  To: Tom Tromey, Simon Marchi; +Cc: gdb-patches

On 4/25/24 2:25 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
> 
> Simon> Add type annotations to ada-unicode.py, just enough to make pyright
> Simon> happy:
> 
> Ok.
> Approved-By: Tom Tromey <tom@tromey.com>

Thanks, will push.

> Simon> +        print(
> Simon> +            f"   {{{r.range_start}, {r.range_end}, {r.upper_delta}, {r.lower_delta}}},",
> Simon> +            file=f,
> Simon> +        )
> 
> This trailing comma seems weird.

When it decides to put the arguments one per like, black adds that
trailing comma.  I guess it avoids having a diff on this line should you
add another argument after that.

You can also add the trailing comma yourself to force black to put
things one per line:

foo = (1, 2, 3)
bar = (
    1,
    2,
    3,
)

print("foo")
print(
    "foo",
)

It sometimes makes things more readable (not in the dummy examples
above, but in real life code).

Simon

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-04-25 18:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-25 17:26 [PATCH] gdb: add type annotations to ada-unicode.py Simon Marchi
2024-04-25 18:25 ` Tom Tromey
2024-04-25 18:37   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).