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

Skip to content

Commit 58ba47f

Browse files
committed
Merge fixes for #13854 and #12157.
2 parents 7448220 + 29471de commit 58ba47f

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

Lib/multiprocessing/pool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ def __init__(self, cache, chunksize, length, callback, error_callback):
576576
if chunksize <= 0:
577577
self._number_left = 0
578578
self._event.set()
579+
del cache[self._job]
579580
else:
580581
self._number_left = length//chunksize + bool(length % chunksize)
581582

Lib/multiprocessing/process.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ def _bootstrap(self):
262262
except SystemExit as e:
263263
if not e.args:
264264
exitcode = 1
265-
elif type(e.args[0]) is int:
265+
elif isinstance(e.args[0], int):
266266
exitcode = e.args[0]
267267
else:
268-
sys.stderr.write(e.args[0] + '\n')
269-
exitcode = 1
268+
sys.stderr.write(str(e.args[0]) + '\n')
269+
exitcode = 0 if isinstance(e.args[0], str) else 1
270270
except:
271271
exitcode = 1
272272
import traceback

Lib/test/test_multiprocessing.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,36 @@ def _test_stderr_flush(cls, testfn):
439439
1/0 # MARKER
440440

441441

442+
@classmethod
443+
def _test_sys_exit(cls, reason, testfn):
444+
sys.stderr = open(testfn, 'w')
445+
sys.exit(reason)
446+
447+
def test_sys_exit(self):
448+
# See Issue 13854
449+
if self.TYPE == 'threads':
450+
return
451+
452+
testfn = test.support.TESTFN
453+
self.addCleanup(test.support.unlink, testfn)
454+
455+
for reason, code in (([1, 2, 3], 1), ('ignore this', 0)):
456+
p = self.Process(target=self._test_sys_exit, args=(reason, testfn))
457+
p.daemon = True
458+
p.start()
459+
p.join(5)
460+
self.assertEqual(p.exitcode, code)
461+
462+
with open(testfn, 'r') as f:
463+
self.assertEqual(f.read().rstrip(), str(reason))
464+
465+
for reason in (True, False, 8):
466+
p = self.Process(target=sys.exit, args=(reason,))
467+
p.daemon = True
468+
p.start()
469+
p.join(5)
470+
self.assertEqual(p.exitcode, reason)
471+
442472
#
443473
#
444474
#
@@ -1342,6 +1372,18 @@ def test_terminate(self):
13421372
join()
13431373
self.assertLess(join.elapsed, 0.5)
13441374

1375+
def test_empty_iterable(self):
1376+
# See Issue 12157
1377+
p = self.Pool(1)
1378+
1379+
self.assertEqual(p.map(sqr, []), [])
1380+
self.assertEqual(list(p.imap(sqr, [])), [])
1381+
self.assertEqual(list(p.imap_unordered(sqr, [])), [])
1382+
self.assertEqual(p.map_async(sqr, []).get(), [])
1383+
1384+
p.close()
1385+
p.join()
1386+
13451387
def raising():
13461388
raise KeyError("key")
13471389

@@ -2487,7 +2529,7 @@ class ProcessesMixin(object):
24872529
'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
24882530
'Condition', 'Event', 'Value', 'Array', 'RawValue',
24892531
'RawArray', 'current_process', 'active_children', 'Pipe',
2490-
'connection', 'JoinableQueue'
2532+
'connection', 'JoinableQueue', 'Pool'
24912533
)))
24922534

24932535
testcases_processes = create_test_cases(ProcessesMixin, type='processes')
@@ -2501,7 +2543,7 @@ class ManagerMixin(object):
25012543
locals().update(get_attributes(manager, (
25022544
'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
25032545
'Condition', 'Event', 'Value', 'Array', 'list', 'dict',
2504-
'Namespace', 'JoinableQueue'
2546+
'Namespace', 'JoinableQueue', 'Pool'
25052547
)))
25062548

25072549
testcases_manager = create_test_cases(ManagerMixin, type='manager')
@@ -2515,7 +2557,7 @@ class ThreadsMixin(object):
25152557
'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
25162558
'Condition', 'Event', 'Value', 'Array', 'current_process',
25172559
'active_children', 'Pipe', 'connection', 'dict', 'list',
2518-
'Namespace', 'JoinableQueue'
2560+
'Namespace', 'JoinableQueue', 'Pool'
25192561
)))
25202562

25212563
testcases_threads = create_test_cases(ThreadsMixin, type='threads')

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Core and Builtins
2121
Library
2222
-------
2323

24+
- Issue #13854: Make multiprocessing properly handle non-integer
25+
non-string argument to SystemExit.
26+
27+
- Issue #12157: Make pool.map() empty iterables correctly. Initial
28+
patch by mouad.
29+
2430
- Issue #11823: disassembly now shows argument counts on calls with keyword args.
2531

2632
- Issue #14711: os.stat_float_times() has been deprecated.

0 commit comments

Comments
 (0)