Skip to content

Commit c611a5b

Browse files
bpo-29746: Update marshal docs to Python 3. (python#547)
1 parent 93710c1 commit c611a5b

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

Doc/c-api/marshal.rst

+4-4
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

+7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ Glossary
131131
binary file
132132
A :term:`file object` able to read and write
133133
:term:`bytes-like objects <bytes-like object>`.
134+
Examples of binary files are files opened in binary mode (``'rb'``,
135+
``'wb'`` or ``'rb+'``), :data:`sys.stdin.buffer`,
136+
:data:`sys.stdout.buffer`, and instances of :class:`io.BytesIO` and
137+
:class:`gzip.GzipFile`.
134138

135139
.. seealso::
136140
A :term:`text file` reads and writes :class:`str` objects.
@@ -966,6 +970,9 @@ Glossary
966970
A :term:`file object` able to read and write :class:`str` objects.
967971
Often, a text file actually accesses a byte-oriented datastream
968972
and handles the :term:`text encoding` automatically.
973+
Examples of text files are files opened in text mode (``'r'`` or ``'w'``),
974+
:data:`sys.stdin`, :data:`sys.stdout`, and instances of
975+
:class:`io.StringIO`.
969976

970977
.. seealso::
971978
A :term:`binary file` reads and write :class:`bytes` objects.

Doc/library/marshal.rst

+8-11
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

+12-14
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);
@@ -1086,7 +1086,7 @@ r_object(RFILE *p)
10861086
if (PyErr_Occurred())
10871087
break;
10881088
if (n < 0 || n > SIZE32_MAX) {
1089-
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
1089+
PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
10901090
break;
10911091
}
10921092
v = PyBytes_FromStringAndSize((char *)NULL, n);
@@ -1110,7 +1110,7 @@ r_object(RFILE *p)
11101110
if (PyErr_Occurred())
11111111
break;
11121112
if (n < 0 || n > SIZE32_MAX) {
1113-
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1113+
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
11141114
break;
11151115
}
11161116
goto _read_ascii;
@@ -1150,7 +1150,7 @@ r_object(RFILE *p)
11501150
if (PyErr_Occurred())
11511151
break;
11521152
if (n < 0 || n > SIZE32_MAX) {
1153-
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1153+
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
11541154
break;
11551155
}
11561156
if (n != 0) {
@@ -1612,7 +1612,7 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
16121612
if (wf.ptr - base > PY_SSIZE_T_MAX) {
16131613
Py_DECREF(wf.str);
16141614
PyErr_SetString(PyExc_OverflowError,
1615-
"too much marshal data for a string");
1615+
"too much marshal data for a bytes object");
16161616
return NULL;
16171617
}
16181618
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
@@ -1658,8 +1658,7 @@ PyDoc_STRVAR(dump_doc,
16581658
"dump(value, file[, version])\n\
16591659
\n\
16601660
Write the value on the open file. The value must be a supported type.\n\
1661-
The file must be an open file object such as sys.stdout or returned by\n\
1662-
open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1661+
The file must be a writeable binary file.\n\
16631662
\n\
16641663
If the value has (or contains an object that has) an unsupported type, a\n\
16651664
ValueError exception is raised - but garbage data will also be written\n\
@@ -1715,8 +1714,7 @@ PyDoc_STRVAR(load_doc,
17151714
Read one value from the open file and return it. If no valid value is\n\
17161715
read (e.g. because the data has a different Python version's\n\
17171716
incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1718-
The file must be an open file object opened in binary mode ('rb' or\n\
1719-
'r+b').\n\
1717+
The file must be a readable binary file.\n\
17201718
\n\
17211719
Note: If an object containing an unsupported type was marshalled with\n\
17221720
dump(), load() will substitute None for the unmarshallable type.");
@@ -1735,7 +1733,7 @@ marshal_dumps(PyObject *self, PyObject *args)
17351733
PyDoc_STRVAR(dumps_doc,
17361734
"dumps(value[, version])\n\
17371735
\n\
1738-
Return the string that would be written to a file by dump(value, file).\n\
1736+
Return the bytes object that would be written to a file by dump(value, file).\n\
17391737
The value must be a supported type. Raise a ValueError exception if\n\
17401738
value has (or contains an object that has) an unsupported type.\n\
17411739
\n\
@@ -1771,8 +1769,8 @@ marshal_loads(PyObject *self, PyObject *args)
17711769
PyDoc_STRVAR(loads_doc,
17721770
"loads(bytes)\n\
17731771
\n\
1774-
Convert the bytes object to a value. If no valid value is found, raise\n\
1775-
EOFError, ValueError or TypeError. Extra characters in the input are\n\
1772+
Convert the bytes-like object to a value. If no valid value is found,\n\
1773+
raise EOFError, ValueError or TypeError. Extra bytes in the input are\n\
17761774
ignored.");
17771775

17781776
static PyMethodDef marshal_methods[] = {
@@ -1810,8 +1808,8 @@ Functions:\n\
18101808
\n\
18111809
dump() -- write value to a file\n\
18121810
load() -- read value from a file\n\
1813-
dumps() -- write value to a string\n\
1814-
loads() -- read value from a string");
1811+
dumps() -- marshal value as a bytes object\n\
1812+
loads() -- read value from a bytes-like object");
18151813

18161814

18171815

0 commit comments

Comments
 (0)