From a8a7c689905f816db0e73e3cf643cd5daef76932 Mon Sep 17 00:00:00 2001 From: Zhiqiang Fan Date: Sun, 27 Sep 2015 09:38:04 -0600 Subject: [PATCH] print usage when no argument is specified for python3 When running just 'glance' under python3, we will get the error: ERROR: 'Namespace' object has no attribute 'func' This is because map() is used to decode sys.argv, but under Python3 it returns a map object which is an iterable. Some code later tries to use this in a boolean context and it will always return True, even if it's empty. Change-Id: I2f03e462cb813833b75b9f2de7badd10b10cddff Closes-Bug: #1295356 --- glanceclient/shell.py | 3 ++- glanceclient/tests/unit/test_shell.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/glanceclient/shell.py b/glanceclient/shell.py index 06b2c46f..8123cd8b 100755 --- a/glanceclient/shell.py +++ b/glanceclient/shell.py @@ -759,7 +759,8 @@ class HelpFormatter(argparse.HelpFormatter): def main(): try: - OpenStackImagesShell().main(map(encodeutils.safe_decode, sys.argv[1:])) + argv = [encodeutils.safe_decode(a) for a in sys.argv[1:]] + OpenStackImagesShell().main(argv) except KeyboardInterrupt: utils.exit('... terminating glance client', exit_code=130) except Exception as e: diff --git a/glanceclient/tests/unit/test_shell.py b/glanceclient/tests/unit/test_shell.py index 3247d052..45c62f6e 100644 --- a/glanceclient/tests/unit/test_shell.py +++ b/glanceclient/tests/unit/test_shell.py @@ -504,6 +504,20 @@ class ShellTest(testutils.TestCase): glance_shell = openstack_shell.OpenStackImagesShell() self.assertRaises(exc.CommandError, glance_shell.main, args.split()) + @mock.patch('sys.argv', ['glance']) + @mock.patch('sys.stdout', six.StringIO()) + @mock.patch('sys.stderr', six.StringIO()) + def test_main_noargs(self): + # Ensure that main works with no command-line arguments + try: + openstack_shell.main() + except SystemExit: + self.fail('Unexpected SystemExit') + + # We expect the normal usage as a result + self.assertIn('Command-line interface to the OpenStack Images API', + sys.stdout.getvalue()) + class ShellTestWithKeystoneV3Auth(ShellTest): # auth environment to use