Debian Codegraph @

testing/test_serializer.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import os
4 import subprocess
5 import sys
6 from pathlib import Path
7
8 import pytest
9
10 import execnet
11
12 # We use the execnet folder in order to avoid triggering a missing apipkg.
13 pyimportdir = os.fspath(Path(execnet.__file__).parent)
14
15
16 class PythonWrapper:
17 def __init__(self, executable: str, tmp_path: Path) -> None:
18 self.executable = executable
19 self.tmp_path = tmp_path
20
21 def dump(self, obj_rep: str) -> bytes:
22 script_file = self.tmp_path.joinpath("dump.py")
23 script_file.write_text(
24 f"""
25 import sys
26 sys.path.insert(0, {pyimportdir!r})
27 import gateway_base as serializer
28 sys.stdout = sys.stdout.detach()
29 sys.stdout.write(serializer.dumps_internal({obj_rep}))
30 """
31 )
32 res = subprocess.run(
33 [str(self.executable), str(script_file)], capture_output=True, check=True
34 )
35 return res.stdout
36
37 def load(self, data: bytes) -> list[str]:
38 script_file = self.tmp_path.joinpath("load.py")
39 script_file.write_text(
40 rf"""
41 import sys
42 sys.path.insert(0, {pyimportdir!r})
43 import gateway_base as serializer
44 from io import BytesIO
45 data = {data!r}
46 io = BytesIO(data)
47 loader = serializer.Unserializer(io)
48 obj = loader.load()
49 sys.stdout.write(type(obj).__name__ + "\n")
50 sys.stdout.write(repr(obj))
51 """
52 )
53 res = subprocess.run(
54 [str(self.executable), str(script_file)],
55 capture_output=True,
56 check=True,
57 )
58
59 return res.stdout.decode("ascii").splitlines()
60
61 def __repr__(self) -> str:
62 return f"<PythonWrapper for {self.executable}>"
63
64
65 @pytest.fixture
66 def py3(tmp_path: Path) -> PythonWrapper:
67 return PythonWrapper(sys.executable, tmp_path)
68
69
70 @pytest.fixture
71 def dump(py3: PythonWrapper):
72 return py3.dump
73
74
75 @pytest.fixture
76 def load(py3: PythonWrapper):
77 return py3.load
78
79
80 simple_tests = [
81 # expected before/after repr
82 ("int", "4"),
83 ("float", "3.25"),
84 ("complex", "(1.78+3.25j)"),
85 ("list", "[1, 2, 3]"),
86 ("tuple", "(1, 2, 3)"),
87 ("dict", "{(1, 2, 3): 32}"),
88 ]
89
90
91 @pytest.mark.parametrize(["tp_name", "repr"], simple_tests)
92 def test_simple(tp_name, repr, dump, load) -> None:
93 p = dump(repr)
94 tp, v = load(p)
95 assert tp == tp_name
96 assert v == repr
97
98
99 def test_set(load, dump) -> None:
100 p = dump("set((1, 2, 3))")
101
102 tp, v = load(p)
103 assert tp == "set"
104 # assert v == "{1, 2, 3}" # ordering prevents this assertion
105 assert v.startswith("{") and v.endswith("}")
106 assert "1" in v and "2" in v and "3" in v
107 p = dump("set()")
108 tp, v = load(p)
109 assert tp == "set"
110 assert v == "set()"
111
112
113 def test_frozenset(load, dump):
114 p = dump("frozenset((1, 2, 3))")
115 tp, v = load(p)
116 assert tp == "frozenset"
117 assert v == "frozenset({1, 2, 3})"
118
119
120 def test_long(load, dump) -> None:
121 really_big = "9223372036854775807324234"
122 p = dump(really_big)
123 tp, v = load(p)
124 assert tp == "int"
125 assert v == really_big
126
127
128 def test_bytes(dump, load) -> None:
129 p = dump("b'hi'")
130 tp, v = load(p)
131 assert tp == "bytes"
132 assert v == "b'hi'"
133
134
135 def test_str(dump, load) -> None:
136 p = dump("'xyz'")
137 tp, s = load(p)
138 assert tp == "str"
139 assert s == "'xyz'"
140
141
142 def test_unicode(load, dump) -> None:
143 p = dump("u'hi'")
144 tp, s = load(p)
145 assert tp == "str"
146 assert s == "'hi'"
147
148
149 def test_bool(dump, load) -> None:
150 p = dump("True")
151 tp, s = load(p)
152 assert s == "True"
153 assert tp == "bool"
154
155
156 def test_none(dump, load) -> None:
157 p = dump("None")
158 _tp, s = load(p)
159 assert s == "None"
160
161
162 def test_tuple_nested_with_empty_in_between(dump, load) -> None:
163 p = dump("(1, (), 3)")
164 tp, s = load(p)
165 assert tp == "tuple"
166 assert s == "(1, (), 3)"
167
168
169 def test_py2_string_loads() -> None:
170 """Regression test for #267."""
171 assert execnet.loads(b"\x02M\x00\x00\x00\x01aQ") == b"a"