diff -r 84cd07899baf Lib/test/test_cmd_line.py --- a/Lib/test/test_cmd_line.py Tue Oct 23 14:39:34 2012 +0300 +++ b/Lib/test/test_cmd_line.py Fri Oct 26 00:28:20 2012 +0300 @@ -4,7 +4,8 @@ import test.test_support, unittest import sys -from test.script_helper import spawn_python, kill_python, python_exit_code +from test.script_helper import (spawn_python, kill_python, python_exit_code, + assert_python_failure) class CmdLineTest(unittest.TestCase): @@ -101,6 +102,14 @@ data = self.start_python('-R', '-c', code) self.assertTrue('hash_randomization=1' in data) + 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('-z', without='-E') + self.assertIn(b'Unknown option', err) + self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) + self.assertEqual(b'', out) + def test_main(): test.test_support.run_unittest(CmdLineTest) test.test_support.reap_children() diff -r 84cd07899baf Python/getopt.c --- a/Python/getopt.c Tue Oct 23 14:39:34 2012 +0300 +++ b/Python/getopt.c Fri Oct 26 00:28:20 2012 +0300 @@ -41,7 +41,7 @@ void _PyOS_ResetGetOpt(void) { - _PyOS_opterr = 1; + _PyOS_opterr = 0; /* prevent printing the error in 2nd loop in main.c */ _PyOS_optind = 1; _PyOS_optarg = NULL; opt_ptr = ""; @@ -86,17 +86,19 @@ opt_ptr = &argv[_PyOS_optind++][1]; } - if ( (option = *opt_ptr++) == '\0') + if ((option = *opt_ptr++) == '\0') return -1; if (option == 'J') { - fprintf(stderr, "-J is reserved for Jython\n"); + if (_PyOS_opterr) + fprintf(stderr, "-J is reserved for Jython\n"); return '_'; } if (option == 'X') { - fprintf(stderr, - "-X is reserved for implementation-specific arguments\n"); + if (_PyOS_opterr) + fprintf(stderr, + "-X is reserved for implementation-specific arguments\n"); return '_'; } @@ -117,7 +119,7 @@ if (_PyOS_optind >= argc) { if (_PyOS_opterr) fprintf(stderr, - "Argument expected for the -%c option\n", option); + "Argument expected for the -%c option\n", option); return '_'; }