Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Lib/test/test_sax.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,8 @@ def test_parse_bytes(self):
with self.assertRaises(SAXException):
self.check_parse(BytesIO(xml_bytes(self.data, 'iso-8859-1', None)))
make_xml_file(self.data, 'iso-8859-1', None)
with support.check_warnings(('unclosed file', ResourceWarning)):
# XXX Failed parser leaks an opened file.
with self.assertRaises(SAXException):
self.check_parse(TESTFN)
# Collect leaked file.
gc.collect()
with self.assertRaises(SAXException):
self.check_parse(TESTFN)
with open(TESTFN, 'rb') as f:
with self.assertRaises(SAXException):
self.check_parse(f)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't check that the file is closed now?

Expand Down
13 changes: 10 additions & 3 deletions Lib/xml/sax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@

def parse(source, handler, errorHandler=ErrorHandler()):
parser = make_parser()
parser.setContentHandler(handler)
parser.setErrorHandler(errorHandler)
parser.parse(source)
try:
parser.setContentHandler(handler)
parser.setErrorHandler(errorHandler)
parser.parse(source)
except:
# Third party parsers can have no close() method
if hasattr(parser, 'close'):
# On error, close the parser to not leak resources like open files
parser.close()
raise

def parseString(string, handler, errorHandler=ErrorHandler()):
import io
Expand Down