[pypy-commit] pypy speedup-unpackiterable: merge virtual-args here, so I don't have to worry about conflicts
fijal
noreply at buildbot.pypy.org
Fri Jul 13 11:44:13 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: speedup-unpackiterable
Changeset: r56060:0c83622b80bd
Date: 2012-07-13 11:40 +0200
http://bitbucket.org/pypy/pypy/changeset/0c83622b80bd/
Log: merge virtual-args here, so I don't have to worry about conflicts
diff --git a/pypy/interpreter/argument.py b/pypy/interpreter/argument.py
--- a/pypy/interpreter/argument.py
+++ b/pypy/interpreter/argument.py
@@ -110,12 +110,10 @@
make_sure_not_resized(self.keywords_w)
make_sure_not_resized(self.arguments_w)
- if w_stararg is not None:
- self._combine_starargs_wrapped(w_stararg)
- # if we have a call where **args are used at the callsite
- # we shouldn't let the JIT see the argument matching
- self._dont_jit = (w_starstararg is not None and
- self._combine_starstarargs_wrapped(w_starstararg))
+ self._combine_wrapped(w_stararg, w_starstararg)
+ # a flag that specifies whether the JIT can unroll loops that operate
+ # on the keywords
+ self._jit_few_keywords = self.keywords is None or jit.isconstant(len(self.keywords))
def __repr__(self):
""" NOT_RPYTHON """
@@ -129,7 +127,7 @@
### Manipulation ###
- @jit.look_inside_iff(lambda self: not self._dont_jit)
+ @jit.look_inside_iff(lambda self: self._jit_few_keywords)
def unpack(self): # slowish
"Return a ([w1,w2...], {'kw':w3...}) pair."
kwds_w = {}
@@ -176,13 +174,14 @@
keywords, values_w = space.view_as_kwargs(w_starstararg)
if keywords is not None: # this path also taken for empty dicts
if self.keywords is None:
- self.keywords = keywords[:] # copy to make non-resizable
- self.keywords_w = values_w[:]
+ self.keywords = keywords
+ self.keywords_w = values_w
else:
- self._check_not_duplicate_kwargs(keywords, values_w)
+ _check_not_duplicate_kwargs(
+ self.space, self.keywords, keywords, values_w)
self.keywords = self.keywords + keywords
self.keywords_w = self.keywords_w + values_w
- return not jit.isconstant(len(self.keywords))
+ return
if space.isinstance_w(w_starstararg, space.w_dict):
keys_w = space.unpackiterable(w_starstararg)
else:
@@ -198,57 +197,17 @@
"a mapping, not %s" % (typename,)))
raise
keys_w = space.unpackiterable(w_keys)
- self._do_combine_starstarargs_wrapped(keys_w, w_starstararg)
- return True
-
- def _do_combine_starstarargs_wrapped(self, keys_w, w_starstararg):
- space = self.space
keywords_w = [None] * len(keys_w)
keywords = [None] * len(keys_w)
- i = 0
- for w_key in keys_w:
- try:
- key = space.str_w(w_key)
- except OperationError, e:
- if e.match(space, space.w_TypeError):
- raise OperationError(
- space.w_TypeError,
- space.wrap("keywords must be strings"))
- if e.match(space, space.w_UnicodeEncodeError):
- # Allow this to pass through
- key = None
- else:
- raise
- else:
- if self.keywords and key in self.keywords:
- raise operationerrfmt(self.space.w_TypeError,
- "got multiple values "
- "for keyword argument "
- "'%s'", key)
- keywords[i] = key
- keywords_w[i] = space.getitem(w_starstararg, w_key)
- i += 1
+ _do_combine_starstarargs_wrapped(space, keys_w, w_starstararg, keywords, keywords_w, self.keywords)
+ self.keyword_names_w = keys_w
if self.keywords is None:
self.keywords = keywords
self.keywords_w = keywords_w
else:
self.keywords = self.keywords + keywords
self.keywords_w = self.keywords_w + keywords_w
- self.keyword_names_w = keys_w
- @jit.look_inside_iff(lambda self, keywords, keywords_w:
- jit.isconstant(len(keywords) and
- jit.isconstant(self.keywords)))
- def _check_not_duplicate_kwargs(self, keywords, keywords_w):
- # looks quadratic, but the JIT should remove all of it nicely.
- # Also, all the lists should be small
- for key in keywords:
- for otherkey in self.keywords:
- if otherkey == key:
- raise operationerrfmt(self.space.w_TypeError,
- "got multiple values "
- "for keyword argument "
- "'%s'", key)
def fixedunpack(self, argcount):
"""The simplest argument parsing: get the 'argcount' arguments,
@@ -269,34 +228,14 @@
### Parsing for function calls ###
- # XXX: this should be @jit.look_inside_iff, but we need key word arguments,
- # and it doesn't support them for now.
+ @jit.unroll_safe
def _match_signature(self, w_firstarg, scope_w, signature, defaults_w=None,
blindargs=0):
"""Parse args and kwargs according to the signature of a code object,
or raise an ArgErr in case of failure.
- Return the number of arguments filled in.
"""
- if jit.we_are_jitted() and self._dont_jit:
- return self._match_signature_jit_opaque(w_firstarg, scope_w,
- signature, defaults_w,
- blindargs)
- return self._really_match_signature(w_firstarg, scope_w, signature,
- defaults_w, blindargs)
-
- @jit.dont_look_inside
- def _match_signature_jit_opaque(self, w_firstarg, scope_w, signature,
- defaults_w, blindargs):
- return self._really_match_signature(w_firstarg, scope_w, signature,
- defaults_w, blindargs)
-
- @jit.unroll_safe
- def _really_match_signature(self, w_firstarg, scope_w, signature,
- defaults_w=None, blindargs=0):
- #
+ # w_firstarg = a first argument to be inserted (e.g. self) or None
# args_w = list of the normal actual parameters, wrapped
- # kwds_w = real dictionary {'keyword': wrapped parameter}
- # argnames = list of formal parameter names
# scope_w = resulting list of wrapped values
#
@@ -304,38 +243,29 @@
# so all values coming from there can be assumed constant. It assumes
# that the length of the defaults_w does not vary too much.
co_argcount = signature.num_argnames() # expected formal arguments, without */**
- has_vararg = signature.has_vararg()
- has_kwarg = signature.has_kwarg()
- extravarargs = None
- input_argcount = 0
+ # put the special w_firstarg into the scope, if it exists
if w_firstarg is not None:
upfront = 1
if co_argcount > 0:
scope_w[0] = w_firstarg
- input_argcount = 1
- else:
- extravarargs = [w_firstarg]
else:
upfront = 0
args_w = self.arguments_w
num_args = len(args_w)
+ avail = num_args + upfront
keywords = self.keywords
- keywords_w = self.keywords_w
num_kwds = 0
if keywords is not None:
num_kwds = len(keywords)
- avail = num_args + upfront
+ # put as many positional input arguments into place as available
+ input_argcount = upfront
if input_argcount < co_argcount:
- # put as many positional input arguments into place as available
- if avail > co_argcount:
- take = co_argcount - input_argcount
- else:
- take = num_args
+ take = min(num_args, co_argcount - upfront)
# letting the JIT unroll this loop is safe, because take is always
# smaller than co_argcount
@@ -344,11 +274,10 @@
input_argcount += take
# collect extra positional arguments into the *vararg
- if has_vararg:
+ if signature.has_vararg():
args_left = co_argcount - upfront
if args_left < 0: # check required by rpython
- assert extravarargs is not None
- starargs_w = extravarargs
+ starargs_w = [w_firstarg]
if num_args:
starargs_w = starargs_w + args_w
elif num_args > args_left:
@@ -357,86 +286,65 @@
starargs_w = []
scope_w[co_argcount] = self.space.newtuple(starargs_w)
elif avail > co_argcount:
- raise ArgErrCount(avail, num_kwds,
- co_argcount, has_vararg, has_kwarg,
- defaults_w, 0)
+ raise ArgErrCount(avail, num_kwds, signature, defaults_w, 0)
- # the code assumes that keywords can potentially be large, but that
- # argnames is typically not too large
- num_remainingkwds = num_kwds
- used_keywords = None
- if keywords:
- # letting JIT unroll the loop is *only* safe if the callsite didn't
- # use **args because num_kwds can be arbitrarily large otherwise.
- used_keywords = [False] * num_kwds
- for i in range(num_kwds):
- name = keywords[i]
- # If name was not encoded as a string, it could be None. In that
- # case, it's definitely not going to be in the signature.
- if name is None:
- continue
- j = signature.find_argname(name)
- if j < 0:
- continue
- elif j < input_argcount:
- # check that no keyword argument conflicts with these. note
- # that for this purpose we ignore the first blindargs,
- # which were put into place by prepend(). This way,
- # keywords do not conflict with the hidden extra argument
- # bound by methods.
- if blindargs <= j:
- raise ArgErrMultipleValues(name)
+ # if a **kwargs argument is needed, create the dict
+ w_kwds = None
+ if signature.has_kwarg():
+ w_kwds = self.space.newdict(kwargs=True)
+ scope_w[co_argcount + signature.has_vararg()] = w_kwds
+
+ # handle keyword arguments
+ num_remainingkwds = 0
+ keywords_w = self.keywords_w
+ kwds_mapping = None
+ if num_kwds:
+ # kwds_mapping maps target indexes in the scope (minus input_argcount)
+ # to positions in the keywords_w list
+ kwds_mapping = [0] * (co_argcount - input_argcount)
+ # initialize manually, for the JIT :-(
+ for i in range(len(kwds_mapping)):
+ kwds_mapping[i] = -1
+ # match the keywords given at the call site to the argument names
+ # the called function takes
+ # this function must not take a scope_w, to make the scope not
+ # escape
+ num_remainingkwds = _match_keywords(
+ signature, blindargs, input_argcount, keywords,
+ kwds_mapping, self._jit_few_keywords)
+ if num_remainingkwds:
+ if w_kwds is not None:
+ # collect extra keyword arguments into the **kwarg
+ _collect_keyword_args(
+ self.space, keywords, keywords_w, w_kwds,
+ kwds_mapping, self.keyword_names_w, self._jit_few_keywords)
else:
- assert scope_w[j] is None
- scope_w[j] = keywords_w[i]
- used_keywords[i] = True # mark as used
- num_remainingkwds -= 1
+ if co_argcount == 0:
+ raise ArgErrCount(avail, num_kwds, signature, defaults_w, 0)
+ raise ArgErrUnknownKwds(self.space, num_remainingkwds, keywords,
+ kwds_mapping, self.keyword_names_w)
+
+ # check for missing arguments and fill them from the kwds,
+ # or with defaults, if available
missing = 0
if input_argcount < co_argcount:
def_first = co_argcount - (0 if defaults_w is None else len(defaults_w))
+ j = 0
+ kwds_index = -1
for i in range(input_argcount, co_argcount):
- if scope_w[i] is not None:
- continue
+ if kwds_mapping is not None:
+ kwds_index = kwds_mapping[j]
+ j += 1
+ if kwds_index >= 0:
+ scope_w[i] = keywords_w[kwds_index]
+ continue
defnum = i - def_first
if defnum >= 0:
scope_w[i] = defaults_w[defnum]
else:
- # error: not enough arguments. Don't signal it immediately
- # because it might be related to a problem with */** or
- # keyword arguments, which will be checked for below.
missing += 1
-
- # collect extra keyword arguments into the **kwarg
- if has_kwarg:
- w_kwds = self.space.newdict(kwargs=True)
- if num_remainingkwds:
- #
- limit = len(keywords)
- if self.keyword_names_w is not None:
- limit -= len(self.keyword_names_w)
- for i in range(len(keywords)):
- if not used_keywords[i]:
- if i < limit:
- w_key = self.space.wrap(keywords[i])
- else:
- w_key = self.keyword_names_w[i - limit]
- self.space.setitem(w_kwds, w_key, keywords_w[i])
- #
- scope_w[co_argcount + has_vararg] = w_kwds
- elif num_remainingkwds:
- if co_argcount == 0:
- raise ArgErrCount(avail, num_kwds,
- co_argcount, has_vararg, has_kwarg,
- defaults_w, missing)
- raise ArgErrUnknownKwds(self.space, num_remainingkwds, keywords,
- used_keywords, self.keyword_names_w)
-
- if missing:
- raise ArgErrCount(avail, num_kwds,
- co_argcount, has_vararg, has_kwarg,
- defaults_w, missing)
-
- return co_argcount + has_vararg + has_kwarg
+ if missing:
+ raise ArgErrCount(avail, num_kwds, signature, defaults_w, missing)
@@ -448,11 +356,12 @@
scope_w must be big enough for signature.
"""
try:
- return self._match_signature(w_firstarg,
- scope_w, signature, defaults_w, 0)
+ self._match_signature(w_firstarg,
+ scope_w, signature, defaults_w, 0)
except ArgErr, e:
raise operationerrfmt(self.space.w_TypeError,
"%s() %s", fnname, e.getmsg())
+ return signature.scope_length()
def _parse(self, w_firstarg, signature, defaults_w, blindargs=0):
"""Parse args and kwargs according to the signature of a code object,
@@ -499,6 +408,102 @@
space.setitem(w_kwds, w_key, self.keywords_w[i])
return w_args, w_kwds
+# JIT helper functions
+# these functions contain functionality that the JIT is not always supposed to
+# look at. They should not get a self arguments, which makes the amount of
+# arguments annoying :-(
+
+ at jit.look_inside_iff(lambda space, existingkeywords, keywords, keywords_w:
+ jit.isconstant(len(keywords) and
+ jit.isconstant(existingkeywords)))
+def _check_not_duplicate_kwargs(space, existingkeywords, keywords, keywords_w):
+ # looks quadratic, but the JIT should remove all of it nicely.
+ # Also, all the lists should be small
+ for key in keywords:
+ for otherkey in existingkeywords:
+ if otherkey == key:
+ raise operationerrfmt(space.w_TypeError,
+ "got multiple values "
+ "for keyword argument "
+ "'%s'", key)
+
+def _do_combine_starstarargs_wrapped(space, keys_w, w_starstararg, keywords,
+ keywords_w, existingkeywords):
+ i = 0
+ for w_key in keys_w:
+ try:
+ key = space.str_w(w_key)
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ raise OperationError(
+ space.w_TypeError,
+ space.wrap("keywords must be strings"))
+ if e.match(space, space.w_UnicodeEncodeError):
+ # Allow this to pass through
+ key = None
+ else:
+ raise
+ else:
+ if existingkeywords and key in existingkeywords:
+ raise operationerrfmt(space.w_TypeError,
+ "got multiple values "
+ "for keyword argument "
+ "'%s'", key)
+ keywords[i] = key
+ keywords_w[i] = space.getitem(w_starstararg, w_key)
+ i += 1
+
+ at jit.look_inside_iff(
+ lambda signature, blindargs, input_argcount,
+ keywords, kwds_mapping, jiton: jiton)
+def _match_keywords(signature, blindargs, input_argcount,
+ keywords, kwds_mapping, _):
+ # letting JIT unroll the loop is *only* safe if the callsite didn't
+ # use **args because num_kwds can be arbitrarily large otherwise.
+ num_kwds = num_remainingkwds = len(keywords)
+ for i in range(num_kwds):
+ name = keywords[i]
+ # If name was not encoded as a string, it could be None. In that
+ # case, it's definitely not going to be in the signature.
+ if name is None:
+ continue
+ j = signature.find_argname(name)
+ # if j == -1 nothing happens, because j < input_argcount and
+ # blindargs > j
+ if j < input_argcount:
+ # check that no keyword argument conflicts with these. note
+ # that for this purpose we ignore the first blindargs,
+ # which were put into place by prepend(). This way,
+ # keywords do not conflict with the hidden extra argument
+ # bound by methods.
+ if blindargs <= j:
+ raise ArgErrMultipleValues(name)
+ else:
+ kwds_mapping[j - input_argcount] = i # map to the right index
+ num_remainingkwds -= 1
+ return num_remainingkwds
+
+ at jit.look_inside_iff(
+ lambda space, keywords, keywords_w, w_kwds, kwds_mapping,
+ keyword_names_w, jiton: jiton)
+def _collect_keyword_args(space, keywords, keywords_w, w_kwds, kwds_mapping,
+ keyword_names_w, _):
+ limit = len(keywords)
+ if keyword_names_w is not None:
+ limit -= len(keyword_names_w)
+ for i in range(len(keywords)):
+ # again a dangerous-looking loop that either the JIT unrolls
+ # or that is not too bad, because len(kwds_mapping) is small
+ for j in kwds_mapping:
+ if i == j:
+ break
+ else:
+ if i < limit:
+ w_key = space.wrap(keywords[i])
+ else:
+ w_key = keyword_names_w[i - limit]
+ space.setitem(w_kwds, w_key, keywords_w[i])
+
class ArgumentsForTranslation(Arguments):
def __init__(self, space, args_w, keywords=None, keywords_w=None,
w_stararg=None, w_starstararg=None):
@@ -654,11 +659,9 @@
class ArgErrCount(ArgErr):
- def __init__(self, got_nargs, nkwds, expected_nargs, has_vararg, has_kwarg,
+ def __init__(self, got_nargs, nkwds, signature,
defaults_w, missing_args):
- self.expected_nargs = expected_nargs
- self.has_vararg = has_vararg
- self.has_kwarg = has_kwarg
+ self.signature = signature
self.num_defaults = 0 if defaults_w is None else len(defaults_w)
self.missing_args = missing_args
@@ -666,16 +669,16 @@
self.num_kwds = nkwds
def getmsg(self):
- n = self.expected_nargs
+ n = self.signature.num_argnames()
if n == 0:
msg = "takes no arguments (%d given)" % (
self.num_args + self.num_kwds)
else:
defcount = self.num_defaults
- has_kwarg = self.has_kwarg
+ has_kwarg = self.signature.has_kwarg()
num_args = self.num_args
num_kwds = self.num_kwds
- if defcount == 0 and not self.has_vararg:
+ if defcount == 0 and not self.signature.has_vararg():
msg1 = "exactly"
if not has_kwarg:
num_args += num_kwds
@@ -714,13 +717,13 @@
class ArgErrUnknownKwds(ArgErr):
- def __init__(self, space, num_remainingkwds, keywords, used_keywords,
+ def __init__(self, space, num_remainingkwds, keywords, kwds_mapping,
keyword_names_w):
name = ''
self.num_kwds = num_remainingkwds
if num_remainingkwds == 1:
for i in range(len(keywords)):
- if not used_keywords[i]:
+ if i not in kwds_mapping:
name = keywords[i]
if name is None:
# We'll assume it's unicode. Encode it.
diff --git a/pypy/interpreter/test/test_argument.py b/pypy/interpreter/test/test_argument.py
--- a/pypy/interpreter/test/test_argument.py
+++ b/pypy/interpreter/test/test_argument.py
@@ -57,6 +57,9 @@
def __nonzero__(self):
raise NotImplementedError
+class kwargsdict(dict):
+ pass
+
class DummySpace(object):
def newtuple(self, items):
return tuple(items)
@@ -76,9 +79,13 @@
return list(it)
def view_as_kwargs(self, x):
+ if len(x) == 0:
+ return [], []
return None, None
def newdict(self, kwargs=False):
+ if kwargs:
+ return kwargsdict()
return {}
def newlist(self, l=[]):
@@ -299,6 +306,22 @@
args._match_signature(None, l, Signature(["a", "b", "c"], None, "**"))
assert l == [1, 2, 3, {'d': 4}]
+ def test_match_kwds_creates_kwdict(self):
+ space = DummySpace()
+ kwds = [("c", 3), ('d', 4)]
+ for i in range(4):
+ kwds_w = dict(kwds[:i])
+ keywords = kwds_w.keys()
+ keywords_w = kwds_w.values()
+ w_kwds = dummy_wrapped_dict(kwds[i:])
+ if i == 3:
+ w_kwds = None
+ args = Arguments(space, [1, 2], keywords, keywords_w, w_starstararg=w_kwds)
+ l = [None, None, None, None]
+ args._match_signature(None, l, Signature(["a", "b", "c"], None, "**"))
+ assert l == [1, 2, 3, {'d': 4}]
+ assert isinstance(l[-1], kwargsdict)
+
def test_duplicate_kwds(self):
space = DummySpace()
excinfo = py.test.raises(OperationError, Arguments, space, [], ["a"],
@@ -546,34 +569,47 @@
def test_missing_args(self):
# got_nargs, nkwds, expected_nargs, has_vararg, has_kwarg,
# defaults_w, missing_args
- err = ArgErrCount(1, 0, 0, False, False, None, 0)
+ sig = Signature([], None, None)
+ err = ArgErrCount(1, 0, sig, None, 0)
s = err.getmsg()
assert s == "takes no arguments (1 given)"
- err = ArgErrCount(0, 0, 1, False, False, [], 1)
+
+ sig = Signature(['a'], None, None)
+ err = ArgErrCount(0, 0, sig, [], 1)
s = err.getmsg()
assert s == "takes exactly 1 argument (0 given)"
- err = ArgErrCount(3, 0, 2, False, False, [], 0)
+
+ sig = Signature(['a', 'b'], None, None)
+ err = ArgErrCount(3, 0, sig, [], 0)
s = err.getmsg()
assert s == "takes exactly 2 arguments (3 given)"
- err = ArgErrCount(3, 0, 2, False, False, ['a'], 0)
+ err = ArgErrCount(3, 0, sig, ['a'], 0)
s = err.getmsg()
assert s == "takes at most 2 arguments (3 given)"
- err = ArgErrCount(1, 0, 2, True, False, [], 1)
+
+ sig = Signature(['a', 'b'], '*', None)
+ err = ArgErrCount(1, 0, sig, [], 1)
s = err.getmsg()
assert s == "takes at least 2 arguments (1 given)"
- err = ArgErrCount(0, 1, 2, True, False, ['a'], 1)
+ err = ArgErrCount(0, 1, sig, ['a'], 1)
s = err.getmsg()
assert s == "takes at least 1 non-keyword argument (0 given)"
- err = ArgErrCount(2, 1, 1, False, True, [], 0)
+
+ sig = Signature(['a'], None, '**')
+ err = ArgErrCount(2, 1, sig, [], 0)
s = err.getmsg()
assert s == "takes exactly 1 non-keyword argument (2 given)"
- err = ArgErrCount(0, 1, 1, False, True, [], 1)
+ err = ArgErrCount(0, 1, sig, [], 1)
s = err.getmsg()
assert s == "takes exactly 1 non-keyword argument (0 given)"
- err = ArgErrCount(0, 1, 1, True, True, [], 1)
+
+ sig = Signature(['a'], '*', '**')
+ err = ArgErrCount(0, 1, sig, [], 1)
s = err.getmsg()
assert s == "takes at least 1 non-keyword argument (0 given)"
- err = ArgErrCount(2, 1, 1, False, True, ['a'], 0)
+
+ sig = Signature(['a'], None, '**')
+ err = ArgErrCount(2, 1, sig, ['a'], 0)
s = err.getmsg()
assert s == "takes at most 1 non-keyword argument (2 given)"
@@ -596,11 +632,14 @@
def test_unknown_keywords(self):
space = DummySpace()
- err = ArgErrUnknownKwds(space, 1, ['a', 'b'], [True, False], None)
+ err = ArgErrUnknownKwds(space, 1, ['a', 'b'], [0], None)
s = err.getmsg()
assert s == "got an unexpected keyword argument 'b'"
+ err = ArgErrUnknownKwds(space, 1, ['a', 'b'], [1], None)
+ s = err.getmsg()
+ assert s == "got an unexpected keyword argument 'a'"
err = ArgErrUnknownKwds(space, 2, ['a', 'b', 'c'],
- [True, False, False], None)
+ [0], None)
s = err.getmsg()
assert s == "got 2 unexpected keyword arguments"
@@ -610,7 +649,7 @@
defaultencoding = 'utf-8'
space = DummySpaceUnicode()
err = ArgErrUnknownKwds(space, 1, ['a', None, 'b', 'c'],
- [True, False, True, True],
+ [0, 3, 2],
[unichr(0x1234), u'b', u'c'])
s = err.getmsg()
assert s == "got an unexpected keyword argument '\xe1\x88\xb4'"
diff --git a/pypy/jit/tl/pypyjit_demo.py b/pypy/jit/tl/pypyjit_demo.py
--- a/pypy/jit/tl/pypyjit_demo.py
+++ b/pypy/jit/tl/pypyjit_demo.py
@@ -1,19 +1,27 @@
import pypyjit
pypyjit.set_param(threshold=200)
+kwargs = {"z": 1}
-def g(*args):
- return len(args)
+def f(*args, **kwargs):
+ result = g(1, *args, **kwargs)
+ return result + 2
-def f(n):
- s = 0
- for i in range(n):
- l = [i, n, 2]
- s += g(*l)
- return s
+def g(x, y, z=2):
+ return x - y + z
+
+def main():
+ res = 0
+ i = 0
+ while i < 10000:
+ res = f(res, z=i)
+ g(1, res, **kwargs)
+ i += 1
+ return res
+
try:
- print f(301)
+ print main()
except Exception, e:
print "Exception: ", type(e)
diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py
--- a/pypy/objspace/std/dictmultiobject.py
+++ b/pypy/objspace/std/dictmultiobject.py
@@ -50,8 +50,8 @@
elif kwargs:
assert w_type is None
- from pypy.objspace.std.kwargsdict import KwargsDictStrategy
- strategy = space.fromcache(KwargsDictStrategy)
+ from pypy.objspace.std.kwargsdict import EmptyKwargsDictStrategy
+ strategy = space.fromcache(EmptyKwargsDictStrategy)
else:
strategy = space.fromcache(EmptyDictStrategy)
if w_type is None:
@@ -591,6 +591,17 @@
def wrapkey(space, key):
return space.wrap(key)
+ def view_as_kwargs(self, w_dict):
+ d = self.unerase(w_dict.dstorage)
+ l = len(d)
+ keys, values = [None] * l, [None] * l
+ i = 0
+ for key, val in d.iteritems():
+ keys[i] = key
+ values[i] = val
+ i += 1
+ return keys, values
+
create_itertor_classes(StringDictStrategy)
diff --git a/pypy/objspace/std/kwargsdict.py b/pypy/objspace/std/kwargsdict.py
--- a/pypy/objspace/std/kwargsdict.py
+++ b/pypy/objspace/std/kwargsdict.py
@@ -4,10 +4,19 @@
from pypy.rlib import rerased, jit
from pypy.objspace.std.dictmultiobject import (DictStrategy,
create_itertor_classes,
+ EmptyDictStrategy,
ObjectDictStrategy,
StringDictStrategy)
+class EmptyKwargsDictStrategy(EmptyDictStrategy):
+ def switch_to_string_strategy(self, w_dict):
+ strategy = self.space.fromcache(KwargsDictStrategy)
+ storage = strategy.get_empty_storage()
+ w_dict.strategy = strategy
+ w_dict.dstorage = storage
+
+
class KwargsDictStrategy(DictStrategy):
erase, unerase = rerased.new_erasing_pair("kwargsdict")
erase = staticmethod(erase)
@@ -142,7 +151,8 @@
w_dict.dstorage = storage
def view_as_kwargs(self, w_dict):
- return self.unerase(w_dict.dstorage)
+ keys, values_w = self.unerase(w_dict.dstorage)
+ return keys[:], values_w[:] # copy to make non-resizable
def getiterkeys(self, w_dict):
return self.unerase(w_dict.dstorage)[0]
diff --git a/pypy/objspace/std/test/test_dictmultiobject.py b/pypy/objspace/std/test/test_dictmultiobject.py
--- a/pypy/objspace/std/test/test_dictmultiobject.py
+++ b/pypy/objspace/std/test/test_dictmultiobject.py
@@ -889,6 +889,9 @@
return W_DictMultiObject.allocate_and_init_instance(
self, module=module, instance=instance)
+ def view_as_kwargs(self, w_d):
+ return w_d.view_as_kwargs() # assume it's a multidict
+
def finditem_str(self, w_dict, s):
return w_dict.getitem_str(s) # assume it's a multidict
@@ -1105,6 +1108,10 @@
assert self.impl.getitem(s) == 1000
assert s.unwrapped
+ def test_view_as_kwargs(self):
+ self.fill_impl()
+ assert self.fakespace.view_as_kwargs(self.impl) == (["fish", "fish2"], [1000, 2000])
+
## class TestMeasuringDictImplementation(BaseTestRDictImplementation):
## ImplementionClass = MeasuringDictImplementation
## DevolvedClass = MeasuringDictImplementation
diff --git a/pypy/objspace/std/test/test_kwargsdict.py b/pypy/objspace/std/test/test_kwargsdict.py
--- a/pypy/objspace/std/test/test_kwargsdict.py
+++ b/pypy/objspace/std/test/test_kwargsdict.py
@@ -86,6 +86,27 @@
d = W_DictMultiObject(space, strategy, storage)
w_l = d.w_keys() # does not crash
+def test_view_as_kwargs():
+ from pypy.objspace.std.dictmultiobject import EmptyDictStrategy
+ strategy = KwargsDictStrategy(space)
+ keys = ["a", "b", "c"]
+ values = [1, 2, 3]
+ storage = strategy.erase((keys, values))
+ d = W_DictMultiObject(space, strategy, storage)
+ assert (space.view_as_kwargs(d) == keys, values)
+
+ strategy = EmptyDictStrategy(space)
+ storage = strategy.get_empty_storage()
+ d = W_DictMultiObject(space, strategy, storage)
+ assert (space.view_as_kwargs(d) == [], [])
+
+def test_from_empty_to_kwargs():
+ strategy = EmptyKwargsDictStrategy(space)
+ storage = strategy.get_empty_storage()
+ d = W_DictMultiObject(space, strategy, storage)
+ d.setitem_str("a", 3)
+ assert isinstance(d.strategy, KwargsDictStrategy)
+
from pypy.objspace.std.test.test_dictmultiobject import BaseTestRDictImplementation, BaseTestDevolvedDictImplementation
def get_impl(self):
@@ -117,4 +138,6 @@
return args
d = f(a=1)
assert "KwargsDictStrategy" in self.get_strategy(d)
+ d = f()
+ assert "EmptyKwargsDictStrategy" in self.get_strategy(d)
diff --git a/pypy/rlib/jit.py b/pypy/rlib/jit.py
--- a/pypy/rlib/jit.py
+++ b/pypy/rlib/jit.py
@@ -148,6 +148,8 @@
thing._annspecialcase_ = "specialize:call_location"
args = _get_args(func)
+ predicateargs = _get_args(predicate)
+ assert len(args) == len(predicateargs), "%s and predicate %s need the same numbers of arguments" % (func, predicate)
d = {
"dont_look_inside": dont_look_inside,
"predicate": predicate,
More information about the pypy-commit
mailing list