summaryrefslogtreecommitdiffstats
path: root/Lib/sre_constants.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-11-10 11:49:00 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-11-10 11:49:00 (GMT)
commitad446d57a929a642644c3a86c7359ce1638077b0 (patch)
treefb31d0b7aa5e3d09f5370c606d988d06c30b925f /Lib/sre_constants.py
parenteb99e5157498ae37117a54cdb01fea082d842a0d (diff)
downloadcpython-ad446d57a929a642644c3a86c7359ce1638077b0.zip
cpython-ad446d57a929a642644c3a86c7359ce1638077b0.tar.gz
cpython-ad446d57a929a642644c3a86c7359ce1638077b0.tar.bz2
Issue #22578: Added attributes to the re.error class.
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 7480bf3..bdea5e4 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):