bpo-30264: ExpatParser now closes the source (#1476)
ExpatParser.parse() of xml.sax.xmlreader now closes the source: close
the file object or the urllib object if source is a string (not an
open file-like object).
Add test_parse_close_source() unit test.
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
index a4228dc..87a8658 100644
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -2,7 +2,8 @@
# $Id$
from xml.sax import make_parser, ContentHandler, \
- SAXException, SAXReaderNotAvailable, SAXParseException
+ SAXException, SAXReaderNotAvailable, SAXParseException, \
+ saxutils
try:
make_parser()
except SAXReaderNotAvailable:
@@ -173,6 +174,21 @@
input.setEncoding('iso-8859-1')
self.check_parse(input)
+ def test_parse_close_source(self):
+ builtin_open = open
+ non_local = {'fileobj': None}
+
+ def mock_open(*args):
+ fileobj = builtin_open(*args)
+ non_local['fileobj'] = fileobj
+ return fileobj
+
+ with support.swap_attr(saxutils, 'open', mock_open):
+ make_xml_file(self.data, 'iso-8859-1', None)
+ with self.assertRaises(SAXException):
+ self.check_parse(TESTFN)
+ self.assertTrue(non_local['fileobj'].closed)
+
def check_parseString(self, s):
from xml.sax import parseString
result = StringIO()