Skip to content

Commit 6d1330d

Browse files
pythongh-85308: Add argparse tests for reading non-ASCII arguments from file
1 parent 15bfabd commit 6d1330d

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

Lib/test/test_argparse.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -1505,14 +1505,15 @@ class TestArgumentsFromFile(TempDirMixin, ParserTestCase):
15051505
def setUp(self):
15061506
super(TestArgumentsFromFile, self).setUp()
15071507
file_texts = [
1508-
('hello', 'hello world!\n'),
1509-
('recursive', '-a\n'
1510-
'A\n'
1511-
'@hello'),
1512-
('invalid', '@no-such-path\n'),
1508+
('hello', os.fsencode(self.hello) + b'\n'),
1509+
('recursive', b'-a\n'
1510+
b'A\n'
1511+
b'@hello'),
1512+
('invalid', b'@no-such-path\n'),
1513+
('undecodable', self.undecodable + b'\n'),
15131514
]
15141515
for path, text in file_texts:
1515-
with open(path, 'w', encoding="utf-8") as file:
1516+
with open(path, 'wb') as file:
15161517
file.write(text)
15171518

15181519
parser_signature = Sig(fromfile_prefix_chars='@')
@@ -1522,15 +1523,24 @@ def setUp(self):
15221523
Sig('y', nargs='+'),
15231524
]
15241525
failures = ['', '-b', 'X', '@invalid', '@missing']
1526+
hello = 'hello world!' + os_helper.FS_NONASCII
15251527
successes = [
15261528
('X Y', NS(a=None, x='X', y=['Y'])),
15271529
('X -a A Y Z', NS(a='A', x='X', y=['Y', 'Z'])),
1528-
('@hello X', NS(a=None, x='hello world!', y=['X'])),
1529-
('X @hello', NS(a=None, x='X', y=['hello world!'])),
1530-
('-a B @recursive Y Z', NS(a='A', x='hello world!', y=['Y', 'Z'])),
1531-
('X @recursive Z -a B', NS(a='B', x='X', y=['hello world!', 'Z'])),
1530+
('@hello X', NS(a=None, x=hello, y=['X'])),
1531+
('X @hello', NS(a=None, x='X', y=[hello])),
1532+
('-a B @recursive Y Z', NS(a='A', x=hello, y=['Y', 'Z'])),
1533+
('X @recursive Z -a B', NS(a='B', x='X', y=[hello, 'Z'])),
15321534
(["-a", "", "X", "Y"], NS(a='', x='X', y=['Y'])),
15331535
]
1536+
if os_helper.TESTFN_UNDECODABLE:
1537+
undecodable = os_helper.TESTFN_UNDECODABLE.lstrip(b'@')
1538+
successes += [
1539+
('@undecodable X', NS(a=None, x=os.fsdecode(undecodable), y=['X'])),
1540+
('X @undecodable', NS(a=None, x='X', y=[os.fsdecode(undecodable)])),
1541+
]
1542+
else:
1543+
undecodable = b''
15341544

15351545

15361546
class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
@@ -1539,10 +1549,10 @@ class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
15391549
def setUp(self):
15401550
super(TestArgumentsFromFileConverter, self).setUp()
15411551
file_texts = [
1542-
('hello', 'hello world!\n'),
1552+
('hello', b'hello world!\n'),
15431553
]
15441554
for path, text in file_texts:
1545-
with open(path, 'w', encoding="utf-8") as file:
1555+
with open(path, 'wb') as file:
15461556
file.write(text)
15471557

15481558
class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):

0 commit comments

Comments
 (0)