Skip to content

Commit 929b704

Browse files
BoboTiGscoder
authored andcommitted
bpo-31658: Make xml.sax.parse accepting Path objects (GH-8564)
1 parent e9927e1 commit 929b704

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

Doc/library/xml.sax.reader.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,17 @@ The :class:`XMLReader` interface supports the following methods:
102102

103103
Process an input source, producing SAX events. The *source* object can be a
104104
system identifier (a string identifying the input source -- typically a file
105-
name or a URL), a file-like object, or an :class:`InputSource` object. When
105+
name or a URL), a :class:`pathlib.Path` or :term:`path-like <path-like object>`
106+
object, or an :class:`InputSource` object. When
106107
:meth:`parse` returns, the input is completely processed, and the parser object
107108
can be discarded or reset.
108109

109110
.. versionchanged:: 3.5
110111
Added support of character streams.
111112

113+
.. versionchanged:: 3.8
114+
Added support of path-like objects.
115+
112116

113117
.. method:: XMLReader.getContentHandler()
114118

Lib/test/test_sax.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import shutil
2222
from urllib.error import URLError
2323
from test import support
24-
from test.support import findfile, run_unittest, TESTFN
24+
from test.support import findfile, run_unittest, FakePath, TESTFN
2525

2626
TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata")
2727
TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata")
@@ -182,6 +182,10 @@ def test_parse_bytes(self):
182182
with self.assertRaises(SAXException):
183183
self.check_parse(f)
184184

185+
def test_parse_path_object(self):
186+
make_xml_file(self.data, 'utf-8', None)
187+
self.check_parse(FakePath(TESTFN))
188+
185189
def test_parse_InputSource(self):
186190
# accept data without declared but with explicitly specified encoding
187191
make_xml_file(self.data, 'iso-8859-1', None)
@@ -397,6 +401,13 @@ def test_string(self):
397401
self.checkContent(prep.getByteStream(),
398402
b"This was read from a file.")
399403

404+
def test_path_objects(self):
405+
# If the source is a Path object, use it as a system ID and open it.
406+
prep = prepare_input_source(FakePath(self.file))
407+
self.assertIsNone(prep.getCharacterStream())
408+
self.checkContent(prep.getByteStream(),
409+
b"This was read from a file.")
410+
400411
def test_binary_file(self):
401412
# If the source is a binary file-like object, use it as a byte
402413
# stream.

Lib/xml/sax/saxutils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ def prepare_input_source(source, base=""):
339339
"""This function takes an InputSource and an optional base URL and
340340
returns a fully resolved InputSource object ready for reading."""
341341

342+
if isinstance(source, os.PathLike):
343+
source = os.fspath(source)
342344
if isinstance(source, str):
343345
source = xmlreader.InputSource(source)
344346
elif hasattr(source, "read"):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`xml.sax.parse` now supports :term:`path-like <path-like object>`.
2+
Patch by Mickaël Schoentgen.

0 commit comments

Comments
 (0)