Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r86742:897a49107fda
Date: 2016-08-30 16:18 +0200
http://bitbucket.org/pypy/pypy/changeset/897a49107fda/

Log:    SRE_Match.__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
@@ -106,6 +106,8 @@
     def repr_w(self):
         space = self.space
         u = space.unicode_w(space.repr(self.w_pattern))
+        if len(u) > 200:
+            u = u[:200]
         flag_items = []
         flags = self.flags
         if self.is_known_unicode():
@@ -497,6 +499,17 @@
         self.srepat = srepat
         self.ctx = ctx
 
+    def repr_w(self):
+        space = self.space
+        ctx = self.ctx
+        start, end = ctx.match_start, ctx.match_end
+        w_s = slice_w(space, ctx, start, end, space.w_None)
+        u = space.unicode_w(space.repr(w_s))
+        if len(u) > 50:
+            u = u[:50]
+        return space.wrap(u'<_sre.SRE_Match object; span=(%d, %d), match=%s>' 
% 
+                          (start, end, u))
+
     def cannot_copy_w(self):
         space = self.space
         raise oefmt(space.w_TypeError, "cannot copy this match object")
@@ -651,6 +664,7 @@
     'SRE_Match',
     __copy__     = interp2app(W_SRE_Match.cannot_copy_w),
     __deepcopy__ = interp2app(W_SRE_Match.cannot_copy_w),
+    __repr__     = interp2app(W_SRE_Match.repr_w),
     group        = interp2app(W_SRE_Match.group_w),
     groups       = interp2app(W_SRE_Match.groups_w),
     groupdict    = interp2app(W_SRE_Match.groupdict_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
@@ -301,6 +301,11 @@
         import re
         raises(TypeError, re.match, 'hel+', list('hello'))
 
+    def test_match_repr(self):
+        import re
+        m = re.search("ab+c", "xabbbcd")
+        assert repr(m) == "<_sre.SRE_Match object; span=(1, 6), match='abbbc'>"
+
     def test_group_bugs(self):
         import re
         r = re.compile(r"""
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to