cpython: 6ab3193e890e (original) (raw)
Mercurial > cpython
changeset 90490:6ab3193e890e 3.4
Issue #9815: assertRaises now tries to clear references to local variables in the exception's traceback. [#9815]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Tue, 29 Apr 2014 01:23:50 +0200 |
parents | 7052fdd90a11 |
children | 553fe27521be 37786ae8cc1c |
files | Lib/unittest/case.py Lib/unittest/test/test_assertions.py Misc/NEWS |
diffstat | 3 files changed, 37 insertions(+), 0 deletions(-)[+] [-] Lib/unittest/case.py 3 Lib/unittest/test/test_assertions.py 31 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -9,6 +9,7 @@ import re import warnings import collections import contextlib +import traceback from . import result from .util import (strclass, safe_repr, _count_diff_all_purpose, @@ -178,6 +179,8 @@ class _AssertRaisesContext(_AssertRaises self.obj_name)) else: self._raiseFailure("{} not raised".format(exc_name))
else:[](#l1.15)
traceback.clear_frames(tb)[](#l1.16) if not issubclass(exc_type, self.expected):[](#l1.17) # let unexpected exceptions pass through[](#l1.18) return False[](#l1.19)
--- a/Lib/unittest/test/test_assertions.py +++ b/Lib/unittest/test/test_assertions.py @@ -1,5 +1,6 @@ import datetime import warnings +import weakref import unittest from itertools import product @@ -97,6 +98,36 @@ class Test_Assertions(unittest.TestCase) else: self.fail("assertRaises() didn't let exception pass through")
- def test_assertRaises_frames_survival(self):
# Issue #9815: assertRaises should avoid keeping local variables[](#l2.15)
# in a traceback alive.[](#l2.16)
class A:[](#l2.17)
pass[](#l2.18)
wr = None[](#l2.19)
class Foo(unittest.TestCase):[](#l2.21)
def foo(self):[](#l2.23)
nonlocal wr[](#l2.24)
a = A()[](#l2.25)
wr = weakref.ref(a)[](#l2.26)
try:[](#l2.27)
raise IOError[](#l2.28)
except IOError:[](#l2.29)
raise ValueError[](#l2.30)
def test_functional(self):[](#l2.32)
self.assertRaises(ValueError, self.foo)[](#l2.33)
def test_with(self):[](#l2.35)
with self.assertRaises(ValueError):[](#l2.36)
self.foo()[](#l2.37)
Foo("test_functional").run()[](#l2.39)
self.assertIsNone(wr())[](#l2.40)
Foo("test_with").run()[](#l2.41)
self.assertIsNone(wr())[](#l2.42)
+ def testAssertNotRegex(self): self.assertNotRegex('Ala ma kota', r'r+') try:
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -39,6 +39,9 @@ Core and Builtins Library ------- +- Issue #9815: assertRaises now tries to clear references to local variables