[pypy-svn] r73124 - pypy/branch/cleanup-objspace-init/pypy/objspace/std

benjamin at codespeak.net benjamin at codespeak.net
Mon Mar 29 20:46:40 CEST 2010


Author: benjamin
Date: Mon Mar 29 20:46:39 2010
New Revision: 73124

Modified:
   pypy/branch/cleanup-objspace-init/pypy/objspace/std/dicttype.py
   pypy/branch/cleanup-objspace-init/pypy/objspace/std/objspace.py
Log:
dict.fromkeys becomes interp level, killing the evil app hack

Modified: pypy/branch/cleanup-objspace-init/pypy/objspace/std/dicttype.py
==============================================================================
--- pypy/branch/cleanup-objspace-init/pypy/objspace/std/dicttype.py	(original)
+++ pypy/branch/cleanup-objspace-init/pypy/objspace/std/dicttype.py	Mon Mar 29 20:46:39 2010
@@ -1,3 +1,5 @@
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root
+from pypy.interpreter.error import OperationError
 from pypy.interpreter import gateway
 from pypy.objspace.std.stdtypedef import *
 from pypy.objspace.std.register_all import register_all
@@ -124,7 +126,6 @@
     def itervalues(d):
         return iter(dict.values(d))
 ''', filename=__file__)
-#XXX what about dict.fromkeys()?
 
 dict_update__ANY             = app.interphook("update")
 dict_popitem__ANY            = app.interphook("popitem")
@@ -138,6 +139,24 @@
 
 register_all(vars(), globals())
 
+ at gateway.unwrap_spec(ObjSpace, W_Root, W_Root, W_Root)
+def descr_fromkeys(space, w_type, w_keys, w_fill=None):
+    from pypy.objspace.std.dictmultiobject import W_DictMultiObject
+    if w_fill is None:
+        w_fill = space.w_None
+    w_dict = W_DictMultiObject.allocate_and_init_instance(space, w_type)
+    w_iter = space.iter(w_keys)
+    while True:
+        try:
+            w_key = space.next(w_iter)
+        except OperationError, e:
+            if not e.match(space, space.w_StopIteration):
+                raise
+            break
+        space.setitem(w_dict, w_key, w_fill)
+    return w_dict
+
+
 # ____________________________________________________________
 
 def descr__new__(space, w_dicttype, __args__):
@@ -160,6 +179,7 @@
     __new__ = newmethod(descr__new__,
                         unwrap_spec=[gateway.ObjSpace,gateway.W_Root,gateway.Arguments]),
     __hash__ = no_hash_descr,
+    fromkeys = gateway.interp2app(descr_fromkeys, as_classmethod=True),
     )
 dict_typedef.registermethods(globals())
 

Modified: pypy/branch/cleanup-objspace-init/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/branch/cleanup-objspace-init/pypy/objspace/std/objspace.py	(original)
+++ pypy/branch/cleanup-objspace-init/pypy/objspace/std/objspace.py	Mon Mar 29 20:46:39 2010
@@ -3,8 +3,7 @@
 from pypy.interpreter.baseobjspace import ObjSpace, Wrappable, UnpackValueError
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.typedef import get_unique_interplevel_subclass
-from pypy.objspace.std import (builtinshortcut, stdtypedef, frame, model,
-                               typeobject)
+from pypy.objspace.std import builtinshortcut, stdtypedef, frame, model
 from pypy.objspace.descroperation import DescrOperation, raiseattrerror
 from pypy.rlib.objectmodel import instantiate
 from pypy.rlib.debug import make_sure_not_resized
@@ -118,22 +117,6 @@
         # the type of old-style classes
         self.w_classobj = self.builtin.get('__metaclass__')
 
-        # fix up a problem where multimethods apparently don't
-        # like to define this at interp-level
-        # HACK HACK HACK
-        old_flags = self.w_dict.__flags__
-        self.w_dict.__flags__ |= typeobject._HEAPTYPE
-        self.appexec([self.w_dict], """
-            (dict):
-                def fromkeys(cls, seq, value=None):
-                    r = cls()
-                    for s in seq:
-                        r[s] = value
-                    return r
-                dict.fromkeys = classmethod(fromkeys)
-        """)
-        self.w_dict.__flags__ = old_flags
-
         # final setup
         self.setup_builtin_modules()
         # Adding transparent proxy call



More information about the Pypy-commit mailing list