本文整理汇总了Python中pypy.rpython.lltypesystem.ll2ctypes.ctypes2lltype函数的典型用法代码示例。如果您正苦于以下问题:Python ctypes2lltype函数的具体用法?Python ctypes2lltype怎么用?Python ctypes2lltype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ctypes2lltype函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_arrayofstruct
def test_arrayofstruct(self):
S1 = lltype.Struct('S1', ('x', lltype.Signed))
A = lltype.Array(S1, hints={'nolength': True})
a = lltype.malloc(A, 5, flavor='raw')
a[0].x = 100
a[1].x = 101
a[2].x = 102
a[3].x = 103
a[4].x = 104
ac = lltype2ctypes(a, normalize=False)
assert ac.contents.items[0].x == 100
assert ac.contents.items[2].x == 102
ac.contents.items[3].x += 500
assert a[3].x == 603
a[4].x += 600
assert ac.contents.items[4].x == 704
a1 = ctypes2lltype(lltype.Ptr(A), ac)
assert a1 == a
assert a1[2].x == 102
aitem1 = ctypes2lltype(lltype.Ptr(S1),
ctypes.pointer(ac.contents.items[1]))
assert aitem1.x == 101
assert aitem1 == a1[1]
lltype.free(a, flavor='raw')
assert not ALLOCATED # detects memory leaks in the test
开发者ID:antoine1fr,项目名称:pygirl,代码行数:25,代码来源:test_ll2ctypes.py
示例2: test_primitive
def test_primitive():
assert lltype2ctypes(5) == 5
assert lltype2ctypes('?') == ord('?')
assert lltype2ctypes('\xE0') == 0xE0
assert ctypes2lltype(lltype.Signed, 5) == 5
assert ctypes2lltype(lltype.Char, ord('a')) == 'a'
assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF'
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:7,代码来源:test_ll2ctypes.py
示例3: test_gcref_truth
def test_gcref_truth(self):
p0 = ctypes.c_void_p(0)
ref0 = ctypes2lltype(llmemory.GCREF, p0)
assert not ref0
p1234 = ctypes.c_void_p(1234)
ref1234 = ctypes2lltype(llmemory.GCREF, p1234)
assert p1234
开发者ID:alkorzt,项目名称:pypy,代码行数:8,代码来源:test_ll2ctypes.py
示例4: test_gcref_forth_and_back
def test_gcref_forth_and_back(self):
cp = ctypes.c_void_p(1234)
v = ctypes2lltype(llmemory.GCREF, cp)
assert lltype2ctypes(v).value == cp.value
v1 = ctypes2lltype(llmemory.GCREF, cp)
assert v == v1
assert v
v2 = ctypes2lltype(llmemory.GCREF, ctypes.c_void_p(1235))
assert v2 != v
开发者ID:alkorzt,项目名称:pypy,代码行数:9,代码来源:test_ll2ctypes.py
示例5: test_primitive
def test_primitive(self):
assert lltype2ctypes(5) == 5
assert lltype2ctypes('?') == ord('?')
assert lltype2ctypes('\xE0') == 0xE0
assert lltype2ctypes(unichr(1234)) == 1234
assert ctypes2lltype(lltype.Signed, 5) == 5
assert ctypes2lltype(lltype.Char, ord('a')) == 'a'
assert ctypes2lltype(lltype.UniChar, ord(u'x')) == u'x'
assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF'
assert lltype2ctypes(5.25) == 5.25
assert ctypes2lltype(lltype.Float, 5.25) == 5.25
assert lltype2ctypes(u'x') == ord(u'x')
res = lltype2ctypes(rffi.r_singlefloat(-3.5))
assert isinstance(res, ctypes.c_float)
assert res.value == -3.5
res = ctypes2lltype(lltype.SingleFloat, ctypes.c_float(-3.5))
assert isinstance(res, rffi.r_singlefloat)
assert float(res) == -3.5
assert lltype2ctypes(rffi.r_ulong(-1)) == sys.maxint * 2 + 1
res = ctypes2lltype(lltype.Unsigned, sys.maxint * 2 + 1)
assert (res, type(res)) == (rffi.r_ulong(-1), rffi.r_ulong)
res = lltype2ctypes(llmemory.sizeof(lltype.Signed))
assert res == struct.calcsize("l")
S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
res = lltype2ctypes(llmemory.sizeof(S))
assert res == struct.calcsize("ll")
p = lltype.nullptr(S)
cptr = lltype2ctypes(p)
assert not cptr
py.test.raises(ValueError, 'cptr.contents') # NULL pointer access
res = ctypes2lltype(lltype.Ptr(S), cptr)
assert res == p
assert not ALLOCATED # detects memory leaks in the test
开发者ID:antoine1fr,项目名称:pygirl,代码行数:35,代码来源:test_ll2ctypes.py
示例6: test_substructures
def test_substructures(self):
S1 = lltype.Struct('S1', ('x', lltype.Signed))
BIG = lltype.Struct('BIG', ('s1a', S1), ('s1b', S1))
s = lltype.malloc(BIG, flavor='raw')
s.s1a.x = 123
s.s1b.x = 456
sc = lltype2ctypes(s)
assert sc.contents.s1a.x == 123
assert sc.contents.s1b.x == 456
sc.contents.s1a.x += 1
sc.contents.s1b.x += 10
assert s.s1a.x == 124
assert s.s1b.x == 466
s.s1a.x += 3
s.s1b.x += 30
assert sc.contents.s1a.x == 127
assert sc.contents.s1b.x == 496
lltype.free(s, flavor='raw')
s = lltype.malloc(BIG, flavor='raw')
s1ac = lltype2ctypes(s.s1a)
s1ac.contents.x = 53
sc = lltype2ctypes(s)
assert sc.contents.s1a.x == 53
sc.contents.s1a.x += 1
assert s1ac.contents.x == 54
assert s.s1a.x == 54
s.s1a.x += 2
assert s1ac.contents.x == 56
assert sc.contents.s1a.x == 56
sc.contents.s1a.x += 3
assert s1ac.contents.x == 59
assert s.s1a.x == 59
t = ctypes2lltype(lltype.Ptr(BIG), sc)
assert t == s
assert t.s1a == s.s1a
assert t.s1a.x == 59
s.s1b.x = 8888
assert t.s1b == s.s1b
assert t.s1b.x == 8888
t1 = ctypes2lltype(lltype.Ptr(S1), s1ac)
assert t.s1a == t1
assert t1.x == 59
t1.x += 1
assert sc.contents.s1a.x == 60
lltype.free(s, flavor='raw')
assert not ALLOCATED # detects memory leaks in the test
开发者ID:antoine1fr,项目名称:pygirl,代码行数:48,代码来源:test_ll2ctypes.py
示例7: test_cstruct_to_ll
def test_cstruct_to_ll():
S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
s = lltype.malloc(S, flavor='raw')
s2 = lltype.malloc(S, flavor='raw')
s.x = 123
sc = lltype2ctypes(s)
t = ctypes2lltype(lltype.Ptr(S), sc)
assert lltype.typeOf(t) == lltype.Ptr(S)
assert s == t
assert not (s != t)
assert t == s
assert not (t != s)
assert t != lltype.nullptr(S)
assert not (t == lltype.nullptr(S))
assert lltype.nullptr(S) != t
assert not (lltype.nullptr(S) == t)
assert t != s2
assert not (t == s2)
assert s2 != t
assert not (s2 == t)
assert t.x == 123
t.x += 1
assert s.x == 124
s.x += 1
assert t.x == 125
lltype.free(s, flavor='raw')
lltype.free(s2, flavor='raw')
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:27,代码来源:test_ll2ctypes.py
示例8: test_carray_to_ll
def test_carray_to_ll():
A = lltype.Array(lltype.Signed, hints={'nolength': True})
a = lltype.malloc(A, 10, flavor='raw')
a2 = lltype.malloc(A, 10, flavor='raw')
a[0] = 100
a[1] = 101
a[2] = 110
ac = lltype2ctypes(a)
b = ctypes2lltype(lltype.Ptr(A), ac)
assert lltype.typeOf(b) == lltype.Ptr(A)
assert b == a
assert not (b != a)
assert a == b
assert not (a != b)
assert b != lltype.nullptr(A)
assert not (b == lltype.nullptr(A))
assert lltype.nullptr(A) != b
assert not (lltype.nullptr(A) == b)
assert b != a2
assert not (b == a2)
assert a2 != b
assert not (a2 == b)
assert b[2] == 110
b[2] *= 2
assert a[2] == 220
a[2] *= 3
assert b[2] == 660
lltype.free(a, flavor='raw')
lltype.free(a2, flavor='raw')
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:29,代码来源:test_ll2ctypes.py
示例9: test_forced_ptr_cast
def test_forced_ptr_cast(self):
import array
A = lltype.Array(lltype.Signed, hints={'nolength': True})
B = lltype.Array(lltype.Char, hints={'nolength': True})
a = lltype.malloc(A, 10, flavor='raw')
for i in range(10):
a[i] = i*i
b = rffi.cast(lltype.Ptr(B), a)
checker = array.array('l')
for i in range(10):
checker.append(i*i)
expected = checker.tostring()
for i in range(len(expected)):
assert b[i] == expected[i]
c = rffi.cast(rffi.VOIDP, a)
addr = lltype2ctypes(c)
#assert addr == ctypes.addressof(a._obj._ctypes_storage)
d = ctypes2lltype(rffi.VOIDP, addr)
assert lltype.typeOf(d) == rffi.VOIDP
assert c == d
e = rffi.cast(lltype.Ptr(A), d)
for i in range(10):
assert e[i] == i*i
c = lltype.nullptr(rffi.VOIDP.TO)
addr = rffi.cast(lltype.Signed, c)
assert addr == 0
lltype.free(a, flavor='raw')
assert not ALLOCATED # detects memory leaks in the test
开发者ID:antoine1fr,项目名称:pygirl,代码行数:34,代码来源:test_ll2ctypes.py
示例10: test_gcref_casts
def test_gcref_casts(self):
p0 = ctypes.c_void_p(0)
ref0 = ctypes2lltype(llmemory.GCREF, p0)
assert lltype.cast_ptr_to_int(ref0) == 0
assert llmemory.cast_ptr_to_adr(ref0) == llmemory.NULL
NODE = lltype.GcStruct('NODE')
assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref0) == lltype.nullptr(NODE)
node = lltype.malloc(NODE)
ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node)
intval = rffi.cast(lltype.Signed, node)
intval1 = rffi.cast(lltype.Signed, ref1)
assert intval == intval1
ref2 = ctypes2lltype(llmemory.GCREF, intval1)
assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref2) == node
开发者ID:alkorzt,项目名称:pypy,代码行数:21,代码来源:test_ll2ctypes.py
示例11: test_indirect_recursive_struct_more
def test_indirect_recursive_struct_more(self):
NODE = lltype.ForwardReference()
NODE2 = lltype.Struct('NODE2', ('ping', lltype.Ptr(NODE)))
NODE.become(lltype.Struct('NODE', ('pong', NODE2)))
# Building NODE2 first used to fail.
get_ctypes_type(NODE2)
CNODEPTR = get_ctypes_type(NODE)
pc = CNODEPTR()
pc.pong.ping = ctypes.pointer(pc)
p = ctypes2lltype(lltype.Ptr(NODE), ctypes.pointer(pc))
assert p.pong.ping == p
开发者ID:alkorzt,项目名称:pypy,代码行数:13,代码来源:test_ll2ctypes.py
示例12: test_c_callback_with_void_arg_2
def test_c_callback_with_void_arg_2(self):
ftest = []
def f(x):
ftest.append(x)
F = lltype.FuncType([lltype.Void], lltype.Void)
fn = lltype.functionptr(F, 'askjh', _callable=f, _void0=-5)
fn(-5)
assert ftest == [-5]
fn2 = lltype2ctypes(fn)
fn2()
assert ftest == [-5, -5]
fn3 = ctypes2lltype(lltype.Ptr(F), fn2)
fn3(-5)
assert ftest == [-5, -5, -5]
开发者ID:alkorzt,项目名称:pypy,代码行数:14,代码来源:test_ll2ctypes.py
示例13: test_recursive_struct_more
def test_recursive_struct_more(self):
NODE = lltype.ForwardReference()
NODE.become(lltype.Struct('NODE', ('value', lltype.Signed),
('next', lltype.Ptr(NODE))))
CNODEPTR = get_ctypes_type(NODE)
pc = CNODEPTR()
pc.value = 42
pc.next = ctypes.pointer(pc)
p = ctypes2lltype(lltype.Ptr(NODE), ctypes.pointer(pc))
assert p.value == 42
assert p.next == p
pc2 = lltype2ctypes(p)
assert pc2.contents.value == 42
assert pc2.contents.next.contents.value == 42
开发者ID:alkorzt,项目名称:pypy,代码行数:14,代码来源:test_ll2ctypes.py
示例14: test_funcptr2
def test_funcptr2(self):
FUNCTYPE = lltype.FuncType([rffi.CCHARP], lltype.Signed)
cstrlen = standard_c_lib.strlen
llstrlen = ctypes2lltype(lltype.Ptr(FUNCTYPE), cstrlen)
assert lltype.typeOf(llstrlen) == lltype.Ptr(FUNCTYPE)
p = rffi.str2charp("hi there")
res = llstrlen(p)
assert res == 8
cstrlen2 = lltype2ctypes(llstrlen)
cp = lltype2ctypes(p)
assert cstrlen2.restype == ctypes.c_long
res = cstrlen2(cp)
assert res == 8
rffi.free_charp(p)
assert not ALLOCATED # detects memory leaks in the test
开发者ID:antoine1fr,项目名称:pygirl,代码行数:15,代码来源:test_ll2ctypes.py
示例15: test_funcptr1
def test_funcptr1(self):
def dummy(n):
return n+1
FUNCTYPE = lltype.FuncType([lltype.Signed], lltype.Signed)
cdummy = lltype2ctypes(llhelper(lltype.Ptr(FUNCTYPE), dummy))
assert isinstance(cdummy,
ctypes.CFUNCTYPE(ctypes.c_long, ctypes.c_long))
res = cdummy(41)
assert res == 42
lldummy = ctypes2lltype(lltype.Ptr(FUNCTYPE), cdummy)
assert lltype.typeOf(lldummy) == lltype.Ptr(FUNCTYPE)
res = lldummy(41)
assert res == 42
assert not ALLOCATED # detects memory leaks in the test
开发者ID:antoine1fr,项目名称:pygirl,代码行数:15,代码来源:test_ll2ctypes.py
示例16: test_arrayoffloat
def test_arrayoffloat(self):
a = lltype.malloc(rffi.FLOATP.TO, 3, flavor='raw')
a[0] = rffi.r_singlefloat(0.0)
a[1] = rffi.r_singlefloat(1.1)
a[2] = rffi.r_singlefloat(2.2)
ac = lltype2ctypes(a, normalize=False)
assert ac.contents.items[0] == 0.0
assert abs(ac.contents.items[1] - 1.1) < 1E-6
assert abs(ac.contents.items[2] - 2.2) < 1E-6
b = ctypes2lltype(rffi.FLOATP, ac)
assert isinstance(b[0], rffi.r_singlefloat)
assert float(b[0]) == 0.0
assert isinstance(b[1], rffi.r_singlefloat)
assert abs(float(b[1]) - 1.1) < 1E-6
assert isinstance(b[2], rffi.r_singlefloat)
assert abs(float(b[2]) - 2.2) < 1E-6
开发者ID:antoine1fr,项目名称:pygirl,代码行数:16,代码来源:test_ll2ctypes.py
示例17: test_opaque_tagged_pointers
def test_opaque_tagged_pointers(self):
from pypy.rpython.annlowlevel import cast_base_ptr_to_instance
from pypy.rpython.annlowlevel import cast_instance_to_base_ptr
from pypy.rpython.lltypesystem import rclass
class Opaque(object):
llopaque = True
def hide(self):
ptr = cast_instance_to_base_ptr(self)
return lltype.cast_opaque_ptr(llmemory.GCREF, ptr)
@staticmethod
def show(gcref):
ptr = lltype.cast_opaque_ptr(lltype.Ptr(rclass.OBJECT), gcref)
return cast_base_ptr_to_instance(Opaque, ptr)
opaque = Opaque()
round = ctypes2lltype(llmemory.GCREF, lltype2ctypes(opaque.hide()))
assert Opaque.show(round) is opaque
开发者ID:ieure,项目名称:pypy,代码行数:20,代码来源:test_ll2ctypes.py
示例18: test_ll2ctypes_array_from_c
def test_ll2ctypes_array_from_c(self):
from pypy.rpython.lltypesystem import rffi, ll2ctypes
A = rffi.CArray(Char)
a = malloc(A, 6, flavor='raw')
a[0] = 'a'
a[1] = 'b'
a[2] = 'c'
a[3] = 'd'
a[4] = '\x00'
a[5] = '\x00'
# side effects when converting to c structure
c = ll2ctypes.lltype2ctypes(a)
a = ll2ctypes.ctypes2lltype(Ptr(A), c)
def llf():
s = ''
for i in range(4):
s += a[i]
print s
return s == 'abcd'
fn = self.getcompiled(llf)
assert fn()
开发者ID:AishwaryaKM,项目名称:python-tutorial,代码行数:21,代码来源:test_lltyped.py
示例19: test_varsized_struct
def test_varsized_struct(self):
S = lltype.Struct('S', ('x', lltype.Signed),
('a', lltype.Array(lltype.Char)))
s1 = lltype.malloc(S, 6, flavor='raw')
s1.x = 5
s1.a[2] = 'F'
sc = lltype2ctypes(s1, normalize=False)
assert isinstance(sc.contents, ctypes.Structure)
assert sc.contents.x == 5
assert sc.contents.a.length == 6
assert sc.contents.a.items[2] == ord('F')
sc.contents.a.items[3] = ord('P')
assert s1.a[3] == 'P'
s1.a[1] = 'y'
assert sc.contents.a.items[1] == ord('y')
# now go back to lltype...
res = ctypes2lltype(lltype.Ptr(S), sc)
assert res == s1
assert res.x == 5
assert len(res.a) == 6
lltype.free(s1, flavor='raw')
assert not ALLOCATED # detects memory leaks in the test
开发者ID:alkorzt,项目名称:pypy,代码行数:22,代码来源:test_ll2ctypes.py
示例20: test_with_explicit_length
def test_with_explicit_length(self):
A = lltype.Array(lltype.Signed)
a1 = lltype.malloc(A, 5, flavor='raw')
a1[0] = 42
c1 = lltype2ctypes(a1, normalize=False)
assert c1.contents.length == 5
assert c1.contents.items[0] == 42
res = ctypes2lltype(lltype.Ptr(A), c1)
assert res == a1
assert len(res) == 5
assert res[0] == 42
res[0] += 1
assert c1.contents.items[0] == 43
assert a1[0] == 43
a1[0] += 2
assert c1.contents.items[0] == 45
assert a1[0] == 45
c1.contents.items[0] += 3
assert res[0] == 48
assert a1[0] == 48
lltype.free(a1, flavor='raw')
assert not ALLOCATED # detects memory leaks in the test
开发者ID:alkorzt,项目名称:pypy,代码行数:22,代码来源:test_ll2ctypes.py
注:本文中的pypy.rpython.lltypesystem.ll2ctypes.ctypes2lltype函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论