[pypy-svn] r68353 - in pypy/branch/improve-kwd-args/pypy: annotation interpreter interpreter/test module/__builtin__ objspace/flow rpython

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Oct 12 22:23:57 CEST 2009


Author: cfbolz
Date: Mon Oct 12 22:23:56 2009
New Revision: 68353

Modified:
   pypy/branch/improve-kwd-args/pypy/annotation/bookkeeper.py
   pypy/branch/improve-kwd-args/pypy/interpreter/argument.py
   pypy/branch/improve-kwd-args/pypy/interpreter/gateway.py
   pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py
   pypy/branch/improve-kwd-args/pypy/interpreter/test/test_compiler.py
   pypy/branch/improve-kwd-args/pypy/interpreter/test/test_function.py
   pypy/branch/improve-kwd-args/pypy/interpreter/test/test_gateway.py
   pypy/branch/improve-kwd-args/pypy/module/__builtin__/functional.py
   pypy/branch/improve-kwd-args/pypy/objspace/flow/objspace.py
   pypy/branch/improve-kwd-args/pypy/rpython/callparse.py
   pypy/branch/improve-kwd-args/pypy/rpython/rbuiltin.py
Log:
make Arguments immutable again and fix variaous breakages


Modified: pypy/branch/improve-kwd-args/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/annotation/bookkeeper.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/annotation/bookkeeper.py	Mon Oct 12 22:23:56 2009
@@ -762,6 +762,7 @@
             getattr(s_obj, 'from_ellipsis', False)):    # see newtuple()
             return [Ellipsis]
         raise CallPatternTooComplex, "'*' argument must be SomeTuple"
+    viewiterable = unpackiterable
 
     def is_w(self, one, other):
         return one is other

Modified: pypy/branch/improve-kwd-args/pypy/interpreter/argument.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/argument.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/argument.py	Mon Oct 12 22:23:56 2009
@@ -30,12 +30,6 @@
         make_sure_not_resized(self.arguments_w)
         self._combine_wrapped(w_stararg, w_starstararg)
 
-    @staticmethod
-    def factory(space, args_w, kwds_w=None,
-                w_stararg=None, w_starstararg=None):
-        return Arguments(space, args_w, kwds_w,
-                         w_stararg, w_starstararg)
-
     def num_args(self): # only used in module/__builtin__/interp_classobj.py
         return len(self.arguments_w)
 
@@ -64,8 +58,8 @@
 
     def prepend(self, w_firstarg): # used often
         "Return a new Arguments with a new argument inserted first."
-        return self.factory(self.space, [w_firstarg] + self.arguments_w,
-                            self.keywords, self.keywords_w)
+        return Arguments(self.space, [w_firstarg] + self.arguments_w,
+                         self.keywords, self.keywords_w)
             
     def _combine_wrapped(self, w_stararg, w_starstararg):
         "unpack the *arg and **kwd into arguments_w and keywords_w"
@@ -159,7 +153,6 @@
             upfront = 0
         
         args_w = self.arguments_w
-        assert args_w is not None, "tried to match arguments again"
         num_args = len(args_w)
 
         keywords = self.keywords
@@ -184,7 +177,9 @@
         # 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:
+            used_keywords = [False] * num_kwds
             for i in range(num_kwds):
                 name = keywords[i]
                 try:
@@ -202,7 +197,7 @@
                 else:
                     assert scope_w[j] is None
                     scope_w[j] = keywords_w[i]
-                    keywords[i] = None # mark as used
+                    used_keywords[i] = True # mark as used
                     num_remainingkwds -= 1
         missing = 0
         if input_argcount < co_argcount:
@@ -242,21 +237,18 @@
             w_kwds = self.space.newdict()
             if num_remainingkwds:
                 for i in range(len(keywords)):
-                    key = keywords[i]
-                    if key is not None:
+                    if not used_keywords[i]:
+                        key = keywords[i]
                         self.space.setitem(w_kwds, self.space.wrap(key), keywords_w[i])
             scope_w[co_argcount + has_vararg] = w_kwds
         elif num_remainingkwds:
-            raise ArgErrUnknownKwds(num_remainingkwds, keywords)
+            raise ArgErrUnknownKwds(num_remainingkwds, keywords, used_keywords)
 
         if missing:
             raise ArgErrCount(avail, num_kwds,
                               (co_argcount, has_vararg, has_kwarg),
                               defaults_w, missing)
 
-        self.arguments_w = None
-        self.keywords = None
-        self.keywords_w = None
         return co_argcount + has_vararg + has_kwarg
     
 
@@ -358,17 +350,6 @@
                          data_w[shape_cnt:end_keys], w_star,
                           w_starstar)
 
-    def copy(self):
-        if self.keywords is None:
-            keywords = None
-            keywords_w = None
-        else:
-            keywords = self.keywords[:]
-            keywords_w = self.keywords_w[:]
-        return Arguments(self.space, self.arguments_w[:], keywords,
-                         keywords_w)
-
-
 class ArgumentsForTranslation(Arguments):
     def __init__(self, space, args_w, keywords=None, keywords_w=None,
                  w_stararg=None, w_starstararg=None):
@@ -383,23 +364,19 @@
         self._combine_wrapped(self.w_stararg, self.w_starstararg)
         self.combine_has_happened = True
 
-    @staticmethod
-    def factory(space, args_w, kwds_w=None,
-                w_stararg=None, w_starstararg=None):
-        return ArgumentsForTranslation(space, args_w, kwds_w,
-                                       w_stararg, w_starstararg)
+    def prepend(self, w_firstarg): # used often
+        "Return a new Arguments with a new argument inserted first."
+        return ArgumentsForTranslation(self.space, [w_firstarg] + self.arguments_w,
+                                       self.keywords, self.keywords_w, self.w_stararg,
+                                       self.w_starstararg)
 
     def copy(self):
-        if self.keywords is None:
-            keywords = None
-            keywords_w = None
-        else:
-            keywords = self.keywords[:]
-            keywords_w = self.keywords_w[:]
-        return ArgumentsForTranslation(self.space, self.arguments_w[:], keywords,
-                                       keywords_w, self.w_stararg,
+        return ArgumentsForTranslation(self.space, self.arguments_w,
+                                       self.keywords, self.keywords_w, self.w_stararg,
                                        self.w_starstararg)
 
+
+            
     def _match_signature(self, w_firstarg, scope_w, argnames, has_vararg=False,
                          has_kwarg=False, defaults_w=[], blindargs=0):
         self.combine_if_necessary()
@@ -598,13 +575,13 @@
 
 class ArgErrUnknownKwds(ArgErr):
 
-    def __init__(self, num_remainingkwds, keywords):
+    def __init__(self, num_remainingkwds, keywords, used_keywords):
         self.kwd_name = ''
         self.num_kwds = num_remainingkwds
         if num_remainingkwds == 1:
-            for kwd_name in keywords:
-                if kwd_name is not None:
-                    self.kwd_name = kwd_name
+            for i in range(len(keywords)):
+                if not used_keywords[i]:
+                    self.kwd_name = keywords[i]
                     break
 
     def getmsg(self, fnname):

Modified: pypy/branch/improve-kwd-args/pypy/interpreter/gateway.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/gateway.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/gateway.py	Mon Oct 12 22:23:56 2009
@@ -870,10 +870,11 @@
                 else:
                     # ...which is merged with the previous arguments, if any
                     if len(args_w) > 1:
+                        # XXXXXXXXX
                         more_args_w, more_kwds_w = args.unpack()
                         args = Arguments(space,
                                          list(args_w[:-1]) + more_args_w,
-                                         more_kwds_w)
+                                         more_kwds_w.keys(), more_kwds_w.values())
             w_func = self.wget(space, name) 
             return space.call_args(w_func, args)
         def get_function(space):

Modified: pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/test/test_argument.py	Mon Oct 12 22:23:56 2009
@@ -241,43 +241,43 @@
         space = DummySpace()
         args = make_arguments_for_translation(space, [1,2,3])
         sig = (['a', 'b', 'c'], None, None)
-        data = args.copy().match_signature(sig, [])
+        data = args.match_signature(sig, [])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1])
         sig = (['a', 'b', 'c'], None, None)
-        data = args.copy().match_signature(sig, [2, 3])
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1,2,3,4,5])
         sig = (['a', 'b', 'c'], 'r', None)
-        data = args.copy().match_signature(sig, [])
+        data = args.match_signature(sig, [])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1], {'c': 3, 'b': 2})
         sig = (['a', 'b', 'c'], None, None)
-        data = args.copy().match_signature(sig, [])
+        data = args.match_signature(sig, [])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1], {'c': 5})
         sig = (['a', 'b', 'c'], None, None)
-        data = args.copy().match_signature(sig, [2, 3])
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1], {'c': 5, 'd': 7})
         sig = (['a', 'b', 'c'], None, 'kw')
-        data = args.copy().match_signature(sig, [2, 3])
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
         args = make_arguments_for_translation(space, [1,2,3,4,5], {'e': 5, 'd': 7})
         sig = (['a', 'b', 'c'], 'r', 'kw')
-        data = args.copy().match_signature(sig, [2, 3])
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
@@ -285,7 +285,7 @@
                                        w_stararg=[1],
                                        w_starstararg={'c': 5, 'd': 7})
         sig = (['a', 'b', 'c'], None, 'kw')
-        data = args.copy().match_signature(sig, [2, 3])
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 
@@ -293,7 +293,7 @@
                                        w_stararg=[3,4,5],
                                        w_starstararg={'e': 5, 'd': 7})
         sig = (['a', 'b', 'c'], 'r', 'kw')
-        data = args.copy().match_signature(sig, [2, 3])
+        data = args.match_signature(sig, [2, 3])
         new_args = args.unmatch_signature(sig, data)
         assert args.unpack() == new_args.unpack()
 

Modified: pypy/branch/improve-kwd-args/pypy/interpreter/test/test_compiler.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/test/test_compiler.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/test/test_compiler.py	Mon Oct 12 22:23:56 2009
@@ -330,8 +330,8 @@
         space = self.space
         w_mod = space.appexec((), '():\n import warnings\n return warnings\n') #sys.getmodule('warnings')
         w_filterwarnings = space.getattr(w_mod, space.wrap('filterwarnings'))
-        filter_arg = Arguments(space, [ space.wrap('error') ],
-                       dict(module=space.wrap('<tmp>')))
+        filter_arg = Arguments(space, [ space.wrap('error') ], ["module"],
+                               [space.wrap("<tmp>")])
 
         for code in ('''
 def wrong1():

Modified: pypy/branch/improve-kwd-args/pypy/interpreter/test/test_function.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/test/test_function.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/test/test_function.py	Mon Oct 12 22:23:56 2009
@@ -480,20 +480,20 @@
         w_meth1 = descr_function_get(space, func, obj1, space.type(obj1))
         meth1 = space.unwrap(w_meth1)
         assert isinstance(meth1, Method)
-        assert meth1.call_args(args.copy()) == obj1
+        assert meth1.call_args(args) == obj1
         # Check method returned from method.__get__()
         # --- meth1 is already bound so meth1.__get__(*) is meth1.
         w_meth2 = meth1.descr_method_get(obj2, space.type(obj2))
         meth2 = space.unwrap(w_meth2)
         assert isinstance(meth2, Method)
-        assert meth2.call_args(args.copy()) == obj1
+        assert meth2.call_args(args) == obj1
         # Check method returned from unbound_method.__get__()
         w_meth3 = descr_function_get(space, func, None, space.type(obj2))
         meth3 = space.unwrap(w_meth3)
         w_meth4 = meth3.descr_method_get(obj2, space.w_None)
         meth4 = space.unwrap(w_meth4)
         assert isinstance(meth4, Method)
-        assert meth4.call_args(args.copy()) == obj2
+        assert meth4.call_args(args) == obj2
         # Check method returned from unbound_method.__get__()
         # --- with an incompatible class
         w_meth5 = meth3.descr_method_get(space.wrap('hello'), space.w_str)

Modified: pypy/branch/improve-kwd-args/pypy/interpreter/test/test_gateway.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/interpreter/test/test_gateway.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/interpreter/test/test_gateway.py	Mon Oct 12 22:23:56 2009
@@ -77,7 +77,7 @@
                                                    gateway.W_Root,
                                                    gateway.Arguments])
         w = self.space.wrap
-        args = argument.Arguments(self.space, [w(123), w(23)], {},
+        args = argument.Arguments(self.space, [w(123), w(23)], [], [],
                                   w_stararg = w((0, True)),
                                   w_starstararg = w({'boo': 10}))
         w_result = code.funcrun(FakeFunc(self.space, "c"), args)
@@ -116,8 +116,7 @@
         gg = gateway.app2interp_temp(app_general)
         args = gateway.Arguments(self.space, [w(6), w(7)])
         assert self.space.int_w(gg(self.space, w(3), args)) == 23
-        args = gateway.Arguments(self.space, [w(6)], {'hello': w(7),
-                                                      'world': w(8)})
+        args = gateway.Arguments(self.space, [w(6)], ['hello', 'world'], [w(7), w(8)])
         assert self.space.int_w(gg(self.space, w(3), args)) == 213
 
     def test_interp2app(self):

Modified: pypy/branch/improve-kwd-args/pypy/module/__builtin__/functional.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/module/__builtin__/functional.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/module/__builtin__/functional.py	Mon Oct 12 22:23:56 2009
@@ -121,7 +121,7 @@
         w_key = kwargs["key"]
     except KeyError:
         w_key = None
-    if len(kwargs) != 1:
+    if len(kwargs) > 1:
         msg = "%s() got unexpected keyword argument" % (implementation_of,)
         raise OperationError(space.w_TypeError, space.wrap(msg))
     w_iter = space.iter(w_sequence)

Modified: pypy/branch/improve-kwd-args/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/objspace/flow/objspace.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/objspace/flow/objspace.py	Mon Oct 12 22:23:56 2009
@@ -360,6 +360,12 @@
         return self.do_operation_with_implicit_exceptions('setitem', w_obj, 
                                                           w_key, w_val)
 
+    def call_function(self, w_func, *args_w):
+        from pypy.interpreter.argument import ArgumentsForTranslation
+        nargs = len(args_w)
+        args = ArgumentsForTranslation(self, list(args_w))
+        return self.call_args(w_func, args)
+
     def call_args(self, w_callable, args):
         try:
             fn = self.unwrap(w_callable)

Modified: pypy/branch/improve-kwd-args/pypy/rpython/callparse.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/rpython/callparse.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/rpython/callparse.py	Mon Oct 12 22:23:56 2009
@@ -178,6 +178,7 @@
                 raise ValueError
             return list(items)
         raise CallPatternTooComplex, "'*' argument must be a tuple"
+    viewiterable = unpackiterable
 
     def is_w(self, one, other):
         return one is other

Modified: pypy/branch/improve-kwd-args/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/branch/improve-kwd-args/pypy/rpython/rbuiltin.py	(original)
+++ pypy/branch/improve-kwd-args/pypy/rpython/rbuiltin.py	Mon Oct 12 22:23:56 2009
@@ -52,9 +52,10 @@
 
 def call_args_expand(hop, takes_kwds = True):
     hop = hop.copy()
-    from pypy.interpreter.argument import Arguments
-    arguments = Arguments.fromshape(None, hop.args_s[1].const, # shape
-                                    range(hop.nb_args-2))
+    from pypy.interpreter.argument import ArgumentsForTranslation
+    arguments = ArgumentsForTranslation.fromshape(
+            None, hop.args_s[1].const, # shape
+            range(hop.nb_args-2))
     if arguments.w_starstararg is not None:
         raise TyperError("**kwds call not implemented")
     if arguments.w_stararg is not None:
@@ -75,12 +76,13 @@
             hop.args_s.append(s_tuple.items[i])
             hop.args_r.append(r_tuple.items_r[i])
 
-    kwds = arguments.kwds_w or {}
-    if not takes_kwds and kwds:
+    keywords = arguments.keywords
+    if not takes_kwds and keywords:
         raise TyperError("kwds args not supported")
     # prefix keyword arguments with 'i_'
     kwds_i = {}
-    for key, index in kwds.items():
+    for i, key in enumerate(keywords):
+        index = arguments.keywords_w[i]
         kwds_i['i_'+key] = index
 
     return hop, kwds_i



More information about the Pypy-commit mailing list