Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #9815: assertRaises now tries to clear references to local vari…
…ables in the exception's traceback.
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import datetime | ||
import warnings | ||
import weakref | ||
import unittest | ||
from itertools import product | ||
|
||
|
@@ -97,6 +98,36 @@ def _raise(e): | |
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()) | ||
This comment has been minimized.
Sorry, something went wrong.
blueyed
Contributor
|
||
|
||
def testAssertNotRegex(self): | ||
self.assertNotRegex('Ala ma kota', r'r+') | ||
try: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Maybe clearing the traceback frames should only get done for expected exceptions? (https://bugs.python.org/issue39766)