Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc8e9b0

Browse files
Issue #22915: SAX parser now supports files opened with file descriptor or
bytes path.
1 parent 7a6915e commit fc8e9b0

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lib/test/test_sax.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,30 @@ def test_expat_file_nonascii(self):
648648

649649
self.assertEqual(result.getvalue(), xml_test_out)
650650

651+
def test_expat_binary_file_bytes_name(self):
652+
fname = os.fsencode(TEST_XMLFILE)
653+
parser = create_parser()
654+
result = BytesIO()
655+
xmlgen = XMLGenerator(result)
656+
657+
parser.setContentHandler(xmlgen)
658+
with open(fname, 'rb') as f:
659+
parser.parse(f)
660+
661+
self.assertEqual(result.getvalue(), xml_test_out)
662+
663+
def test_expat_binary_file_int_name(self):
664+
parser = create_parser()
665+
result = BytesIO()
666+
xmlgen = XMLGenerator(result)
667+
668+
parser.setContentHandler(xmlgen)
669+
with open(TEST_XMLFILE, 'rb') as f:
670+
with open(f.fileno(), 'rb', closefd=False) as f2:
671+
parser.parse(f2)
672+
673+
self.assertEqual(result.getvalue(), xml_test_out)
674+
651675
# ===== DTDHandler support
652676

653677
class TestDTDHandler:

Lib/xml/sax/saxutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def prepare_input_source(source, base=""):
346346
f = source
347347
source = xmlreader.InputSource()
348348
source.setByteStream(f)
349-
if hasattr(f, "name"):
349+
if hasattr(f, "name") and isinstance(f.name, str):
350350
source.setSystemId(f.name)
351351

352352
if source.getByteStream() is None:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- Issue #22915: SAX parser now supports files opened with file descriptor or
40+
bytes path.
41+
3942
- Issue #22609: Constructors and update methods of mapping classes in the
4043
collections module now accept the self keyword argument.
4144

0 commit comments

Comments
 (0)