public inbox for gdb-cvs@sourceware.org help / color / mirror / Atom feed
From: Simon Marchi <simark@sourceware.org> To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: add type annotations to ada-unicode.py Date: Thu, 25 Apr 2024 18:37:58 +0000 (GMT) [thread overview] Message-ID: <20240425183759.17A0B3858401@sourceware.org> (raw) https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d3c2603167baf9f6fea006f8b747b8186f89b177 commit d3c2603167baf9f6fea006f8b747b8186f89b177 Author: Simon Marchi <simon.marchi@efficios.com> Date: Thu Apr 25 13:26:11 2024 -0400 gdb: add type annotations to ada-unicode.py 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 Approved-By: Tom Tromey <tom@tromey.com> Diff: --- 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 06c5de0f6e5..f128dee69f5 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, + )
reply other threads:[~2024-04-25 18:37 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240425183759.17A0B3858401@sourceware.org \ --to=simark@sourceware.org \ --cc=gdb-cvs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).