Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-85308: Add argparse tests for reading non-ASCII arguments from file #94160

Merged
merged 3 commits into from
Jun 24, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,14 +1505,15 @@ class TestArgumentsFromFile(TempDirMixin, ParserTestCase):
def setUp(self):
super(TestArgumentsFromFile, self).setUp()
file_texts = [
('hello', 'hello world!\n'),
('recursive', '-a\n'
'A\n'
'@hello'),
('invalid', '@no-such-path\n'),
('hello', self.hello.encode(sys.getfilesystemencoding()) + b'\n'),
('recursive', b'-a\n'
b'A\n'
b'@hello'),
('invalid', b'@no-such-path\n'),
('undecodable', self.undecodable + b'\n'),
]
for path, text in file_texts:
with open(path, 'w', encoding="utf-8") as file:
with open(path, 'wb') as file:
file.write(text)

parser_signature = Sig(fromfile_prefix_chars='@')
Expand All @@ -1522,15 +1523,26 @@ def setUp(self):
Sig('y', nargs='+'),
]
failures = ['', '-b', 'X', '@invalid', '@missing']
hello = 'hello world!' + os_helper.FS_NONASCII
successes = [
('X Y', NS(a=None, x='X', y=['Y'])),
('X -a A Y Z', NS(a='A', x='X', y=['Y', 'Z'])),
('@hello X', NS(a=None, x='hello world!', y=['X'])),
('X @hello', NS(a=None, x='X', y=['hello world!'])),
('-a B @recursive Y Z', NS(a='A', x='hello world!', y=['Y', 'Z'])),
('X @recursive Z -a B', NS(a='B', x='X', y=['hello world!', 'Z'])),
('@hello X', NS(a=None, x=hello, y=['X'])),
('X @hello', NS(a=None, x='X', y=[hello])),
('-a B @recursive Y Z', NS(a='A', x=hello, y=['Y', 'Z'])),
('X @recursive Z -a B', NS(a='B', x='X', y=[hello, 'Z'])),
(["-a", "", "X", "Y"], NS(a='', x='X', y=['Y'])),
]
if os_helper.TESTFN_UNDECODABLE:
undecodable = os_helper.TESTFN_UNDECODABLE.lstrip(b'@')
decoded_undecodable = undecodable.decode(sys.getfilesystemencoding(),
sys.getfilesystemencodeerrors())
successes += [
('@undecodable X', NS(a=None, x=decoded_undecodable, y=['X'])),
('X @undecodable', NS(a=None, x='X', y=[decoded_undecodable])),
]
else:
undecodable = b''


class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
Expand All @@ -1539,10 +1551,10 @@ class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
def setUp(self):
super(TestArgumentsFromFileConverter, self).setUp()
file_texts = [
('hello', 'hello world!\n'),
('hello', b'hello world!\n'),
]
for path, text in file_texts:
with open(path, 'w', encoding="utf-8") as file:
with open(path, 'wb') as file:
file.write(text)

class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
Expand Down