diff options
| author | Henning Janssen <henning.janssen@gmx.net> | 2022-02-12 21:40:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-12 21:40:07 +0100 |
| commit | 1e3666018329cadf8e147607824614aebf7e2099 (patch) | |
| tree | 4e64472bbbb1c33cbca210a5dec632e2629ffff0 /src/lxml/tests | |
| parent | ac829d561c0bf71fb8cc704305ffc18bd26c6abb (diff) | |
| download | python-lxml-1e3666018329cadf8e147607824614aebf7e2099.tar.gz | |
Allow Path-like objects for file arguments (GH-337)
Use "PyOS_FSPath()" if available (Py3.6+). Otherwise, manually check for "__fspath__", in case an object defines it.
Diffstat (limited to 'src/lxml/tests')
| -rw-r--r-- | src/lxml/tests/common_imports.py | 6 | ||||
| -rw-r--r-- | src/lxml/tests/test_dtd.py | 10 | ||||
| -rw-r--r-- | src/lxml/tests/test_etree.py | 49 | ||||
| -rw-r--r-- | src/lxml/tests/test_xmlschema.py | 7 | ||||
| -rw-r--r-- | src/lxml/tests/test_xslt.py | 15 |
5 files changed, 84 insertions, 3 deletions
diff --git a/src/lxml/tests/common_imports.py b/src/lxml/tests/common_imports.py index 57097e3c..68db7c2b 100644 --- a/src/lxml/tests/common_imports.py +++ b/src/lxml/tests/common_imports.py @@ -251,6 +251,12 @@ class LargeFileLikeUnicode(LargeFileLike): yield self.chars yield _str('</root>') +class SimpleFSPath(object): + def __init__(self, path): + self.path = path + def __fspath__(self): + return self.path + def fileInTestDir(name): _testdir = os.path.dirname(__file__) return os.path.join(_testdir, name) diff --git a/src/lxml/tests/test_dtd.py b/src/lxml/tests/test_dtd.py index 779f9e84..5c9b1c02 100644 --- a/src/lxml/tests/test_dtd.py +++ b/src/lxml/tests/test_dtd.py @@ -9,7 +9,7 @@ import unittest, sys from .common_imports import ( etree, html, BytesIO, _bytes, _str, HelperTestCase, make_doctest, skipIf, - fileInTestDir, fileUrlInTestDir + fileInTestDir, fileUrlInTestDir, SimpleFSPath ) @@ -24,6 +24,14 @@ class ETreeDtdTestCase(HelperTestCase): dtd = etree.DTD(fileInTestDir("test.dtd")) self.assertTrue(dtd.validate(root)) + + def test_dtd_file_pathlike(self): + parse = etree.parse + tree = parse(fileInTestDir("test.xml")) + root = tree.getroot() + + dtd = etree.DTD(SimpleFSPath(fileInTestDir("test.dtd"))) + self.assertTrue(dtd.validate(root)) def test_dtd_stringio(self): root = etree.XML(_bytes("<b/>")) diff --git a/src/lxml/tests/test_etree.py b/src/lxml/tests/test_etree.py index ef5c54b7..e5f08469 100644 --- a/src/lxml/tests/test_etree.py +++ b/src/lxml/tests/test_etree.py @@ -25,6 +25,7 @@ from .common_imports import etree, StringIO, BytesIO, HelperTestCase from .common_imports import fileInTestDir, fileUrlInTestDir, read_file, path2url, tmpfile from .common_imports import SillyFileLike, LargeFileLikeUnicode, doctest, make_doctest from .common_imports import canonicalize, _str, _bytes +from .common_imports import SimpleFSPath print(""" TESTED VERSION: %s""" % etree.__version__ + """ @@ -4599,6 +4600,20 @@ class ETreeOnlyTestCase(HelperTestCase): self.assertEqual('child1', c2.getprevious().tag) self.assertEqual('abc', c2.getprevious().tail) + def test_parse_source_pathlike(self): + etree = self.etree + tounicode = self.etree.tounicode + + tree = etree.parse(SimpleFSPath(fileInTestDir('test.xml'))) + self.assertEqual(_bytes('<a><b></b></a>'), + canonicalize(tounicode(tree))) + + def test_iterparse_source_pathlike(self): + iterparse = self.etree.iterparse + + events = list(iterparse(SimpleFSPath(fileInTestDir('test.xml')))) + self.assertEqual(2, len(events)) + # helper methods def _writeElement(self, element, encoding='us-ascii', compression=0): @@ -4883,6 +4898,14 @@ class ETreeC14NTestCase(HelperTestCase): data = read_file(filename, 'rb') self.assertEqual(_bytes('<a><b></b></a>'), data) + + def test_c14n_file_pathlike(self): + tree = self.parse(_bytes('<a><b/></a>')) + with tmpfile() as filename: + tree.write_c14n(SimpleFSPath(filename)) + data = read_file(filename, 'rb') + self.assertEqual(_bytes('<a><b></b></a>'), + data) def test_c14n_file_gzip(self): tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>')) @@ -4892,6 +4915,15 @@ class ETreeC14NTestCase(HelperTestCase): data = f.read() self.assertEqual(_bytes('<a>'+'<b></b>'*200+'</a>'), data) + + def test_c14n_file_gzip_pathlike(self): + tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>')) + with tmpfile() as filename: + tree.write_c14n(SimpleFSPath(filename), compression=9) + with gzip.open(filename, 'rb') as f: + data = f.read() + self.assertEqual(_bytes('<a>'+'<b></b>'*200+'</a>'), + data) def test_c14n2_file_gzip(self): tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>')) @@ -5182,6 +5214,14 @@ class ETreeWriteTestCase(HelperTestCase): data = read_file(filename, 'rb') self.assertEqual(_bytes('<a><b/></a>'), data) + + def test_write_file_pathlike(self): + tree = self.parse(_bytes('<a><b/></a>')) + with tmpfile() as filename: + tree.write(SimpleFSPath(filename)) + data = read_file(filename, 'rb') + self.assertEqual(_bytes('<a><b/></a>'), + data) def test_write_file_gzip(self): tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>')) @@ -5192,6 +5232,15 @@ class ETreeWriteTestCase(HelperTestCase): self.assertEqual(_bytes('<a>'+'<b/>'*200+'</a>'), data) + def test_write_file_gzip_pathlike(self): + tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>')) + with tmpfile() as filename: + tree.write(SimpleFSPath(filename), compression=9) + with gzip.open(filename, 'rb') as f: + data = f.read() + self.assertEqual(_bytes('<a>'+'<b/>'*200+'</a>'), + data) + def test_write_file_gzip_parse(self): tree = self.parse(_bytes('<a>'+'<b/>'*200+'</a>')) with tmpfile() as filename: diff --git a/src/lxml/tests/test_xmlschema.py b/src/lxml/tests/test_xmlschema.py index c5653c1e..dbfc251a 100644 --- a/src/lxml/tests/test_xmlschema.py +++ b/src/lxml/tests/test_xmlschema.py @@ -8,7 +8,7 @@ from __future__ import absolute_import import unittest -from .common_imports import etree, BytesIO, HelperTestCase, fileInTestDir, make_doctest +from .common_imports import etree, BytesIO, HelperTestCase, fileInTestDir, make_doctest, SimpleFSPath class ETreeXMLSchemaTestCase(HelperTestCase): @@ -387,6 +387,11 @@ class ETreeXMLSchemaTestCase(HelperTestCase): etree.XMLSchema(schema_element) etree.XMLSchema(schema_element) + def test_xmlschema_pathlike(self): + schema = etree.XMLSchema(file=SimpleFSPath(fileInTestDir('test.xsd'))) + tree_valid = self.parse('<a><b></b></a>') + self.assertTrue(schema.validate(tree_valid)) + class ETreeXMLSchemaResolversTestCase(HelperTestCase): resolver_schema_int = BytesIO("""\ diff --git a/src/lxml/tests/test_xslt.py b/src/lxml/tests/test_xslt.py index cde23357..0ef07669 100644 --- a/src/lxml/tests/test_xslt.py +++ b/src/lxml/tests/test_xslt.py @@ -29,7 +29,7 @@ except NameError: # Python 3 basestring = str from .common_imports import ( - etree, BytesIO, HelperTestCase, fileInTestDir, _bytes, make_doctest, skipif + etree, BytesIO, HelperTestCase, fileInTestDir, _bytes, make_doctest, skipif, SimpleFSPath ) @@ -195,6 +195,19 @@ class ETreeXSLTTestCase(HelperTestCase): res[0] = f.read().decode("UTF-16") finally: os.unlink(f.name) + + def test_xslt_write_output_file_pathlike(self): + with self._xslt_setup() as res: + f = NamedTemporaryFile(delete=False) + try: + try: + res[0].write_output(SimpleFSPath(f.name), compression=9) + finally: + f.close() + with gzip.GzipFile(f.name) as f: + res[0] = f.read().decode("UTF-16") + finally: + os.unlink(f.name) def test_xslt_write_output_file_path_urlescaped(self): # libxml2 should not unescape file paths. |
