Skip to content

Commit 0f9b509

Browse files
committed
Add assertions to verify ResourceWarning content
Check that warning message contains 'unclosed file' and the filename to ensure it's from iterparse, not the file destructor. Addresses review feedback from serhiy-storchaka. Signed-off-by: Osama Abdelkader <[email protected]>
1 parent fcd7ca0 commit 0f9b509

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Lib/test/test_xml_etree.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,15 +696,19 @@ def test_iterparse(self):
696696
next(it)
697697
self.assertEqual(str(cm.exception),
698698
'junk after document element: line 1, column 12')
699-
with self.assertWarns(ResourceWarning):
699+
with self.assertWarns(ResourceWarning) as wm:
700700
del cm, it
701701
gc_collect()
702+
self.assertIn('unclosed file', str(wm.warning))
703+
self.assertIn(TESTFN, str(wm.warning))
702704

703-
# Deleting iterator without close() should emit ResourceWarning (bpo-43292)
704-
with self.assertWarns(ResourceWarning):
705+
# Not exhausting the iterator still closes the resource (bpo-43292)
706+
with self.assertWarns(ResourceWarning) as wm:
705707
it = iterparse(SIMPLE_XMLFILE)
706708
del it
707709
gc_collect()
710+
self.assertIn('unclosed file', str(wm.warning))
711+
self.assertIn(SIMPLE_XMLFILE, str(wm.warning))
708712

709713
# Explicitly calling close() should not emit warning
710714
with warnings_helper.check_no_resource_warning(self):
@@ -713,12 +717,14 @@ def test_iterparse(self):
713717
del it
714718

715719
# Not closing before del should emit ResourceWarning
716-
with self.assertWarns(ResourceWarning):
720+
with self.assertWarns(ResourceWarning) as wm:
717721
it = iterparse(SIMPLE_XMLFILE)
718722
action, elem = next(it)
719723
self.assertEqual((action, elem.tag), ('end', 'element'))
720724
del it, elem
721725
gc_collect()
726+
self.assertIn('unclosed file', str(wm.warning))
727+
self.assertIn(SIMPLE_XMLFILE, str(wm.warning))
722728

723729
with warnings_helper.check_no_resource_warning(self):
724730
it = iterparse(SIMPLE_XMLFILE)

0 commit comments

Comments
 (0)