Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c66319

Browse files
committed
#16306: Fix multiple error messages when unknown command line parameters where passed to the interpreter. Patch by Hieu Nguyen.
1 parent 6a5fc4c commit 7c66319

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import sys
88
import subprocess
99
import tempfile
10-
from test.script_helper import spawn_python, kill_python, assert_python_ok, assert_python_failure
10+
from test.script_helper import (spawn_python, kill_python, assert_python_ok,
11+
assert_python_failure)
1112

1213

1314
# XXX (ncoghlan): Move to script_helper and make consistent with run_python
@@ -376,6 +377,12 @@ def test_del___main__(self):
376377
assert_python_ok(filename)
377378

378379

380+
def test_unknown_options(self):
381+
rc, out, err = assert_python_failure('-z', __cleanenv=True)
382+
self.assertIn(b'Unknown option', err)
383+
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
384+
self.assertEqual(b'', out)
385+
379386
def test_main():
380387
test.support.run_unittest(CmdLineTest)
381388
test.support.reap_children()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ Tony Nelson
757757
Chad Netzer
758758
Max Neunhöffer
759759
George Neville-Neil
760+
Hieu Nguyen
760761
Johannes Nicolai
761762
Samuel Nicolary
762763
Gustavo Niemeyer

Misc/NEWS

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

13+
- Issue #16306: Fix multiple error messages when unknown command line
14+
parameters where passed to the interpreter. Patch by Hieu Nguyen.
15+
1316
- Issue #16453: Fix equality testing of dead weakref objects.
1417

1518
- Issue #9535: Fix pending signals that have been received but not yet

Python/getopt.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static wchar_t *opt_ptr = L"";
4545

4646
void _PyOS_ResetGetOpt(void)
4747
{
48-
_PyOS_opterr = 1;
48+
_PyOS_opterr = 0; /* prevent printing the error in 2nd loop in main.c */
4949
_PyOS_optind = 1;
5050
_PyOS_optarg = NULL;
5151
opt_ptr = L"";
@@ -90,18 +90,18 @@ int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
9090
opt_ptr = &argv[_PyOS_optind++][1];
9191
}
9292

93-
if ( (option = *opt_ptr++) == L'\0')
93+
if ((option = *opt_ptr++) == L'\0')
9494
return -1;
9595

9696
if (option == 'J') {
97-
fprintf(stderr, "-J is reserved for Jython\n");
97+
if (_PyOS_opterr)
98+
fprintf(stderr, "-J is reserved for Jython\n");
9899
return '_';
99100
}
100101

101102
if ((ptr = wcschr(optstring, option)) == NULL) {
102103
if (_PyOS_opterr)
103-
fprintf(stderr, "Unknown option: -%c\n", (char)option);
104-
104+
fprintf(stderr, "Unknown option: -%c\n", (char)option);
105105
return '_';
106106
}
107107

0 commit comments

Comments
 (0)