Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-132121: Always escape non-printable characters in pygettext #132122

Merged
merged 3 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_tools/i18n_data/ascii-escapes.pot
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ msgstr ""

#. some characters in the 128-255 range
#: escapes.py:20
msgid "€   ÿ"
msgid "\302\200 \302\240 ÿ"
msgstr ""

#. some characters >= 256 encoded as 2, 3 and 4 bytes, respectively
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Always escape non-printable Unicode characters in :program:`pygettext`.
14 changes: 10 additions & 4 deletions Tools/i18n/pygettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,10 @@ def make_escapes(pass_nonascii):
# Allow non-ascii characters to pass through so that e.g. 'msgid
# "Höhe"' would not result in 'msgid "H\366he"'. Otherwise we
# escape any character outside the 32..126 range.
mod = 128
escape = escape_ascii
else:
mod = 256
escape = escape_nonascii
escapes = [r"\%03o" % i for i in range(mod)]
escapes = [r"\%03o" % i for i in range(256)]
for i in range(32, 127):
escapes[i] = chr(i)
escapes[ord('\\')] = r'\\'
Expand All @@ -206,7 +204,15 @@ def make_escapes(pass_nonascii):


def escape_ascii(s, encoding):
return ''.join(escapes[ord(c)] if ord(c) < 128 else c for c in s)
escaped = ''
for c in s:
if ord(c) < 128:
escaped += escapes[ord(c)]
elif c.isprintable():
escaped += c
else:
escaped += escape_nonascii(c, encoding)
return escaped


def escape_nonascii(s, encoding):
Expand Down
Loading