[Python-checkins] cpython (3.4): Issue #22915: SAX parser now supports files opened with file descriptor or

serhiy.storchaka python-checkins at python.org
Thu Nov 27 21:17:10 CET 2014


https://hg.python.org/cpython/rev/27ae1a476ef7
changeset:   93627:27ae1a476ef7
branch:      3.4
parent:      93625:89bb4384f1e1
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Thu Nov 27 22:13:16 2014 +0200
summary:
  Issue #22915: SAX parser now supports files opened with file descriptor or
bytes path.

files:
  Lib/test/test_sax.py    |  24 ++++++++++++++++++++++++
  Lib/xml/sax/saxutils.py |   2 +-
  Misc/NEWS               |   3 +++
  3 files changed, 28 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -648,6 +648,30 @@
 
         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
--- a/Lib/xml/sax/saxutils.py
+++ b/Lib/xml/sax/saxutils.py
@@ -346,7 +346,7 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list