From: Serhiy Storchaka Date: Thu, 27 Nov 2014 20:13:16 +0000 (+0200) Subject: Issue #22915: SAX parser now supports files opened with file descriptor or X-Git-Tag: v3.4.3rc1~298 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fc8e9b0e726c14d10cf33864710364b3cb4ae368;p=python Issue #22915: SAX parser now supports files opened with file descriptor or bytes path. --- diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index cfa18f7da5..a6238b2786 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -648,6 +648,30 @@ class ExpatReaderTest(XmlTestBase): self.assertEqual(result.getvalue(), xml_test_out) + def test_expat_binary_file_bytes_name(self): + fname = os.fsencode(TEST_XMLFILE) + parser = create_parser() + result = BytesIO() + xmlgen = XMLGenerator(result) + + parser.setContentHandler(xmlgen) + with open(fname, 'rb') as f: + parser.parse(f) + + self.assertEqual(result.getvalue(), xml_test_out) + + def test_expat_binary_file_int_name(self): + parser = create_parser() + result = BytesIO() + xmlgen = XMLGenerator(result) + + parser.setContentHandler(xmlgen) + with open(TEST_XMLFILE, 'rb') as f: + with open(f.fileno(), 'rb', closefd=False) as f2: + parser.parse(f2) + + self.assertEqual(result.getvalue(), xml_test_out) + # ===== DTDHandler support class TestDTDHandler: diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py index 74de9b07fc..1d3d0ecc5f 100644 --- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -346,7 +346,7 @@ def prepare_input_source(source, base=""): f = source source = xmlreader.InputSource() source.setByteStream(f) - if hasattr(f, "name"): + if hasattr(f, "name") and isinstance(f.name, str): source.setSystemId(f.name) if source.getByteStream() is None: diff --git a/Misc/NEWS b/Misc/NEWS index c1abebed31..7c17a370d4 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -36,6 +36,9 @@ Core and Builtins Library ------- +- Issue #22915: SAX parser now supports files opened with file descriptor or + bytes path. + - Issue #22609: Constructors and update methods of mapping classes in the collections module now accept the self keyword argument.