Skip to content

Commit 54028e2

Browse files
committed
pythongh-155907: Complete PyMarshal C API tests
Add tests on PyMarshal_ReadObjectFromString() and PyMarshal_WriteObjectToString(). Add test on PyMarshal_WriteObjectToFile(NULL).
1 parent fb46c67 commit 54028e2

2 files changed

Lines changed: 269 additions & 89 deletions

File tree

‎Lib/test/test_capi/test_marshal.py‎

Lines changed: 195 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
1+
# Test PyMarshal C API
2+
13
import marshal
24
import os.path
5+
import struct
36
import unittest
47

58
from test import support
69
from test.support import import_helper
710
from test.support import os_helper
8-
from test.test_marshal import HelperMixin, omit_last_byte
911

1012

1113
# Skip this test if _testcapi is are not available.
1214
_testcapi = import_helper.import_module('_testcapi')
1315

1416

17+
def noop_func():
18+
pass
19+
20+
NULL = None
21+
SIMPLE_OBJECT = 123
22+
# Only test a few objects: see test_marshal for more exhaustive tests
23+
TEST_OBJECTS = (
24+
'\u20ac',
25+
b'abc',
26+
True,
27+
123,
28+
45.6,
29+
7+8j,
30+
'long line '*1000,
31+
# Check that serializing code object is allowed (allow_code = 1)
32+
noop_func.__code__,
33+
)
34+
UNMARSHALLABLE = object()
35+
36+
# Invalid marshal data
37+
JUNK_BYTES = b'\xff' * 32
38+
39+
40+
def read_file(filename):
41+
with open(filename, 'rb') as fp:
42+
return fp.read()
43+
44+
45+
def write_file(filename, data):
46+
with open(filename, 'wb') as fp:
47+
fp.write(data)
48+
49+
1550
@support.cpython_only
16-
class CAPI_TestCase(unittest.TestCase, HelperMixin):
51+
class CAPI_TestCase(unittest.TestCase):
1752

1853
def test_read_from_file_error(self):
1954
# A read error is reported as OSError, not EOFError.
@@ -38,89 +73,178 @@ def test_write_to_file_error(self):
3873
_testcapi.pymarshal_write_object_to_file(obj, '/dev/full',
3974
marshal.version)
4075

41-
def test_write_unmarshallable_to_file(self):
42-
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
43-
with self.assertRaisesRegex(ValueError, 'unmarshallable object'):
44-
_testcapi.pymarshal_write_object_to_file(object(), os_helper.TESTFN,
45-
marshal.version)
76+
def check_object(self, obj2, obj):
77+
self.assertEqual(obj2, obj)
78+
self.assertEqual(type(obj2), type(obj))
4679

4780
def test_write_long_to_file(self):
48-
for v in range(marshal.version + 1):
49-
_testcapi.pymarshal_write_long_to_file(0x12345678, os_helper.TESTFN, v)
50-
with open(os_helper.TESTFN, 'rb') as f:
51-
data = f.read()
52-
os_helper.unlink(os_helper.TESTFN)
53-
self.assertEqual(data, b'\x78\x56\x34\x12')
81+
# Test PyMarshal_WriteLongToFile()
82+
write_long_to_file = _testcapi.pymarshal_write_long_to_file
83+
filename = os_helper.TESTFN
84+
self.addCleanup(os_helper.unlink, filename)
85+
86+
def mask32(value):
87+
res = value & (2 ** 32 - 1)
88+
if res >= 2147483648:
89+
return res - 4294967296
90+
else:
91+
return res
92+
93+
limit = 2 ** 31
94+
for version in range(marshal.version + 1):
95+
for value in (
96+
_testcapi.LONG_MIN,
97+
_testcapi.LONG_MAX,
98+
-limit - 2,
99+
-limit,
100+
-limit + 2,
101+
limit - 2,
102+
limit,
103+
limit + 2,
104+
0,
105+
123,
106+
-123,
107+
):
108+
with self.subTest(value=value, version=version):
109+
write_long_to_file(value, filename, version)
110+
data = read_file(filename)
111+
self.assertEqual(len(data), 4)
112+
value2 = struct.unpack('<i', data)[0]
113+
self.assertEqual(value2, mask32(value))
54114

55115
def test_write_object_to_file(self):
56-
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000)
57-
for v in range(marshal.version + 1):
58-
_testcapi.pymarshal_write_object_to_file(obj, os_helper.TESTFN, v)
59-
with open(os_helper.TESTFN, 'rb') as f:
60-
data = f.read()
61-
os_helper.unlink(os_helper.TESTFN)
62-
self.assertEqual(marshal.loads(data), obj)
116+
# Test PyMarshal_WriteObjectToFile()
117+
write_object_to_file = _testcapi.pymarshal_write_object_to_file
118+
filename = os_helper.TESTFN
119+
self.addCleanup(os_helper.unlink, filename)
120+
121+
for version in range(marshal.version + 1):
122+
for obj in TEST_OBJECTS:
123+
with self.subTest(obj=obj, version=version):
124+
write_object_to_file(obj, filename, version)
125+
data = read_file(filename)
126+
self.assertEqual(marshal.loads(data), obj)
127+
128+
with self.assertRaises(SystemError):
129+
write_object_to_file(NULL, filename, version)
130+
131+
with self.assertRaisesRegex(ValueError, 'unmarshallable object'):
132+
write_object_to_file(UNMARSHALLABLE, filename, version)
63133

64134
def test_read_short_from_file(self):
65-
with open(os_helper.TESTFN, 'wb') as f:
66-
f.write(b'\x34\x12xxxx')
67-
r, p = _testcapi.pymarshal_read_short_from_file(os_helper.TESTFN)
68-
os_helper.unlink(os_helper.TESTFN)
69-
self.assertEqual(r, 0x1234)
70-
self.assertEqual(p, 2)
71-
72-
with open(os_helper.TESTFN, 'wb') as f:
73-
f.write(b'\x12')
135+
# Test PyMarshal_ReadShortFromFile()
136+
read_short_from_file = _testcapi.pymarshal_read_short_from_file
137+
filename = os_helper.TESTFN
138+
self.addCleanup(os_helper.unlink, filename)
139+
140+
for value in (
141+
-2**15,
142+
2**15-1,
143+
0,
144+
123,
145+
-123,
146+
):
147+
with self.subTest(value=value):
148+
data = struct.pack('<h', value) + b'xxxx'
149+
write_file(filename, data)
150+
value2 = read_short_from_file(filename)
151+
self.assertEqual(value2, value)
152+
153+
write_file(filename, b'\x12') # less than 2 bytes
74154
with self.assertRaises(EOFError):
75-
_testcapi.pymarshal_read_short_from_file(os_helper.TESTFN)
76-
os_helper.unlink(os_helper.TESTFN)
155+
read_short_from_file(filename)
77156

78157
def test_read_long_from_file(self):
79-
with open(os_helper.TESTFN, 'wb') as f:
80-
f.write(b'\x78\x56\x34\x12xxxx')
81-
r, p = _testcapi.pymarshal_read_long_from_file(os_helper.TESTFN)
82-
os_helper.unlink(os_helper.TESTFN)
83-
self.assertEqual(r, 0x12345678)
84-
self.assertEqual(p, 4)
85-
86-
with open(os_helper.TESTFN, 'wb') as f:
87-
f.write(b'\x56\x34\x12')
158+
# Test PyMarshal_ReadLongFromFile()
159+
read_long_from_file = _testcapi.pymarshal_read_long_from_file
160+
filename = os_helper.TESTFN
161+
self.addCleanup(os_helper.unlink, filename)
162+
163+
for value in (
164+
_testcapi.INT_MIN,
165+
_testcapi.INT_MAX,
166+
0,
167+
123,
168+
-123,
169+
):
170+
with self.subTest(value=value):
171+
data = struct.pack('<i', value)
172+
write_file(filename, data)
173+
value2 = read_long_from_file(filename)
174+
self.assertEqual(value2, value)
175+
176+
write_file(filename, b'\x56\x34\x12') # less than 4 bytes
177+
with self.assertRaises(EOFError):
178+
read_long_from_file(filename)
179+
180+
def check_read_object(self, read_object_func, check_pos=True):
181+
filename = os_helper.TESTFN
182+
self.addCleanup(os_helper.unlink, filename)
183+
184+
version = marshal.version
185+
for obj in TEST_OBJECTS:
186+
with self.subTest(obj=obj):
187+
data = marshal.dumps(obj, version)
188+
data += b'abc' # following data is ignored
189+
write_file(filename, data)
190+
obj2, pos = read_object_func(filename)
191+
self.check_object(obj2, obj)
192+
if check_pos:
193+
self.assertEqual(pos, len(data))
194+
195+
data = marshal.dumps(SIMPLE_OBJECT, version)
196+
data = data[:-1] # truncate last byte
197+
write_file(filename, data)
88198
with self.assertRaises(EOFError):
89-
_testcapi.pymarshal_read_long_from_file(os_helper.TESTFN)
90-
os_helper.unlink(os_helper.TESTFN)
199+
read_object_func(filename)
200+
201+
write_file(filename, JUNK_BYTES)
202+
with self.assertRaisesRegex(ValueError, 'bad marshal data'):
203+
read_object_func(filename)
91204

92205
def test_read_last_object_from_file(self):
93-
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
94-
for v in range(marshal.version + 1):
95-
data = marshal.dumps(obj, v)
96-
with open(os_helper.TESTFN, 'wb') as f:
97-
f.write(data + b'xxxx')
98-
r, p = _testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN)
99-
os_helper.unlink(os_helper.TESTFN)
100-
self.assertEqual(r, obj)
101-
102-
with open(os_helper.TESTFN, 'wb') as f:
103-
f.write(omit_last_byte(data))
104-
with self.assertRaises(EOFError):
105-
_testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN)
106-
os_helper.unlink(os_helper.TESTFN)
206+
# Test PyMarshal_ReadLastObjectFromFile()
207+
read_last_object_from_file = _testcapi.pymarshal_read_last_object_from_file
208+
self.check_read_object(read_last_object_from_file)
107209

108210
def test_read_object_from_file(self):
109-
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
110-
for v in range(marshal.version + 1):
111-
data = marshal.dumps(obj, v)
112-
with open(os_helper.TESTFN, 'wb') as f:
113-
f.write(data + b'xxxx')
114-
r, p = _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
115-
os_helper.unlink(os_helper.TESTFN)
116-
self.assertEqual(r, obj)
117-
self.assertEqual(p, len(data))
118-
119-
with open(os_helper.TESTFN, 'wb') as f:
120-
f.write(omit_last_byte(data))
121-
with self.assertRaises(EOFError):
122-
_testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
123-
os_helper.unlink(os_helper.TESTFN)
211+
# Test PyMarshal_ReadObjectFromFile()
212+
read_object_from_file = _testcapi.pymarshal_read_object_from_file
213+
self.check_read_object(read_object_from_file, check_pos=False)
214+
215+
def test_pymarshal_readobjectfromstring(self):
216+
# Test PyMarshal_ReadObjectFromString()
217+
readobjectfromstring = _testcapi.pymarshal_readobjectfromstring
218+
for obj in TEST_OBJECTS:
219+
for version in range(marshal.version + 1):
220+
with self.subTest(obj=obj, version=version):
221+
data = marshal.dumps(obj, version)
222+
obj2 = readobjectfromstring(data)
223+
self.check_object(obj2, obj)
224+
225+
data = marshal.dumps(SIMPLE_OBJECT, marshal.version)
226+
data = data[:-1] # truncate last byte
227+
with self.assertRaises(EOFError):
228+
readobjectfromstring(data)
229+
230+
with self.assertRaisesRegex(ValueError, 'bad marshal data'):
231+
readobjectfromstring(JUNK_BYTES)
232+
233+
def test_pymarshal_writeobjecttostring(self):
234+
# Test PyMarshal_WriteObjectToString()
235+
writeobjecttostring = _testcapi.pymarshal_writeobjecttostring
236+
for version in range(marshal.version + 1):
237+
for obj in TEST_OBJECTS:
238+
with self.subTest(obj=obj, version=version):
239+
data = writeobjecttostring(obj, version)
240+
obj2 = marshal.loads(data)
241+
self.check_object(obj2, obj)
242+
243+
with self.assertRaisesRegex(ValueError, 'unmarshallable object'):
244+
writeobjecttostring(UNMARSHALLABLE, version)
245+
246+
with self.assertRaises(SystemError):
247+
writeobjecttostring(NULL, version)
124248

125249

126250
if __name__ == "__main__":

0 commit comments

Comments
 (0)