From: R David Murray Date: Sat, 8 Sep 2012 16:15:25 +0000 (-0400) Subject: #15847: allow args to be a tuple in parse_args X-Git-Tag: v2.7.5~109^2~331 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a99c7dedcb9310d80f2cf29a747e05fdde2716bc;p=python #15847: allow args to be a tuple in parse_args This fixes a regression introduced by the fix for issue #13922. Although args is not documented as being allowed to be a tuple, previously this worked and so naturally there are programs in the field that depend on it. Patch by Zbyszek Jędrzejewski-Szmek. --- diff --git a/Lib/argparse.py b/Lib/argparse.py index a73845594f..ab8ff2faec 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1692,9 +1692,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): return args def parse_known_args(self, args=None, namespace=None): - # args default to the system args if args is None: + # args default to the system args args = _sys.argv[1:] + else: + # make sure that args are mutable + args = list(args) # default Namespace built from parser defaults if namespace is None: diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 77f5aca21e..99c1babb4a 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -4486,6 +4486,24 @@ class TestTypeFunctionCallWithNonStringDefault(TestCase): class TestParseKnownArgs(TestCase): + def test_arguments_tuple(self): + parser = argparse.ArgumentParser() + parser.parse_args(()) + + def test_arguments_list(self): + parser = argparse.ArgumentParser() + parser.parse_args([]) + + def test_arguments_tuple_positional(self): + parser = argparse.ArgumentParser() + parser.add_argument('x') + parser.parse_args(('x',)) + + def test_arguments_list_positional(self): + parser = argparse.ArgumentParser() + parser.add_argument('x') + parser.parse_args(['x']) + def test_optionals(self): parser = argparse.ArgumentParser() parser.add_argument('--foo')