AttributeError: 'str' object has no attribute 'copy' from lxml.etree._XSLTResultTree.write_output

When OSError happens during lxml.etree._XSLTResultTree.write_output, exception AttributeError: 'str' object has no attribute 'copy' is raised, instead of some error indicating the real cause of the problem. Consider the following poor man's test to attempt to write to a file in subdirectory which does not exist. diff --git a/src/lxml/tests/test_xslt.py b/src/lxml/tests/test_xslt.py index f6b48fb9..6fd5dada 100644 --- a/src/lxml/tests/test_xslt.py +++ b/src/lxml/tests/test_xslt.py @@ -223,6 +223,20 @@ class ETreeXSLTTestCase(HelperTestCase): finally: os.unlink(f.name) + def test_xslt_write_output_file_oserror(self): + with self._xslt_setup() as res: + f = NamedTemporaryFile(suffix='.xml.gz', delete=False) + name_in_subdir = f.name.replace('.xml.gz', '/a.xml.gz') + try: + try: + res[0].write_output(name_in_subdir, compression=1) + 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_unicode(self): expected = ''' <?xml version="1.0"?> When running this test, I get ====================================================================== ERROR: test_xslt_write_output_file_oserror (src.lxml.tests.test_xslt.ETreeXSLTTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/adelton/project/lxml/src/lxml/tests/test_xslt.py", line 232, in test_xslt_write_output_file_oserror res[0].write_output(name_in_subdir, compression=1) File "src/lxml/xslt.pxi", line 753, in lxml.etree._XSLTResultTree.write_output if self._context_node is not None: File "src/lxml/etree.pyx", line 207, in lxml.etree.LxmlError.__init__ else: AttributeError: 'str' object has no attribute 'copy' It seems like the handling within lxml.etree.LxmlError fails, making it hard to catch the real error in the code which uses lxml. -- Jan Pazdziora Senior Principal Software Engineer, Security Engineering, Red Hat

Jan Pazdziora schrieb am 17.04.19 um 10:06:
Thanks for the report. Yes, the code there is instantiating the exception incorrectly, which leads to this failure. Fixed here: https://github.com/lxml/lxml/commit/013ae28b8503ea21e1f86453340413e6690b910e Stefan

Jan Pazdziora schrieb am 17.04.19 um 10:06:
Thanks for the report. Yes, the code there is instantiating the exception incorrectly, which leads to this failure. Fixed here: https://github.com/lxml/lxml/commit/013ae28b8503ea21e1f86453340413e6690b910e Stefan
participants (2)
-
Jan Pazdziora
-
Stefan Behnel