File: marshal_basic.py

package info (click to toggle)
micropython 1.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 48,944 kB
  • sloc: ansic: 317,850; python: 59,539; xml: 4,241; makefile: 3,530; sh: 1,421; javascript: 744; asm: 681; cpp: 45; exp: 11; pascal: 6
file content (38 lines) | stat: -rw-r--r-- 1,166 bytes parent folder | download
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
33
34
35
36
37
38
# Test the marshal module, basic functionality.

try:
    import marshal

    (lambda: 0).__code__
except (AttributeError, ImportError):
    print("SKIP")
    raise SystemExit

ftype = type(lambda: 0)

# Test basic dumps and loads.
print(ftype(marshal.loads(marshal.dumps((lambda: a).__code__)), {"a": 4})())

# Test dumps of a result from compile().
ftype(marshal.loads(marshal.dumps(compile("print(a)", "", "exec"))), {"print": print, "a": 5})()

# Test marshalling a function with arguments.
print(ftype(marshal.loads(marshal.dumps((lambda x, y: x + y).__code__)), {})(1, 2))

# Test marshalling a function with default arguments.
print(ftype(marshal.loads(marshal.dumps((lambda x=0: x).__code__)), {})("arg"))

# Test marshalling a function containing constant objects (a tuple).
print(ftype(marshal.loads(marshal.dumps((lambda: (None, ...)).__code__)), {})())

# Test instantiating multiple code's with different globals dicts.
code = marshal.loads(marshal.dumps((lambda: a).__code__))
f1 = ftype(code, {"a": 1})
f2 = ftype(code, {"a": 2})
print(f1(), f2())

# Test unmarshallable object.
try:
    marshal.dumps(type)
except ValueError:
    print("ValueError")