bpo-31658: Make xml.sax.parse accepting Path objects (GH-8564) · python/cpython@929b704

@@ -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

25252626

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)

184184185+

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.")

399403404+

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.