mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #22578: Added attributes to the re.error class.
This commit is contained in:
parent
eb99e51574
commit
ad446d57a9
5 changed files with 174 additions and 62 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue