[pypy-svn] r15159 - pypy/dist/pypy/objspace/std
arigo at codespeak.net
arigo at codespeak.net
Wed Jul 27 11:30:22 CEST 2005
Author: arigo
Date: Wed Jul 27 11:30:08 2005
New Revision: 15159
Modified:
pypy/dist/pypy/objspace/std/dictobject.py
pypy/dist/pypy/objspace/std/dicttype.py
pypy/dist/pypy/objspace/std/model.py
pypy/dist/pypy/objspace/std/objspace.py
Log:
issue20 resolved
- Implemented dictionary iterators.
- Asserts that we don't forget to put object implementations in
objspace/std/model.py -- we indeed forgot dictproxyobject.
Modified: pypy/dist/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictobject.py (original)
+++ pypy/dist/pypy/objspace/std/dictobject.py Wed Jul 27 11:30:08 2005
@@ -170,10 +170,8 @@
dict_has_key__Dict_ANY = contains__Dict_ANY
def iter__Dict(space, w_dict):
- from pypy.objspace.std import iterobject
- w_keys = dict_keys__Dict(space, w_dict)
- return iterobject.W_SeqIterObject(space, w_keys)
-
+ return W_DictIter_Keys(space, w_dict)
+
def eq__Dict_Dict(space, w_left, w_right):
if space.is_true(space.is_(w_left, w_right)):
return space.w_True
@@ -255,6 +253,15 @@
for entry in w_self.data
if entry.w_value is not None])
+def dict_iteritems__Dict(space, w_self):
+ return W_DictIter_Items(space, w_self)
+
+def dict_iterkeys__Dict(space, w_self):
+ return W_DictIter_Keys(space, w_self)
+
+def dict_itervalues__Dict(space, w_self):
+ return W_DictIter_Values(space, w_self)
+
def dict_clear__Dict(space, w_self):
w_self.data = [Entry()]
w_self.used = 0
@@ -298,5 +305,58 @@
repr__Dict = str__Dict
+
+# ____________________________________________________________
+# Iteration
+
+class W_DictIterObject(W_Object):
+ from pypy.objspace.std.dicttype import dictiter_typedef as typedef
+
+ def __init__(w_self, space, w_dictobject):
+ W_Object.__init__(w_self, space)
+ w_self.w_dictobject = w_dictobject
+ w_self.len = w_dictobject.used
+ w_self.pos = 0
+
+registerimplementation(W_DictIterObject)
+
+class W_DictIter_Keys(W_DictIterObject):
+ def return_entry(w_self, entry):
+ return entry.w_key
+
+class W_DictIter_Values(W_DictIterObject):
+ def return_entry(w_self, entry):
+ return entry.w_value
+
+class W_DictIter_Items(W_DictIterObject):
+ def return_entry(w_self, entry):
+ return w_self.space.newtuple([entry.w_key, entry.w_value])
+
+
+def iter__DictIterObject(space, w_dictiter):
+ return w_dictiter
+
+def next__DictIterObject(space, w_dictiter):
+ w_dict = w_dictiter.w_dictobject
+ if w_dict is not None:
+ if w_dictiter.len != w_dict.used:
+ w_dictiter.len = -1 # Make this error state sticky
+ raise OperationError(space.w_RuntimeError,
+ space.wrap("dictionary changed size during iteration"))
+ # look for the next entry
+ i = w_dictiter.pos
+ data = w_dict.data
+ while i < len(data):
+ entry = data[i]
+ i += 1
+ if entry.w_value is not None:
+ w_dictiter.pos = i
+ return w_dictiter.return_entry(entry)
+ # no more entries
+ w_dictiter.w_dictobject = None
+ raise OperationError(space.w_StopIteration, space.w_None)
+
+# ____________________________________________________________
+
from pypy.objspace.std import dicttype
register_all(vars(), dicttype)
Modified: pypy/dist/pypy/objspace/std/dicttype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dicttype.py (original)
+++ pypy/dist/pypy/objspace/std/dicttype.py Wed Jul 27 11:30:08 2005
@@ -100,3 +100,7 @@
unwrap_spec=[gateway.ObjSpace,gateway.W_Root,gateway.Arguments]),
)
dict_typedef.registermethods(globals())
+
+
+dictiter_typedef = StdTypeDef("dictionaryiterator",
+ )
Modified: pypy/dist/pypy/objspace/std/model.py
==============================================================================
--- pypy/dist/pypy/objspace/std/model.py (original)
+++ pypy/dist/pypy/objspace/std/model.py Wed Jul 27 11:30:08 2005
@@ -49,6 +49,7 @@
from pypy.objspace.std import noneobject
from pypy.objspace.std import iterobject
from pypy.objspace.std import unicodeobject
+ from pypy.objspace.std import dictproxyobject
from pypy.objspace.std import fake
import pypy.objspace.std.default # register a few catch-all multimethods
@@ -61,6 +62,7 @@
tupleobject.W_TupleObject: [],
listobject.W_ListObject: [],
dictobject.W_DictObject: [],
+ dictobject.W_DictIterObject: [],
stringobject.W_StringObject: [],
typeobject.W_TypeObject: [],
sliceobject.W_SliceObject: [],
@@ -68,10 +70,17 @@
noneobject.W_NoneObject: [],
iterobject.W_SeqIterObject: [],
unicodeobject.W_UnicodeObject: [],
+ dictproxyobject.W_DictProxyObject: [],
}
for type in self.typeorder:
self.typeorder[type].append((type, None))
+ # check if we missed implementations
+ from pypy.objspace.std.objspace import _registered_implementations
+ for implcls in _registered_implementations:
+ assert implcls in self.typeorder, (
+ "please add %r in StdTypeModel.typeorder" % (implcls,))
+
# register the order in which types are converted into each others
# when trying to dispatch multimethods.
# XXX build these lists a bit more automatically later
Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py (original)
+++ pypy/dist/pypy/objspace/std/objspace.py Wed Jul 27 11:30:08 2005
@@ -14,11 +14,11 @@
import sys
import os
+_registered_implementations = {}
def registerimplementation(implcls):
- # this function should ultimately register the implementation class somewhere
- # it may be modified to take 'typedef' instead of requiring it to be
- # stored in 'implcls' itself
+ # hint to objspace.std.model to register the implementation class
assert issubclass(implcls, W_Object)
+ _registered_implementations[implcls] = True
##################################################################
More information about the Pypy-commit
mailing list