Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24590)

Unified Diff: Lib/sre_constants.py

Issue 22578: Add addition attributes to re.error
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Doc/library/re.rst ('k') | Lib/sre_parse.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/sre_constants.py Fri Oct 10 11:14:49 2014 +0300
+++ b/Lib/sre_constants.py Fri Oct 10 11:49:08 2014 +0300
@@ -21,8 +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
# operators
FAILURE = "failure"
« no previous file with comments | « Doc/library/re.rst ('k') | Lib/sre_parse.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+