本文整理汇总了Python中pypy.rpython.test.test_llinterp.interpret函数的典型用法代码示例。如果您正苦于以下问题:Python interpret函数的具体用法?Python interpret怎么用?Python interpret使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了interpret函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_callback_field_bound_method
def test_callback_field_bound_method():
py.test.skip("needs an rtyping hack to help the js backend "
"or generalized bound methods support in many places")
class A:
def x(self, i):
return float(i)
aa = A()
def callback(x):
return 8.3 + x
def callback_field(i):
a = CD()
if i:
a.callback_field = aa.x
else:
a.callback_field = callback
return a.callback_field(i+1)
res = interpret(callback_field, [1], type_system="ootype")
assert res == 2.0
assert isinstance(res, float)
res = interpret(callback_field, [0], type_system="ootype")
assert res == 8.3
示例2: test_overflow_bug
def test_overflow_bug():
CASE = [
(-144, -248), # \ cycle
(-248, -144), # /
(-488, -416), # \ two usages of -488
(-488, -480), # /
(-488, -488), # - one self-application of -488
]
class FakeAssembler:
def regalloc_mov(self, src, dst):
print "mov", src, dst
def regalloc_push(self, x):
print "push", x
def regalloc_pop(self, x):
print "pop", x
def regalloc_immedmem2mem(self, x, y):
print "?????????????????????????"
def main():
srclocs = [StackLoc(9999, x, "i") for x, y in CASE]
dstlocs = [StackLoc(9999, y, "i") for x, y in CASE]
remap_frame_layout(FakeAssembler(), srclocs, dstlocs, eax)
# it works when run directly
main()
# but it used to crash when translated,
# because of a -sys.maxint-2 overflowing to sys.maxint
from pypy.rpython.test.test_llinterp import interpret
interpret(main, [])
示例3: test_convert_pointers
def test_convert_pointers(self):
py.test.skip("in-progress")
from pypy.rpython.rctypes.rchar_p import ll_strlen
strlen = CFUNCTYPE(c_int, c_char_p)() # not directly executable!
strlen.__name__ = 'strlen'
strlen.llinterp_friendly_version = ll_strlen
PTR = c_char_p("hello")
BUF = create_string_buffer(10)
BUF.value = "hello"
def func(n):
# constant arguments
assert strlen("hello") == 5
assert strlen(PTR) == 5
assert strlen(BUF) == 5
# variable arguments
s = chr(n) + 'bc'
assert strlen(s) == 3
assert strlen(c_char_p(s)) == 3
assert strlen((c_char * 6)('a', 'b')) == 2
buf = create_string_buffer(10)
buf.value = "hello"
assert strlen(buf) == 5
interpret(func, [65])
示例4: test_some_generic_function_call
def test_some_generic_function_call(self):
def h(x):
return int(x)
def c(x):
return int(x) + 1
def default(x):
return int(x) + 3
def g(a, x):
if x == -1:
a = None
if x > 0:
if x == 1:
a = h
else:
a = c
x = x + 0.01
return a(x)
def f(x):
return g(default, x)
g._annenforceargs_ = policy.Sig(annmodel.SomeGenericCallable(
args=[annmodel.SomeFloat()], result=annmodel.SomeInteger()),
float)
assert interpret(f, [1.]) == 1
assert interpret(f, [10.]) == 11
assert interpret(f, [-3.]) == 0
示例5: test_rpython_merge_RWeakValueDictionary2
def test_rpython_merge_RWeakValueDictionary2():
class A(object):
def __init__(self):
self.d = RWeakValueDictionary(A)
def f(self, key):
a = A()
self.d.set(key, a)
return a
empty = A()
def f(x):
a = A()
if x:
a = empty
a2 = a.f("a")
assert a.d.get("a") is a2
f(0)
interpret(f, [0])
f(1)
interpret(f, [1])
def g(x):
if x:
d = RWeakValueDictionary(X)
else:
d = RWeakValueDictionary(Y)
d.set("x", X())
return 41
py.test.raises(Exception, interpret, g, [1])
示例6: test_convert_from_llvalue
def test_convert_from_llvalue(self):
def func():
x = c_ushort(5)
pointer(x)[0] += 1
assert x.value == 6
x = c_char('A')
pointer(x)[0] = chr(ord(pointer(x)[0]) + 1)
assert x.value == 'B'
x = c_longlong(5)
pointer(x)[0] += 1
assert x.value == r_longlong(6)
x = c_ulonglong(5)
pointer(x)[0] += 1
assert x.value == r_ulonglong(6)
# x = c_float(2.5)
# pointer(x)[0] += 0.25
# assert x.value == 2.75
# pointer(x)[0] -= 1
# assert x.value == 1.75
x = c_double(2.5)
pointer(x)[0] += 0.25
assert x.value == 2.75
pointer(x)[0] -= 1
assert x.value == 1.75
# x = c_wchar(u'A')
# pointer(x)[0] = unichr(ord(pointer(x)[0]) + 1)
# assert x.value == u'B'
interpret(func, [])
示例7: test_mix_class_record_instance
def test_mix_class_record_instance():
I = Instance("test", ROOT, {"a": Signed})
R = Record({"x": Signed})
L = List(Signed)
c1 = runtimeClass(I)
c2 = runtimeClass(R)
c3 = runtimeClass(L)
c4 = runtimeClass(Class)
def fn(flag):
if flag == 0:
return c1
elif flag == 1:
return c2
elif flag == 2:
return c3
else:
return c4
res = interpret(fn, [0], type_system='ootype')
assert res is c1
res = interpret(fn, [1], type_system='ootype')
assert res is c2
res = interpret(fn, [2], type_system='ootype')
assert res is c3
res = interpret(fn, [3], type_system='ootype')
assert res is c4
示例8: test_ntpath
def test_ntpath():
import ntpath
def f():
assert ntpath.join("\\foo", "bar") == "\\foo\\bar"
assert ntpath.join("c:\\foo", "spam\\egg") == "c:\\foo\\spam\\egg"
assert ntpath.join("c:\\foo", "d:\\bar") == "d:\\bar"
interpret(f, [])
示例9: test_specialize_null_pointer
def test_specialize_null_pointer(self):
def fn():
p = POINTER(c_int)()
assert not p
p.contents = c_int(12)
assert p
interpret(fn, [])
示例10: test_prebuilt
def test_prebuilt():
c = C(111)
b = B(939393)
def makeint(n):
if n < 0:
x = c
elif n > 0:
x = C(n)
else:
x = b
return x
def fn(n):
x = makeint(n)
if isinstance(x, B):
return 'B', x.normalint
elif isinstance(x, C):
return 'C', x.smallint
else:
return 'A', 0
res = interpret(fn, [12], taggedpointers=True)
assert res.item0 == 'C'
assert res.item1 == 12
res = interpret(fn, [-1], taggedpointers=True)
assert res.item0 == 'C'
assert res.item1 == 111
res = interpret(fn, [0], taggedpointers=True)
assert res.item0 == 'B'
assert res.item1 == 939393
示例11: test_specialize_array_add_1_0
def test_specialize_array_add_1_0(self):
def f():
a1 = numpy.array(range(4, 10))
a2 = numpy.array([3])
return a1 + a2
data = [i + 3 for i in range(4, 10)]
res = interpret(f, [])
for i in range(len(data)):
assert res.dataptr[i] == data[i]
def f():
a = numpy.array(range(4, 10))
return a + 3
res = interpret(f, [])
for i in range(len(data)):
assert res.dataptr[i] == data[i]
def f():
a = numpy.array(range(4, 10))
return 3 + a
res = interpret(f, [])
for i in range(len(data)):
assert res.dataptr[i] == data[i]
示例12: test_around_extcall
def test_around_extcall(self):
if sys.platform == "win32":
py.test.skip('No pipes on windows')
import os
from pypy.annotation import model as annmodel
from pypy.rlib.objectmodel import invoke_around_extcall
from pypy.rpython.extfuncregistry import register_external
read_fd, write_fd = os.pipe()
try:
# we need an external function that is not going to get wrapped around
# before()/after() calls, in order to call it from before()/after()...
def mywrite(s):
os.write(write_fd, s)
def llimpl(s):
s = ''.join(s.chars)
os.write(write_fd, s)
register_external(mywrite, [str], annmodel.s_None, 'll_mywrite',
llfakeimpl=llimpl, sandboxsafe=True)
def before():
mywrite("B")
def after():
mywrite("A")
def f():
os.write(write_fd, "-")
invoke_around_extcall(before, after)
os.write(write_fd, "E")
interpret(f, [])
data = os.read(read_fd, 99)
assert data == "-BEA"
finally:
os.close(write_fd)
os.close(read_fd)
示例13: test_specialize_array_sub_1_0
def test_specialize_array_sub_1_0(self):
def f():
a1 = numpy.array(range(4, 10))
a2 = numpy.array([3])
return a1 - a2
data = [i - 3 for i in range(4, 10)]
res = interpret(f, [])
for i in range(len(data)):
assert res.dataptr[i] == data[i]
def f():
a = numpy.array(range(4, 10))
return a - 3
res = interpret(f, [])
for i in range(len(data)):
assert res.dataptr[i] == data[i]
def f():
a = numpy.array(range(4, 10))
return 3 - a
data = [3 - i for i in range(4, 10)]
res = interpret(f, [])
for i in range(len(data)):
assert res.dataptr[i] == data[i]
示例14: test_convert_pointers
def test_convert_pointers(self):
strlen = CFUNCTYPE(c_int, c_void_p)()
strlen.__name__ = 'strlen'
def ll_strlen_from_void_p(adr):
i = 0
while adr.char[i] != '\x00':
i += 1
return i
strlen.llinterp_friendly_version = ll_strlen_from_void_p
PTR = c_char_p("hello")
BUF = create_string_buffer(10)
BUF.value = "hello"
ARR = (c_byte * 10)(65, 66, 67)
def func(n):
# constant arguments XXX in-progress
## assert strlen("hello") == 5
## assert strlen(PTR) == 5
## assert strlen(BUF) == 5
## assert strlen(ARR) == 3
# variable arguments
s = chr(n) + 'bc'
assert strlen(s) == 3
assert strlen(c_char_p(s)) == 3
assert strlen((c_char * 6)('a', 'b')) == 2
# XXX Bytes are not chars in llinterp.
# assert strlen((c_byte * 6)(104,101,108,108,111)) == 5
buf = create_string_buffer(10)
buf.value = "hello"
assert strlen(buf) == 5
interpret(func, [65])
示例15: test_specialize_broadcast
def test_specialize_broadcast(self):
def f():
a = numpy.empty((4, 3), dtype="i")
b = numpy.array([33])
a[:, :] = b
return a
res = interpret(f, [])
for i in range(4 * 3):
assert res.dataptr[i] == 33
def f():
a = numpy.empty((4, 3), dtype="i")
b = numpy.array([33])
a[:,] = b
return a
res = interpret(f, [])
for i in range(4 * 3):
assert res.dataptr[i] == 33
def f():
a = numpy.empty((4, 3, 2), dtype="i")
a[:] = numpy.array([33])
a[0, :] = numpy.array([22])
return a
res = interpret(f, [])
data = [22] * 6 + [33] * 18
for i in range(3 * 4 * 2):
assert res.dataptr[i] == data[i]