From ad446d57a929a642644c3a86c7359ce1638077b0 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 10 Nov 2014 13:49:00 +0200 Subject: Issue #22578: Added attributes to the re.error class. --- Lib/sre_constants.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'Lib/sre_constants.py') 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): -- cgit v1.2.1