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

Skip to content

Commit 8a6d1fe

Browse files
committed
#16306: merge with 3.2.
2 parents a5c4309 + a0dd22e commit 8a6d1fe

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,24 @@ def test_del___main__(self):
370370
print("del sys.modules['__main__']", file=script)
371371
assert_python_ok(filename)
372372

373-
374373
def test_unknown_options(self):
375-
rc, out, err = assert_python_failure('-z', __cleanenv=True)
376-
self.assertIn(b'Unknown option', err)
374+
rc, out, err = assert_python_failure('-E', '-z')
375+
self.assertIn(b'Unknown option: -z', err)
376+
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
377+
self.assertEqual(b'', out)
378+
# Add "without='-E'" to prevent _assert_python to append -E
379+
# to env_vars and change the output of stderr
380+
rc, out, err = assert_python_failure('-z', without='-E')
381+
self.assertIn(b'Unknown option: -z', err)
377382
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
378383
self.assertEqual(b'', out)
384+
rc, out, err = assert_python_failure('-a', '-z', without='-E')
385+
self.assertIn(b'Unknown option: -a', err)
386+
# only the first unknown option is reported
387+
self.assertNotIn(b'Unknown option: -z', err)
388+
self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1)
389+
self.assertEqual(b'', out)
390+
379391

380392
def test_main():
381393
test.support.run_unittest(CmdLineTest)

Modules/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ Py_Main(int argc, wchar_t **argv)
338338

339339
/* Hash randomization needed early for all string operations
340340
(including -W and -X options). */
341+
_PyOS_opterr = 0; /* prevent printing the error in 1st pass */
341342
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
342343
if (c == 'm' || c == 'c') {
343344
/* -c / -m is the last option: following arguments are

Python/getopt.c

Lines changed: 1 addition & 1 deletion
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 = 0; /* prevent printing the error in 2nd loop in main.c */
48+
_PyOS_opterr = 1;
4949
_PyOS_optind = 1;
5050
_PyOS_optarg = NULL;
5151
opt_ptr = L"";

0 commit comments

Comments
 (0)