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

Skip to content

Commit 29471de

Browse files
committed
Issue #13854: Properly handle non-integer, non-string arg to SystemExit
Previously multiprocessing only expected int or str. It also wrongly used an exit code of 1 when the argument was a string instead of zero.
1 parent e41682b commit 29471de

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Lib/multiprocessing/process.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ def _bootstrap(self):
271271
except SystemExit as e:
272272
if not e.args:
273273
exitcode = 1
274-
elif type(e.args[0]) is int:
274+
elif isinstance(e.args[0], int):
275275
exitcode = e.args[0]
276276
else:
277-
sys.stderr.write(e.args[0] + '\n')
278-
exitcode = 1
277+
sys.stderr.write(str(e.args[0]) + '\n')
278+
exitcode = 0 if isinstance(e.args[0], str) else 1
279279
except:
280280
exitcode = 1
281281
import traceback

Lib/test/test_multiprocessing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,36 @@ def _test_stderr_flush(cls, testfn):
390390
1/0 # MARKER
391391

392392

393+
@classmethod
394+
def _test_sys_exit(cls, reason, testfn):
395+
sys.stderr = open(testfn, 'w')
396+
sys.exit(reason)
397+
398+
def test_sys_exit(self):
399+
# See Issue 13854
400+
if self.TYPE == 'threads':
401+
return
402+
403+
testfn = test.support.TESTFN
404+
self.addCleanup(test.support.unlink, testfn)
405+
406+
for reason, code in (([1, 2, 3], 1), ('ignore this', 0)):
407+
p = self.Process(target=self._test_sys_exit, args=(reason, testfn))
408+
p.daemon = True
409+
p.start()
410+
p.join(5)
411+
self.assertEqual(p.exitcode, code)
412+
413+
with open(testfn, 'r') as f:
414+
self.assertEqual(f.read().rstrip(), str(reason))
415+
416+
for reason in (True, False, 8):
417+
p = self.Process(target=sys.exit, args=(reason,))
418+
p.daemon = True
419+
p.start()
420+
p.join(5)
421+
self.assertEqual(p.exitcode, reason)
422+
393423
#
394424
#
395425
#

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Core and Builtins
7070
Library
7171
-------
7272

73+
- Issue #13854: Make multiprocessing properly handle non-integer
74+
non-string argument to SystemExit.
75+
7376
- Issue #12157: Make pool.map() empty iterables correctly. Initial
7477
patch by mouad.
7578

0 commit comments

Comments
 (0)