summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-05-31 07:39:47 (GMT)
committerGitHub <noreply@github.com>2019-05-31 07:39:47 (GMT)
commit14a0e16c8805f7ba7c98132ead815dcfdf0e9d33 (patch)
tree61218c9101386ee2ce9eaab2a226be191e5950b6 /Lib
parent65fb2c08c0d66fcf96fb1eb06270feadec830866 (diff)
downloadcpython-14a0e16c8805f7ba7c98132ead815dcfdf0e9d33.zip
cpython-14a0e16c8805f7ba7c98132ead815dcfdf0e9d33.tar.gz
cpython-14a0e16c8805f7ba7c98132ead815dcfdf0e9d33.tar.bz2
bpo-36548: Improve the repr of re flags. (GH-12715)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/re.py48
-rw-r--r--Lib/test/test_re.py12
2 files changed, 44 insertions, 16 deletions
diff --git a/Lib/re.py b/Lib/re.py
index 68d62dc..8f1d55d 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -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
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 137c31d..4817d76 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -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):
"""