Skip to content

Commit f8a48fb

Browse files
[3.14] gh-159185: Document that PyMarshal_WriteObjectToFile can silently truncate before 3.16 (GH-159201) (#159203)
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. (cherry picked from commit b3714a5) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
1 parent 09e1e66 commit f8a48fb

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
@@ -29,6 +29,12 @@ the :py:mod:`Python module documentation <marshal>` for details.
2929
This function can fail, in which case it sets the error indicator.
3030
Use :c:func:`PyErr_Occurred` to check for that.
3131
32+
.. note::
33+
34+
In Python 3.15 and earlier, this function does not set the error
35+
indicator if writing to *file* fails.
36+
The same caveat applies as for :c:func:`PyMarshal_WriteObjectToFile`.
37+
3238
.. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
3339
3440
Marshal a Python object, *value*, to *file*.
@@ -37,6 +43,17 @@ the :py:mod:`Python module documentation <marshal>` for details.
3743
This function can fail, in which case it sets the error indicator.
3844
Use :c:func:`PyErr_Occurred` to check for that.
3945
46+
.. note::
47+
48+
In Python 3.15 and earlier, this function can write only part of the
49+
data to *file* and return without setting the error indicator, for
50+
example if *value* contains an object which cannot be marshalled or
51+
if writing to *file* fails (see :gh:`155907`).
52+
Code which has to support these versions cannot rely on
53+
:c:func:`PyErr_Occurred` alone to know that all the data was written.
54+
It can use :c:func:`PyMarshal_WriteObjectToString` and write the
55+
result itself, or validate what was written.
56+
4057
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
4158
4259
Return a bytes object containing the marshalled representation of *value*.

0 commit comments

Comments
 (0)