Skip to content

Commit e2d2f9f

Browse files
encukoumcepl
authored andcommitted
Backport test.support.check_no_warnings
1 parent e59fc7a commit e2d2f9f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Lib/test/support/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,30 @@ def check_warnings(*filters, **kwargs):
12441244
return _filterwarnings(filters, quiet)
12451245

12461246

1247+
@contextlib.contextmanager
1248+
def check_no_warnings(testcase, message='', category=Warning, force_gc=False):
1249+
"""Context manager to check that no warnings are emitted.
1250+
1251+
This context manager enables a given warning within its scope
1252+
and checks that no warnings are emitted even with that warning
1253+
enabled.
1254+
1255+
If force_gc is True, a garbage collection is attempted before checking
1256+
for warnings. This may help to catch warnings emitted when objects
1257+
are deleted, such as ResourceWarning.
1258+
1259+
Other keyword arguments are passed to warnings.filterwarnings().
1260+
"""
1261+
with warnings.catch_warnings(record=True) as warns:
1262+
warnings.filterwarnings('always',
1263+
message=message,
1264+
category=category)
1265+
yield
1266+
if force_gc:
1267+
gc_collect()
1268+
testcase.assertEqual(warns, [])
1269+
1270+
12471271
@contextlib.contextmanager
12481272
def check_no_resource_warning(testcase):
12491273
"""Context manager to check that no ResourceWarning is emitted.

0 commit comments

Comments
 (0)