Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2feb642

Browse files
Issue #26325: Added test.support.check_no_resource_warning() to check that
no ResourceWarning is emitted.
2 parents 885bdc4 + 94a619d commit 2feb642

5 files changed

Lines changed: 32 additions & 20 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
# threads
103103
"threading_setup", "threading_cleanup", "reap_threads", "start_threads",
104104
# miscellaneous
105-
"check_warnings", "EnvironmentVarGuard", "run_with_locale", "swap_item",
105+
"check_warnings", "check_no_resource_warning", "EnvironmentVarGuard",
106+
"run_with_locale", "swap_item",
106107
"swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict",
107108
"run_with_tz",
108109
]
@@ -1149,6 +1150,27 @@ def check_warnings(*filters, **kwargs):
11491150
return _filterwarnings(filters, quiet)
11501151

11511152

1153+
@contextlib.contextmanager
1154+
def check_no_resource_warning(testcase):
1155+
"""Context manager to check that no ResourceWarning is emitted.
1156+
1157+
Usage:
1158+
1159+
with check_no_resource_warning(self):
1160+
f = open(...)
1161+
...
1162+
del f
1163+
1164+
You must remove the object which may emit ResourceWarning before
1165+
the end of the context manager.
1166+
"""
1167+
with warnings.catch_warnings(record=True) as warns:
1168+
warnings.filterwarnings('always', category=ResourceWarning)
1169+
yield
1170+
gc_collect()
1171+
testcase.assertEqual(warns, [])
1172+
1173+
11521174
class CleanImport(object):
11531175
"""Context manager to force import to return a new module reference.
11541176

Lib/test/test_asyncio/test_subprocess.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,9 @@ def test_popen_error(self):
427427

428428
create = asyncio.create_subprocess_exec(sys.executable, '-c',
429429
'pass', loop=self.loop)
430-
with warnings.catch_warnings(record=True) as warns:
430+
with support.check_no_resource_warning(self):
431431
with self.assertRaises(exc):
432432
self.loop.run_until_complete(create)
433-
self.assertEqual(warns, [])
434433

435434

436435
if sys.platform != 'win32':

Lib/test/test_io.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -681,18 +681,14 @@ def test_fileio_closefd(self):
681681
f2.readline()
682682

683683
def test_nonbuffered_textio(self):
684-
with warnings.catch_warnings(record=True) as recorded:
684+
with support.check_no_resource_warning(self):
685685
with self.assertRaises(ValueError):
686686
self.open(support.TESTFN, 'w', buffering=0)
687-
support.gc_collect()
688-
self.assertEqual(recorded, [])
689687

690688
def test_invalid_newline(self):
691-
with warnings.catch_warnings(record=True) as recorded:
689+
with support.check_no_resource_warning(self):
692690
with self.assertRaises(ValueError):
693691
self.open(support.TESTFN, 'w', newline='invalid')
694-
support.gc_collect()
695-
self.assertEqual(recorded, [])
696692

697693

698694
class CIOTest(IOTest):
@@ -3366,10 +3362,8 @@ def cleanup_fds():
33663362
# When using closefd=False, there's no warning
33673363
r, w = os.pipe()
33683364
fds += r, w
3369-
with warnings.catch_warnings(record=True) as recorded:
3365+
with support.check_no_resource_warning(self):
33703366
open(r, *args, closefd=False, **kwargs)
3371-
support.gc_collect()
3372-
self.assertEqual(recorded, [])
33733367

33743368
def test_warn_on_dealloc_fd(self):
33753369
self._check_warn_on_dealloc_fd("rb", buffering=0)

Lib/test/test_xml_etree.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,11 @@ def test_iterparse(self):
569569
self.assertFalse(f.closed)
570570
self.assertEqual(str(cm.exception), "unknown event 'bogus'")
571571

572-
with warnings.catch_warnings(record=True) as w:
573-
warnings.filterwarnings("always", category=ResourceWarning)
572+
with support.check_no_resource_warning(self):
574573
with self.assertRaises(ValueError) as cm:
575574
iterparse(SIMPLE_XMLFILE, events)
576575
self.assertEqual(str(cm.exception), "unknown event 'bogus'")
577576
del cm
578-
support.gc_collect()
579-
self.assertEqual(w, [])
580577

581578
source = io.BytesIO(
582579
b"<?xml version='1.0' encoding='iso-8859-1'?>\n"
@@ -603,15 +600,12 @@ def test_iterparse(self):
603600
it = iterparse(TESTFN)
604601
action, elem = next(it)
605602
self.assertEqual((action, elem.tag), ('end', 'document'))
606-
with warnings.catch_warnings(record=True) as w:
607-
warnings.filterwarnings("always", category=ResourceWarning)
603+
with support.check_no_resource_warning(self):
608604
with self.assertRaises(ET.ParseError) as cm:
609605
next(it)
610606
self.assertEqual(str(cm.exception),
611607
'junk after document element: line 1, column 12')
612608
del cm, it
613-
support.gc_collect()
614-
self.assertEqual(w, [])
615609

616610
def test_writefile(self):
617611
elem = ET.Element("tag")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ Documentation
673673
Tests
674674
-----
675675

676+
- Issue #26325: Added test.support.check_no_resource_warning() to check that
677+
no ResourceWarning is emitted.
678+
676679
- Issue #25940: Changed test_ssl to use self-signed.pythontest.net. This
677680
avoids relying on svn.python.org, which recently changed root certificate.
678681

0 commit comments

Comments
 (0)