]> granicus.if.org Git - python/commitdiff
Issue #22915: SAX parser now supports files opened with file descriptor or
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 27 Nov 2014 20:13:16 +0000 (22:13 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 27 Nov 2014 20:13:16 +0000 (22:13 +0200)
bytes path.

Lib/test/test_sax.py
Lib/xml/sax/saxutils.py
Misc/NEWS

index cfa18f7da5e0509228e640c5e8c45d04373b0a69..a6238b278617071e9535cd276357e6ec9b55a84b 100644 (file)
@@ -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:
index 74de9b07fc11b57092afc3b6de9bf45b5c99d58d..1d3d0ecc5f981d4bb3d0d58460bfce5969c87008 100644 (file)
@@ -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:
index c1abebed31142682edcaaef3d67f3e40f509eb81..7c17a370d4c760ba44d0619c654f6e67ca5821ae 100644 (file)
--- 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.