Skip to content

Commit 605e9c6

Browse files
gh-85308: Add argparse tests for reading non-ASCII arguments from file (GH-94160)
1 parent 0a40025 commit 605e9c6

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

Lib/test/support/os_helper.py

+5
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@
141141
try:
142142
name.decode(sys.getfilesystemencoding())
143143
except UnicodeDecodeError:
144+
try:
145+
name.decode(sys.getfilesystemencoding(),
146+
sys.getfilesystemencodeerrors())
147+
except UnicodeDecodeError:
148+
continue
144149
TESTFN_UNDECODABLE = os.fsencode(TESTFN_ASCII) + name
145150
break
146151

Lib/test/test_argparse.py

+23-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,25 @@ 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+
decoded_undecodable = os.fsdecode(undecodable)
1539+
successes += [
1540+
('@undecodable X', NS(a=None, x=decoded_undecodable, y=['X'])),
1541+
('X @undecodable', NS(a=None, x='X', y=[decoded_undecodable])),
1542+
]
1543+
else:
1544+
undecodable = b''
15341545

15351546

15361547
class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
@@ -1539,10 +1550,10 @@ class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
15391550
def setUp(self):
15401551
super(TestArgumentsFromFileConverter, self).setUp()
15411552
file_texts = [
1542-
('hello', 'hello world!\n'),
1553+
('hello', b'hello world!\n'),
15431554
]
15441555
for path, text in file_texts:
1545-
with open(path, 'w', encoding="utf-8") as file:
1556+
with open(path, 'wb') as file:
15461557
file.write(text)
15471558

15481559
class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):

0 commit comments

Comments
 (0)