summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/struct_micropython.py
blob: 4b9dfe1378a301ced58a0e2a1d29e135e83f47d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# test MicroPython-specific features of struct

try:
    import ustruct as struct
except:
    try:
        import struct
    except ImportError:
        print("SKIP")
        raise SystemExit

class A():
    pass

# pack and unpack objects
o = A()
s = struct.pack("<O", o)
o2 = struct.unpack("<O", s)
print(o is o2[0])

# pack and unpack pointer to a string
# This requires uctypes to get the address of the string and instead of
# putting this in a dedicated test that can be skipped we simply pass
# if the import fails.
try:
    import uctypes
    o = uctypes.addressof('abc')
    s = struct.pack("<S", o)
    o2 = struct.unpack("<S", s)
    assert o2[0] == 'abc'
except ImportError:
    pass