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

Skip to content

Commit 94a619d

Browse files
Issue #26325: Added test.support.check_no_resource_warning() to check that
no ResourceWarning is emitted.
1 parent e93b06a commit 94a619d

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
@@ -100,7 +100,8 @@
100100
# threads
101101
"threading_setup", "threading_cleanup", "reap_threads", "start_threads",
102102
# miscellaneous
103-
"check_warnings", "EnvironmentVarGuard", "run_with_locale", "swap_item",
103+
"check_warnings", "check_no_resource_warning", "EnvironmentVarGuard",
104+
"run_with_locale", "swap_item",
104105
"swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict",
105106
"run_with_tz",
106107
]
@@ -1147,6 +1148,27 @@ def check_warnings(*filters, **kwargs):
11471148
return _filterwarnings(filters, quiet)
11481149

11491150

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

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
@@ -567,14 +567,11 @@ def test_iterparse(self):
567567
self.assertFalse(f.closed)
568568
self.assertEqual(str(cm.exception), "unknown event 'bogus'")
569569

570-
with warnings.catch_warnings(record=True) as w:
571-
warnings.filterwarnings("always", category=ResourceWarning)
570+
with support.check_no_resource_warning(self):
572571
with self.assertRaises(ValueError) as cm:
573572
iterparse(SIMPLE_XMLFILE, events)
574573
self.assertEqual(str(cm.exception), "unknown event 'bogus'")
575574
del cm
576-
support.gc_collect()
577-
self.assertEqual(w, [])
578575

579576
source = io.BytesIO(
580577
b"<?xml version='1.0' encoding='iso-8859-1'?>\n"
@@ -601,15 +598,12 @@ def test_iterparse(self):
601598
it = iterparse(TESTFN)
602599
action, elem = next(it)
603600
self.assertEqual((action, elem.tag), ('end', 'document'))
604-
with warnings.catch_warnings(record=True) as w:
605-
warnings.filterwarnings("always", category=ResourceWarning)
601+
with support.check_no_resource_warning(self):
606602
with self.assertRaises(ET.ParseError) as cm:
607603
next(it)
608604
self.assertEqual(str(cm.exception),
609605
'junk after document element: line 1, column 12')
610606
del cm, it
611-
support.gc_collect()
612-
self.assertEqual(w, [])
613607

614608
def test_writefile(self):
615609
elem = ET.Element("tag")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ Documentation
216216
Tests
217217
-----
218218

219+
- Issue #26325: Added test.support.check_no_resource_warning() to check that
220+
no ResourceWarning is emitted.
221+
219222
- Issue #25940: Changed test_ssl to use self-signed.pythontest.net. This
220223
avoids relying on svn.python.org, which recently changed root certificate.
221224

0 commit comments

Comments
 (0)