From a5c50d3e51024c0a158f2cd4772406dd8d5a68cd Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sun, 31 Aug 2025 10:17:41 +0100 Subject: [PATCH 1/4] Commit --- .pre-commit-config.yaml | 4 ++++ Tools/i18n/.ruff.toml | 8 ++++++++ Tools/i18n/makelocalealias.py | 14 +++++++------- Tools/i18n/msgfmt.py | 22 +++++++++++----------- Tools/i18n/pygettext.py | 6 +++--- 5 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 Tools/i18n/.ruff.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d101f5c37e60b1..4c5e266065b98a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,6 +14,10 @@ repos: name: Run Ruff (lint) on Tools/build/ args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml] files: ^Tools/build/ + - id: ruff + name: Run Ruff (lint) on Tools/i18n/ + args: [ --exit-non-zero-on-fix, --config=Tools/i18n/.ruff.toml ] + files: ^Tools/i18n/ - id: ruff name: Run Ruff (lint) on Argument Clinic args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml] diff --git a/Tools/i18n/.ruff.toml b/Tools/i18n/.ruff.toml new file mode 100644 index 00000000000000..cf0c0b084cf046 --- /dev/null +++ b/Tools/i18n/.ruff.toml @@ -0,0 +1,8 @@ +extend = "../../.ruff.toml" # Inherit the project-wide settings + +[lint] +select = [ + "F", # Enable all pyflakes rules + "I", # Enable all isort rules + "UP", # Enable all pyupgrade rules by default +] \ No newline at end of file diff --git a/Tools/i18n/makelocalealias.py b/Tools/i18n/makelocalealias.py index 7f001abc09745d..f825862ffdf350 100755 --- a/Tools/i18n/makelocalealias.py +++ b/Tools/i18n/makelocalealias.py @@ -8,6 +8,7 @@ """ import locale import sys + _locale = locale # Location of the X11 alias file. @@ -100,16 +101,15 @@ def parse_glibc_supported(filename): def pprint(data): items = sorted(data.items()) for k, v in items: - print(' %-40s%a,' % ('%a:' % k, v)) + print(f" {k!a:<40}{v!a},") def print_differences(data, olddata): items = sorted(olddata.items()) for k, v in items: if k not in data: - print('# removed %a' % k) + print(f'# removed {k!a}') elif olddata[k] != data[k]: - print('# updated %a -> %a to %a' % \ - (k, olddata[k], data[k])) + print(f'# updated {k!a} -> {olddata[k]!a} to {data[k]!a}') # Additions are not mentioned def optimize(data): @@ -132,7 +132,7 @@ def check(data): errors = 0 for k, v in data.items(): if locale.normalize(k) != v: - print('ERROR: %a -> %a != %a' % (k, locale.normalize(k), v), + print(f'ERROR: {k!a} -> {locale.normalize(k)!a} != {v!a}', file=sys.stderr) errors += 1 return errors @@ -142,10 +142,10 @@ def check(data): parser = argparse.ArgumentParser() parser.add_argument('--locale-alias', default=LOCALE_ALIAS, help='location of the X11 alias file ' - '(default: %a)' % LOCALE_ALIAS) + f'(default: {LOCALE_ALIAS})') parser.add_argument('--glibc-supported', default=SUPPORTED, help='location of the glibc SUPPORTED locales file ' - '(default: %a)' % SUPPORTED) + f'(default: {SUPPORTED})') args = parser.parse_args() data = locale.locale_alias.copy() diff --git a/Tools/i18n/msgfmt.py b/Tools/i18n/msgfmt.py index 65254a7c375456..3351511bbc8f2b 100755 --- a/Tools/i18n/msgfmt.py +++ b/Tools/i18n/msgfmt.py @@ -24,14 +24,14 @@ Display version information and exit. """ -import os -import sys +import array import ast +import codecs import getopt +import os import struct -import array +import sys from email.parser import HeaderParser -import codecs __version__ = "1.2" @@ -113,7 +113,7 @@ def make(filename, outfile): try: with open(infile, 'rb') as f: lines = f.readlines() - except IOError as msg: + except OSError as msg: print(msg, file=sys.stderr) sys.exit(1) @@ -126,6 +126,7 @@ def make(filename, outfile): sys.exit(1) section = msgctxt = None + msgid = msgstr = b'' fuzzy = 0 # Start off assuming Latin-1, so everything decodes without failure, @@ -177,7 +178,7 @@ def make(filename, outfile): # This is a message with plural forms elif l.startswith('msgid_plural'): if section != ID: - print('msgid_plural not preceded by msgid on %s:%d' % (infile, lno), + print(f'msgid_plural not preceded by msgid on {infile}:{lno}', file=sys.stderr) sys.exit(1) l = l[12:] @@ -188,7 +189,7 @@ def make(filename, outfile): section = STR if l.startswith('msgstr['): if not is_plural: - print('plural without msgid_plural on %s:%d' % (infile, lno), + print(f'plural without msgid_plural on {infile}:{lno}', file=sys.stderr) sys.exit(1) l = l.split(']', 1)[1] @@ -196,7 +197,7 @@ def make(filename, outfile): msgstr += b'\0' # Separator of the various plural forms else: if is_plural: - print('indexed msgstr required for plural on %s:%d' % (infile, lno), + print(f'indexed msgstr required for plural on {infile}:{lno}', file=sys.stderr) sys.exit(1) l = l[6:] @@ -212,8 +213,7 @@ def make(filename, outfile): elif section == STR: msgstr += l.encode(encoding) else: - print('Syntax error on %s:%d' % (infile, lno), \ - 'before:', file=sys.stderr) + print(f'Syntax error on {infile}:{lno} before:', file=sys.stderr) print(l, file=sys.stderr) sys.exit(1) # Add last entry @@ -226,7 +226,7 @@ def make(filename, outfile): try: with open(outfile,"wb") as f: f.write(output) - except IOError as msg: + except OSError as msg: print(msg, file=sys.stderr) diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index f46b05067d7fde..83431ab8cf463a 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -193,7 +193,7 @@ def make_escapes(pass_nonascii): escape = escape_ascii else: escape = escape_nonascii - escapes = [r"\%03o" % i for i in range(256)] + escapes = [fr"\{i:03o}" for i in range(256)] for i in range(32, 127): escapes[i] = chr(i) escapes[ord('\\')] = r'\\' @@ -387,7 +387,7 @@ def unparse_spec(name, spec): parts.append(f'{pos + 1}c') else: parts.append(str(pos + 1)) - return f'{name}:{','.join(parts)}' + return f"{name}:{','.join(parts)}" def process_keywords(keywords, *, no_default_keywords): @@ -796,7 +796,7 @@ class Options: try: with open(options.excludefilename) as fp: options.toexclude = fp.readlines() - except IOError: + except OSError: print(f"Can't read --exclude-file: {options.excludefilename}", file=sys.stderr) sys.exit(1) From 48b7e9f2945cd7ece23f1eb9436b3adcea272a62 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Sun, 31 Aug 2025 12:51:48 +0100 Subject: [PATCH 2/4] Whitespace Co-authored-by: Tomas R. --- .pre-commit-config.yaml | 2 +- Tools/i18n/.ruff.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4c5e266065b98a..61db8c456c2e64 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,7 +16,7 @@ repos: files: ^Tools/build/ - id: ruff name: Run Ruff (lint) on Tools/i18n/ - args: [ --exit-non-zero-on-fix, --config=Tools/i18n/.ruff.toml ] + args: [--exit-non-zero-on-fix, --config=Tools/i18n/.ruff.toml] files: ^Tools/i18n/ - id: ruff name: Run Ruff (lint) on Argument Clinic diff --git a/Tools/i18n/.ruff.toml b/Tools/i18n/.ruff.toml index cf0c0b084cf046..ab37f86d590667 100644 --- a/Tools/i18n/.ruff.toml +++ b/Tools/i18n/.ruff.toml @@ -5,4 +5,4 @@ select = [ "F", # Enable all pyflakes rules "I", # Enable all isort rules "UP", # Enable all pyupgrade rules by default -] \ No newline at end of file +] From f24d714f853e0763f967ea222b483fd6d456bf30 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sun, 31 Aug 2025 15:53:28 +0100 Subject: [PATCH 3/4] Set target version instead --- Tools/i18n/.ruff.toml | 1 + Tools/i18n/pygettext.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/i18n/.ruff.toml b/Tools/i18n/.ruff.toml index ab37f86d590667..27a17aed3605bf 100644 --- a/Tools/i18n/.ruff.toml +++ b/Tools/i18n/.ruff.toml @@ -1,4 +1,5 @@ extend = "../../.ruff.toml" # Inherit the project-wide settings +target-version = "py313" [lint] select = [ diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index 83431ab8cf463a..ddf4474d2bce55 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -387,7 +387,7 @@ def unparse_spec(name, spec): parts.append(f'{pos + 1}c') else: parts.append(str(pos + 1)) - return f"{name}:{','.join(parts)}" + return f'{name}:{','.join(parts)}' def process_keywords(keywords, *, no_default_keywords): From 99d8b81b461fffbcb0a02239108aba0a2c6f6501 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:03:50 +0100 Subject: [PATCH 4/4] consistency with other `.ruff.toml`s --- Tools/i18n/.ruff.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Tools/i18n/.ruff.toml b/Tools/i18n/.ruff.toml index 27a17aed3605bf..a8f4f2f5a96d7b 100644 --- a/Tools/i18n/.ruff.toml +++ b/Tools/i18n/.ruff.toml @@ -1,9 +1,10 @@ extend = "../../.ruff.toml" # Inherit the project-wide settings + target-version = "py313" [lint] select = [ - "F", # Enable all pyflakes rules - "I", # Enable all isort rules - "UP", # Enable all pyupgrade rules by default + "F", # pyflakes + "I", # isort + "UP", # pyupgrade ]