Issue #22578: Added attributes to the re.error class.

This commit is contained in:
Serhiy Storchaka 2014-11-10 13:49:00 +02:00
parent eb99e51574
commit ad446d57a9
5 changed files with 174 additions and 62 deletions

View file

@ -21,7 +21,35 @@ from _sre import MAXREPEAT, MAXGROUPS
# should this really be here?
class error(Exception):
pass
def __init__(self, msg, pattern=None, pos=None):
self.msg = msg
self.pattern = pattern
self.pos = pos
if pattern is not None and pos is not None:
msg = '%s at position %d' % (msg, pos)
if isinstance(pattern, str):
newline = '\n'
else:
newline = b'\n'
self.lineno = pattern.count(newline, 0, pos) + 1
self.colno = pos - pattern.rfind(newline, 0, pos)
if newline in pattern:
msg = '%s (line %d, column %d)' % (msg, self.lineno, self.colno)
else:
self.lineno = self.colno = None
super().__init__(msg)
def linecol(doc, pos):
if isinstance(pattern, str):
newline = '\n'
else:
newline = b'\n'
lineno = pattern.count(newline, 0, pos) + 1
if lineno == 1:
colno = pos + 1
else:
colno = pos - doc.rindex(newline, 0, pos)
return lineno, colno
class _NamedIntConstant(int):