summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhilipp A <flying-sheep@web.de>2016-12-04 18:01:33 +0100
committerPhilipp A <flying-sheep@web.de>2016-12-04 18:01:33 +0100
commitabf54e0d9ad306e17d2dd244782b04a945caf633 (patch)
tree8b6d69643cfcac6e1234066e4d1bb2bd6b144f71 /src
parent9d041753c4ea35ee51b41f14b8fd0647750e98a1 (diff)
downloadpython-lxml-abf54e0d9ad306e17d2dd244782b04a945caf633.tar.gz
Make XMLSyntaxError have normal SyntaxError metadata
Diffstat (limited to 'src')
-rw-r--r--src/lxml/parser.pxi22
-rw-r--r--src/lxml/tests/test_errors.py18
-rw-r--r--src/lxml/xmlerror.pxi3
3 files changed, 37 insertions, 6 deletions
diff --git a/src/lxml/parser.pxi b/src/lxml/parser.pxi
index 46eb99cc..639e5acf 100644
--- a/src/lxml/parser.pxi
+++ b/src/lxml/parser.pxi
@@ -15,10 +15,20 @@ class ParseError(LxmlSyntaxError):
For compatibility with ElementTree 1.3 and later.
"""
- def __init__(self, message, code, line, column):
+ def __init__(self, message, code, line, column, filename = None):
super(_ParseError, self).__init__(message)
- self.position = (line, column)
+ self.lineno, self.offset = (line, column - 1)
self.code = code
+ self.filename = filename
+
+ @property
+ def position(self):
+ return self.lineno, self.offset + 1
+
+ @position.setter
+ def position(self, new_pos):
+ self.lineno, column = new_pos
+ self.offset = column - 1
cdef object _ParseError = ParseError
@@ -630,9 +640,10 @@ cdef int _raiseParseError(xmlparser.xmlParserCtxt* ctxt, filename,
column = ctxt.lastError.int2
if ctxt.lastError.line > 0:
message = u"line %d: %s" % (line, message)
- raise XMLSyntaxError(message, code, line, column)
+ raise XMLSyntaxError(message, code, line, column, filename)
else:
- raise XMLSyntaxError(None, xmlerror.XML_ERR_INTERNAL_ERROR, 0, 0)
+ raise XMLSyntaxError(None, xmlerror.XML_ERR_INTERNAL_ERROR, 0, 0,
+ filename)
cdef xmlDoc* _handleParseResult(_ParserContext context,
xmlparser.xmlParserCtxt* c_ctxt,
@@ -1341,7 +1352,8 @@ cdef class _FeedParser(_BaseParser):
"""
if not self._feed_parser_running:
raise XMLSyntaxError(u"no element found",
- xmlerror.XML_ERR_INTERNAL_ERROR, 0, 0)
+ xmlerror.XML_ERR_INTERNAL_ERROR, 0, 0,
+ self._filename)
context = self._getPushParserContext()
pctxt = context._c_ctxt
diff --git a/src/lxml/tests/test_errors.py b/src/lxml/tests/test_errors.py
index 068e5e3f..784847d5 100644
--- a/src/lxml/tests/test_errors.py
+++ b/src/lxml/tests/test_errors.py
@@ -49,6 +49,24 @@ class ErrorTestCase(HelperTestCase):
finally:
sys.settrace(trace_func)
+ def test_xmlsyntaxerror_has_info(self):
+ broken_xml_name = 'test_broken.xml'
+ broken_xml_path = os.path.join(this_dir, broken_xml_name)
+ fail_msg = 'test_broken.xml should raise an etree.XMLSyntaxError'
+ try:
+ etree.parse(broken_xml_path)
+ except etree.XMLSyntaxError as e:
+ # invariant
+ self.assertEqual(e.position, (e.lineno, e.offset + 1), 'position and lineno/offset out of sync')
+ # SyntaxError info derived from file & contents
+ self.assertTrue(e.filename.endswith(broken_xml_name), 'filename must be preserved')
+ self.assertEqual(e.lineno, 1)
+ self.assertEqual(e.offset, 10)
+ except Exception as e:
+ self.fail('{}, not {}'.format(fail_msg, type(e)))
+ else:
+ self.fail('test_broken.xml should raise an etree.XMLSyntaxError')
+
def test_suite():
suite = unittest.TestSuite()
diff --git a/src/lxml/xmlerror.pxi b/src/lxml/xmlerror.pxi
index 12f84c9b..31154685 100644
--- a/src/lxml/xmlerror.pxi
+++ b/src/lxml/xmlerror.pxi
@@ -231,12 +231,13 @@ cdef class _BaseErrorLog:
message = default_message
line = self._first_error.line
column = self._first_error.column
+ filename = self._first_error.filename
if line > 0:
if column > 0:
message = u"%s, line %d, column %d" % (message, line, column)
else:
message = u"%s, line %d" % (message, line)
- return exctype(message, code, line, column)
+ return exctype(message, code, line, column, filename)
@cython.final
cdef _buildExceptionMessage(self, default_message):