本文整理汇总了Python中pypy.interpreter.argument.Arguments类的典型用法代码示例。如果您正苦于以下问题:Python Arguments类的具体用法?Python Arguments怎么用?Python Arguments使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Arguments类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: appcaller
def appcaller(space, *args_w):
if not isinstance(space, ObjSpace):
raise TypeError("first argument must be a space instance.")
# redirect if the space handles this specially
# XXX can this be factored a bit less flow space dependently?
if hasattr(space, 'specialcases'):
sc = space.specialcases
if ApplevelClass in sc:
ret_w = sc[ApplevelClass](space, self, name, args_w)
if ret_w is not None: # it was RPython
return ret_w
# the last argument can be an Arguments
if not args_w:
args = Arguments(space, [])
else:
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)
示例2: callparse
def callparse(rtyper, graph, hop, opname, r_self=None):
"""Parse the arguments of 'hop' when calling the given 'graph'.
"""
rinputs = getrinputs(rtyper, graph)
space = RPythonCallsSpace()
def args_h(start):
return [VarHolder(i, hop.args_s[i]) for i in range(start, hop.nb_args)]
if r_self is None:
start = 1
else:
start = 0
rinputs[0] = r_self
if opname == "simple_call":
arguments = Arguments(space, args_h(start))
elif opname == "call_args":
arguments = Arguments.fromshape(space, hop.args_s[start].const, args_h(start + 1)) # shape
# parse the arguments according to the function we are calling
signature = graph.signature
defs_h = []
if graph.defaults:
for x in graph.defaults:
defs_h.append(ConstHolder(x))
try:
holders = arguments.match_signature(signature, defs_h)
except ArgErr, e:
raise TyperError, "signature mismatch: %s" % e.getmsg(graph.name)
示例3: test_prepend
def test_prepend(self):
space = DummySpace()
args = Arguments(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
示例4: test_argument_unicode
def test_argument_unicode(self):
space = DummySpace()
w_starstar = space.wrap({u"abc": 5})
args = Arguments(space, [], w_starstararg=w_starstar)
l = [None]
args._match_signature(None, l, Signature(["abc"]))
assert len(l) == 1
assert l[0] == space.wrap(5)
示例5: test_topacked_frompacked
def test_topacked_frompacked(self):
space = DummySpace()
args = Arguments(space, [1], ['a', 'b'], [2, 3])
w_args, w_kwds = args.topacked()
assert w_args == (1,)
assert w_kwds == {'a': 2, 'b': 3}
args1 = Arguments.frompacked(space, w_args, w_kwds)
assert args.arguments_w == [1]
assert set(args.keywords) == set(['a', 'b'])
assert args.keywords_w[args.keywords.index('a')] == 2
assert args.keywords_w[args.keywords.index('b')] == 3
示例6: test_fixedunpacked
def test_fixedunpacked(self):
space = DummySpace()
args = Arguments(space, [], ["k"], [1])
py.test.raises(ValueError, args.fixedunpack, 1)
args = Arguments(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']
示例7: test_match_kwds2
def test_match_kwds2(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 = 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}]
示例8: test_args_parsing_into_scope
def test_args_parsing_into_scope(self):
space = DummySpace()
args = Arguments(space, [])
calls = []
def _match_signature(w_firstarg, scope_w, signature, defaults_w=None, blindargs=0):
defaults_w = [] if defaults_w is None else defaults_w
calls.append(
(
w_firstarg,
scope_w,
signature.argnames,
signature.has_vararg(),
signature.has_kwarg(),
defaults_w,
blindargs,
)
)
args._match_signature = _match_signature
scope_w = [None, None]
args.parse_into_scope(None, scope_w, "foo", Signature(["a", "b"], None, None))
assert len(calls) == 1
assert calls[0] == (None, scope_w, ["a", "b"], False, False, [], 0)
assert calls[0][1] is scope_w
calls = []
scope_w = [None, None, None, None]
args.parse_into_scope(None, scope_w, "foo", Signature(["a", "b"], "args", "kw"), defaults_w=["x", "y"])
assert len(calls) == 1
assert calls[0] == (None, scope_w, ["a", "b"], True, True, ["x", "y"], 0)
calls = []
scope_w = [None, None, None, None]
args.parse_into_scope("obj", scope_w, "foo", Signature(["a", "b"], "args", "kw"), defaults_w=["x", "y"])
assert len(calls) == 1
assert calls[0] == ("obj", scope_w, ["a", "b"], True, True, ["x", "y"], 0)
class FakeArgErr(ArgErr):
def getmsg(self):
return "msg"
def _match_signature(*args):
raise FakeArgErr()
args._match_signature = _match_signature
excinfo = py.test.raises(
OperationError, args.parse_into_scope, "obj", [None, None], "foo", Signature(["a", "b"], None, None)
)
assert excinfo.value.w_type is TypeError
assert excinfo.value.get_w_value(space) == "foo() msg"
示例9: test_create
def test_create(self):
space = DummySpace()
args_w = []
args = Arguments(space, args_w)
assert args.arguments_w is args_w
assert args.keywords is None
assert args.keywords_w is None
assert args.firstarg() is None
args = Arguments(space, args_w, w_stararg=["*"], w_starstararg={"k": 1})
assert args.arguments_w == ["*"]
assert args.keywords == ["k"]
assert args.keywords_w == [1]
assert args.firstarg() == "*"
示例10: call_args_expand
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))
if arguments.w_starstararg is not None:
raise TyperError("**kwds call not implemented")
if arguments.w_stararg is not None:
# expand the *arg in-place -- it must be a tuple
from pypy.rpython.rtuple import AbstractTupleRepr
if arguments.w_stararg != hop.nb_args - 3:
raise TyperError("call pattern too complex")
hop.nb_args -= 1
v_tuple = hop.args_v.pop()
s_tuple = hop.args_s.pop()
r_tuple = hop.args_r.pop()
if not isinstance(r_tuple, AbstractTupleRepr):
raise TyperError("*arg must be a tuple")
for i in range(len(r_tuple.items_r)):
v_item = r_tuple.getitem_internal(hop.llops, v_tuple, i)
hop.nb_args += 1
hop.args_v.append(v_item)
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:
raise TyperError("kwds args not supported")
# prefix keyword arguments with 'i_'
kwds_i = {}
for key, index in kwds.items():
kwds_i['i_'+key] = index
return hop, kwds_i
示例11: checkmodule
def checkmodule(modname, backend, interactive=False, basepath='pypy.module'):
"Compile a fake PyPy module."
from pypy.objspace.fake.objspace import FakeObjSpace, W_Object
from pypy.translator.driver import TranslationDriver
space = FakeObjSpace()
space.config.translating = True
ModuleClass = __import__(basepath + '.%s' % modname,
None, None, ['Module']).Module
module = ModuleClass(space, space.wrap(modname))
w_moduledict = module.getdict(space)
gateways = find_gateways(modname, basepath, module)
functions = [gw.__spacebind__(space) for gw in gateways]
arguments = Arguments.frompacked(space, W_Object(), W_Object())
dummy_function = copy(functions[0])
def main(argv): # use the standalone mode not to allow SomeObject
dummy_rpython(dummy_function)
for func in functions:
func.call_args(arguments)
return 0
patch_pypy()
driver = TranslationDriver()
driver.setup(main, None)
try:
driver.proceed(['compile_' + backend])
except SystemExit:
raise
except:
if not interactive:
raise
debug(driver)
raise SystemExit(1)
示例12: test_topacked_frompacked
def test_topacked_frompacked(self):
space = DummySpace()
args = Arguments(space, [1], ["a", "b"], [2, 3])
w_args, w_kwds = args.topacked()
assert w_args == (1,)
assert w_kwds == {"a": 2, "b": 3}
args1 = Arguments.frompacked(space, w_args, w_kwds)
assert args.arguments_w == [1]
assert set(args.keywords) == set(["a", "b"])
assert args.keywords_w[args.keywords.index("a")] == 2
assert args.keywords_w[args.keywords.index("b")] == 3
args = Arguments(space, [1])
w_args, w_kwds = args.topacked()
assert w_args == (1,)
assert not w_kwds
示例13: call_many_to_one
def call_many_to_one(space, shape, func, res_dtype, in_args, out):
# out must hav been built. func needs no calc_type, is usually an
# external ufunc
nin = len(in_args)
in_iters = [None] * nin
in_states = [None] * nin
for i in range(nin):
in_i = in_args[i]
assert isinstance(in_i, W_NDimArray)
in_iter, in_state = in_i.create_iter(shape)
in_iters[i] = in_iter
in_states[i] = in_state
shapelen = len(shape)
assert isinstance(out, W_NDimArray)
out_iter, out_state = out.create_iter(shape)
vals = [None] * nin
while not out_iter.done(out_state):
call_many_to_one_driver.jit_merge_point(shapelen=shapelen, func=func,
res_dtype=res_dtype, nin=nin)
for i in range(nin):
vals[i] = in_iters[i].getitem(in_states[i])
w_arglist = space.newlist(vals)
w_out_val = space.call_args(func, Arguments.frompacked(space, w_arglist))
out_iter.setitem(out_state, res_dtype.coerce(space, w_out_val))
for i in range(nin):
in_states[i] = in_iters[i].next(in_states[i])
out_state = out_iter.next(out_state)
return out
示例14: build_args
def build_args(self, op, args_s):
space = RPythonCallsSpace()
if op == "simple_call":
return Arguments(space, list(args_s))
elif op == "call_args":
return Arguments.fromshape(space, args_s[0].const, # shape
list(args_s[1:]))
示例15: test_blindargs
def test_blindargs(self):
space = DummySpace()
kwds = [("a", 3), ("b", 4)]
for i in range(4):
kwds_w = dict(kwds[:i])
keywords = kwds_w.keys()
keywords_w = kwds_w.values()
w_kwds = 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]
args._match_signature(None, l, Signature(["a", "b"], None, "**"), blindargs=2)
assert l == [1, 2, {"a": 3, "b": 4}]
args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds)
l = [None, None, None]
py.test.raises(ArgErrUnknownKwds, args._match_signature, None, l, Signature(["a", "b"]), blindargs=2)