Skip to content

Commit 17fc809

Browse files
committed
#16306: Fix multiple error messages when unknown command line parameters where passed to the interpreter. Patch by Hieu Nguyen.
1 parent 936199c commit 17fc809

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import sys
77
import unittest
88
from test.script_helper import (
9-
assert_python_ok, spawn_python, kill_python, python_exit_code
9+
assert_python_ok, assert_python_failure, spawn_python, kill_python,
10+
python_exit_code
1011
)
1112

1213

@@ -116,6 +117,14 @@ def test_del___main__(self):
116117
assert_python_ok(filename)
117118

118119

120+
def test_unknown_options(self):
121+
# Add "without='-E'" to prevent _assert_python append env_vars -E
122+
# which changes the output of stderr
123+
rc, out, err = assert_python_failure('-z', without='-E')
124+
self.assertIn(b'Unknown option', err)
125+
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
126+
self.assertEqual(b'', out)
127+
119128
def test_main():
120129
test.test_support.run_unittest(CmdLineTest)
121130
test.test_support.reap_children()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ Tony Nelson
701701
Chad Netzer
702702
Max Neunhöffer
703703
George Neville-Neil
704+
Hieu Nguyen
704705
Johannes Nicolai
705706
Samuel Nicolary
706707
Gustavo Niemeyer

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ What's New in Python 2.7.4
99
Core and Builtins
1010
-----------------
1111

12+
- Issue #16306: Fix multiple error messages when unknown command line
13+
parameters where passed to the interpreter. Patch by Hieu Nguyen.
14+
1215
- Issue #15379: Fix passing of non-BMP characters as integers for the charmap
1316
decoder (already working as unicode strings). Patch by Serhiy Storchaka.
1417

Python/getopt.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static char *opt_ptr = "";
4141

4242
void _PyOS_ResetGetOpt(void)
4343
{
44-
_PyOS_opterr = 1;
44+
_PyOS_opterr = 0; /* prevent printing the error in 2nd loop in main.c */
4545
_PyOS_optind = 1;
4646
_PyOS_optarg = NULL;
4747
opt_ptr = "";
@@ -86,17 +86,19 @@ int _PyOS_GetOpt(int argc, char **argv, char *optstring)
8686
opt_ptr = &argv[_PyOS_optind++][1];
8787
}
8888

89-
if ( (option = *opt_ptr++) == '\0')
89+
if ((option = *opt_ptr++) == '\0')
9090
return -1;
9191

9292
if (option == 'J') {
93-
fprintf(stderr, "-J is reserved for Jython\n");
93+
if (_PyOS_opterr)
94+
fprintf(stderr, "-J is reserved for Jython\n");
9495
return '_';
9596
}
9697

9798
if (option == 'X') {
98-
fprintf(stderr,
99-
"-X is reserved for implementation-specific arguments\n");
99+
if (_PyOS_opterr)
100+
fprintf(stderr,
101+
"-X is reserved for implementation-specific arguments\n");
100102
return '_';
101103
}
102104

@@ -117,7 +119,7 @@ int _PyOS_GetOpt(int argc, char **argv, char *optstring)
117119
if (_PyOS_optind >= argc) {
118120
if (_PyOS_opterr)
119121
fprintf(stderr,
120-
"Argument expected for the -%c option\n", option);
122+
"Argument expected for the -%c option\n", option);
121123
return '_';
122124
}
123125

0 commit comments

Comments
 (0)