我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用ctypes.Union()。
def new_union_type(self, name): return self._new_struct_or_union('union', name, ctypes.Union)
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)
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, RemotePtr32): # Pointer to remote32 bits process return RemoteStructurePointer32.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 # if this is a string: just cast the read value to string if ftype._type_ == ctypes.c_char: # Use issubclass instead ? return s.split("\x00", 1)[0] elif ftype._type_ == ctypes.c_wchar: # Use issubclass instead ? # Decode from utf16 -> size /=2 | put it in a wchar array | split at the first "\x00" return (ftype._type_ * (fsize / 2)).from_buffer_copy(s.decode('utf16'))[:].split("\x00", 1)[0] # Sorry.. # I am pretty sur something smarter is possible.. 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
def create_ctype(self): self.ctype = type(self.name.encode('utf-8'), (ctypes.Union,), dict( _fields_ = [ (name, get_ctype(value)) for name, value in self.fields]))
def must_fill(field): return isinstance(field, (Union, Structure, Array))
def check_ctypes_datatype(datatype): valid_datatypes = ( ctypes._SimpleCData, ctypes.Union, ctypes.BigEndianStructure, ctypes.LittleEndianStructure, ctypes.Structure, ) for t in valid_datatypes: if issubclass(datatype, t): return raise TypeError('datatype must be a ctypes data type, but was {}'.format(datatype.__name__))
def define_union(name, *args): return type(name, (Union,), {'_fields_': args, '__repr__': repr_fn})
def ctypes_to_C(ctype): """Map ctypes types to C types.""" if issubclass(ctype, ctypes.Structure): return 'struct %s' % ctype.__name__ elif issubclass(ctype, ctypes.Union): return 'union %s' % ctype.__name__ elif ctype.__name__.startswith('c_'): # FIXME: Is there a better way of extracting the C typename ? # Here, we're following the ctypes convention that each basic type has # the format c_X_p, where X is the C typename, for instance `int` or `float`. return ctype.__name__[2:-2] else: raise TypeError('Unrecognised %s during converstion to C type' % str(ctype))