summaryrefslogtreecommitdiff
path: root/Lib/sre_constants.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/sre_constants.py')
-rw-r--r--Lib/sre_constants.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/Lib/sre_constants.py b/Lib/sre_constants.py
index 7480bf3ae0..bdea5e42b4 100644
--- a/Lib/sre_constants.py
+++ b/Lib/sre_constants.py
@@ -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):