本文整理汇总了Python中ffi.FFI.sizeof方法的典型用法代码示例。如果您正苦于以下问题:Python FFI.sizeof方法的具体用法?Python FFI.sizeof怎么用?Python FFI.sizeof使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ffi.FFI
的用法示例。
在下文中一共展示了FFI.sizeof方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sizeof_cdata
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import sizeof [as 别名]
def test_sizeof_cdata(self):
ffi = FFI(backend=self.Backend())
assert ffi.sizeof(ffi.new("short")) == SIZE_OF_PTR
assert ffi.sizeof(ffi.cast("short", 123)) == SIZE_OF_SHORT
#
a = ffi.new("int[]", [10, 11, 12, 13, 14])
assert len(a) == 5
assert ffi.sizeof(a) == 5 * SIZE_OF_INT
示例2: test_ffi_nonfull_struct
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import sizeof [as 别名]
def test_ffi_nonfull_struct():
py.test.skip("XXX")
ffi = FFI()
ffi.cdef(
"""
struct sockaddr {
int sa_family;
...;
};
"""
)
py.test.raises(VerificationMissing, ffi.sizeof, "struct sockaddr")
ffi.verify(
"""
#include <sys/types.h>
#include <sys/socket.h>
"""
)
assert ffi.sizeof("struct sockaddr") == 14 + ffi.sizeof(int)
示例3: test_sizeof_type
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import sizeof [as 别名]
def test_sizeof_type(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
struct foo { int a; short b, c, d; };
union foo { int a; short b, c, d; };
""")
for c_type, expected_size in [
('char', 1),
('unsigned int', 4),
('char *', SIZE_OF_LONG),
('int[5]', 20),
('struct foo', 12),
('union foo', 4),
]:
size = ffi.sizeof(c_type)
assert size == expected_size
示例4: test_bitfield
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import sizeof [as 别名]
def test_bitfield(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("struct foo { int a:10, b:20, c:3; };")
assert ffi.sizeof("struct foo") == 8
s = ffi.new("struct foo")
s.a = 511
py.test.raises(OverflowError, "s.a = 512")
py.test.raises(OverflowError, "s[0].a = 512")
assert s.a == 511
s.a = -512
py.test.raises(OverflowError, "s.a = -513")
py.test.raises(OverflowError, "s[0].a = -513")
assert s.a == -512
s.c = 3
assert s.c == 3
py.test.raises(OverflowError, "s.c = 4")
py.test.raises(OverflowError, "s[0].c = 4")
s.c = -4
assert s.c == -4