Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r86703:1e8e1e6907c8
Date: 2016-08-29 16:44 +0200
http://bitbucket.org/pypy/pypy/changeset/1e8e1e6907c8/
Log: re.compile().__repr__
diff --git a/pypy/module/_sre/interp_sre.py b/pypy/module/_sre/interp_sre.py
--- a/pypy/module/_sre/interp_sre.py
+++ b/pypy/module/_sre/interp_sre.py
@@ -13,7 +13,7 @@
#
# Constants and exposed functions
-from rpython.rlib.rsre import rsre_core
+from rpython.rlib.rsre import rsre_core, rsre_char
from rpython.rlib.rsre.rsre_char import CODESIZE, MAXREPEAT, MAXGROUPS,
getlower, set_unicode_db
@@ -92,6 +92,10 @@
#
# SRE_Pattern class
+FLAG_NAMES = ["re.TEMPLATE", "re.IGNORECASE", "re.LOCALE", "re.MULTILINE",
+ "re.DOTALL", "re.UNICODE", "re.VERBOSE", "re.DEBUG",
+ "re.ASCII"]
+
class W_SRE_Pattern(W_Root):
_immutable_fields_ = ["code", "flags", "num_groups", "w_groupindex"]
@@ -99,6 +103,43 @@
space = self.space
raise oefmt(space.w_TypeError, "cannot copy this pattern object")
+ def repr_w(self):
+ space = self.space
+ u = space.unicode_w(space.repr(self.w_pattern))
+ flag_items = []
+ flags = self.flags
+ if self.is_known_unicode():
+ if ((flags & (rsre_char.SRE_FLAG_LOCALE |
+ rsre_char.SRE_FLAG_UNICODE |
+ 256)) # rsre_char.SRE_FLAG_ASCII
+ == rsre_char.SRE_FLAG_UNICODE):
+ flags &= ~rsre_char.SRE_FLAG_UNICODE
+ for i, name in enumerate(FLAG_NAMES):
+ if flags & (1 << i):
+ flags -= (1 << i)
+ flag_items.append(name)
+ if flags != 0:
+ flag_items.append('0x%x' % flags)
+ if len(flag_items) == 0:
+ usep = u''
+ uflags = u''
+ else:
+ usep = u', '
+ uflags = u'|'.join([item.decode('latin-1') for item in flag_items])
+ return space.wrap(u're.compile(%s%s%s)' % (u, usep, uflags))
+
+ def is_known_bytes(self):
+ space = self.space
+ if space.is_none(self.w_pattern):
+ return False
+ return not space.isinstance_w(self.w_pattern, space.w_unicode)
+
+ def is_known_unicode(self):
+ space = self.space
+ if space.is_none(self.w_pattern):
+ return False
+ return space.isinstance_w(self.w_pattern, space.w_unicode)
+
def make_ctx(self, w_string, pos=0, endpos=sys.maxint, flags=0):
"""Make a StrMatchContext, BufMatchContext or a UnicodeMatchContext for
searching in the given w_string object."""
@@ -110,8 +151,7 @@
flags = self.flags | flags
if space.isinstance_w(w_string, space.w_unicode):
unicodestr = space.unicode_w(w_string)
- if not (space.is_none(self.w_pattern) or
- space.isinstance_w(self.w_pattern, space.w_unicode)):
+ if self.is_known_bytes():
raise oefmt(space.w_TypeError,
"can't use a bytes pattern on a string-like "
"object")
@@ -122,8 +162,7 @@
return rsre_core.UnicodeMatchContext(self.code, unicodestr,
pos, endpos, flags)
elif space.isinstance_w(w_string, space.w_str):
- if (not space.is_none(self.w_pattern) and
- space.isinstance_w(self.w_pattern, space.w_unicode)):
+ if self.is_known_unicode():
raise oefmt(space.w_TypeError,
"can't use a string pattern on a bytes-like "
"object")
@@ -136,8 +175,7 @@
pos, endpos, flags)
else:
buf = space.readbuf_w(w_string)
- if (not space.is_none(self.w_pattern) and
- space.isinstance_w(self.w_pattern, space.w_unicode)):
+ if self.is_known_unicode():
raise oefmt(space.w_TypeError,
"can't use a string pattern on a bytes-like "
"object")
@@ -422,6 +460,7 @@
__new__ = interp2app(SRE_Pattern__new__),
__copy__ = interp2app(W_SRE_Pattern.cannot_copy_w),
__deepcopy__ = interp2app(W_SRE_Pattern.cannot_copy_w),
+ __repr__ = interp2app(W_SRE_Pattern.repr_w),
__weakref__ = make_weakref_descr(W_SRE_Pattern),
findall = interp2app(W_SRE_Pattern.findall_w),
finditer = interp2app(W_SRE_Pattern.finditer_w),
diff --git a/pypy/module/_sre/test/test_app_sre.py
b/pypy/module/_sre/test/test_app_sre.py
--- a/pypy/module/_sre/test/test_app_sre.py
+++ b/pypy/module/_sre/test/test_app_sre.py
@@ -123,6 +123,15 @@
assert re.fullmatch(r"ab*c", "abbbcdef") is None
assert re.fullmatch(r"ab*c", "abbbc") is not None
+ def test_repr(self):
+ import re
+ r = re.compile(r'f(o"\d)', 0)
+ assert repr(r) == (
+ r"""re.compile('f(o"\\d)')""")
+ r = re.compile(r'f(o"\d)', re.IGNORECASE|re.DOTALL|re.VERBOSE)
+ assert repr(r) == (
+ r"""re.compile('f(o"\\d)', re.IGNORECASE|re.DOTALL|re.VERBOSE)""")
+
class AppTestSreMatch:
spaceconfig = dict(usemodules=('array', ))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit