[pypy-commit] pypy py3.5: groupindex() returns a read-only dictproxy now

arigo pypy.commits at gmail.com
Tue Aug 30 10:28:12 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r86743:c187e8cdb3f6
Date: 2016-08-30 16:27 +0200
http://bitbucket.org/pypy/pypy/changeset/c187e8cdb3f6/

Log:	groupindex() returns a read-only dictproxy now

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
@@ -130,6 +130,12 @@
             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 fget_groupindex(self, space):
+        w_groupindex = self.w_groupindex
+        if space.isinstance_w(w_groupindex, space.w_dict):
+            w_groupindex = space.newdictproxy(w_groupindex)
+        return w_groupindex
+
     def is_known_bytes(self):
         space = self.space
         if space.is_none(self.w_pattern):
@@ -481,7 +487,7 @@
     sub          = interp2app(W_SRE_Pattern.sub_w),
     subn         = interp2app(W_SRE_Pattern.subn_w),
     flags        = interp_attrproperty('flags', W_SRE_Pattern),
-    groupindex   = interp_attrproperty_w('w_groupindex', W_SRE_Pattern),
+    groupindex   = GetSetProperty(W_SRE_Pattern.fget_groupindex),
     groups       = interp_attrproperty('num_groups', W_SRE_Pattern),
     pattern      = interp_attrproperty_w('w_pattern', W_SRE_Pattern),
 )
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
@@ -53,6 +53,7 @@
         assert re.I | re.M == p.flags
         assert 2 == p.groups
         assert {"g": 2} == p.groupindex
+        raises(TypeError, "p.groupindex['g'] = 3")
 
     def test_repeat_minmax_overflow(self):
         import re
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -319,6 +319,11 @@
                 self, module=module, instance=instance,
                 strdict=strdict, kwargs=kwargs)
 
+    def newdictproxy(self, w_dict):
+        # e.g. for module/_sre/
+        from pypy.objspace.std.dictproxyobject import W_DictProxyObject
+        return W_DictProxyObject(w_dict)
+
     def newset(self, iterable_w=None):
         if iterable_w is None:
             return W_SetObject(self, None)


More information about the pypy-commit mailing list