當前位置: 首頁>>代碼示例>>Python>>正文


Python ctypes.Union方法代碼示例

本文整理匯總了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
        ]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_ctypeslib.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:24,代碼來源:test_dtype.py

示例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 
開發者ID:sogeti-esec-lab,項目名稱:LKD,代碼行數:25,代碼來源:remotectypes.py

示例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 #######
############################## 
開發者ID:secdev,項目名稱:scapy,代碼行數:24,代碼來源:structures.py

示例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)) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:37,代碼來源:ctypeslib.py

示例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),
        ]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:16,代碼來源:test_ctypeslib.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:15,代碼來源:test_dtype.py

示例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) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:12,代碼來源:test_regressions.py

示例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) 
開發者ID:sogeti-esec-lab,項目名稱:LKD,代碼行數:12,代碼來源:remotectypes.py

示例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) 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:4,代碼來源:backend_ctypes.py

示例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)) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:41,代碼來源:ctypeslib.py

示例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]) 
開發者ID:mlperf,項目名稱:training_results_v0.6,代碼行數:9,代碼來源:types.py


注:本文中的ctypes.Union方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。