[pypy-svn] r31347 - in pypy/dist/pypy: interpreter objspace/std objspace/std/test
arigo at codespeak.net
arigo at codespeak.net
Wed Aug 16 14:41:17 CEST 2006
Author: arigo
Date: Wed Aug 16 14:41:13 2006
New Revision: 31347
Modified:
pypy/dist/pypy/interpreter/argument.py
pypy/dist/pypy/interpreter/gateway.py
pypy/dist/pypy/objspace/std/dictmultiobject.py
pypy/dist/pypy/objspace/std/dictobject.py
pypy/dist/pypy/objspace/std/dictstrobject.py
pypy/dist/pypy/objspace/std/dicttype.py
pypy/dist/pypy/objspace/std/test/test_dictobject.py
Log:
issue244 testing
Second try at adding keyword args to dict.update().
This time pypy-c seems to translate successfully.
Modified: pypy/dist/pypy/interpreter/argument.py
==============================================================================
--- pypy/dist/pypy/interpreter/argument.py (original)
+++ pypy/dist/pypy/interpreter/argument.py Wed Aug 16 14:41:13 2006
@@ -63,7 +63,10 @@
return ArgumentsPrepended(self, w_firstarg)
def popfirst(self):
- return None, None
+ """For optimization only: might return (w_firstarg, args_with_rest),
+ or might just raise IndexError.
+ """
+ raise IndexError
def match_signature(self, signature, defaults_w):
"""Parse args and kwargs according to the signature of a code object,
@@ -210,7 +213,7 @@
def popfirst(self):
if self.nargs <= 0:
- return None, None
+ raise IndexError
valuestack = self.valuestack
newnargs = self.nargs-1
return valuestack.top(newnargs), ArgumentsFromValuestack(self.space, valuestack, newnargs)
Modified: pypy/dist/pypy/interpreter/gateway.py
==============================================================================
--- pypy/dist/pypy/interpreter/gateway.py (original)
+++ pypy/dist/pypy/interpreter/gateway.py Wed Aug 16 14:41:13 2006
@@ -551,8 +551,11 @@
def funcrun(self, func, args):
space = func.space
- w_obj, newargs = args.popfirst()
- if w_obj is not None:
+ try:
+ w_obj, newargs = args.popfirst()
+ except IndexError:
+ return BuiltinCode.funcrun(self, func, args)
+ else:
try:
w_result = self.func__args__(space, w_obj, newargs)
except KeyboardInterrupt:
@@ -565,8 +568,6 @@
if w_result is None:
w_result = space.w_None
return w_result
- else:
- return BuiltinCode.funcrun(self, func, args)
class BuiltinCode0(BuiltinCode):
def fastcall_0(self, space, w_func):
@@ -789,15 +790,20 @@
if ret_w is not None: # it was RPython
return ret_w
# the last argument can be an Arguments
- if args_w and isinstance(args_w[-1], AbstractArguments):
- # ...which is merged with the previous arguments, if any
- args = args_w[-1]
- if len(args_w) > 1:
- more_args_w, more_kwds_w = args.unpack()
- args = Arguments(space, list(args_w[:-1]) + more_args_w,
- more_kwds_w)
+ if not args_w:
+ args = Arguments(space, [])
else:
- args = Arguments(space, list(args_w))
+ args = args_w[-1]
+ assert args is not None
+ if not isinstance(args, AbstractArguments):
+ args = Arguments(space, list(args_w))
+ else:
+ # ...which is merged with the previous arguments, if any
+ if len(args_w) > 1:
+ more_args_w, more_kwds_w = args.unpack()
+ args = Arguments(space,
+ list(args_w[:-1]) + more_args_w,
+ more_kwds_w)
w_func = self.wget(space, name)
return space.call_args(w_func, args)
def get_function(space):
Modified: pypy/dist/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictmultiobject.py (original)
+++ pypy/dist/pypy/objspace/std/dictmultiobject.py Wed Aug 16 14:41:13 2006
@@ -481,11 +481,11 @@
w_dict.implementation = w_dict.implementation.setitem(w_k, w_v)
else:
if space.is_true(w_src):
- from pypy.objspace.std.dicttype import dict_update__ANY_ANY
- dict_update__ANY_ANY(space, w_dict, w_src)
+ from pypy.objspace.std.dicttype import update1
+ update1(space, w_dict, w_src)
if space.is_true(w_kwds):
- from pypy.objspace.std.dicttype import dict_update__ANY_ANY
- dict_update__ANY_ANY(space, w_dict, w_kwds)
+ from pypy.objspace.std.dicttype import update1
+ update1(space, w_dict, w_kwds)
def getitem__DictMulti_ANY(space, w_dict, w_lookup):
w_value = w_dict.implementation.get(w_lookup)
@@ -569,9 +569,9 @@
return w_res
def dict_copy__DictMulti(space, w_self):
- from pypy.objspace.std.dicttype import dict_update__ANY_ANY
+ from pypy.objspace.std.dicttype import update1
w_new = W_DictMultiObject(space)
- dict_update__ANY_ANY(space, w_new, w_self)
+ update1(space, w_new, w_self)
return w_new
def dict_items__DictMulti(space, w_self):
Modified: pypy/dist/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictobject.py (original)
+++ pypy/dist/pypy/objspace/std/dictobject.py Wed Aug 16 14:41:13 2006
@@ -65,11 +65,11 @@
w_dict.content[w_k] = w_v
else:
if space.is_true(w_src):
- from pypy.objspace.std.dicttype import dict_update__ANY_ANY
- dict_update__ANY_ANY(space, w_dict, w_src)
+ from pypy.objspace.std.dicttype import update1
+ update1(space, w_dict, w_src)
if space.is_true(w_kwds):
- from pypy.objspace.std.dicttype import dict_update__ANY_ANY
- dict_update__ANY_ANY(space, w_dict, w_kwds)
+ from pypy.objspace.std.dicttype import update1
+ update1(space, w_dict, w_kwds)
def getitem__Dict_ANY(space, w_dict, w_lookup):
try:
Modified: pypy/dist/pypy/objspace/std/dictstrobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictstrobject.py (original)
+++ pypy/dist/pypy/objspace/std/dictstrobject.py Wed Aug 16 14:41:13 2006
@@ -121,11 +121,11 @@
w_dict.setitem(w_k, w_v)
else:
if space.is_true(w_src):
- from pypy.objspace.std.dicttype import dict_update__ANY_ANY
- dict_update__ANY_ANY(space, w_dict, w_src)
+ from pypy.objspace.std.dicttype import update1
+ update1(space, w_dict, w_src)
if space.is_true(w_kwds):
- from pypy.objspace.std.dicttype import dict_update__ANY_ANY
- dict_update__ANY_ANY(space, w_dict, w_kwds)
+ from pypy.objspace.std.dicttype import update1
+ update1(space, w_dict, w_kwds)
def getitem__DictStr_ANY(space, w_dict, w_lookup):
try:
Modified: pypy/dist/pypy/objspace/std/dicttype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dicttype.py (original)
+++ pypy/dist/pypy/objspace/std/dicttype.py Wed Aug 16 14:41:13 2006
@@ -30,7 +30,7 @@
dict_setdefault = SMM('setdefault', 3, defaults=(None,),
doc='D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d'
' if k not in D')
-dict_update = SMM('update', 2, defaults=((),),
+dict_update = SMM('update', 1, general__args__=True,
doc='D.update(E, **F) -> None. Update D from E and F:'
' for k in E: D[k] = E[k]\n(if E has keys else: for'
' (k, v) in E: D[k] = v) then: for k in F: D[k] ='
@@ -55,7 +55,7 @@
# gateway is imported in the stdtypedef module
app = gateway.applevel('''
- def update(d, o):
+ def update1(d, o):
if hasattr(o, 'keys'):
for k in o.keys():
d[k] = o[k]
@@ -63,6 +63,14 @@
for k,v in o:
d[k] = v
+ def update(d, *args, **kwargs):
+ if len(args) == 1:
+ update1(d, args[0])
+ elif len(args) > 1:
+ raise TypeError("update takes at most 1 (non-keyword) argument")
+ if kwargs:
+ update1(d, kwargs)
+
def popitem(d):
k = d.keys()
if not k:
@@ -110,7 +118,7 @@
''', filename=__file__)
#XXX what about dict.fromkeys()?
-dict_update__ANY_ANY = app.interphook("update")
+dict_update__ANY = app.interphook("update")
dict_popitem__ANY = app.interphook("popitem")
dict_get__ANY_ANY_ANY = app.interphook("get")
dict_setdefault__ANY_ANY_ANY = app.interphook("setdefault")
@@ -118,6 +126,7 @@
dict_iteritems__ANY = app.interphook("iteritems")
dict_iterkeys__ANY = app.interphook("iterkeys")
dict_itervalues__ANY = app.interphook("itervalues")
+update1 = app.interphook("update1")
register_all(vars(), globals())
Modified: pypy/dist/pypy/objspace/std/test/test_dictobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_dictobject.py (original)
+++ pypy/dist/pypy/objspace/std/test/test_dictobject.py Wed Aug 16 14:41:13 2006
@@ -245,7 +245,17 @@
d = {}
d.update()
assert d == {}
-
+
+ def test_update_kwargs(self):
+ d = {}
+ d.update(foo='bar', baz=1)
+ assert d == {'foo': 'bar', 'baz': 1}
+
+ def test_update_dict_and_kwargs(self):
+ d = {}
+ d.update({'foo': 'bar'}, baz=1)
+ assert d == {'foo': 'bar', 'baz': 1}
+
def test_values(self):
d = {1:2, 3:4}
vals = d.values()
More information about the Pypy-commit
mailing list