Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ad446d57
Commit
ad446d57
authored
Nov 10, 2014
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #22578: Added attributes to the re.error class.
parent
eb99e515
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
174 additions
and
62 deletions
+174
-62
Doc/library/re.rst
Doc/library/re.rst
+25
-2
Lib/sre_constants.py
Lib/sre_constants.py
+29
-1
Lib/sre_parse.py
Lib/sre_parse.py
+82
-59
Lib/test/test_re.py
Lib/test/test_re.py
+36
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/re.rst
View file @
ad446d57
...
...
@@ -733,13 +733,36 @@ form.
Clear the regular expression cache.
.. exception:: error
.. exception:: error
(msg, pattern=None, pos=None)
Exception raised when a string passed to one of the functions here is not a
valid regular expression (for example, it might contain unmatched parentheses)
or when some other error occurs during compilation or matching. It is never an
error if a string contains no match for a pattern.
error if a string contains no match for a pattern. The error instance has
the following additional attributes:
.. attribute:: msg
The unformatted error message.
.. attribute:: pattern
The regular expression pattern.
.. attribute:: pos
The index of *pattern* where compilation failed.
.. attribute:: lineno
The line corresponding to *pos*.
.. attribute:: colno
The column corresponding to *pos*.
.. versionchanged:: 3.5
Added additional attributes.
.. _re-objects:
...
...
Lib/sre_constants.py
View file @
ad446d57
...
...
@@ -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
):
...
...
Lib/sre_parse.py
View file @
ad446d57
This diff is collapsed.
Click to expand it.
Lib/test/test_re.py
View file @
ad446d57
...
...
@@ -1419,6 +1419,42 @@ SUBPATTERN None
self.assertIsNone(re.match(b'(?Li)
\
xc5
', b'
\
xe5
'))
self.assertIsNone(re.match(b'(?Li)
\
xe5
', b'
\
xc5
'))
def test_error(self):
with self.assertRaises(re.error) as cm:
re.compile('(
\
u20ac
))')
err = cm.exception
self.assertIsInstance(err.pattern, str)
self.assertEqual(err.pattern, '(
\
u20ac
))')
self.assertEqual(err.pos, 3)
self.assertEqual(err.lineno, 1)
self.assertEqual(err.colno, 4)
self.assertIn(err.msg, str(err))
self.assertIn(' at position 3', str(err))
self.assertNotIn(' at position 3', err.msg)
# Bytes pattern
with self.assertRaises(re.error) as cm:
re.compile(b'(
\
xa4
))')
err = cm.exception
self.assertIsInstance(err.pattern, bytes)
self.assertEqual(err.pattern, b'(
\
xa4
))')
self.assertEqual(err.pos, 3)
# Multiline pattern
with self.assertRaises(re.error) as cm:
re.compile("""
(
abc
)
)
(
""", re.VERBOSE)
err = cm.exception
self.assertEqual(err.pos, 77)
self.assertEqual(err.lineno, 5)
self.assertEqual(err.colno, 17)
self.assertIn(err.msg, str(err))
self.assertIn(' at position 77', str(err))
self.assertIn('(line 5, column 17)', str(err))
class PatternReprTests(unittest.TestCase):
def check(self, pattern, expected):
...
...
Misc/NEWS
View file @
ad446d57
...
...
@@ -183,6 +183,8 @@ Core and Builtins
Library
-------
-
Issue
#
22578
:
Added
attributes
to
the
re
.
error
class
.
-
Issue
#
12728
:
Different
Unicode
characters
having
the
same
uppercase
but
different
lowercase
are
now
matched
in
case
-
insensitive
regular
expressions
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment