]> granicus.if.org Git - python/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:46:11 +0000 (18:46 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Fri, 23 Nov 2012 16:46:11 +0000 (18:46 +0200)
Lib/test/test_cmd_line.py
Modules/main.c
Python/getopt.c

index b803046c360bbf13f21b33a456fa6c9191befa1b..12f26d9e4506f190b7c70b9c0825c4a55e32ac89 100644 (file)
@@ -116,14 +116,24 @@ class CmdLineTest(unittest.TestCase):
             print >>script, "del sys.modules['__main__']"
         assert_python_ok(filename)
 
-
     def test_unknown_options(self):
-        # Add "without='-E'" to prevent _assert_python append env_vars -E
-        # which changes the output of stderr
+        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', err)
+        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.test_support.run_unittest(CmdLineTest)
index 1ce7c8f213861d0b71596b924081d94cd4421533..ef9b24508ca22fba776e3e98ca24f29c26cd9f7f 100644 (file)
@@ -264,6 +264,7 @@ Py_Main(int argc, char **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 624da9ad895d984aeb0289ebe7d6fd999d3de901..af5b03c4f42eb53c0937d5b09cdb05220ab225ae 100644 (file)
@@ -41,7 +41,7 @@ static char *opt_ptr = "";
 
 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 = "";