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
14a0e16c
Commit
14a0e16c
authored
May 31, 2019
by
Serhiy Storchaka
Committed by
GitHub
May 31, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-36548: Improve the repr of re flags. (GH-12715)
parent
65fb2c08
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
16 deletions
+45
-16
Lib/re.py
Lib/re.py
+32
-16
Lib/test/test_re.py
Lib/test/test_re.py
+12
-0
Misc/NEWS.d/next/Library/2019-04-07-14-30-10.bpo-36548.CJQiYw.rst
...S.d/next/Library/2019-04-07-14-30-10.bpo-36548.CJQiYw.rst
+1
-0
No files found.
Lib/re.py
View file @
14a0e16c
...
...
@@ -141,24 +141,40 @@ __all__ = [
__version__
=
"2.2.1"
class
RegexFlag
(
enum
.
IntFlag
):
ASCII
=
sre_compile
.
SRE_FLAG_ASCII
# assume ascii "locale"
IGNORECASE
=
sre_compile
.
SRE_FLAG_IGNORECASE
# ignore case
LOCALE
=
sre_compile
.
SRE_FLAG_LOCALE
# assume current 8-bit locale
UNICODE
=
sre_compile
.
SRE_FLAG_UNICODE
# assume unicode "locale"
MULTILINE
=
sre_compile
.
SRE_FLAG_MULTILINE
# make anchors look for newline
DOTALL
=
sre_compile
.
SRE_FLAG_DOTALL
# make dot match newline
VERBOSE
=
sre_compile
.
SRE_FLAG_VERBOSE
# ignore whitespace and comments
A
=
ASCII
I
=
IGNORECASE
L
=
LOCALE
U
=
UNICODE
M
=
MULTILINE
S
=
DOTALL
X
=
VERBOSE
ASCII
=
A
=
sre_compile
.
SRE_FLAG_ASCII
# assume ascii "locale"
IGNORECASE
=
I
=
sre_compile
.
SRE_FLAG_IGNORECASE
# ignore case
LOCALE
=
L
=
sre_compile
.
SRE_FLAG_LOCALE
# assume current 8-bit locale
UNICODE
=
U
=
sre_compile
.
SRE_FLAG_UNICODE
# assume unicode "locale"
MULTILINE
=
M
=
sre_compile
.
SRE_FLAG_MULTILINE
# make anchors look for newline
DOTALL
=
S
=
sre_compile
.
SRE_FLAG_DOTALL
# make dot match newline
VERBOSE
=
X
=
sre_compile
.
SRE_FLAG_VERBOSE
# ignore whitespace and comments
# sre extensions (experimental, don't rely on these)
TEMPLATE
=
sre_compile
.
SRE_FLAG_TEMPLATE
# disable backtracking
T
=
TEMPLATE
TEMPLATE
=
T
=
sre_compile
.
SRE_FLAG_TEMPLATE
# disable backtracking
DEBUG
=
sre_compile
.
SRE_FLAG_DEBUG
# dump pattern after compilation
def
__repr__
(
self
):
if
self
.
_name_
is
not
None
:
return
f're.
{
self
.
_name_
}
'
value
=
self
.
_value_
members
=
[]
negative
=
value
<
0
if
negative
:
value
=
~
value
for
m
in
self
.
__class__
:
if
value
&
m
.
_value_
:
value
&=
~
m
.
_value_
members
.
append
(
f're.
{
m
.
_name_
}
'
)
if
value
:
members
.
append
(
hex
(
value
))
res
=
'|'
.
join
(
members
)
if
negative
:
if
len
(
members
)
>
1
:
res
=
f'~(
{
res
}
)'
else
:
res
=
f'~
{
res
}
'
return
res
__str__
=
object
.
__str__
globals
().
update
(
RegexFlag
.
__members__
)
# sre exception
...
...
Lib/test/test_re.py
View file @
14a0e16c
...
...
@@ -2170,6 +2170,18 @@ class PatternReprTests(unittest.TestCase):
self.assertEqual(r[:30], "re.compile('
Very
long
long
lon
")
self.assertEqual(r[-16:], "
,
re
.
IGNORECASE
)
")
def test_flags_repr(self):
self.assertEqual(repr(re.I), "
re
.
IGNORECASE
")
self.assertEqual(repr(re.I|re.S|re.X),
"
re
.
IGNORECASE
|
re
.
DOTALL
|
re
.
VERBOSE
")
self.assertEqual(repr(re.I|re.S|re.X|(1<<20)),
"
re
.
IGNORECASE
|
re
.
DOTALL
|
re
.
VERBOSE
|
0x100000
")
self.assertEqual(repr(~re.I), "
~
re
.
IGNORECASE
")
self.assertEqual(repr(~(re.I|re.S|re.X)),
"
~
(
re
.
IGNORECASE
|
re
.
DOTALL
|
re
.
VERBOSE
)
")
self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))),
"
~
(
re
.
IGNORECASE
|
re
.
DOTALL
|
re
.
VERBOSE
|
0x100000
)
")
class ImplementationTest(unittest.TestCase):
"""
...
...
Misc/NEWS.d/next/Library/2019-04-07-14-30-10.bpo-36548.CJQiYw.rst
0 → 100644
View file @
14a0e16c
Improved the repr of regular expression flags.
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