summaryrefslogtreecommitdiff
path: root/Lib/unittest/test/test_case.py
diff options
context:
space:
mode:
authorkernc <kerncece@gmail.com>2020-06-11 20:03:29 +0200
committerGitHub <noreply@github.com>2020-06-11 14:03:29 -0400
commit46398fba4d66ad342cf2504ef947b5fb857423b2 (patch)
tree307f5e4c871dff3f0b83b8e930b61fddaf362993 /Lib/unittest/test/test_case.py
parent436b648910c27baf8164a6d46d746d36d8a93478 (diff)
downloadcpython-git-46398fba4d66ad342cf2504ef947b5fb857423b2.tar.gz
bpo-29620: iterate over a copy of sys.modules (GH-4800)
unittest.TestCase.assertWarns no longer raises a RuntimeException when accessing a module's ``__warningregistry__`` causes importation of a new module, or when a new module is imported in another thread. Patch by Kernc.
Diffstat (limited to 'Lib/unittest/test/test_case.py')
-rw-r--r--Lib/unittest/test/test_case.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index f855c4dc00..3dedcbe6aa 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -8,6 +8,7 @@ import logging
import warnings
import weakref
import inspect
+import types
from copy import deepcopy
from test import support
@@ -1350,6 +1351,20 @@ test case
pass
self.assertRaises(TypeError, self.assertWarnsRegex, MyWarn, lambda: True)
+ def testAssertWarnsModifySysModules(self):
+ # bpo-29620: handle modified sys.modules during iteration
+ class Foo(types.ModuleType):
+ @property
+ def __warningregistry__(self):
+ sys.modules['@bar@'] = 'bar'
+
+ sys.modules['@foo@'] = Foo('foo')
+ try:
+ self.assertWarns(UserWarning, warnings.warn, 'expected')
+ finally:
+ del sys.modules['@foo@']
+ del sys.modules['@bar@']
+
def testAssertRaisesRegexMismatch(self):
def Stub():
raise Exception('Unexpected')