]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#16306: report only the first unknown option and add more tests. Patch by Serhiy...
authorEzio Melotti <ezio.melotti@gmail.com>
Fri, 23 Nov 2012 16:48:32 +0000 (18:48 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Fri, 23 Nov 2012 16:48:32 +0000 (18:48 +0200)
Lib/test/test_cmd_line.py
Modules/main.c
Python/getopt.c

index d8f244353cd72a0e3825921a49a96ddb42cf136f..e12f3055424428d6b15290ef912602c739628104 100644 (file)
@@ -376,12 +376,24 @@ class CmdLineTest(unittest.TestCase):
             print("del sys.modules['__main__']", file=script)
         assert_python_ok(filename)
 
-
     def test_unknown_options(self):
-        rc, out, err = assert_python_failure('-z', __cleanenv=True)
-        self.assertIn(b'Unknown option', err)
+        rc, out, err = assert_python_failure('-E', '-z')
+        self.assertIn(b'Unknown option: -z', err)
+        self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
+        self.assertEqual(b'', out)
+        # Add "without='-E'" to prevent _assert_python to append -E
+        # to env_vars and change the output of stderr
+        rc, out, err = assert_python_failure('-z', without='-E')
+        self.assertIn(b'Unknown option: -z', err)
         self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
         self.assertEqual(b'', out)
+        rc, out, err = assert_python_failure('-a', '-z', without='-E')
+        self.assertIn(b'Unknown option: -a', err)
+        # only the first unknown option is reported
+        self.assertNotIn(b'Unknown option: -z', err)
+        self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1)
+        self.assertEqual(b'', out)
+
 
 def test_main():
     test.support.run_unittest(CmdLineTest)
index 5d1d8964bfd59d23c29f4cb225c738026522792d..17aebae3ecba63a20a990a0f6384f6b0bc859619 100644 (file)
@@ -339,6 +339,7 @@ Py_Main(int argc, wchar_t **argv)
 
     /* Hash randomization needed early for all string operations
        (including -W and -X options). */
+    _PyOS_opterr = 0;  /* prevent printing the error in 1st pass */
     while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
         if (c == 'm' || c == 'c') {
             /* -c / -m is the last option: following arguments are
index 037aa5db51fb9ac111516b82ba6c26b86195197c..5cf4cbd7bb35e8ee3e3558aaf022d23cd0c93d8a 100644 (file)
@@ -45,7 +45,7 @@ static wchar_t *opt_ptr = L"";
 
 void _PyOS_ResetGetOpt(void)
 {
-    _PyOS_opterr = 0;  /* prevent printing the error in 2nd loop in main.c */
+    _PyOS_opterr = 1;
     _PyOS_optind = 1;
     _PyOS_optarg = NULL;
     opt_ptr = L"";