-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-36548: Improve the repr of re flags. #12715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
serhiy-storchaka
merged 1 commit into
python:master
from
serhiy-storchaka:re-flags-repr
May 31, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,24 +141,40 @@ | |
__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__ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you're adding this line, would it be worth adding a few test cases for it ? |
||
|
||
globals().update(RegexFlag.__members__) | ||
|
||
# sre exception | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2019-04-07-14-30-10.bpo-36548.CJQiYw.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improved the repr of regular expression flags. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to the issue you are solving (but as you're updating this code anyway):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of aliases the canonical name is the one that is defined first. We want to use long names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some lines are already too long, and using two spaces will make them even longer.