mirror of https://github.com/python/cpython
gh-85308: Add argparse tests for reading non-ASCII arguments from file (GH-94160)
This commit is contained in:
parent
0a40025b80
commit
605e9c66ad
|
@ -141,6 +141,11 @@ for name in (
|
||||||
try:
|
try:
|
||||||
name.decode(sys.getfilesystemencoding())
|
name.decode(sys.getfilesystemencoding())
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
|
try:
|
||||||
|
name.decode(sys.getfilesystemencoding(),
|
||||||
|
sys.getfilesystemencodeerrors())
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
continue
|
||||||
TESTFN_UNDECODABLE = os.fsencode(TESTFN_ASCII) + name
|
TESTFN_UNDECODABLE = os.fsencode(TESTFN_ASCII) + name
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
|
@ -1505,14 +1505,15 @@ class TestArgumentsFromFile(TempDirMixin, ParserTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestArgumentsFromFile, self).setUp()
|
super(TestArgumentsFromFile, self).setUp()
|
||||||
file_texts = [
|
file_texts = [
|
||||||
('hello', 'hello world!\n'),
|
('hello', os.fsencode(self.hello) + b'\n'),
|
||||||
('recursive', '-a\n'
|
('recursive', b'-a\n'
|
||||||
'A\n'
|
b'A\n'
|
||||||
'@hello'),
|
b'@hello'),
|
||||||
('invalid', '@no-such-path\n'),
|
('invalid', b'@no-such-path\n'),
|
||||||
|
('undecodable', self.undecodable + b'\n'),
|
||||||
]
|
]
|
||||||
for path, text in file_texts:
|
for path, text in file_texts:
|
||||||
with open(path, 'w', encoding="utf-8") as file:
|
with open(path, 'wb') as file:
|
||||||
file.write(text)
|
file.write(text)
|
||||||
|
|
||||||
parser_signature = Sig(fromfile_prefix_chars='@')
|
parser_signature = Sig(fromfile_prefix_chars='@')
|
||||||
|
@ -1522,15 +1523,25 @@ class TestArgumentsFromFile(TempDirMixin, ParserTestCase):
|
||||||
Sig('y', nargs='+'),
|
Sig('y', nargs='+'),
|
||||||
]
|
]
|
||||||
failures = ['', '-b', 'X', '@invalid', '@missing']
|
failures = ['', '-b', 'X', '@invalid', '@missing']
|
||||||
|
hello = 'hello world!' + os_helper.FS_NONASCII
|
||||||
successes = [
|
successes = [
|
||||||
('X Y', NS(a=None, x='X', y=['Y'])),
|
('X Y', NS(a=None, x='X', y=['Y'])),
|
||||||
('X -a A Y Z', NS(a='A', x='X', y=['Y', 'Z'])),
|
('X -a A Y Z', NS(a='A', x='X', y=['Y', 'Z'])),
|
||||||
('@hello X', NS(a=None, x='hello world!', y=['X'])),
|
('@hello X', NS(a=None, x=hello, y=['X'])),
|
||||||
('X @hello', NS(a=None, x='X', y=['hello world!'])),
|
('X @hello', NS(a=None, x='X', y=[hello])),
|
||||||
('-a B @recursive Y Z', NS(a='A', x='hello world!', y=['Y', 'Z'])),
|
('-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 world!', 'Z'])),
|
('X @recursive Z -a B', NS(a='B', x='X', y=[hello, 'Z'])),
|
||||||
(["-a", "", "X", "Y"], NS(a='', x='X', y=['Y'])),
|
(["-a", "", "X", "Y"], NS(a='', x='X', y=['Y'])),
|
||||||
]
|
]
|
||||||
|
if os_helper.TESTFN_UNDECODABLE:
|
||||||
|
undecodable = os_helper.TESTFN_UNDECODABLE.lstrip(b'@')
|
||||||
|
decoded_undecodable = os.fsdecode(undecodable)
|
||||||
|
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):
|
class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
|
||||||
|
@ -1539,10 +1550,10 @@ class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestArgumentsFromFileConverter, self).setUp()
|
super(TestArgumentsFromFileConverter, self).setUp()
|
||||||
file_texts = [
|
file_texts = [
|
||||||
('hello', 'hello world!\n'),
|
('hello', b'hello world!\n'),
|
||||||
]
|
]
|
||||||
for path, text in file_texts:
|
for path, text in file_texts:
|
||||||
with open(path, 'w', encoding="utf-8") as file:
|
with open(path, 'wb') as file:
|
||||||
file.write(text)
|
file.write(text)
|
||||||
|
|
||||||
class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
|
class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
|
||||||
|
|
Loading…
Reference in New Issue