[pypy-svn] r39853 - in pypy/dist/pypy: module/_stackless objspace/std objspace/std/test

ac at codespeak.net ac at codespeak.net
Sun Mar 4 11:24:53 CET 2007


Author: ac
Date: Sun Mar  4 11:24:50 2007
New Revision: 39853

Modified:
   pypy/dist/pypy/module/_stackless/coroutine.py
   pypy/dist/pypy/module/_stackless/interp_greenlet.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/objspace/std/test/test_typeobject.py
   pypy/dist/pypy/objspace/std/typeobject.py
Log:
issue285 resolved

(asigfrid, arre)
Prevent changes to builtin types.
Add hacks where we change builtin types as part of setting them up.



Modified: pypy/dist/pypy/module/_stackless/coroutine.py
==============================================================================
--- pypy/dist/pypy/module/_stackless/coroutine.py	(original)
+++ pypy/dist/pypy/module/_stackless/coroutine.py	Sun Mar  4 11:24:50 2007
@@ -263,12 +263,20 @@
 
 def makeStaticMethod(module, classname, funcname):
     space = module.space
-    space.appexec(map(space.wrap, (module, classname, funcname)), """
-        (module, klassname, funcname):
-            klass = getattr(module, klassname)
+    w_klass = space.getattr(space.wrap(module), space.wrap(classname))
+    # HACK HACK HACK
+    # make the typeobject mutable for a while
+    from pypy.objspace.std.typeobject import _HEAPTYPE, W_TypeObject
+    assert isinstance(w_klass, W_TypeObject)
+    old_flags = w_klass.__flags__
+    w_klass.__flags__ |= _HEAPTYPE
+    
+    space.appexec([w_klass, space.wrap(funcname)], """
+        (klass, funcname):
             func = getattr(klass, funcname)
             setattr(klass, funcname, staticmethod(func.im_func))
     """)
+    w_klass.__flags__ = old_flags
 
 def post_install(module):
     makeStaticMethod(module, 'coroutine', 'getcurrent')

Modified: pypy/dist/pypy/module/_stackless/interp_greenlet.py
==============================================================================
--- pypy/dist/pypy/module/_stackless/interp_greenlet.py	(original)
+++ pypy/dist/pypy/module/_stackless/interp_greenlet.py	Sun Mar  4 11:24:50 2007
@@ -206,14 +206,21 @@
     space = module.space
     state = AppGreenlet._get_state(space)
     state.post_install()
-    w_module = space.getbuiltinmodule('_stackless')
-    space.appexec([w_module,
+    w_greenlet = get(space, 'greenlet')
+    # HACK HACK HACK
+    # make the typeobject mutable for a while
+    from pypy.objspace.std.typeobject import _HEAPTYPE, W_TypeObject
+    assert isinstance(w_greenlet, W_TypeObject)
+    old_flags = w_greenlet.__flags__
+    w_greenlet.__flags__ |= _HEAPTYPE
+    space.appexec([w_greenlet,
                    state.w_GreenletExit,
                    state.w_GreenletError], """
-    (mod, exit, error):
-        mod.greenlet.GreenletExit = exit
-        mod.greenlet.error = error
+    (greenlet, exit, error):
+        greenlet.GreenletExit = exit
+        greenlet.error = error
     """)
+    w_greenlet.__flags__ = old_flags
 
 AppGreenlet.typedef = TypeDef("greenlet",
     __new__ = interp2app(AppGreenlet.descr_method__new__.im_func,

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Sun Mar  4 11:24:50 2007
@@ -186,6 +186,10 @@
 
         # fix up a problem where multimethods apparently don't 
         # like to define this at interp-level 
+        # HACK HACK HACK
+        from pypy.objspace.std.typeobject import _HEAPTYPE
+        old_flags = self.w_dict.__flags__
+        self.w_dict.__flags__ |= _HEAPTYPE
         self.appexec([self.w_dict], """
             (dict): 
                 def fromkeys(cls, seq, value=None):
@@ -195,6 +199,7 @@
                     return r
                 dict.fromkeys = classmethod(fromkeys)
         """)
+        self.w_dict.__flags__ = old_flags
 
         if self.config.objspace.std.oldstyle:
             self.enable_old_style_classes_as_default_metaclass()

Modified: pypy/dist/pypy/objspace/std/test/test_typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_typeobject.py	Sun Mar  4 11:24:50 2007
@@ -716,3 +716,9 @@
         exec """class A(object):\n  pass\n""" in d
         A = d['A']
         assert A.__module__ == 'yay'
+
+    def test_immutable_builtin(self):
+        raises(TypeError, setattr, list, 'append', 42)
+        raises(TypeError, setattr, list, 'foobar', 42)
+        raises(TypeError, delattr, dict, 'keys')
+        

Modified: pypy/dist/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/typeobject.py	Sun Mar  4 11:24:50 2007
@@ -508,6 +508,10 @@
         if space.is_data_descr(w_descr):
             space.set(w_descr, w_type, w_value)
             return
+    
+    if not w_type.is_heaptype():
+        msg = "can't set attributes on type object '%s'" %(w_type.name,)
+        raise OperationError(space.w_TypeError, space.wrap(msg))
     w_type.dict_w[name] = w_value
 
 def delattr__Type_ANY(space, w_type, w_name):
@@ -521,6 +525,9 @@
         if space.is_data_descr(w_descr):
             space.delete(w_descr, w_type)
             return
+    if not w_type.is_heaptype():
+        msg = "can't delete attributes on type object '%s'" %(w_type.name,)
+        raise OperationError(space.w_TypeError, space.wrap(msg))
     try:
         del w_type.dict_w[name]
         return


More information about the pypy-svn mailing list