bpo-29746: Update marshal docs to Python 3. (#547)

This commit is contained in:
Serhiy Storchaka 2017-03-12 08:53:22 +02:00 committed by GitHub
parent 93710c152e
commit c611a5b1d4
4 changed files with 31 additions and 29 deletions

View File

@ -34,7 +34,7 @@ unmarshalling. Version 2 uses a binary format for floating point numbers.
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version) .. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
Return a string object containing the marshalled representation of *value*. Return a bytes object containing the marshalled representation of *value*.
*version* indicates the file format. *version* indicates the file format.
@ -88,10 +88,10 @@ written using these routines?
:exc:`TypeError`) and returns *NULL*. :exc:`TypeError`) and returns *NULL*.
.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *string, Py_ssize_t len) .. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
Return a Python object from the data stream in a character buffer Return a Python object from the data stream in a byte buffer
containing *len* bytes pointed to by *string*. containing *len* bytes pointed to by *data*.
On error, sets the appropriate exception (:exc:`EOFError` or On error, sets the appropriate exception (:exc:`EOFError` or
:exc:`TypeError`) and returns *NULL*. :exc:`TypeError`) and returns *NULL*.

View File

@ -131,6 +131,10 @@ Glossary
binary file binary file
A :term:`file object` able to read and write A :term:`file object` able to read and write
:term:`bytes-like objects <bytes-like object>`. :term:`bytes-like objects <bytes-like object>`.
Examples of binary files are files opened in binary mode (``'rb'``,
``'wb'`` or ``'rb+'``), :data:`sys.stdin.buffer`,
:data:`sys.stdout.buffer`, and instances of :class:`io.BytesIO` and
:class:`gzip.GzipFile`.
.. seealso:: .. seealso::
A :term:`text file` reads and writes :class:`str` objects. A :term:`text file` reads and writes :class:`str` objects.
@ -966,6 +970,9 @@ Glossary
A :term:`file object` able to read and write :class:`str` objects. A :term:`file object` able to read and write :class:`str` objects.
Often, a text file actually accesses a byte-oriented datastream Often, a text file actually accesses a byte-oriented datastream
and handles the :term:`text encoding` automatically. and handles the :term:`text encoding` automatically.
Examples of text files are files opened in text mode (``'r'`` or ``'w'``),
:data:`sys.stdin`, :data:`sys.stdout`, and instances of
:class:`io.StringIO`.
.. seealso:: .. seealso::
A :term:`binary file` reads and write :class:`bytes` objects. A :term:`binary file` reads and write :class:`bytes` objects.

View File

@ -49,7 +49,7 @@ For format *version* lower than 3, recursive lists, sets and dictionaries cannot
be written (see below). be written (see below).
There are functions that read/write files as well as functions operating on There are functions that read/write files as well as functions operating on
strings. bytes-like objects.
The module defines these functions: The module defines these functions:
@ -57,9 +57,7 @@ The module defines these functions:
.. function:: dump(value, file[, version]) .. function:: dump(value, file[, version])
Write the value on the open file. The value must be a supported type. The Write the value on the open file. The value must be a supported type. The
file must be an open file object such as ``sys.stdout`` or returned by file must be a writeable :term:`binary file`.
:func:`open` or :func:`os.popen`. It must be opened in binary mode (``'wb'``
or ``'w+b'``).
If the value has (or contains an object that has) an unsupported type, a If the value has (or contains an object that has) an unsupported type, a
:exc:`ValueError` exception is raised --- but garbage data will also be written :exc:`ValueError` exception is raised --- but garbage data will also be written
@ -74,8 +72,7 @@ The module defines these functions:
Read one value from the open file and return it. If no valid value is read Read one value from the open file and return it. If no valid value is read
(e.g. because the data has a different Python version's incompatible marshal (e.g. because the data has a different Python version's incompatible marshal
format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The
file must be an open file object opened in binary mode (``'rb'`` or file must be a readable :term:`binary file`.
``'r+b'``).
.. note:: .. note::
@ -85,7 +82,7 @@ The module defines these functions:
.. function:: dumps(value[, version]) .. function:: dumps(value[, version])
Return the string that would be written to a file by ``dump(value, file)``. The Return the bytes object that would be written to a file by ``dump(value, file)``. The
value must be a supported type. Raise a :exc:`ValueError` exception if value value must be a supported type. Raise a :exc:`ValueError` exception if value
has (or contains an object that has) an unsupported type. has (or contains an object that has) an unsupported type.
@ -93,11 +90,11 @@ The module defines these functions:
(see below). (see below).
.. function:: loads(string) .. function:: loads(bytes)
Convert the string to a value. If no valid value is found, raise Convert the :term:`bytes-like object` to a value. If no valid value is found, raise
:exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra characters in the :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra bytes in the
string are ignored. input are ignored.
In addition, the following constants are defined: In addition, the following constants are defined:

View File

@ -549,7 +549,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
w_object(co->co_lnotab, p); w_object(co->co_lnotab, p);
} }
else if (PyObject_CheckBuffer(v)) { else if (PyObject_CheckBuffer(v)) {
/* Write unknown bytes-like objects as a byte string */ /* Write unknown bytes-like objects as a bytes object */
Py_buffer view; Py_buffer view;
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) { if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
w_byte(TYPE_UNKNOWN, p); w_byte(TYPE_UNKNOWN, p);
@ -1086,7 +1086,7 @@ r_object(RFILE *p)
if (PyErr_Occurred()) if (PyErr_Occurred())
break; break;
if (n < 0 || n > SIZE32_MAX) { if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)"); PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
break; break;
} }
v = PyBytes_FromStringAndSize((char *)NULL, n); v = PyBytes_FromStringAndSize((char *)NULL, n);
@ -1110,7 +1110,7 @@ r_object(RFILE *p)
if (PyErr_Occurred()) if (PyErr_Occurred())
break; break;
if (n < 0 || n > SIZE32_MAX) { if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)"); PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
break; break;
} }
goto _read_ascii; goto _read_ascii;
@ -1150,7 +1150,7 @@ r_object(RFILE *p)
if (PyErr_Occurred()) if (PyErr_Occurred())
break; break;
if (n < 0 || n > SIZE32_MAX) { if (n < 0 || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)"); PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
break; break;
} }
if (n != 0) { if (n != 0) {
@ -1612,7 +1612,7 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
if (wf.ptr - base > PY_SSIZE_T_MAX) { if (wf.ptr - base > PY_SSIZE_T_MAX) {
Py_DECREF(wf.str); Py_DECREF(wf.str);
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"too much marshal data for a string"); "too much marshal data for a bytes object");
return NULL; return NULL;
} }
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0) if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
@ -1658,8 +1658,7 @@ PyDoc_STRVAR(dump_doc,
"dump(value, file[, version])\n\ "dump(value, file[, version])\n\
\n\ \n\
Write the value on the open file. The value must be a supported type.\n\ Write the value on the open file. The value must be a supported type.\n\
The file must be an open file object such as sys.stdout or returned by\n\ The file must be a writeable binary file.\n\
open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
\n\ \n\
If the value has (or contains an object that has) an unsupported type, a\n\ If the value has (or contains an object that has) an unsupported type, a\n\
ValueError exception is raised - but garbage data will also be written\n\ ValueError exception is raised - but garbage data will also be written\n\
@ -1715,8 +1714,7 @@ PyDoc_STRVAR(load_doc,
Read one value from the open file and return it. If no valid value is\n\ Read one value from the open file and return it. If no valid value is\n\
read (e.g. because the data has a different Python version's\n\ read (e.g. because the data has a different Python version's\n\
incompatible marshal format), raise EOFError, ValueError or TypeError.\n\ incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
The file must be an open file object opened in binary mode ('rb' or\n\ The file must be a readable binary file.\n\
'r+b').\n\
\n\ \n\
Note: If an object containing an unsupported type was marshalled with\n\ Note: If an object containing an unsupported type was marshalled with\n\
dump(), load() will substitute None for the unmarshallable type."); dump(), load() will substitute None for the unmarshallable type.");
@ -1735,7 +1733,7 @@ marshal_dumps(PyObject *self, PyObject *args)
PyDoc_STRVAR(dumps_doc, PyDoc_STRVAR(dumps_doc,
"dumps(value[, version])\n\ "dumps(value[, version])\n\
\n\ \n\
Return the string that would be written to a file by dump(value, file).\n\ Return the bytes object that would be written to a file by dump(value, file).\n\
The value must be a supported type. Raise a ValueError exception if\n\ The value must be a supported type. Raise a ValueError exception if\n\
value has (or contains an object that has) an unsupported type.\n\ value has (or contains an object that has) an unsupported type.\n\
\n\ \n\
@ -1771,8 +1769,8 @@ marshal_loads(PyObject *self, PyObject *args)
PyDoc_STRVAR(loads_doc, PyDoc_STRVAR(loads_doc,
"loads(bytes)\n\ "loads(bytes)\n\
\n\ \n\
Convert the bytes object to a value. If no valid value is found, raise\n\ Convert the bytes-like object to a value. If no valid value is found,\n\
EOFError, ValueError or TypeError. Extra characters in the input are\n\ raise EOFError, ValueError or TypeError. Extra bytes in the input are\n\
ignored."); ignored.");
static PyMethodDef marshal_methods[] = { static PyMethodDef marshal_methods[] = {
@ -1810,8 +1808,8 @@ Functions:\n\
\n\ \n\
dump() -- write value to a file\n\ dump() -- write value to a file\n\
load() -- read value from a file\n\ load() -- read value from a file\n\
dumps() -- write value to a string\n\ dumps() -- marshal value as a bytes object\n\
loads() -- read value from a string"); loads() -- read value from a bytes-like object");