]> granicus.if.org Git - python/commitdiff
commit the portion of PyXML patch #919008 that is relevant to the
authorFred Drake <fdrake@acm.org>
Sat, 20 Mar 2004 08:15:30 +0000 (08:15 +0000)
committerFred Drake <fdrake@acm.org>
Sat, 20 Mar 2004 08:15:30 +0000 (08:15 +0000)
standard library:
str() of xml.sax.SAXParseException should not fail if the line and/or
column number returned by the locator are None
(tests added)

Lib/test/test_sax.py
Lib/xml/sax/_exceptions.py

index af97888793cb68e9883e3d1a98dc2ef5966e9321..8e279ce5206494b9eddbbd2f0cb08559c9652f4b 100644 (file)
@@ -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
 
 # ===========================================================================
 #
index 7f128f694935f291a78e7164d7a65bfd75c6d331..628e80dff4a965e8e4b61b9359be396a75211910 100644 (file)
@@ -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 =====