diff options
-rw-r--r-- | Lib/test/test_sax.py | 35 | ||||
-rw-r--r-- | Lib/xml/sax/_exceptions.py | 9 |
2 files changed, 42 insertions, 2 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index af97888..8e279ce 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -489,6 +489,41 @@ def test_expat_incomplete(): else: return 0 +def test_sax_parse_exception_str(): + # pass various values from a locator to the SAXParseException to + # make sure that the __str__() doesn't fall apart when None is + # passed instead of an integer line and column number + # + # use "normal" values for the locator: + str(SAXParseException("message", None, + DummyLocator(1, 1))) + # use None for the line number: + str(SAXParseException("message", None, + DummyLocator(None, 1))) + # use None for the column number: + str(SAXParseException("message", None, + DummyLocator(1, None))) + # use None for both: + str(SAXParseException("message", None, + DummyLocator(None, None))) + return 1 + +class DummyLocator: + def __init__(self, lineno, colno): + self._lineno = lineno + self._colno = colno + + def getPublicId(self): + return "pubid" + + def getSystemId(self): + return "sysid" + + def getLineNumber(self): + return self._lineno + + def getColumnNumber(self): + return self._colno # =========================================================================== # diff --git a/Lib/xml/sax/_exceptions.py b/Lib/xml/sax/_exceptions.py index 7f128f6..628e80d 100644 --- a/Lib/xml/sax/_exceptions.py +++ b/Lib/xml/sax/_exceptions.py @@ -91,8 +91,13 @@ class SAXParseException(SAXException): sysid = self.getSystemId() if sysid is None: sysid = "<unknown>" - return "%s:%d:%d: %s" % (sysid, self.getLineNumber(), - self.getColumnNumber(), self._msg) + linenum = self.getLineNumber() + if linenum is None: + linenum = "?" + colnum = self.getColumnNumber() + if colnum is None: + colnum = "?" + return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg) # ===== SAXNOTRECOGNIZEDEXCEPTION ===== |