From e41682b9945091c2e4b95a3f6a4582944fd7598e Mon Sep 17 00:00:00 2001 From: Richard Oudkerk Date: Wed, 6 Jun 2012 19:04:57 +0100 Subject: Issue #12157: pool.map() does not handle empty iterable correctly Initial patch by mouad --- Lib/multiprocessing/pool.py | 1 + Lib/test/test_multiprocessing.py | 18 +++++++++++++++--- Misc/NEWS | 3 +++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py index 0c29e64..ccee961 100644 --- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -584,6 +584,7 @@ class MapResult(ApplyResult): if chunksize <= 0: self._number_left = 0 self._ready = True + del cache[self._job] else: self._number_left = length//chunksize + bool(length % chunksize) diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index 5f1bba3..0d98a14 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -1178,6 +1178,18 @@ class _TestPool(BaseTestCase): join() self.assertLess(join.elapsed, 0.5) + def test_empty_iterable(self): + # See Issue 12157 + p = self.Pool(1) + + self.assertEqual(p.map(sqr, []), []) + self.assertEqual(list(p.imap(sqr, [])), []) + self.assertEqual(list(p.imap_unordered(sqr, [])), []) + self.assertEqual(p.map_async(sqr, []).get(), []) + + p.close() + p.join() + def raising(): raise KeyError("key") @@ -2176,7 +2188,7 @@ class ProcessesMixin(object): 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event', 'Value', 'Array', 'RawValue', 'RawArray', 'current_process', 'active_children', 'Pipe', - 'connection', 'JoinableQueue' + 'connection', 'JoinableQueue', 'Pool' ))) testcases_processes = create_test_cases(ProcessesMixin, type='processes') @@ -2190,7 +2202,7 @@ class ManagerMixin(object): locals().update(get_attributes(manager, ( 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event', 'Value', 'Array', 'list', 'dict', - 'Namespace', 'JoinableQueue' + 'Namespace', 'JoinableQueue', 'Pool' ))) testcases_manager = create_test_cases(ManagerMixin, type='manager') @@ -2204,7 +2216,7 @@ class ThreadsMixin(object): 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event', 'Value', 'Array', 'current_process', 'active_children', 'Pipe', 'connection', 'dict', 'list', - 'Namespace', 'JoinableQueue' + 'Namespace', 'JoinableQueue', 'Pool' ))) testcases_threads = create_test_cases(ThreadsMixin, type='threads') diff --git a/Misc/NEWS b/Misc/NEWS index 2890ebe..071e962 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -70,6 +70,9 @@ Core and Builtins Library ------- +- Issue #12157: Make pool.map() empty iterables correctly. Initial + patch by mouad. + - Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError when the path existed and had the S_ISGID mode bit set when it was not explicitly asked for. This is no longer an exception as mkdir -- cgit v0.12 From 29471de459a9371d7538a9838b1b20c86df29ca7 Mon Sep 17 00:00:00 2001 From: Richard Oudkerk Date: Wed, 6 Jun 2012 19:04:57 +0100 Subject: 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. --- Lib/multiprocessing/process.py | 6 +++--- Lib/test/test_multiprocessing.py | 30 ++++++++++++++++++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index 2b61ee9..3262b50 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -271,11 +271,11 @@ class Process(object): except SystemExit as e: if not e.args: exitcode = 1 - elif type(e.args[0]) is int: + elif isinstance(e.args[0], int): exitcode = e.args[0] else: - sys.stderr.write(e.args[0] + '\n') - exitcode = 1 + sys.stderr.write(str(e.args[0]) + '\n') + exitcode = 0 if isinstance(e.args[0], str) else 1 except: exitcode = 1 import traceback diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index 0d98a14..b812e48 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -390,6 +390,36 @@ class _TestSubclassingProcess(BaseTestCase): 1/0 # MARKER + @classmethod + def _test_sys_exit(cls, reason, testfn): + sys.stderr = open(testfn, 'w') + sys.exit(reason) + + def test_sys_exit(self): + # See Issue 13854 + if self.TYPE == 'threads': + return + + testfn = test.support.TESTFN + self.addCleanup(test.support.unlink, testfn) + + for reason, code in (([1, 2, 3], 1), ('ignore this', 0)): + p = self.Process(target=self._test_sys_exit, args=(reason, testfn)) + p.daemon = True + p.start() + p.join(5) + self.assertEqual(p.exitcode, code) + + with open(testfn, 'r') as f: + self.assertEqual(f.read().rstrip(), str(reason)) + + for reason in (True, False, 8): + p = self.Process(target=sys.exit, args=(reason,)) + p.daemon = True + p.start() + p.join(5) + self.assertEqual(p.exitcode, reason) + # # # diff --git a/Misc/NEWS b/Misc/NEWS index 071e962..3f5c867 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -70,6 +70,9 @@ Core and Builtins Library ------- +- Issue #13854: Make multiprocessing properly handle non-integer + non-string argument to SystemExit. + - Issue #12157: Make pool.map() empty iterables correctly. Initial patch by mouad. -- cgit v0.12