Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fd0b0d

Browse files
committed
commit the portion of PyXML patch #919008 that is relevant to the
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)
1 parent 9de0a2b commit 6fd0b0d

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lib/test/test_sax.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,41 @@ def test_expat_incomplete():
489489
else:
490490
return 0
491491

492+
def test_sax_parse_exception_str():
493+
# pass various values from a locator to the SAXParseException to
494+
# make sure that the __str__() doesn't fall apart when None is
495+
# passed instead of an integer line and column number
496+
#
497+
# use "normal" values for the locator:
498+
str(SAXParseException("message", None,
499+
DummyLocator(1, 1)))
500+
# use None for the line number:
501+
str(SAXParseException("message", None,
502+
DummyLocator(None, 1)))
503+
# use None for the column number:
504+
str(SAXParseException("message", None,
505+
DummyLocator(1, None)))
506+
# use None for both:
507+
str(SAXParseException("message", None,
508+
DummyLocator(None, None)))
509+
return 1
510+
511+
class DummyLocator:
512+
def __init__(self, lineno, colno):
513+
self._lineno = lineno
514+
self._colno = colno
515+
516+
def getPublicId(self):
517+
return "pubid"
518+
519+
def getSystemId(self):
520+
return "sysid"
521+
522+
def getLineNumber(self):
523+
return self._lineno
524+
525+
def getColumnNumber(self):
526+
return self._colno
492527

493528
# ===========================================================================
494529
#

Lib/xml/sax/_exceptions.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@ def __str__(self):
9191
sysid = self.getSystemId()
9292
if sysid is None:
9393
sysid = "<unknown>"
94-
return "%s:%d:%d: %s" % (sysid, self.getLineNumber(),
95-
self.getColumnNumber(), self._msg)
94+
linenum = self.getLineNumber()
95+
if linenum is None:
96+
linenum = "?"
97+
colnum = self.getColumnNumber()
98+
if colnum is None:
99+
colnum = "?"
100+
return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg)
96101

97102

98103
# ===== SAXNOTRECOGNIZEDEXCEPTION =====

0 commit comments

Comments
 (0)