Skip to content

Commit f12f820

Browse files
bpo-29746: Update marshal docs to Python 3. (#547) (#630)
(cherry picked from commit c611a5b)
1 parent be31a83 commit f12f820

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

Doc/c-api/marshal.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ unmarshalling. Version 2 uses a binary format for floating point numbers.
3434
3535
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
3636
37-
Return a string object containing the marshalled representation of *value*.
37+
Return a bytes object containing the marshalled representation of *value*.
3838
*version* indicates the file format.
3939
4040
@@ -88,10 +88,10 @@ written using these routines?
8888
:exc:`TypeError`) and returns *NULL*.
8989
9090
91-
.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *string, Py_ssize_t len)
91+
.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
9292
93-
Return a Python object from the data stream in a character buffer
94-
containing *len* bytes pointed to by *string*.
93+
Return a Python object from the data stream in a byte buffer
94+
containing *len* bytes pointed to by *data*.
9595
9696
On error, sets the appropriate exception (:exc:`EOFError` or
9797
:exc:`TypeError`) and returns *NULL*.

Doc/glossary.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ Glossary
103103
binary file
104104
A :term:`file object` able to read and write
105105
:term:`bytes-like objects <bytes-like object>`.
106+
Examples of binary files are files opened in binary mode (``'rb'``,
107+
``'wb'`` or ``'rb+'``), :data:`sys.stdin.buffer`,
108+
:data:`sys.stdout.buffer`, and instances of :class:`io.BytesIO` and
109+
:class:`gzip.GzipFile`.
106110

107111
.. seealso::
108112
A :term:`text file` reads and writes :class:`str` objects.
@@ -926,6 +930,9 @@ Glossary
926930
A :term:`file object` able to read and write :class:`str` objects.
927931
Often, a text file actually accesses a byte-oriented datastream
928932
and handles the :term:`text encoding` automatically.
933+
Examples of text files are files opened in text mode (``'r'`` or ``'w'``),
934+
:data:`sys.stdin`, :data:`sys.stdout`, and instances of
935+
:class:`io.StringIO`.
929936

930937
.. seealso::
931938
A :term:`binary file` reads and write :class:`bytes` objects.

Doc/library/marshal.rst

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@ For format *version* lower than 3, recursive lists, sets and dictionaries cannot
4949
be written (see below).
5050

5151
There are functions that read/write files as well as functions operating on
52-
strings.
52+
bytes-like objects.
5353

5454
The module defines these functions:
5555

5656

5757
.. function:: dump(value, file[, version])
5858

5959
Write the value on the open file. The value must be a supported type. The
60-
file must be an open file object such as ``sys.stdout`` or returned by
61-
:func:`open` or :func:`os.popen`. It must be opened in binary mode (``'wb'``
62-
or ``'w+b'``).
60+
file must be a writeable :term:`binary file`.
6361

6462
If the value has (or contains an object that has) an unsupported type, a
6563
:exc:`ValueError` exception is raised --- but garbage data will also be written
@@ -74,8 +72,7 @@ The module defines these functions:
7472
Read one value from the open file and return it. If no valid value is read
7573
(e.g. because the data has a different Python version's incompatible marshal
7674
format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The
77-
file must be an open file object opened in binary mode (``'rb'`` or
78-
``'r+b'``).
75+
file must be a readable :term:`binary file`.
7976

8077
.. note::
8178

@@ -85,19 +82,19 @@ The module defines these functions:
8582

8683
.. function:: dumps(value[, version])
8784

88-
Return the string that would be written to a file by ``dump(value, file)``. The
85+
Return the bytes object that would be written to a file by ``dump(value, file)``. The
8986
value must be a supported type. Raise a :exc:`ValueError` exception if value
9087
has (or contains an object that has) an unsupported type.
9188

9289
The *version* argument indicates the data format that ``dumps`` should use
9390
(see below).
9491

9592

96-
.. function:: loads(string)
93+
.. function:: loads(bytes)
9794

98-
Convert the string to a value. If no valid value is found, raise
99-
:exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra characters in the
100-
string are ignored.
95+
Convert the :term:`bytes-like object` to a value. If no valid value is found, raise
96+
:exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra bytes in the
97+
input are ignored.
10198

10299

103100
In addition, the following constants are defined:

Python/marshal.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
549549
w_object(co->co_lnotab, p);
550550
}
551551
else if (PyObject_CheckBuffer(v)) {
552-
/* Write unknown bytes-like objects as a byte string */
552+
/* Write unknown bytes-like objects as a bytes object */
553553
Py_buffer view;
554554
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
555555
w_byte(TYPE_UNKNOWN, p);
@@ -1079,7 +1079,7 @@ r_object(RFILE *p)
10791079
if (PyErr_Occurred())
10801080
break;
10811081
if (n < 0 || n > SIZE32_MAX) {
1082-
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
1082+
PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
10831083
break;
10841084
}
10851085
v = PyBytes_FromStringAndSize((char *)NULL, n);
@@ -1103,7 +1103,7 @@ r_object(RFILE *p)
11031103
if (PyErr_Occurred())
11041104
break;
11051105
if (n < 0 || n > SIZE32_MAX) {
1106-
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1106+
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
11071107
break;
11081108
}
11091109
goto _read_ascii;
@@ -1143,7 +1143,7 @@ r_object(RFILE *p)
11431143
if (PyErr_Occurred())
11441144
break;
11451145
if (n < 0 || n > SIZE32_MAX) {
1146-
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1146+
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
11471147
break;
11481148
}
11491149
if (n != 0) {
@@ -1594,7 +1594,7 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
15941594
if (wf.ptr - base > PY_SSIZE_T_MAX) {
15951595
Py_DECREF(wf.str);
15961596
PyErr_SetString(PyExc_OverflowError,
1597-
"too much marshal data for a string");
1597+
"too much marshal data for a bytes object");
15981598
return NULL;
15991599
}
16001600
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
@@ -1640,8 +1640,7 @@ PyDoc_STRVAR(dump_doc,
16401640
"dump(value, file[, version])\n\
16411641
\n\
16421642
Write the value on the open file. The value must be a supported type.\n\
1643-
The file must be an open file object such as sys.stdout or returned by\n\
1644-
open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1643+
The file must be a writeable binary file.\n\
16451644
\n\
16461645
If the value has (or contains an object that has) an unsupported type, a\n\
16471646
ValueError exception is raised - but garbage data will also be written\n\
@@ -1697,8 +1696,7 @@ PyDoc_STRVAR(load_doc,
16971696
Read one value from the open file and return it. If no valid value is\n\
16981697
read (e.g. because the data has a different Python version's\n\
16991698
incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1700-
The file must be an open file object opened in binary mode ('rb' or\n\
1701-
'r+b').\n\
1699+
The file must be a readable binary file.\n\
17021700
\n\
17031701
Note: If an object containing an unsupported type was marshalled with\n\
17041702
dump(), load() will substitute None for the unmarshallable type.");
@@ -1717,7 +1715,7 @@ marshal_dumps(PyObject *self, PyObject *args)
17171715
PyDoc_STRVAR(dumps_doc,
17181716
"dumps(value[, version])\n\
17191717
\n\
1720-
Return the string that would be written to a file by dump(value, file).\n\
1718+
Return the bytes object that would be written to a file by dump(value, file).\n\
17211719
The value must be a supported type. Raise a ValueError exception if\n\
17221720
value has (or contains an object that has) an unsupported type.\n\
17231721
\n\
@@ -1753,8 +1751,8 @@ marshal_loads(PyObject *self, PyObject *args)
17531751
PyDoc_STRVAR(loads_doc,
17541752
"loads(bytes)\n\
17551753
\n\
1756-
Convert the bytes object to a value. If no valid value is found, raise\n\
1757-
EOFError, ValueError or TypeError. Extra characters in the input are\n\
1754+
Convert the bytes-like object to a value. If no valid value is found,\n\
1755+
raise EOFError, ValueError or TypeError. Extra bytes in the input are\n\
17581756
ignored.");
17591757

17601758
static PyMethodDef marshal_methods[] = {
@@ -1792,8 +1790,8 @@ Functions:\n\
17921790
\n\
17931791
dump() -- write value to a file\n\
17941792
load() -- read value from a file\n\
1795-
dumps() -- write value to a string\n\
1796-
loads() -- read value from a string");
1793+
dumps() -- marshal value as a bytes object\n\
1794+
loads() -- read value from a bytes-like object");
17971795

17981796

17991797

0 commit comments

Comments
 (0)