Skip to content

Commit da7ac18

Browse files
authored
[3.12] gh-130197: pygettext: Test the --escape option (GH-131902) (GH-132033)
(cherry picked from commit 87d9983)
1 parent 6f6b8b4 commit da7ac18

File tree

5 files changed

+143
-15
lines changed

5 files changed

+143
-15
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR ORGANIZATION
3+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: PACKAGE VERSION\n"
8+
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
9+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
10+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
11+
"Language-Team: LANGUAGE <LL@li.org>\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"Generated-By: pygettext.py 1.5\n"
16+
17+
18+
#: escapes.py:5
19+
msgid ""
20+
"\"\t\n"
21+
"\r\\"
22+
msgstr ""
23+
24+
#: escapes.py:8
25+
msgid ""
26+
"\000\001\002\003\004\005\006\007\010\t\n"
27+
"\013\014\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
28+
msgstr ""
29+
30+
#: escapes.py:13
31+
msgid " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
32+
msgstr ""
33+
34+
#: escapes.py:17
35+
msgid "\177"
36+
msgstr ""
37+
38+
#: escapes.py:20
39+
msgid "€   ÿ"
40+
msgstr ""
41+
42+
#: escapes.py:23
43+
msgid "α ㄱ 𓂀"
44+
msgstr ""
45+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR ORGANIZATION
3+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: PACKAGE VERSION\n"
8+
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
9+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
10+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
11+
"Language-Team: LANGUAGE <LL@li.org>\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"Generated-By: pygettext.py 1.5\n"
16+
17+
18+
#: escapes.py:5
19+
msgid ""
20+
"\"\t\n"
21+
"\r\\"
22+
msgstr ""
23+
24+
#: escapes.py:8
25+
msgid ""
26+
"\000\001\002\003\004\005\006\007\010\t\n"
27+
"\013\014\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
28+
msgstr ""
29+
30+
#: escapes.py:13
31+
msgid " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
32+
msgstr ""
33+
34+
#: escapes.py:17
35+
msgid "\177"
36+
msgstr ""
37+
38+
#: escapes.py:20
39+
msgid "\302\200 \302\240 \303\277"
40+
msgstr ""
41+
42+
#: escapes.py:23
43+
msgid "\316\261 \343\204\261 \360\223\202\200"
44+
msgstr ""
45+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import gettext as _
2+
3+
4+
# Special characters that are always escaped in the POT file
5+
_('"\t\n\r\\')
6+
7+
# All ascii characters 0-31
8+
_('\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n'
9+
'\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15'
10+
'\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f')
11+
12+
# All ascii characters 32-126
13+
_(' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ'
14+
'[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~')
15+
16+
# ascii char 127
17+
_('\x7f')
18+
19+
# some characters in the 128-255 range
20+
_('\x80 \xa0 ÿ')
21+
22+
# some characters >= 256 encoded as 2, 3 and 4 bytes, respectively
23+
_('α ㄱ 𓂀')

Lib/test/test_tools/test_i18n.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,8 @@ class _(object):
369369

370370
def test_pygettext_output(self):
371371
"""Test that the pygettext output exactly matches snapshots."""
372-
for input_file in DATA_DIR.glob('*.py'):
373-
output_file = input_file.with_suffix('.pot')
374-
with self.subTest(input_file=f'i18n_data/{input_file}'):
375-
contents = input_file.read_text(encoding='utf-8')
376-
with temp_cwd(None):
377-
Path(input_file.name).write_text(contents)
378-
assert_python_ok('-Xutf8', self.script, '--docstrings', input_file.name)
379-
output = Path('messages.pot').read_text(encoding='utf-8')
380-
372+
for input_file, output_file, output in extract_from_snapshots():
373+
with self.subTest(input_file=input_file):
381374
expected = output_file.read_text(encoding='utf-8')
382375
self.assert_POT_equal(expected, output)
383376

@@ -408,15 +401,37 @@ def test_files_list(self):
408401
self.assertNotIn(text3, data)
409402

410403

411-
def update_POT_snapshots():
412-
for input_file in DATA_DIR.glob('*.py'):
413-
output_file = input_file.with_suffix('.pot')
404+
def extract_from_snapshots():
405+
snapshots = {
406+
'messages.py': ('--docstrings',),
407+
'fileloc.py': ('--docstrings',),
408+
'docstrings.py': ('--docstrings',),
409+
# == Test character escaping
410+
# Escape ascii and unicode:
411+
'escapes.py': ('--escape',),
412+
# Escape only ascii and let unicode pass through:
413+
('escapes.py', 'ascii-escapes.pot'): (),
414+
}
415+
416+
for filename, args in snapshots.items():
417+
if isinstance(filename, tuple):
418+
filename, output_file = filename
419+
output_file = DATA_DIR / output_file
420+
input_file = DATA_DIR / filename
421+
else:
422+
input_file = DATA_DIR / filename
423+
output_file = input_file.with_suffix('.pot')
414424
contents = input_file.read_bytes()
415425
with temp_cwd(None):
416426
Path(input_file.name).write_bytes(contents)
417-
assert_python_ok('-Xutf8', Test_pygettext.script, '--docstrings', input_file.name)
418-
output = Path('messages.pot').read_text(encoding='utf-8')
427+
assert_python_ok('-Xutf8', Test_pygettext.script, *args,
428+
input_file.name)
429+
yield (input_file, output_file,
430+
Path('messages.pot').read_text(encoding='utf-8'))
431+
419432

433+
def update_POT_snapshots():
434+
for _, output_file, output in extract_from_snapshots():
420435
output = normalize_POT_file(output)
421436
output_file.write_text(output, encoding='utf-8')
422437

Tools/i18n/pygettext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def make_escapes(pass_nonascii):
207207
global escapes, escape
208208
if pass_nonascii:
209209
# Allow non-ascii characters to pass through so that e.g. 'msgid
210-
# "Höhe"' would result not result in 'msgid "H\366he"'. Otherwise we
210+
# "Höhe"' would not result in 'msgid "H\366he"'. Otherwise we
211211
# escape any character outside the 32..126 range.
212212
mod = 128
213213
escape = escape_ascii

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy