From f7c41580578fdae4f8ab6c8eb6135902de5214d1 Mon Sep 17 00:00:00 2001 From: Michael Foord Date: Sun, 10 Jun 2012 20:36:32 +0100 Subject: [PATCH 1/4] Adding patch.stopall method to unittest.mock --- Doc/library/unittest.mock.rst | 8 ++++++-- Lib/unittest/mock.py | 22 ++++++++++++++++++++-- Lib/unittest/test/testmock/testpatch.py | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 12b0275aab6..1b396972613 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -1354,8 +1354,12 @@ method of a `TestCase`: As an added bonus you no longer need to keep a reference to the `patcher` object. -In fact `start` and `stop` are just aliases for the context manager -`__enter__` and `__exit__` methods. +It is also possible to stop all patches which have been started by using +`patch.stopall`. + +.. function:: patch.stopall + + Stop all active patches. TEST_PREFIX diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 4ae3d16139e..95570aa3a99 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1002,6 +1002,7 @@ def _is_started(patcher): class _patch(object): attribute_name = None + _active_patches = set() def __init__( self, getter, attribute, new, spec, create, @@ -1270,8 +1271,18 @@ class _patch(object): if _is_started(patcher): patcher.__exit__(*exc_info) - start = __enter__ - stop = __exit__ + + def start(self): + """Activate a patch, returning any created mock.""" + result = self.__enter__() + self._active_patches.add(self) + return result + + + def stop(self): + """Stop an active patch.""" + self._active_patches.discard(self) + return self.__exit__() @@ -1562,9 +1573,16 @@ def _clear_dict(in_dict): del in_dict[key] +def _patch_stopall(): + """Stop all active patches.""" + for patch in list(_patch._active_patches): + patch.stop() + + patch.object = _patch_object patch.dict = _patch_dict patch.multiple = _patch_multiple +patch.stopall = _patch_stopall patch.TEST_PREFIX = 'test' magic_methods = ( diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py index 62568554da2..c1091b4e9b3 100644 --- a/Lib/unittest/test/testmock/testpatch.py +++ b/Lib/unittest/test/testmock/testpatch.py @@ -1762,6 +1762,24 @@ class PatchTest(unittest.TestCase): p.stop() + def test_patch_stopall(self): + unlink = os.unlink + chdir = os.chdir + path = os.path + patch('os.unlink', something).start() + patch('os.chdir', something_else).start() + + @patch('os.path') + def patched(mock_path): + patch.stopall() + self.assertIs(os.path, mock_path) + self.assertIs(os.unlink, unlink) + self.assertIs(os.chdir, chdir) + + patched() + self.assertIs(os.path, path) + + if __name__ == '__main__': unittest.main() From 911fd327f1ca38035d11c5f6688d9c74e069822d Mon Sep 17 00:00:00 2001 From: Michael Foord Date: Sun, 10 Jun 2012 20:38:54 +0100 Subject: [PATCH 2/4] unittest.mock minor doc update --- Doc/library/unittest.mock.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 1b396972613..bed698afe40 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -1359,7 +1359,7 @@ It is also possible to stop all patches which have been started by using .. function:: patch.stopall - Stop all active patches. + Stop all active patches. Only stops patches started with `start`. TEST_PREFIX From a26ad5a0a177e308108867fa7dce06f1d55fa1af Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Mon, 11 Jun 2012 12:59:07 +1000 Subject: [PATCH 3/4] #15015: Fix accessing an non-existing attribute. --- Lib/concurrent/futures/_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/concurrent/futures/_base.py b/Lib/concurrent/futures/_base.py index 88b5fbdd860..1e098be33ff 100644 --- a/Lib/concurrent/futures/_base.py +++ b/Lib/concurrent/futures/_base.py @@ -470,8 +470,8 @@ class Future(object): return True else: LOGGER.critical('Future %s in unexpected state: %s', - id(self.future), - self.future._state) + id(self), + self._state) raise RuntimeError('Future in unexpected state') def set_result(self, result): From 88e19779ad010a60c4b13fe0197a9a7a79af7587 Mon Sep 17 00:00:00 2001 From: Stefan Krah Date: Mon, 11 Jun 2012 08:57:17 +0200 Subject: [PATCH 4/4] 1) Replace long-winded abort() construct by assert(). 2) Remove micro optimization (inline checking for NaN before calling mpd_qcheck_nans()) that probably has no benefit in this case. --- Modules/_decimal/libmpdec/mpdecimal.c | 65 +++++++++++++-------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/Modules/_decimal/libmpdec/mpdecimal.c b/Modules/_decimal/libmpdec/mpdecimal.c index f5be7474009..ff6d867e49c 100644 --- a/Modules/_decimal/libmpdec/mpdecimal.c +++ b/Modules/_decimal/libmpdec/mpdecimal.c @@ -5713,30 +5713,28 @@ void mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status) { - mpd_context_t workctx; /* function context */ + mpd_context_t workctx; MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1); if (mpd_isspecial(a)) { if (mpd_qcheck_nan(result, a, ctx, status)) { return; } - if (mpd_isinfinite(a)) { - if (mpd_isnegative(a)) { - mpd_qcopy(result, a, status); - return; - } - else { - mpd_clear_flags(result); - mpd_qmaxcoeff(result, ctx, status); - if (mpd_isnan(result)) { - return; - } - result->exp = ctx->emax - ctx->prec + 1; - return; - } + + assert(mpd_isinfinite(a)); + if (mpd_isnegative(a)) { + mpd_qcopy(result, a, status); + return; + } + else { + mpd_clear_flags(result); + mpd_qmaxcoeff(result, ctx, status); + if (mpd_isnan(result)) { + return; + } + result->exp = mpd_etop(ctx); + return; } - /* debug */ - abort(); /* GCOV_NOT_REACHED */ } mpd_workcontext(&workctx, ctx); @@ -5769,21 +5767,21 @@ mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, if (mpd_qcheck_nan(result, a, ctx, status)) { return; } - if (mpd_isinfinite(a)) { - if (mpd_ispositive(a)) { - mpd_qcopy(result, a, status); - } - else { - mpd_clear_flags(result); - mpd_qmaxcoeff(result, ctx, status); - if (mpd_isnan(result)) { - return; - } - mpd_set_flags(result, MPD_NEG); - result->exp = mpd_etop(ctx); - } - return; + + assert(mpd_isinfinite(a)); + if (mpd_ispositive(a)) { + mpd_qcopy(result, a, status); } + else { + mpd_clear_flags(result); + mpd_qmaxcoeff(result, ctx, status); + if (mpd_isnan(result)) { + return; + } + mpd_set_flags(result, MPD_NEG); + result->exp = mpd_etop(ctx); + } + return; } mpd_workcontext(&workctx, ctx); @@ -5814,9 +5812,8 @@ mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, { int c; - if (mpd_isnan(a) || mpd_isnan(b)) { - if (mpd_qcheck_nans(result, a, b, ctx, status)) - return; + if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; } c = _mpd_cmp(a, b);