Author: Ronan Lamy <[email protected]>
Branch: kill-flowobjspace
Changeset: r60733:c0328610075e
Date: 2013-01-28 04:17 +0000
http://bitbucket.org/pypy/pypy/changeset/c0328610075e/

Log:    move ArgumentsForTranslation and friends to rpython/annotator/

diff --git a/rpython/flowspace/argument.py b/rpython/annotator/argument.py
copy from rpython/flowspace/argument.py
copy to rpython/annotator/argument.py
--- a/rpython/flowspace/argument.py
+++ b/rpython/annotator/argument.py
@@ -1,77 +1,6 @@
 """
 Arguments objects.
 """
-
-class Signature(object):
-    _immutable_ = True
-    _immutable_fields_ = ["argnames[*]"]
-    __slots__ = ("argnames", "varargname", "kwargname")
-
-    def __init__(self, argnames, varargname=None, kwargname=None):
-        self.argnames = argnames
-        self.varargname = varargname
-        self.kwargname = kwargname
-
-    def find_argname(self, name):
-        try:
-            return self.argnames.index(name)
-        except ValueError:
-            return -1
-
-    def num_argnames(self):
-        return len(self.argnames)
-
-    def has_vararg(self):
-        return self.varargname is not None
-
-    def has_kwarg(self):
-        return self.kwargname is not None
-
-    def scope_length(self):
-        scopelen = len(self.argnames)
-        scopelen += self.has_vararg()
-        scopelen += self.has_kwarg()
-        return scopelen
-
-    def getallvarnames(self):
-        argnames = self.argnames
-        if self.varargname is not None:
-            argnames = argnames + [self.varargname]
-        if self.kwargname is not None:
-            argnames = argnames + [self.kwargname]
-        return argnames
-
-    def __repr__(self):
-        return "Signature(%r, %r, %r)" % (
-                self.argnames, self.varargname, self.kwargname)
-
-    def __eq__(self, other):
-        if not isinstance(other, Signature):
-            return NotImplemented
-        return (self.argnames == other.argnames and
-                self.varargname == other.varargname and
-                self.kwargname == other.kwargname)
-
-    def __ne__(self, other):
-        if not isinstance(other, Signature):
-            return NotImplemented
-        return not self == other
-
-
-    # make it look tuply for its use in the annotator
-
-    def __len__(self):
-        return 3
-
-    def __getitem__(self, i):
-        if i == 0:
-            return self.argnames
-        if i == 1:
-            return self.varargname
-        if i == 2:
-            return self.kwargname
-        raise IndexError
-
 class ArgumentsForTranslation(object):
     def __init__(self, space, args_w, keywords=None, keywords_w=None,
                  w_stararg=None, w_starstararg=None):
@@ -374,41 +303,6 @@
     return args._rawshape(nextra)
 
 
-class CallSpec(object):
-    """Represents the arguments passed into a function call, i.e. the
-    `a, b, *c, **d` part in `return func(a, b, *c, **d)`.
-    """
-    def __init__(self, space, args_w, keywords=None, w_stararg=None,
-            w_starstararg=None):
-        self.w_stararg = w_stararg
-        assert w_starstararg is None, "No **-unpacking in RPython"
-        self.combine_has_happened = False
-        self.space = space
-        assert isinstance(args_w, list)
-        self.arguments_w = args_w
-        self.keywords = keywords or {}
-
-    def unpack(self):
-        "Return a ([w1,w2...], {'kw':w3...}) pair."
-        if self.w_stararg is not None:
-            stargs_w = self.space.unpackiterable(self.w_stararg)
-            args_w = self.arguments_w + stargs_w
-        else:
-            args_w = self.arguments_w
-        return args_w, self.keywords
-
-    def flatten(self):
-        """ Argument <-> list of w_objects together with "shape" information 
"""
-        shape_cnt  = len(self.arguments_w)    # Number of positional args
-        shape_keys = tuple(sorted(self.keywords))
-        shape_star = self.w_stararg is not None   # Flag: presence of *arg
-        shape_stst = False # Flag: presence of **kwds
-        data_w = self.arguments_w + [self.keywords[key] for key in shape_keys]
-        if shape_star:
-            data_w.append(self.w_stararg)
-        return (shape_cnt, shape_keys, shape_star, shape_stst), data_w
-
-
 #
 # ArgErr family of exceptions raised in case of argument mismatch.
 # We try to give error messages following CPython's, which are very 
informative.
diff --git a/rpython/annotator/bookkeeper.py b/rpython/annotator/bookkeeper.py
--- a/rpython/annotator/bookkeeper.py
+++ b/rpython/annotator/bookkeeper.py
@@ -19,7 +19,7 @@
 from rpython.annotator.dictdef import DictDef
 from rpython.annotator import description
 from rpython.annotator.signature import annotationoftype
-from rpython.flowspace.argument import ArgumentsForTranslation
+from rpython.annotator.argument import ArgumentsForTranslation
 from rpython.rlib.objectmodel import r_dict, Symbolic
 from rpython.tool.algo.unionfind import UnionFind
 from rpython.rtyper.lltypesystem import lltype, llmemory
diff --git a/rpython/annotator/description.py b/rpython/annotator/description.py
--- a/rpython/annotator/description.py
+++ b/rpython/annotator/description.py
@@ -3,7 +3,7 @@
 from rpython.annotator.signature import enforce_signature_args, 
enforce_signature_return
 from rpython.flowspace.model import Constant, FunctionGraph
 from rpython.flowspace.bytecode import cpython_code_signature
-from rpython.flowspace.argument import rawshape, ArgErr
+from rpython.annotator.argument import rawshape, ArgErr
 from rpython.tool.sourcetools import valid_identifier
 from rpython.tool.pairtype import extendabletype
 
diff --git a/rpython/flowspace/test/test_argument.py 
b/rpython/annotator/test/test_argument.py
copy from rpython/flowspace/test/test_argument.py
copy to rpython/annotator/test/test_argument.py
--- a/rpython/flowspace/test/test_argument.py
+++ b/rpython/annotator/test/test_argument.py
@@ -1,55 +1,7 @@
 # -*- coding: utf-8 -*-
 import py
-from rpython.flowspace.argument import (ArgumentsForTranslation, rawshape,
-    Signature)
-
-
-class TestSignature(object):
-    def test_helpers(self):
-        sig = Signature(["a", "b", "c"], None, None)
-        assert sig.num_argnames() == 3
-        assert not sig.has_vararg()
-        assert not sig.has_kwarg()
-        assert sig.scope_length() == 3
-        assert sig.getallvarnames() == ["a", "b", "c"]
-        sig = Signature(["a", "b", "c"], "c", None)
-        assert sig.num_argnames() == 3
-        assert sig.has_vararg()
-        assert not sig.has_kwarg()
-        assert sig.scope_length() == 4
-        assert sig.getallvarnames() == ["a", "b", "c", "c"]
-        sig = Signature(["a", "b", "c"], None, "c")
-        assert sig.num_argnames() == 3
-        assert not sig.has_vararg()
-        assert sig.has_kwarg()
-        assert sig.scope_length() == 4
-        assert sig.getallvarnames() == ["a", "b", "c", "c"]
-        sig = Signature(["a", "b", "c"], "d", "c")
-        assert sig.num_argnames() == 3
-        assert sig.has_vararg()
-        assert sig.has_kwarg()
-        assert sig.scope_length() == 5
-        assert sig.getallvarnames() == ["a", "b", "c", "d", "c"]
-
-    def test_eq(self):
-        sig1 = Signature(["a", "b", "c"], "d", "c")
-        sig2 = Signature(["a", "b", "c"], "d", "c")
-        assert sig1 == sig2
-
-
-    def test_find_argname(self):
-        sig = Signature(["a", "b", "c"], None, None)
-        assert sig.find_argname("a") == 0
-        assert sig.find_argname("b") == 1
-        assert sig.find_argname("c") == 2
-        assert sig.find_argname("d") == -1
-
-    def test_tuply(self):
-        sig = Signature(["a", "b", "c"], "d", "e")
-        x, y, z = sig
-        assert x == ["a", "b", "c"]
-        assert y == "d"
-        assert z == "e"
+from rpython.annotator.argument import ArgumentsForTranslation, rawshape
+from rpython.flowspace.argument import Signature
 
 class dummy_wrapped_dict(dict):
     def __nonzero__(self):
diff --git a/rpython/flowspace/argument.py b/rpython/flowspace/argument.py
--- a/rpython/flowspace/argument.py
+++ b/rpython/flowspace/argument.py
@@ -72,307 +72,6 @@
             return self.kwargname
         raise IndexError
 
-class ArgumentsForTranslation(object):
-    def __init__(self, space, args_w, keywords=None, keywords_w=None,
-                 w_stararg=None, w_starstararg=None):
-        self.w_stararg = w_stararg
-        self.w_starstararg = w_starstararg
-        self.combine_has_happened = False
-        self.space = space
-        assert isinstance(args_w, list)
-        self.arguments_w = args_w
-        self.keywords = keywords
-        self.keywords_w = keywords_w
-        self.keyword_names_w = None
-
-    def __repr__(self):
-        """ NOT_RPYTHON """
-        name = self.__class__.__name__
-        if not self.keywords:
-            return '%s(%s)' % (name, self.arguments_w,)
-        else:
-            return '%s(%s, %s, %s)' % (name, 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"
-        if w_stararg is not None:
-            self._combine_starargs_wrapped(w_stararg)
-        if w_starstararg is not None:
-            self._combine_starstarargs_wrapped(w_starstararg)
-
-    def _combine_starargs_wrapped(self, w_stararg):
-        # unpack the * arguments
-        space = self.space
-        args_w = space.unpackiterable(w_stararg)
-        self.arguments_w = self.arguments_w + args_w
-
-    def _combine_starstarargs_wrapped(self, w_starstararg):
-        # unpack the ** arguments
-        space = self.space
-        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
-                self.keywords_w = values_w
-            else:
-                if set(keywords) & set(self.keywords):
-                    raise TypeError("got multiple values for keyword arguments 
'%s'", set(keywords) & set(self.keywords))
-                self.keywords = self.keywords + keywords
-                self.keywords_w = self.keywords_w + values_w
-            return
-        if space.isinstance_w(w_starstararg, space.w_dict):
-            keys_w = space.unpackiterable(w_starstararg)
-        else:
-            w_keys = space.call_method(w_starstararg, "keys")
-            keys_w = space.unpackiterable(w_keys)
-        keywords_w = [None] * len(keys_w)
-        keywords = [None] * len(keys_w)
-        for i, w_key in enumerate(keys_w):
-            key = space.str_w(w_key)
-            if key in self.keywords:
-                raise TypeError("got multiple values for keyword argument 
'%s'" % key)
-            keywords[i] = key
-            keywords_w[i] = space.getitem(w_starstararg, w_key)
-        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
-
-
-    def fixedunpack(self, argcount):
-        """The simplest argument parsing: get the 'argcount' arguments,
-        or raise a real ValueError if the length is wrong."""
-        if self.keywords:
-            raise ValueError, "no keyword arguments expected"
-        if len(self.arguments_w) > argcount:
-            raise ValueError, "too many arguments (%d expected)" % argcount
-        elif len(self.arguments_w) < argcount:
-            raise ValueError, "not enough arguments (%d expected)" % argcount
-        return self.arguments_w
-
-    def combine_if_necessary(self):
-        if self.combine_has_happened:
-            return
-        self._combine_wrapped(self.w_stararg, self.w_starstararg)
-        self.combine_has_happened = True
-
-    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):
-        return ArgumentsForTranslation(self.space, self.arguments_w,
-                                       self.keywords, self.keywords_w, 
self.w_stararg,
-                                       self.w_starstararg)
-
-    def _match_signature(self, scope_w, signature, defaults_w=None):
-        """Parse args and kwargs according to the signature of a code object,
-        or raise an ArgErr in case of failure.
-        """
-        #   args_w = list of the normal actual parameters, wrapped
-        #   scope_w = resulting list of wrapped values
-        #
-        self.combine_if_necessary()
-        co_argcount = signature.num_argnames() # expected formal arguments, 
without */**
-
-        args_w = self.arguments_w
-        num_args = len(args_w)
-        keywords = self.keywords or []
-        num_kwds = len(keywords)
-
-        # put as many positional input arguments into place as available
-        take = min(num_args, co_argcount)
-        scope_w[:take] = args_w[:take]
-        input_argcount = take
-
-        # collect extra positional arguments into the *vararg
-        if signature.has_vararg():
-            if num_args > co_argcount:
-                starargs_w = args_w[co_argcount:]
-            else:
-                starargs_w = []
-            scope_w[co_argcount] = self.space.newtuple(starargs_w)
-        elif num_args > co_argcount:
-            raise ArgErrCount(num_args, num_kwds, signature, defaults_w, 0)
-
-        # if a **kwargs argument is needed, explode
-        if signature.has_kwarg():
-            raise TypeError("Keyword arguments as **kwargs is not supported by 
RPython")
-
-        # 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 = [-1] * (co_argcount - input_argcount)
-            # 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 = len(keywords)
-            for i, name in enumerate(keywords):
-                # 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
-                if j < input_argcount:
-                    # check that no keyword argument conflicts with these.
-                    if j >= 0:
-                        raise ArgErrMultipleValues(name)
-                else:
-                    kwds_mapping[j - input_argcount] = i # map to the right 
index
-                    num_remainingkwds -= 1
-
-            if num_remainingkwds:
-                if co_argcount == 0:
-                    raise ArgErrCount(num_args, 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 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:
-                    missing += 1
-            if missing:
-                raise ArgErrCount(num_args, num_kwds, signature, defaults_w, 
missing)
-
-    def unpack(self):
-        "Return a ([w1,w2...], {'kw':w3...}) pair."
-        self.combine_if_necessary()
-        kwds_w = {}
-        if self.keywords:
-            for i in range(len(self.keywords)):
-                kwds_w[self.keywords[i]] = self.keywords_w[i]
-        return self.arguments_w, kwds_w
-
-
-    def match_signature(self, signature, defaults_w):
-        """Parse args and kwargs according to the signature of a code object,
-        or raise an ArgErr in case of failure.
-        """
-        scopelen = signature.scope_length()
-        scope_w = [None] * scopelen
-        self._match_signature(scope_w, signature, defaults_w)
-        return scope_w
-
-    def unmatch_signature(self, signature, data_w):
-        """kind of inverse of match_signature"""
-        args_w, kwds_w = self.unpack()
-        need_cnt = len(args_w)
-        need_kwds = kwds_w.keys()
-        space = self.space
-        argnames, varargname, kwargname = signature
-        cnt = len(argnames)
-        data_args_w = data_w[:cnt]
-        if varargname:
-            data_w_stararg = data_w[cnt]
-            cnt += 1
-        else:
-            data_w_stararg = space.newtuple([])
-
-        unfiltered_kwds_w = {}
-        if kwargname:
-            data_w_starargarg = data_w[cnt]
-            for w_key in space.unpackiterable(data_w_starargarg):
-                key = space.str_w(w_key)
-                w_value = space.getitem(data_w_starargarg, w_key)
-                unfiltered_kwds_w[key] = w_value
-            cnt += 1
-        assert len(data_w) == cnt
-
-        ndata_args_w = len(data_args_w)
-        if ndata_args_w >= need_cnt:
-            args_w = data_args_w[:need_cnt]
-            for argname, w_arg in zip(argnames[need_cnt:], 
data_args_w[need_cnt:]):
-                unfiltered_kwds_w[argname] = w_arg
-            assert not space.is_true(data_w_stararg)
-        else:
-            stararg_w = space.unpackiterable(data_w_stararg)
-            datalen = len(data_args_w)
-            args_w = [None] * (datalen + len(stararg_w))
-            for i in range(0, datalen):
-                args_w[i] = data_args_w[i]
-            for i in range(0, len(stararg_w)):
-                args_w[i + datalen] = stararg_w[i]
-            assert len(args_w) == need_cnt
-
-        keywords = []
-        keywords_w = []
-        for key in need_kwds:
-            keywords.append(key)
-            keywords_w.append(unfiltered_kwds_w[key])
-
-        return ArgumentsForTranslation(self.space, args_w, keywords, 
keywords_w)
-
-    @staticmethod
-    def fromshape(space, (shape_cnt,shape_keys,shape_star,shape_stst), data_w):
-        args_w = data_w[:shape_cnt]
-        p = end_keys = shape_cnt + len(shape_keys)
-        if shape_star:
-            w_star = data_w[p]
-            p += 1
-        else:
-            w_star = None
-        if shape_stst:
-            w_starstar = data_w[p]
-            p += 1
-        else:
-            w_starstar = None
-        return ArgumentsForTranslation(space, args_w, list(shape_keys),
-                                       data_w[shape_cnt:end_keys], w_star,
-                                       w_starstar)
-
-    def flatten(self):
-        """ Argument <-> list of w_objects together with "shape" information 
"""
-        shape_cnt, shape_keys, shape_star, shape_stst = self._rawshape()
-        data_w = self.arguments_w + [self.keywords_w[self.keywords.index(key)]
-                                         for key in shape_keys]
-        if shape_star:
-            data_w.append(self.w_stararg)
-        if shape_stst:
-            data_w.append(self.w_starstararg)
-        return (shape_cnt, shape_keys, shape_star, shape_stst), data_w
-
-    def _rawshape(self, nextra=0):
-        assert not self.combine_has_happened
-        shape_cnt  = len(self.arguments_w)+nextra        # Number of 
positional args
-        if self.keywords:
-            shape_keys = self.keywords[:]                # List of keywords 
(strings)
-            shape_keys.sort()
-        else:
-            shape_keys = []
-        shape_star = self.w_stararg is not None   # Flag: presence of *arg
-        shape_stst = self.w_starstararg is not None # Flag: presence of **kwds
-        return shape_cnt, tuple(shape_keys), shape_star, shape_stst # 
shape_keys are sorted
-
-def rawshape(args, nextra=0):
-    return args._rawshape(nextra)
-
 
 class CallSpec(object):
     """Represents the arguments passed into a function call, i.e. the
@@ -407,111 +106,3 @@
         if shape_star:
             data_w.append(self.w_stararg)
         return (shape_cnt, shape_keys, shape_star, shape_stst), data_w
-
-
-#
-# ArgErr family of exceptions raised in case of argument mismatch.
-# We try to give error messages following CPython's, which are very 
informative.
-#
-
-class ArgErr(Exception):
-
-    def getmsg(self):
-        raise NotImplementedError
-
-class ArgErrCount(ArgErr):
-
-    def __init__(self, got_nargs, nkwds, signature,
-                 defaults_w, missing_args):
-        self.signature = signature
-
-        self.num_defaults = 0 if defaults_w is None else len(defaults_w)
-        self.missing_args = missing_args
-        self.num_args = got_nargs
-        self.num_kwds = nkwds
-
-    def getmsg(self):
-        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.signature.has_kwarg()
-            num_args = self.num_args
-            num_kwds = self.num_kwds
-            if defcount == 0 and not self.signature.has_vararg():
-                msg1 = "exactly"
-                if not has_kwarg:
-                    num_args += num_kwds
-                    num_kwds = 0
-            elif not self.missing_args:
-                msg1 = "at most"
-            else:
-                msg1 = "at least"
-                has_kwarg = False
-                n -= defcount
-            if n == 1:
-                plural = ""
-            else:
-                plural = "s"
-            if has_kwarg or num_kwds > 0:
-                msg2 = " non-keyword"
-            else:
-                msg2 = ""
-            msg = "takes %s %d%s argument%s (%d given)" % (
-                msg1,
-                n,
-                msg2,
-                plural,
-                num_args)
-        return msg
-
-class ArgErrMultipleValues(ArgErr):
-
-    def __init__(self, argname):
-        self.argname = argname
-
-    def getmsg(self):
-        msg = "got multiple values for keyword argument '%s'" % (
-            self.argname)
-        return msg
-
-class ArgErrUnknownKwds(ArgErr):
-
-    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 i not in kwds_mapping:
-                    name = keywords[i]
-                    if name is None:
-                        # We'll assume it's unicode. Encode it.
-                        # Careful, I *think* it should not be possible to
-                        # get an IndexError here but you never know.
-                        try:
-                            if keyword_names_w is None:
-                                raise IndexError
-                            # note: negative-based indexing from the end
-                            w_name = keyword_names_w[i - len(keywords)]
-                        except IndexError:
-                            name = '?'
-                        else:
-                            w_enc = space.wrap(space.sys.defaultencoding)
-                            w_err = space.wrap("replace")
-                            w_name = space.call_method(w_name, "encode", w_enc,
-                                                       w_err)
-                            name = space.str_w(w_name)
-                    break
-        self.kwd_name = name
-
-    def getmsg(self):
-        if self.num_kwds == 1:
-            msg = "got an unexpected keyword argument '%s'" % (
-                self.kwd_name)
-        else:
-            msg = "got %d unexpected keyword arguments" % (
-                self.num_kwds)
-        return msg
diff --git a/rpython/flowspace/test/test_argument.py 
b/rpython/flowspace/test/test_argument.py
--- a/rpython/flowspace/test/test_argument.py
+++ b/rpython/flowspace/test/test_argument.py
@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 import py
-from rpython.flowspace.argument import (ArgumentsForTranslation, rawshape,
-    Signature)
+from rpython.flowspace.argument import Signature
 
 
 class TestSignature(object):
@@ -50,277 +49,3 @@
         assert x == ["a", "b", "c"]
         assert y == "d"
         assert z == "e"
-
-class dummy_wrapped_dict(dict):
-    def __nonzero__(self):
-        raise NotImplementedError
-
-class kwargsdict(dict):
-    pass
-
-class DummySpace(object):
-    def newtuple(self, items):
-        return tuple(items)
-
-    def is_true(self, obj):
-        if isinstance(obj, dummy_wrapped_dict):
-            return bool(dict(obj))
-        return bool(obj)
-
-    def fixedview(self, it):
-        return list(it)
-
-    def listview(self, it):
-        return list(it)
-
-    def unpackiterable(self, it):
-        return list(it)
-
-    def view_as_kwargs(self, x):
-        if len(x) == 0:
-            return [], []
-        return None, None
-
-    def newdict(self):
-        return {}
-
-    def newlist(self, l=[]):
-        return l
-
-    def setitem(self, obj, key, value):
-        obj[key] = value
-
-    def getitem(self, obj, key):
-        return obj[key]
-
-    def wrap(self, obj):
-        return obj
-
-    def str_w(self, s):
-        return str(s)
-
-    def len(self, x):
-        return len(x)
-
-    def int_w(self, x):
-        return x
-
-    def eq_w(self, x, y):
-        return x == y
-
-    def isinstance(self, obj, cls):
-        return isinstance(obj, cls)
-    isinstance_w = isinstance
-
-    def exception_match(self, w_type1, w_type2):
-        return issubclass(w_type1, w_type2)
-
-    def call_method(self, obj, name, *args):
-        method = getattr(obj, name)
-        return method(*args)
-
-    def type(self, obj):
-        class Type:
-            def getname(self, space, default='?'):
-                return type(obj).__name__
-        return Type()
-
-
-    w_TypeError = TypeError
-    w_AttributeError = AttributeError
-    w_UnicodeEncodeError = UnicodeEncodeError
-    w_dict = dict
-    w_str = str
-
-def make_arguments_for_translation(space, args_w, keywords_w={},
-                                   w_stararg=None, w_starstararg=None):
-    return ArgumentsForTranslation(space, args_w, keywords_w.keys(),
-                                   keywords_w.values(), w_stararg,
-                                   w_starstararg)
-
-class TestArgumentsForTranslation(object):
-
-    def test_prepend(self):
-        space = DummySpace()
-        args = ArgumentsForTranslation(space, ["0"])
-        args1 = args.prepend("thingy")
-        assert args1 is not args
-        assert args1.arguments_w == ["thingy", "0"]
-        assert args1.keywords is args.keywords
-        assert args1.keywords_w is args.keywords_w
-
-    def test_fixedunpacked(self):
-        space = DummySpace()
-
-        args = ArgumentsForTranslation(space, [], ["k"], [1])
-        py.test.raises(ValueError, args.fixedunpack, 1)
-
-        args = ArgumentsForTranslation(space, ["a", "b"])
-        py.test.raises(ValueError, args.fixedunpack, 0)
-        py.test.raises(ValueError, args.fixedunpack, 1)
-        py.test.raises(ValueError, args.fixedunpack, 3)
-        py.test.raises(ValueError, args.fixedunpack, 4)
-
-        assert args.fixedunpack(2) == ['a', 'b']
-
-    def test_unmatch_signature(self):
-        space = DummySpace()
-        args = make_arguments_for_translation(space, [1,2,3])
-        sig = Signature(['a', 'b', 'c'], None, None)
-        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 = Signature(['a', 'b', 'c'], None, None)
-        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 = Signature(['a', 'b', 'c'], 'r', None)
-        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 = Signature(['a', 'b', 'c'], None, None)
-        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 = Signature(['a', 'b', 'c'], None, None)
-        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 = Signature(['a', 'b', 'c'], None, 'kw')
-        py.test.raises(TypeError, args.match_signature, sig, [2, 3])
-
-    def test_rawshape(self):
-        space = DummySpace()
-        args = make_arguments_for_translation(space, [1,2,3])
-        assert rawshape(args) == (3, (), False, False)
-
-        args = make_arguments_for_translation(space, [1])
-        assert rawshape(args, 2) == (3, (), False, False)
-
-        args = make_arguments_for_translation(space, [1,2,3,4,5])
-        assert rawshape(args) == (5, (), False, False)
-
-        args = make_arguments_for_translation(space, [1], {'c': 3, 'b': 2})
-        assert rawshape(args) == (1, ('b', 'c'), False, False)
-
-        args = make_arguments_for_translation(space, [1], {'c': 5})
-        assert rawshape(args) == (1, ('c', ), False, False)
-
-        args = make_arguments_for_translation(space, [1], {'c': 5, 'd': 7})
-        assert rawshape(args) == (1, ('c', 'd'), False, False)
-
-        args = make_arguments_for_translation(space, [1,2,3,4,5], {'e': 5, 
'd': 7})
-        assert rawshape(args) == (5, ('d', 'e'), False, False)
-
-        args = make_arguments_for_translation(space, [], {},
-                                       w_stararg=[1],
-                                       w_starstararg={'c': 5, 'd': 7})
-        assert rawshape(args) == (0, (), True, True)
-
-        args = make_arguments_for_translation(space, [1,2], {'g': 9},
-                                       w_stararg=[3,4,5],
-                                       w_starstararg={'e': 5, 'd': 7})
-        assert rawshape(args) == (2, ('g', ), True, True)
-
-    def test_copy_and_shape(self):
-        space = DummySpace()
-        args = ArgumentsForTranslation(space, ['a'], ['x'], [1],
-                                       ['w1'], {'y': 'w2'})
-        args1 = args.copy()
-        args.combine_if_necessary()
-        assert rawshape(args1) == (1, ('x',), True, True)
-
-
-    def test_flatten(self):
-        space = DummySpace()
-        args = make_arguments_for_translation(space, [1,2,3])
-        assert args.flatten() == ((3, (), False, False), [1, 2, 3])
-
-        args = make_arguments_for_translation(space, [1])
-        assert args.flatten() == ((1, (), False, False), [1])
-
-        args = make_arguments_for_translation(space, [1,2,3,4,5])
-        assert args.flatten() == ((5, (), False, False), [1,2,3,4,5])
-
-        args = make_arguments_for_translation(space, [1], {'c': 3, 'b': 2})
-        assert args.flatten() == ((1, ('b', 'c'), False, False), [1, 2, 3])
-
-        args = make_arguments_for_translation(space, [1], {'c': 5})
-        assert args.flatten() == ((1, ('c', ), False, False), [1, 5])
-
-        args = make_arguments_for_translation(space, [1], {'c': 5, 'd': 7})
-        assert args.flatten() == ((1, ('c', 'd'), False, False), [1, 5, 7])
-
-        args = make_arguments_for_translation(space, [1,2,3,4,5], {'e': 5, 
'd': 7})
-        assert args.flatten() == ((5, ('d', 'e'), False, False), [1, 2, 3, 4, 
5, 7, 5])
-
-        args = make_arguments_for_translation(space, [], {},
-                                       w_stararg=[1],
-                                       w_starstararg={'c': 5, 'd': 7})
-        assert args.flatten() == ((0, (), True, True), [[1], {'c': 5, 'd': 7}])
-
-        args = make_arguments_for_translation(space, [1,2], {'g': 9},
-                                       w_stararg=[3,4,5],
-                                       w_starstararg={'e': 5, 'd': 7})
-        assert args.flatten() == ((2, ('g', ), True, True), [1, 2, 9, [3, 4, 
5], {'e': 5, 'd': 7}])
-
-    def test_stararg_flowspace_variable(self):
-        space = DummySpace()
-        var = object()
-        shape = ((2, ('g', ), True, False), [1, 2, 9, var])
-        args = make_arguments_for_translation(space, [1,2], {'g': 9},
-                                       w_stararg=var)
-        assert args.flatten() == shape
-
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-
-    def test_fromshape(self):
-        space = DummySpace()
-        shape = ((3, (), False, False), [1, 2, 3])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((1, (), False, False), [1])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((5, (), False, False), [1,2,3,4,5])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((1, ('b', 'c'), False, False), [1, 2, 3])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((1, ('c', ), False, False), [1, 5])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((1, ('c', 'd'), False, False), [1, 5, 7])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((5, ('d', 'e'), False, False), [1, 2, 3, 4, 5, 7, 5])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((0, (), True, True), [[1], {'c': 5, 'd': 7}])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
-        shape = ((2, ('g', ), True, True), [1, 2, 9, [3, 4, 5], {'e': 5, 'd': 
7}])
-        args = ArgumentsForTranslation.fromshape(space, *shape)
-        assert args.flatten() == shape
-
diff --git a/rpython/rtyper/callparse.py b/rpython/rtyper/callparse.py
--- a/rpython/rtyper/callparse.py
+++ b/rpython/rtyper/callparse.py
@@ -1,4 +1,4 @@
-from rpython.flowspace.argument import ArgumentsForTranslation, ArgErr
+from rpython.annotator.argument import ArgumentsForTranslation, ArgErr
 from rpython.annotator import model as annmodel
 from rpython.rtyper import rtuple
 from rpython.rtyper.error import TyperError
diff --git a/rpython/rtyper/rbuiltin.py b/rpython/rtyper/rbuiltin.py
--- a/rpython/rtyper/rbuiltin.py
+++ b/rpython/rtyper/rbuiltin.py
@@ -44,7 +44,7 @@
 
 def call_args_expand(hop, takes_kwds = True):
     hop = hop.copy()
-    from rpython.flowspace.argument import ArgumentsForTranslation
+    from rpython.annotator.argument import ArgumentsForTranslation
     arguments = ArgumentsForTranslation.fromshape(
             None, hop.args_s[1].const, # shape
             range(hop.nb_args-2))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to