Example #1
0
    def __init__(self, mem_desc, struct):
        """
        Attach to an existing queue or create a new one at the location defined by
        the pool parameter.

        Parameters
        ----------
        addr
            Address of an memory area.
        size
            Size of the memory area at addr
        struct
            Definition of an uctype structure.
            This describes the messages we will manage in the queue.
        magic
            Optional.
        """
        hdr_size = uctypes.sizeof(FIFO_HEADER)
        elem_size = uctypes.sizeof(struct)
        hdr = uctypes.struct(mem_desc.addr, FIFO_HEADER)
        entries = (mem_desc.size - hdr_size) // elem_size
        if hdr.magic != FIFO_MAGIC or hdr.elem_size != elem_size or hdr.elements != entries:
            print("MemFifo: init queue")
            hdr.rd_i = 0
            hdr.wr_i = 0
            hdr.elem_size = elem_size
            hdr.elements = entries
            hdr.magic = FIFO_MAGIC
            hdr.full = False
        self._hdr = hdr
        self._data_addr = mem_desc.addr + uctypes.sizeof(self._hdr)
        self._struct = struct
Example #2
0
 def __init__(self, path):
     self._fbdev = open(path, 'w+')
     self._fix_info_data = bytearray(sizeof(_fb_fix_screeninfo))
     fd = self._fbdev.fileno()
     ioctl(fd, _FBIOGET_FSCREENINFO, self._fix_info_data, mut=True)
     self._fix_info = struct(addressof(self._fix_info_data),
                             _fb_fix_screeninfo)
     self._var_info_data = bytearray(sizeof(_fb_var_screeninfo))
     ioctl(fd, _FBIOGET_VSCREENINFO, self._var_info_data, mut=True)
     self._var_info = struct(addressof(self._var_info_data),
                             _fb_var_screeninfo)
     self._fb_data = {}
Example #3
0
 def __init__(self):
     self._fbdev = open('/dev/fb0', 'w+')
     self._fix_info_data = bytearray(uctypes.sizeof(fb_fix_screeninfo))
     fd = self._fbdev.fileno()
     ioctl(fd, FBIOGET_FSCREENINFO, self._fix_info_data, mut=True)
     self._fix_info = uctypes.struct(uctypes.addressof(self._fix_info_data),
                                     fb_fix_screeninfo)
     self._var_info_data = bytearray(uctypes.sizeof(fb_var_screeninfo))
     ioctl(fd, FBIOGET_VSCREENINFO, self._var_info_data, mut=True)
     self._var_info = uctypes.struct(uctypes.addressof(self._var_info_data),
                                     fb_var_screeninfo)
     self._fb_data = {}
Example #4
0
 def codeobj2rawcode(codeobj):
     buf = bytearray(uctypes.sizeof(mp_raw_code_t_layout))
     rc = uctypes.struct(uctypes.addressof(buf), mp_raw_code_t_layout)
     rc.kind = 2  # MP_CODE_BYTECODE
     rc.fun_data = uctypes.addressof(codeobj.get_code())
     rc.const_table = uctypes.addressof(codeobj.get_const_table())
     return rc
Example #5
0
 def __init__(self, addr, size):
     self._head = uctypes.struct(addr, POOL_DEF)
     if self._head.magic != POOL_MAGIC:
         print("create new pool, addr={:x}, size={}".format(addr, size))
         self._head.magic = POOL_MAGIC
         self._head.top = addr + size
         self._head.addr = addr + uctypes.sizeof(POOL_DEF)
Example #6
0
 def __init__(self):
     self._beep_dev = open(_BEEP_DEV, 'b+')
     self._mixer = Mixer()
     self._pcm = PCM()
     self._tone_data = bytearray(sizeof(_input_event))
     self._tone_event = struct(addressof(self._tone_data), _input_event)
     self._timeout = Timeout(0, None)
     self._cancel = None
     self._lock = _thread.allocate_lock()
Example #7
0
def get_bluetooth_rfcomm_socket(address, channel):
    addr_data = bytearray(sizeof(sockaddr_rc))
    addr = struct(addressof(addr_data), sockaddr_rc)
    addr.rc_family = AF_BLUETOOTH
    str2ba(address, addr.rc_bdaddr)
    addr.rc_channel = channel

    sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)
    sock.connect(addr_data)
    return sock
    def __init__(self):
        self._sock = socket(_AF_BLUETOOTH, SOCK_STREAM, _BTPROTO_RFCOMM)

        addr_data = bytearray(sizeof(_sockaddr_rc))
        addr = struct(addressof(addr_data), _sockaddr_rc)

        addr.rc_family = _AF_BLUETOOTH
        _str2ba(_BDADDR_ANY, addr.rc_bdaddr)
        addr.rc_channel = 1

        self._sock.bind(addr_data)
Example #9
0
 def __init__(self, path):
     info_data = bytearray(sizeof(_SF_INFO))
     info = struct(addressof(info_data), _SF_INFO)
     self._file = _sf_open(path, _SMF_READ, info_data)
     if not self._file:
         raise SoundFileError(_sf_strerror(0))
     self._frames = info.frames
     self._samplerate = info.samplerate
     self._channels = info.channels
     self._format = info.format
     self._sections = info.sections
     self._seekable = bool(info.seekable)
    def __init__(self, name):
        # TODO: Need a way to convert name to MAC address (maybe)
        self._sock = socket(_AF_BLUETOOTH, SOCK_STREAM, _BTPROTO_RFCOMM)

        addr_data = bytearray(sizeof(_sockaddr_rc))
        addr = struct(addressof(addr_data), _sockaddr_rc)

        addr.rc_family = _AF_BLUETOOTH
        _str2ba(name, addr.rc_bdaddr)
        addr.rc_channel = 1

        self._sock.connect(addr_data)
 def handle_request(self):
     addr_data = bytearray(sizeof(sockaddr_rc))
     addr = struct(addressof(addr_data), sockaddr_rc)
     addr.rc_family = AF_BLUETOOTH
     str2ba(self.client_address[0], addr.rc_bdaddr)
     addr.rc_channel = self.client_address[1]
     self.socket.connect(addr_data)
     try:
         self.process_request(self.socket, self.client_address)
     except:
         self.socket.close()
         raise
    def __init__(self, server_address, RequestHandlerClass):
        self.server_address = server_address
        self.RequestHandlerClass = RequestHandlerClass

        self.socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)

        try:
            addr_data = bytearray(sizeof(sockaddr_rc))
            addr = struct(addressof(addr_data), sockaddr_rc)
            addr.rc_family = AF_BLUETOOTH
            str2ba(server_address[0], addr.rc_bdaddr)
            addr.rc_channel = server_address[1]
            self.socket.bind(addr_data)
            # self.server_address = self.socket.getsockname()
            self.socket.listen(self.request_queue_size)
        except:
            self.server_close()
            raise
Example #13
0
 def __getattr__(self, name):
     if name == "evtcode":
         return self._evtcode
     elif name == "evtname":
         return self._evtname
     elif name == "subevtcode":
         return self._subevtcode
     elif name == "subevtname":
         return self._subevtname
     elif name == "struct_size":
         return uctypes.sizeof(self.struct)
     elif name == "struct":
         if self._struct is None:
             return None
         return uctypes.struct(uctypes.addressof(self.data),
                               self._struct.get(self._module, self._struct),
                               uctypes.LITTLE_ENDIAN)
     elif name == "length":
         return len(self._data) if self._data else 0
     elif name == "data":
         return self._data[:self.length]
Example #14
0
def update_display(disp_list, value):
    # update the time on the displays
    font_name = font[selected_face]

    for tft in disp_list:
        number = value % 10
        value //= 10
        path = root + '//' + font_name + '//' + digits[number] + '.raw'
        with open(path, mode='rb') as f:
            buf = f.read(uctypes.sizeof(IMG_HEADER, uctypes.LITTLE_ENDIAN))
            header = uctypes.struct(uctypes.addressof(buf), IMG_HEADER,
                                    uctypes.LITTLE_ENDIAN)
            width = header.width
            height = header.height
            buffer = bytearray(width * height)
            f.readinto(buffer, width * height)
            f.close()
            fb = framebuf.FrameBuffer(buffer, width, height, framebuf.GS8)
            colorfb = fb.pixel(0, 0)
            tft.fill(colorfb)
            tft.blit(fb, (tft.width - width) // 2, (tft.height - height) // 2)
            tft.show()
import uctypes

S1 = {}
assert uctypes.sizeof(S1) == 0

S2 = {"a": uctypes.UINT8 | 0}
assert uctypes.sizeof(S2) == 1

S3 = {
    "a": uctypes.UINT8 | 0,
    "b": uctypes.UINT8 | 1,
}
assert uctypes.sizeof(S3) == 2

S4 = {
    "a": uctypes.UINT8 | 0,
    "b": uctypes.UINT32 | 4,
    "c": uctypes.UINT8 | 8,
}
assert uctypes.sizeof(S4) == 12

S5 = {
    "a": uctypes.UINT8 | 0,
    "b": uctypes.UINT32 | 4,
    "c": uctypes.UINT8 | 8,
    "d": uctypes.UINT32 | 0,
    "sub": (4, {
        "b0": uctypes.UINT8 | 0,
        "b1": uctypes.UINT8 | 1,
    }),
}
Example #16
0
def new(sdesc):
    buf = bytearray(uctypes.sizeof(sdesc))
    s = uctypes.struct(uctypes.addressof(buf), sdesc, uctypes.NATIVE)
    return s
try:
    import uctypes
except ImportError:
    print("SKIP")
    raise SystemExit

print(uctypes.sizeof({'f': uctypes.FLOAT32}))
print(uctypes.sizeof({'f': uctypes.FLOAT64}))
Example #18
0
def new(sdesc):
    buf = bytearray(uctypes.sizeof(sdesc))
    s = uctypes.struct(uctypes.addressof(buf), sdesc, uctypes.NATIVE)
    return s
Example #19
0
import sys
import uctypes

if sys.byteorder != "little":
    print("SKIP")
    sys.exit()

desc = {
    "ptr": (uctypes.PTR | 0, uctypes.UINT8),
    "ptr16": (uctypes.PTR | 0, uctypes.UINT16),
    "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}),
}

bytes = b"01"

addr = uctypes.addressof(bytes)
buf = addr.to_bytes(uctypes.sizeof(desc), "little")

S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.LITTLE_ENDIAN)

print(S.ptr[0])
assert S.ptr[0] == ord("0")
print(S.ptr[1])
assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b)
print(S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0]))
assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49)
Example #20
0
import uctypes

S1 = {}
assert uctypes.sizeof(S1) == 0

S2 = {"a": uctypes.UINT8 | 0}
assert uctypes.sizeof(S2) == 1

S3 = {
    "a": uctypes.UINT8 | 0,
    "b": uctypes.UINT8 | 1,
}
assert uctypes.sizeof(S3) == 2

S4 = {
    "a": uctypes.UINT8 | 0,
    "b": uctypes.UINT32 | 4,
    "c": uctypes.UINT8 | 8,
}
assert uctypes.sizeof(S4) == 12

S5 = {
    "a": uctypes.UINT8 | 0,
    "b": uctypes.UINT32 | 4,
    "c": uctypes.UINT8 | 8,
    "d": uctypes.UINT32 | 0,
    "sub": (4, {
        "b0": uctypes.UINT8 | 0,
        "b1": uctypes.UINT8 | 1,
    }),
}
Example #21
0
_I2C_FUNC_SMBUS_WRITE_WORD_DATA = const(0x00400000)
_I2C_FUNC_SMBUS_PROC_CALL = const(0x00800000)
_I2C_FUNC_SMBUS_READ_BLOCK_DATA = const(0x01000000)
_I2C_FUNC_SMBUS_WRITE_BLOCK_DATA = const(0x02000000)
_I2C_FUNC_SMBUS_READ_I2C_BLOCK = const(0x04000000)
_I2C_FUNC_SMBUS_WRITE_I2C_BLOCK = const(0x08000000)

_I2C_SMBUS_BLOCK_MAX = const(32)

_i2c_smbus_data = {
    'byte': UINT8 | 0,
    'word': UINT16 | 0,
    'block': (ARRAY | 0, UINT8 | (_I2C_SMBUS_BLOCK_MAX + 2))
}

_size_of_i2c_smbus_data = sizeof(_i2c_smbus_data)

_I2C_SMBUS_READ = const(1)
_I2C_SMBUS_WRITE = const(0)

_I2C_SMBUS_QUICK = const(0)
_I2C_SMBUS_BYTE = const(1)
_I2C_SMBUS_BYTE_DATA = const(2)
_I2C_SMBUS_WORD_DATA = const(3)
_I2C_SMBUS_PROC_CALL = const(4)
_I2C_SMBUS_BLOCK_DATA = const(5)
_I2C_SMBUS_I2C_BLOCK_BROKEN = const(6)
_I2C_SMBUS_BLOCK_PROC_CALL = const(7)
_I2C_SMBUS_I2C_BLOCK_DATA = const(8)

# from linux/i2c-dev.h
Example #22
0
try:
    import uctypes
except ImportError:
    print("SKIP")
    raise SystemExit

desc = {
    "f1": 0 | uctypes.UINT32,
    "f2": 4 | uctypes.UINT8,
}

# uctypes.NATIVE is default
print(uctypes.sizeof(desc) == uctypes.sizeof(desc, uctypes.NATIVE))

# Here we assume that that we run on a platform with convential ABI
# (which rounds up structure size based on max alignment). For platforms
# where that doesn't hold, this tests should be just disabled in the runner.
print(
    uctypes.sizeof(desc, uctypes.NATIVE) > uctypes.sizeof(
        desc, uctypes.LITTLE_ENDIAN))

# When taking sizeof of instantiated structure, layout type param
# is prohibited (because structure already has its layout type).
s = uctypes.struct(0, desc, uctypes.LITTLE_ENDIAN)
try:
    uctypes.sizeof(s, uctypes.LITTLE_ENDIAN)
except TypeError:
    print("TypeError")
Example #23
0
    "sub": (
        0,
        {
            "b1":
            uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
            "b2":
            uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
        },
    ),
}

data = bytearray(b"01234567")

S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)

print(uctypes.sizeof(S.arr))
assert uctypes.sizeof(S.arr) == 2

print(uctypes.sizeof(S.arr2))
assert uctypes.sizeof(S.arr2) == 2

print(uctypes.sizeof(S.arr3))

try:
    print(uctypes.sizeof(S.arr3[0]))
except TypeError:
    print("TypeError")

print(uctypes.sizeof(S.arr4))
assert uctypes.sizeof(S.arr4) == 6
Example #24
0
    "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
    # arr2 is array at offset 0, size 2, of structures defined recursively
    "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
    "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
    "arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}),
    "sub": (0, {
        'b1': uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
        'b2': uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
    }),
}

data = bytearray(b"01234567")

S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)

print(uctypes.sizeof(S.arr))
assert uctypes.sizeof(S.arr) == 2

print(uctypes.sizeof(S.arr2))
assert uctypes.sizeof(S.arr2) == 2

print(uctypes.sizeof(S.arr3))

try:
    print(uctypes.sizeof(S.arr3[0]))
except TypeError:
    print("TypeError")

print(uctypes.sizeof(S.arr4))
assert uctypes.sizeof(S.arr4) == 6
Example #25
0
_I2C_FUNC_SMBUS_WRITE_WORD_DATA = 0x00400000
_I2C_FUNC_SMBUS_PROC_CALL = 0x00800000
_I2C_FUNC_SMBUS_READ_BLOCK_DATA = 0x01000000
_I2C_FUNC_SMBUS_WRITE_BLOCK_DATA = 0x02000000
_I2C_FUNC_SMBUS_READ_I2C_BLOCK = 0x04000000
_I2C_FUNC_SMBUS_WRITE_I2C_BLOCK = 0x08000000

_I2C_SMBUS_BLOCK_MAX = 32

_i2c_smbus_data = {
    'byte': uctypes.UINT8 | 0,
    'word': uctypes.UINT16 | 0,
    'block': (uctypes.ARRAY | 0, uctypes.UINT8 | (_I2C_SMBUS_BLOCK_MAX + 2))
}

_size_of_i2c_smbus_data = uctypes.sizeof(_i2c_smbus_data)

_I2C_SMBUS_READ = 1
_I2C_SMBUS_WRITE = 0

_I2C_SMBUS_QUICK = 0
_I2C_SMBUS_BYTE = 1
_I2C_SMBUS_BYTE_DATA = 2
_I2C_SMBUS_WORD_DATA = 3
_I2C_SMBUS_PROC_CALL = 4
_I2C_SMBUS_BLOCK_DATA = 5
_I2C_SMBUS_I2C_BLOCK_BROKEN = 6
_I2C_SMBUS_BLOCK_PROC_CALL = 7
_I2C_SMBUS_I2C_BLOCK_DATA = 8

# from linux/i2c-dev.h
Example #26
0
def overlay_struct(buf, desc):
    desc_size = uctypes.sizeof(desc, uctypes.BIG_ENDIAN)
    if desc_size > len(buf):
        raise ValueError("desc is too big (%d > %d)" % (desc_size, len(buf)))
    return uctypes.struct(uctypes.addressof(buf), desc, uctypes.BIG_ENDIAN)
try:
    import uctypes
except ImportError:
    print("SKIP")
    raise SystemExit

print(uctypes.sizeof({'f':uctypes.FLOAT32}))
print(uctypes.sizeof({'f':uctypes.FLOAT64}))
Example #28
0
# Test uctypes array, load and store, with array size > 1

try:
    import uctypes
except ImportError:
    print("SKIP")
    raise SystemExit

N = 5

for endian in ("NATIVE", "LITTLE_ENDIAN", "BIG_ENDIAN"):
    for type_ in ("INT8", "UINT8", "INT16", "UINT16", "INT32", "UINT32",
                  "INT64", "UINT64"):
        desc = {"arr": (uctypes.ARRAY | 0, getattr(uctypes, type_) | N)}
        sz = uctypes.sizeof(desc)
        data = bytearray(sz)
        s = uctypes.struct(uctypes.addressof(data), desc,
                           getattr(uctypes, endian))
        for i in range(N):
            try:
                s.arr[i] = i - 2
            except OverflowError:
                print("OverflowError")
        print(endian, type_, sz, *(s.arr[i] for i in range(N)))
Example #29
0
if sys.byteorder != "little":
    print("SKIP")
    raise SystemExit

desc = {
    "ptr": (uctypes.PTR | 0, uctypes.UINT8),
    "ptr16": (uctypes.PTR | 0, uctypes.UINT16),
    "ptr2": (uctypes.PTR | 0, {
        "b": uctypes.UINT8 | 0
    }),
}

bts = b"01"

addr = uctypes.addressof(bts)
buf = addr.to_bytes(uctypes.sizeof(desc, uctypes.LITTLE_ENDIAN), "little")

S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.LITTLE_ENDIAN)

print(addr == int(S.ptr))
print(addr == int(S.ptr2))

print(S.ptr[0])
assert S.ptr[0] == ord("0")
print(S.ptr[1])
assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b)
print(S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0]))
Example #30
0
if sys.byteorder != "little":
    print("SKIP")
    sys.exit()

desc = {
    "ptr": (uctypes.PTR | 0, uctypes.UINT8),
    "ptr16": (uctypes.PTR | 0, uctypes.UINT16),
    "ptr2": (uctypes.PTR | 0, {
        "b": uctypes.UINT8 | 0
    }),
}

bytes = b"01"

addr = uctypes.addressof(bytes)
buf = addr.to_bytes(uctypes.sizeof(desc))

S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.LITTLE_ENDIAN)

print(S.ptr[0])
assert S.ptr[0] == ord("0")
print(S.ptr[1])
assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b)
print(S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0]))
assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49)
Example #31
0
import uctypes

desc = {
    # arr is array at offset 0, of UINT8 elements, array size is 2
    "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
    # arr2 is array at offset 0, size 2, of structures defined recursively
    "arr2": (uctypes.ARRAY | 0, 2, {
        "b": uctypes.UINT8 | 0
    }),
    "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
}

data = bytearray(b"01234567")

S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)

print(uctypes.sizeof(S.arr))
assert uctypes.sizeof(S.arr) == 2

print(uctypes.sizeof(S.arr2))
assert uctypes.sizeof(S.arr2) == 2

print(uctypes.sizeof(S.arr3))

try:
    print(uctypes.sizeof(S.arr3[0]))
except TypeError:
    print("TypeError")
Example #32
0
import uctypes

desc = {
    # arr is array at offset 0, of UINT8 elements, array size is 2
    "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
    # arr2 is array at offset 0, size 2, of structures defined recursively
    "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
    "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
}

data = bytearray(b"01234567")

S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)

print(uctypes.sizeof(S.arr))
assert uctypes.sizeof(S.arr) == 2

print(uctypes.sizeof(S.arr2))
assert uctypes.sizeof(S.arr2) == 2

print(uctypes.sizeof(S.arr3))

try:
    print(uctypes.sizeof(S.arr3[0]))
except TypeError:
    print("TypeError")
Example #33
0
import lvgl as lv
import ustruct
import uctypes

# Calculate pointer size on current machine, and corresponding fmt

ptr_size = uctypes.sizeof({'p': (uctypes.PTR, uctypes.VOID)})
fmt_options = {2: 'H', 4: 'L', 8: 'Q'}
buf_fmt = fmt_options[ptr_size] if ptr_size in fmt_options else None


def aligned_buf(buf, alignment):
    """Return an aligned buffer

    Given a buffer, return a memory view within that buffer, which starts
    at an aligned address in RAM.
    The returned memory view is possibly smaller.

    !! You must keep a reference to the original buffer to prevent the
       garbage collector from collecting the aligned view!

    Arguments:

    buf         -- An object that implements buffer protocol
    alignment   -- Integer value

    """

    p = lv.C_Pointer()
    p.ptr_val = buf
    if not buf_fmt: return None
import uctypes

if sys.byteorder != "little":
    print("SKIP")
    sys.exit()


desc = {
    "ptr": (uctypes.PTR | 0, uctypes.UINT8),
    "ptr16": (uctypes.PTR | 0, uctypes.UINT16),
    "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}),
}

bytes = b"01"

addr = uctypes.addressof(bytes)
buf = addr.to_bytes(uctypes.sizeof(desc))

S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.NATIVE)

print(S.ptr[0])
assert S.ptr[0] == ord("0")
print(S.ptr[1])
assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b)
print (S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0]))
assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49)
try:
    import uctypes
except ImportError:
    print("SKIP")
    raise SystemExit

desc = {
    "f1": 0 | uctypes.UINT32,
    "f2": 4 | uctypes.UINT8,
}


# uctypes.NATIVE is default
print(uctypes.sizeof(desc) == uctypes.sizeof(desc, uctypes.NATIVE))

# Here we assume that that we run on a platform with convential ABI
# (which rounds up structure size based on max alignment). For platforms
# where that doesn't hold, this tests should be just disabled in the runner.
print(uctypes.sizeof(desc, uctypes.NATIVE) > uctypes.sizeof(desc, uctypes.LITTLE_ENDIAN))

# When taking sizeof of instantiated structure, layout type param
# is prohibited (because structure already has its layout type).
s = uctypes.struct(0, desc, uctypes.LITTLE_ENDIAN)
try:
    uctypes.sizeof(s, uctypes.LITTLE_ENDIAN)
except TypeError:
    print("TypeError")
Example #36
0
if sys.byteorder != "little":
    print("SKIP")
    raise SystemExit

desc = {
    "ptr": (uctypes.PTR | 0, uctypes.UINT8),
    "ptr16": (uctypes.PTR | 0, uctypes.UINT16),
    "ptr2": (uctypes.PTR | 0, {
        "b": uctypes.UINT8 | 0
    }),
}

bytes = b"01"

addr = uctypes.addressof(bytes)
buf = addr.to_bytes(uctypes.sizeof(desc), sys.byteorder)

S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.NATIVE)

print(S.ptr[0])
assert S.ptr[0] == ord("0")
print(S.ptr[1])
assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b)
print(S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0]))
assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49)
Example #37
0
def make_struct(desc):
    desc_size = uctypes.sizeof(desc, uctypes.BIG_ENDIAN)
    buf = bytearray(desc_size)
    return buf, uctypes.struct(uctypes.addressof(buf), desc, uctypes.BIG_ENDIAN)
Example #38
0
    raise SystemExit

if sys.byteorder != "little":
    print("SKIP")
    raise SystemExit


desc = {
    "ptr": (uctypes.PTR | 0, uctypes.UINT8),
    "ptr16": (uctypes.PTR | 0, uctypes.UINT16),
    "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}),
}

bytes = b"01"

addr = uctypes.addressof(bytes)
buf = addr.to_bytes(uctypes.sizeof(desc), "little")

S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.NATIVE)

print(S.ptr[0])
assert S.ptr[0] == ord("0")
print(S.ptr[1])
assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b)
print(S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0]))
assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49)