[Python-checkins] cpython (2.7): #16306: Fix multiple error messages when unknown command line parameters where

ezio.melotti python-checkins at python.org
Sun Nov 18 13:07:15 CET 2012


http://hg.python.org/cpython/rev/d2df83d7c649
changeset: 80503:d2df83d7c649
branch: 2.7
parent: 80499:8feaa8d04c56
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Sun Nov 18 13:46:38 2012 +0200
summary:
 #16306: Fix multiple error messages when unknown command line parameters where passed to the interpreter. Patch by Hieu Nguyen.
files:
 Lib/test/test_cmd_line.py | 11 ++++++++++-
 Misc/ACKS | 1 +
 Misc/NEWS | 3 +++
 Python/getopt.c | 14 ++++++++------
 4 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -6,7 +6,8 @@
 import sys
 import unittest
 from test.script_helper import (
- assert_python_ok, spawn_python, kill_python, python_exit_code
+ assert_python_ok, assert_python_failure, spawn_python, kill_python,
+ python_exit_code
 )
 
 
@@ -116,6 +117,14 @@
 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('-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 --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -701,6 +701,7 @@
 Chad Netzer
 Max Neunhöffer
 George Neville-Neil
+Hieu Nguyen
 Johannes Nicolai
 Samuel Nicolary
 Gustavo Niemeyer
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
 Core and Builtins
 -----------------
 
+- Issue #16306: Fix multiple error messages when unknown command line
+ parameters where passed to the interpreter. Patch by Hieu Nguyen.
+
 - Issue #15379: Fix passing of non-BMP characters as integers for the charmap
 decoder (already working as unicode strings). Patch by Serhiy Storchaka.
 
diff --git a/Python/getopt.c b/Python/getopt.c
--- a/Python/getopt.c
+++ b/Python/getopt.c
@@ -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 '_';
 }
 
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /