Skip to content

Commit b3714a5

Browse files
authored
gh-159185: Document that PyMarshal_WriteObjectToFile can silently truncate before 3.16 (GH-159201)
gh-159185: Document that the PyMarshal file writers can fail silently before 3.16 In Python 3.15 and earlier, PyMarshal_WriteObjectToFile() ignores most marshalling errors and all I/O errors: it returns without setting the error indicator after writing only part of the data. PyMarshal_WriteLongToFile() ignores I/O errors in the same way. That was fixed in 3.16 by gh-155907, which is not backported. Tell the users of the older versions so that they can check what was written.
1 parent cd71799 commit b3714a5

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

‎Doc/c-api/marshal.rst‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ and :exc:`MemoryError` if memory allocation fails.
4343
This function can fail, in which case it sets the error indicator.
4444
Use :c:func:`PyErr_Occurred` to check for that.
4545
46+
.. note::
47+
48+
In Python 3.15 and earlier, this function does not set the error
49+
indicator if writing to *file* fails.
50+
The same caveat applies as for :c:func:`PyMarshal_WriteObjectToFile`.
51+
4652
.. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
4753
4854
Marshal a Python object, *value*, to *file*.
@@ -51,6 +57,17 @@ and :exc:`MemoryError` if memory allocation fails.
5157
This function can fail, in which case it sets the error indicator.
5258
Use :c:func:`PyErr_Occurred` to check for that.
5359
60+
.. note::
61+
62+
In Python 3.15 and earlier, this function can write only part of the
63+
data to *file* and return without setting the error indicator, for
64+
example if *value* contains an object which cannot be marshalled or
65+
if writing to *file* fails (see :gh:`155907`).
66+
Code which has to support these versions cannot rely on
67+
:c:func:`PyErr_Occurred` alone to know that all the data was written.
68+
It can use :c:func:`PyMarshal_WriteObjectToString` and write the
69+
result itself, or validate what was written.
70+
5471
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
5572
5673
Return a bytes object containing the marshalled representation of *value*.

0 commit comments

Comments
 (0)