Skip to content

Commit 5e770e9

Browse files
arihant2mathyouknowone
authored andcommitted
update getopt to 3.13.3
1 parent d1b7dc5 commit 5e770e9

26 files changed

Lines changed: 1549 additions & 6 deletions

‎Lib/test/support/i18n_helper.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import re
2+
import subprocess
3+
import sys
4+
import unittest
5+
from pathlib import Path
6+
from test.support import REPO_ROOT, TEST_HOME_DIR, requires_subprocess
7+
from test.test_tools import skip_if_missing
8+
9+
10+
pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py'
11+
12+
msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)',
13+
re.DOTALL)
14+
msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"')
15+
16+
17+
def _generate_po_file(path, *, stdout_only=True):
18+
res = subprocess.run([sys.executable, pygettext,
19+
'--no-location', '-o', '-', path],
20+
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
21+
text=True)
22+
if stdout_only:
23+
return res.stdout
24+
return res
25+
26+
27+
def _extract_msgids(po):
28+
msgids = []
29+
for msgid in msgid_pattern.findall(po):
30+
msgid_string = ''.join(msgid_string_pattern.findall(msgid))
31+
msgid_string = msgid_string.replace(r'\"', '"')
32+
if msgid_string:
33+
msgids.append(msgid_string)
34+
return sorted(msgids)
35+
36+
37+
def _get_snapshot_path(module_name):
38+
return Path(TEST_HOME_DIR) / 'translationdata' / module_name / 'msgids.txt'
39+
40+
41+
@requires_subprocess()
42+
class TestTranslationsBase(unittest.TestCase):
43+
44+
def assertMsgidsEqual(self, module):
45+
'''Assert that msgids extracted from a given module match a
46+
snapshot.
47+
48+
'''
49+
skip_if_missing('i18n')
50+
res = _generate_po_file(module.__file__, stdout_only=False)
51+
self.assertEqual(res.returncode, 0)
52+
self.assertEqual(res.stderr, '')
53+
msgids = _extract_msgids(res.stdout)
54+
snapshot_path = _get_snapshot_path(module.__name__)
55+
snapshot = snapshot_path.read_text().splitlines()
56+
self.assertListEqual(msgids, snapshot)
57+
58+
59+
def update_translation_snapshots(module):
60+
contents = _generate_po_file(module.__file__)
61+
msgids = _extract_msgids(contents)
62+
snapshot_path = _get_snapshot_path(module.__name__)
63+
snapshot_path.write_text('\n'.join(msgids))

‎Lib/test/test_getopt.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# test_getopt.py
22
# David Goodger <dgoodger@bigfoot.com> 2000-08-19
33

4-
from test.support.os_helper import EnvironmentVarGuard
54
import doctest
6-
import unittest
7-
85
import getopt
6+
import sys
7+
import unittest
8+
from test.support.i18n_helper import TestTranslationsBase, update_translation_snapshots
9+
from test.support.os_helper import EnvironmentVarGuard
910

1011
sentinel = object()
1112

1213
class GetoptTests(unittest.TestCase):
1314
def setUp(self):
1415
self.env = self.enterContext(EnvironmentVarGuard())
15-
if "POSIXLY_CORRECT" in self.env:
16-
del self.env["POSIXLY_CORRECT"]
16+
del self.env["POSIXLY_CORRECT"]
1717

1818
def assertError(self, *args, **kwargs):
1919
self.assertRaises(getopt.GetoptError, *args, **kwargs)
@@ -173,10 +173,20 @@ def test_libref_examples():
173173
['a1', 'a2']
174174
"""
175175

176+
177+
class TestTranslations(TestTranslationsBase):
178+
def test_translations(self):
179+
self.assertMsgidsEqual(getopt)
180+
181+
176182
def load_tests(loader, tests, pattern):
177183
tests.addTest(doctest.DocTestSuite())
178184
return tests
179185

180186

181-
if __name__ == "__main__":
187+
if __name__ == '__main__':
188+
# To regenerate translation snapshots
189+
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
190+
update_translation_snapshots(getopt)
191+
sys.exit(0)
182192
unittest.main()

‎Lib/test/test_tools/__init__.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Support functions for testing scripts in the Tools directory."""
2+
import contextlib
3+
import importlib
4+
import os.path
5+
import unittest
6+
from test import support
7+
from test.support import import_helper
8+
9+
10+
if not support.has_subprocess_support:
11+
raise unittest.SkipTest("test module requires subprocess")
12+
13+
14+
basepath = os.path.normpath(
15+
os.path.dirname( # <src/install dir>
16+
os.path.dirname( # Lib
17+
os.path.dirname( # test
18+
os.path.dirname(__file__))))) # test_tools
19+
20+
toolsdir = os.path.join(basepath, 'Tools')
21+
scriptsdir = os.path.join(toolsdir, 'scripts')
22+
23+
def skip_if_missing(tool=None):
24+
if tool:
25+
tooldir = os.path.join(toolsdir, tool)
26+
else:
27+
tool = 'scripts'
28+
tooldir = scriptsdir
29+
if not os.path.isdir(tooldir):
30+
raise unittest.SkipTest(f'{tool} directory could not be found')
31+
32+
@contextlib.contextmanager
33+
def imports_under_tool(name, *subdirs):
34+
tooldir = os.path.join(toolsdir, name, *subdirs)
35+
with import_helper.DirsOnSysPath(tooldir) as cm:
36+
yield cm
37+
38+
def import_tool(toolname):
39+
with import_helper.DirsOnSysPath(scriptsdir):
40+
return importlib.import_module(toolname)
41+
42+
def load_tests(*args):
43+
return support.load_package_tests(os.path.dirname(__file__), *args)

‎Lib/test/test_tools/__main__.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from test.test_tools import load_tests
2+
import unittest
3+
4+
unittest.main()
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#: docstrings.py:7
19+
#, docstring
20+
msgid ""
21+
msgstr ""
22+
23+
#: docstrings.py:18
24+
#, docstring
25+
msgid ""
26+
"multiline\n"
27+
" docstring\n"
28+
" "
29+
msgstr ""
30+
31+
#: docstrings.py:25
32+
#, docstring
33+
msgid "docstring1"
34+
msgstr ""
35+
36+
#: docstrings.py:30
37+
#, docstring
38+
msgid "Hello, {}!"
39+
msgstr ""
40+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Test docstring extraction
2+
from gettext import gettext as _
3+
4+
5+
# Empty docstring
6+
def test(x):
7+
""""""
8+
9+
10+
# Leading empty line
11+
def test2(x):
12+
13+
"""docstring""" # XXX This should be extracted but isn't.
14+
15+
16+
# XXX Multiline docstrings should be cleaned with `inspect.cleandoc`.
17+
def test3(x):
18+
"""multiline
19+
docstring
20+
"""
21+
22+
23+
# Multiple docstrings - only the first should be extracted
24+
def test4(x):
25+
"""docstring1"""
26+
"""docstring2"""
27+
28+
29+
def test5(x):
30+
"""Hello, {}!""".format("world!") # XXX This should not be extracted.
31+
32+
33+
# Nested docstrings
34+
def test6(x):
35+
def inner(y):
36+
"""nested docstring""" # XXX This should be extracted but isn't.
37+
38+
39+
class Outer:
40+
class Inner:
41+
"nested class docstring" # XXX This should be extracted but isn't.
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+
_('α ㄱ 𓂀')
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
#: fileloc.py:5 fileloc.py:6
19+
msgid "foo"
20+
msgstr ""
21+
22+
#: fileloc.py:9
23+
msgid "bar"
24+
msgstr ""
25+
26+
#: fileloc.py:14 fileloc.py:18
27+
#, docstring
28+
msgid "docstring"
29+
msgstr ""
30+
31+
#: fileloc.py:22 fileloc.py:26
32+
#, docstring
33+
msgid "baz"
34+
msgstr ""
35+

0 commit comments

Comments
 (0)