[Python-checkins] cpython (3.4): Issue #9815: assertRaises now tries to clear references to local variables in

antoine.pitrou python-checkins at python.org
Tue Apr 29 01:25:47 CEST 2014


http://hg.python.org/cpython/rev/6ab3193e890e
changeset:   90490:6ab3193e890e
branch:      3.4
parent:      90482:7052fdd90a11
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Tue Apr 29 01:23:50 2014 +0200
summary:
  Issue #9815: assertRaises now tries to clear references to local variables in the exception's traceback.

files:
  Lib/unittest/case.py                 |   3 +
  Lib/unittest/test/test_assertions.py |  31 ++++++++++++++++
  Misc/NEWS                            |   3 +
  3 files changed, 37 insertions(+), 0 deletions(-)


diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -9,6 +9,7 @@
 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 @@
                                                                 self.obj_name))
             else:
                 self._raiseFailure("{} not raised".format(exc_name))
+        else:
+            traceback.clear_frames(tb)
         if not issubclass(exc_type, self.expected):
             # let unexpected exceptions pass through
             return False
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py
--- 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 @@
         else:
             self.fail("assertRaises() didn't let exception pass through")
 
+    def test_assertRaises_frames_survival(self):
+        # Issue #9815: assertRaises should avoid keeping local variables
+        # in a traceback alive.
+        class A:
+            pass
+        wr = None
+
+        class Foo(unittest.TestCase):
+
+            def foo(self):
+                nonlocal wr
+                a = A()
+                wr = weakref.ref(a)
+                try:
+                    raise IOError
+                except IOError:
+                    raise ValueError
+
+            def test_functional(self):
+                self.assertRaises(ValueError, self.foo)
+
+            def test_with(self):
+                with self.assertRaises(ValueError):
+                    self.foo()
+
+        Foo("test_functional").run()
+        self.assertIsNone(wr())
+        Foo("test_with").run()
+        self.assertIsNone(wr())
+
     def testAssertNotRegex(self):
         self.assertNotRegex('Ala ma kota', r'r+')
         try:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,9 @@
 Library
 -------
 
+- Issue #9815: assertRaises now tries to clear references to local variables
+  in the exception's traceback.
+
 - Issue #13204: Calling sys.flags.__new__ would crash the interpreter,
   now it raises a TypeError.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list