1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
# -*- coding: utf-8 -*-
import py
from rpython.annotator.argument import ArgumentsForTranslation, rawshape
from rpython.flowspace.argument import Signature, CallSpec
class MockArgs(ArgumentsForTranslation):
def newtuple(self, items):
return tuple(items)
def unpackiterable(self, it):
return list(it)
class TestArgumentsForTranslation(object):
def test_prepend(self):
args = MockArgs(["0"])
args1 = args.prepend("thingy")
assert args1 is not args
assert args1.arguments_w == ["thingy", "0"]
assert args1.keywords == args.keywords
def test_fixedunpacked(self):
args = MockArgs([], {"k": 1})
py.test.raises(ValueError, args.fixedunpack, 1)
args = MockArgs(["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):
args = MockArgs([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 = MockArgs([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 = MockArgs([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 = MockArgs([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 = MockArgs([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()
def test_rawshape(self):
args = MockArgs([1, 2, 3])
assert rawshape(args) == (3, (), False)
args = MockArgs([1, 2, 3, 4, 5])
assert rawshape(args) == (5, (), False)
args = MockArgs([1], {'c': 3, 'b': 2})
assert rawshape(args) == (1, ('b', 'c'), False)
args = MockArgs([1], {'c': 5})
assert rawshape(args) == (1, ('c', ), False)
args = MockArgs([1], {'c': 5, 'd': 7})
assert rawshape(args) == (1, ('c', 'd'), False)
args = MockArgs([1, 2, 3, 4, 5], {'e': 5, 'd': 7})
assert rawshape(args) == (5, ('d', 'e'), False)
def test_stararg_flowspace_variable(self):
var = object()
shape = ((2, ('g', ), True), [1, 2, 9, var])
args = MockArgs([1, 2], {'g': 9}, w_stararg=var)
assert args.flatten() == shape
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
def test_fromshape(self):
shape = ((3, (), False), [1, 2, 3])
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
shape = ((1, (), False), [1])
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
shape = ((5, (), False), [1, 2, 3, 4, 5])
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
shape = ((1, ('b', 'c'), False), [1, 2, 3])
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
shape = ((1, ('c', ), False), [1, 5])
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
shape = ((1, ('c', 'd'), False), [1, 5, 7])
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
shape = ((5, ('d', 'e'), False), [1, 2, 3, 4, 5, 7, 5])
args = MockArgs.fromshape(*shape)
assert args.flatten() == shape
|