gh-132121: Always escape non-printable characters in pygettext (GH-13… · python/cpython@a693eaa

GitHub

GitHub CopilotWrite better code with AI | MCP RegistryIntegrate external tools | ActionsAutomate any workflow | CodespacesInstant dev environments | IssuesPlan and track work | Code ReviewManage code changes | Code QualityEnforce quality at merge | Why GitHub | Marketplace | View all features | Enterprises | Small and medium teams | Startups | View all use cases | View all industries | View all solutions | AI | Software Development | DevOps | Security | View all topics | Customer stories | Events & webinars | Ebooks & reports | Business insights | Trust center | Partners | View all resources

File tree

Lib/test/test_tools/i18n_data

Misc/NEWS.d/next/Tools-Demos

Tools/i18n

Original file line numberDiff line numberDiff line change@@ -41,7 +41,7 @@ msgstr ""

41414242#. some characters in the 128-255 range

4343#:escapes.py:20

44-msgid"€ ÿ"

44+msgid"\302\200 \302\240 ÿ"

4545msgstr""

46464747#. some characters >= 256 encoded as 2, 3 and 4 bytes, respectively

Original file line numberDiff line numberDiff line change@@ -0,0 +1 @@

1+Always escape non-printable Unicode characters in :program:`pygettext`.

Original file line numberDiff line numberDiff line change@@ -190,12 +190,10 @@ def make_escapes(pass_nonascii):

190190# Allow non-ascii characters to pass through so that e.g. 'msgid

191191# "Höhe"' would not result in 'msgid "H\366he"'. Otherwise we

192192# escape any character outside the 32..126 range.

193-mod=128

194193escape=escape_ascii

195194else:

196-mod=256

197195escape=escape_nonascii

198-escapes= [r"\%03o"%iforiinrange(mod)]

196+escapes= [r"\%03o"%iforiinrange(256)]

199197foriinrange(32, 127):

200198escapes[i] =chr(i)

201199escapes[ord('\\')] =r'\\'

@@ -206,7 +204,9 @@ def make_escapes(pass_nonascii):

206204207205208206defescape_ascii(s, encoding):

209-return''.join(escapes[ord(c)] iford(c) <128elsecforcins)

207+return''.join(escapes[ord(c)] iford(c) <128elsec

208+ifc.isprintable() elseescape_nonascii(c, encoding)

209+forcins)

210210211211212212defescape_nonascii(s, encoding):