Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/c-api/marshal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ and :exc:`MemoryError` if memory allocation fails.
Marshal a Python object, *value*, to *file*.
*version* indicates the file format.
The marshalled representation of *value* is built in memory
and then written to *file*.
Nothing is written if *value* cannot be marshalled.
This function can fail, in which case it sets the error indicator.
Use :c:func:`PyErr_Occurred` to check for that.
Expand All @@ -68,6 +72,11 @@ and :exc:`MemoryError` if memory allocation fails.
It can use :c:func:`PyMarshal_WriteObjectToString` and write the
result itself, or validate what was written.
.. versionchanged:: next
Previously, the data was written to *file* as it was produced,
using a buffer of constant size,
and incomplete data was written if *value* could not be marshalled.
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
Return a bytes object containing the marshalled representation of *value*.
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_capi/test_marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,39 @@ def test_write_object_to_file(self):
write_object_to_file(obj, filename, version)
data = read_file(filename)
self.assertEqual(marshal.loads(data), obj)
self.assertEqual(data, marshal.dumps(obj, version))

# Larger than a stdio buffer, with references to objects
# written long before.
shared = (float('1.5'), b'bytes' * 10)
obj = [shared, 'long line ' * 10_000, shared, [shared] * 10_000]
write_object_to_file(obj, filename, version)
data = read_file(filename)
self.assertEqual(data, marshal.dumps(obj, version))
obj2 = marshal.loads(data)
self.assertEqual(obj2, obj)
if version >= 3:
self.assertIs(obj2[2], obj2[0])
self.assertIs(obj2[3][-1], obj2[0])

with self.assertRaises(SystemError):
write_object_to_file(NULL, filename, version)

with self.assertRaisesRegex(ValueError, 'cannot marshal object objects'):
write_object_to_file(UNMARSHALLABLE, filename, version)

# Nothing is written if the object cannot be marshalled,
# even if the error is only detected after more data than
# fits in a stdio buffer.
for obj in (UNMARSHALLABLE,
[1, 'abc', UNMARSHALLABLE],
[shared, 'long line ' * 10_000, shared,
UNMARSHALLABLE]):
with self.assertRaisesRegex(ValueError,
'cannot marshal object objects'):
write_object_to_file(obj, filename, version)
self.assertEqual(read_file(filename), b'')

def test_read_short_from_file(self):
# Test PyMarshal_ReadShortFromFile()
read_short_from_file = _testcapi.pymarshal_read_short_from_file
Expand Down
Loading
Loading