本文整理汇总了Python中ctypes.Union方法的典型用法代码示例。如果您正苦于以下问题:Python ctypes.Union方法的具体用法?Python ctypes.Union怎么用?Python ctypes.Union使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctypes
的用法示例。
在下文中一共展示了ctypes.Union方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_padded_union
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def test_padded_union(self):
dt = np.dtype(dict(
names=['a', 'b'],
offsets=[0, 0],
formats=[np.uint16, np.uint32],
itemsize=5,
))
ct = np.ctypeslib.as_ctypes_type(dt)
assert_(issubclass(ct, ctypes.Union))
assert_equal(ctypes.sizeof(ct), dt.itemsize)
assert_equal(ct._fields_, [
('a', ctypes.c_uint16),
('b', ctypes.c_uint32),
('', ctypes.c_char * 5), # padding
])
示例2: test_union_packed
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def test_union_packed(self):
class Struct(ctypes.Structure):
_fields_ = [
('one', ctypes.c_uint8),
('two', ctypes.c_uint32)
]
_pack_ = 1
class Union(ctypes.Union):
_pack_ = 1
_fields_ = [
('a', ctypes.c_uint8),
('b', ctypes.c_uint16),
('c', ctypes.c_uint32),
('d', Struct),
]
expected = np.dtype(dict(
names=['a', 'b', 'c', 'd'],
formats=['u1', np.uint16, np.uint32, [('one', 'u1'), ('two', np.uint32)]],
offsets=[0, 0, 0, 0],
itemsize=ctypes.sizeof(Union)
))
self.check(Union, expected)
示例3: _handle_field_getattr
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def _handle_field_getattr(self, ftype, fosset, fsize):
s = self._target.read_memory(self._base_addr + fosset, fsize)
if ftype in self._field_type_to_remote_type:
return self._field_type_to_remote_type[ftype].from_buffer_with_target(bytearray(s), target=self._target).value
if issubclass(ftype, _ctypes._Pointer): # Pointer
return RemoteStructurePointer.from_buffer_with_target_and_ptr_type(bytearray(s), target=self._target, ptr_type=ftype)
if issubclass(ftype, RemotePtr64): # Pointer to remote64 bits process
return RemoteStructurePointer64.from_buffer_with_target_and_ptr_type(bytearray(s), target=self._target, ptr_type=ftype)
if issubclass(ftype, RemoteStructureUnion): # Structure|Union already transfomed in remote
return ftype(self._base_addr + fosset, self._target)
if issubclass(ftype, ctypes.Structure): # Structure that must be transfomed
return RemoteStructure.from_structure(ftype)(self._base_addr + fosset, self._target)
if issubclass(ftype, ctypes.Union): # Union that must be transfomed
return RemoteUnion.from_structure(ftype)(self._base_addr + fosset, self._target)
if issubclass(ftype, _ctypes.Array): # Arrays
return create_remote_array(ftype._type_, ftype._length_)(self._base_addr + fosset, self._target)
# Normal types
# Follow the ctypes usage: if it's not directly inherited from _SimpleCData
# We do not apply the .value
# Seems weird but it's mandatory AND useful :D (in pe_parse)
if _SimpleCData not in ftype.__bases__:
return ftype.from_buffer(bytearray(s))
return ftype.from_buffer(bytearray(s)).value
示例4: _struct_to_dict
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def _struct_to_dict(struct_obj):
results = {}
for fname, ctype in struct_obj.__class__._fields_:
val = getattr(struct_obj, fname)
if fname == "next":
# Already covered by the trick below
continue
if issubclass(ctype, (Structure, ctypes.Union)):
results[fname] = _struct_to_dict(val)
elif val and hasattr(val, "contents"):
# Let's resolve recursive pointers
if hasattr(val.contents, "next"):
results[fname] = _resolve_list(val)
else:
results[fname] = val
else:
results[fname] = val
return results
##############################
####### WinAPI handles #######
##############################
示例5: as_ctypes_type
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def as_ctypes_type(dtype):
"""
Convert a dtype into a ctypes type.
Parameters
----------
dtype : dtype
The dtype to convert
Returns
-------
ctypes
A ctype scalar, union, array, or struct
Raises
------
NotImplementedError
If the conversion is not possible
Notes
-----
This function does not losslessly round-trip in either direction.
``np.dtype(as_ctypes_type(dt))`` will:
- insert padding fields
- reorder fields to be sorted by offset
- discard field titles
``as_ctypes_type(np.dtype(ctype))`` will:
- discard the class names of ``Structure``s and ``Union``s
- convert single-element ``Union``s into single-element ``Structure``s
- insert padding fields
"""
return _ctype_from_dtype(_dtype(dtype))
示例6: test_union
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def test_union(self):
dt = np.dtype(dict(
names=['a', 'b'],
offsets=[0, 0],
formats=[np.uint16, np.uint32]
))
ct = np.ctypeslib.as_ctypes_type(dt)
assert_(issubclass(ct, ctypes.Union))
assert_equal(ctypes.sizeof(ct), dt.itemsize)
assert_equal(ct._fields_, [
('a', ctypes.c_uint16),
('b', ctypes.c_uint32),
])
示例7: test_union
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def test_union(self):
class Union(ctypes.Union):
_fields_ = [
('a', ctypes.c_uint8),
('b', ctypes.c_uint16),
]
expected = np.dtype(dict(
names=['a', 'b'],
formats=[np.uint8, np.uint16],
offsets=[0, 0],
itemsize=2
))
self.check(Union, expected)
示例8: test_ipy2_gh536
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def test_ipy2_gh536(self):
"""https://github.com/IronLanguages/ironpython2/issues/536"""
import ctypes
class bar(ctypes.Union):
_fields_ = [("t", ctypes.c_uint), ("b", ctypes.c_uint)]
o = bar()
o.t = 1
self.assertEqual(1, o.b)
self.assertEqual(o.t, o.b)
示例9: is_union_type
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def is_union_type(x):
return issubclass(x, ctypes.Union)
# ### My types ### #
# # 64bits pointer types # #
# I know direct inheritance from _SimpleCData seems bad
# But it seems to be the only way to have the normal
# ctypes.Structure way of working (need to investigate)
示例10: new_union_type
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def new_union_type(self, name):
return self._new_struct_or_union('union', name, ctypes.Union)
示例11: as_ctypes_type
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def as_ctypes_type(dtype):
r"""
Convert a dtype into a ctypes type.
Parameters
----------
dtype : dtype
The dtype to convert
Returns
-------
ctype
A ctype scalar, union, array, or struct
Raises
------
NotImplementedError
If the conversion is not possible
Notes
-----
This function does not losslessly round-trip in either direction.
``np.dtype(as_ctypes_type(dt))`` will:
- insert padding fields
- reorder fields to be sorted by offset
- discard field titles
``as_ctypes_type(np.dtype(ctype))`` will:
- discard the class names of `ctypes.Structure`\ s and
`ctypes.Union`\ s
- convert single-element `ctypes.Union`\ s into single-element
`ctypes.Structure`\ s
- insert padding fields
"""
return _ctype_from_dtype(_dtype(dtype))
示例12: _return_context
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Union [as 别名]
def _return_context(value):
"""return TVMContext"""
# use bit unpacking from int64 view
# We use this to get around ctypes issue on Union of Structure
data = struct.pack("=q", value.v_int64)
arr = struct.unpack("=ii", data)
return TVMContext(arr[0], arr[1])