File Doc/c-api/marshal.rst changed (mode: 100644) (index a6d0f4688d..c6d1d02a2f) |
... |
... |
unmarshalling. Version 2 uses a binary format for floating point numbers. |
34 |
34 |
|
|
35 |
35 |
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version) |
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version) |
36 |
36 |
|
|
37 |
|
Return a string object containing the marshalled representation of *value*. |
|
|
37 |
|
Return a bytes object containing the marshalled representation of *value*. |
38 |
38 |
*version* indicates the file format. |
*version* indicates the file format. |
39 |
39 |
|
|
40 |
40 |
|
|
|
... |
... |
written using these routines? |
88 |
88 |
:exc:`TypeError`) and returns *NULL*. |
:exc:`TypeError`) and returns *NULL*. |
89 |
89 |
|
|
90 |
90 |
|
|
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) |
92 |
92 |
|
|
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*. |
95 |
95 |
|
|
96 |
96 |
On error, sets the appropriate exception (:exc:`EOFError` or |
On error, sets the appropriate exception (:exc:`EOFError` or |
97 |
97 |
:exc:`TypeError`) and returns *NULL*. |
:exc:`TypeError`) and returns *NULL*. |
File Doc/glossary.rst changed (mode: 100644) (index e07ab0df2f..495934afe7) |
... |
... |
Glossary |
131 |
131 |
binary file |
binary file |
132 |
132 |
A :term:`file object` able to read and write |
A :term:`file object` able to read and write |
133 |
133 |
:term:`bytes-like objects <bytes-like object>`. |
: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`. |
134 |
138 |
|
|
135 |
139 |
.. seealso:: |
.. seealso:: |
136 |
140 |
A :term:`text file` reads and writes :class:`str` objects. |
A :term:`text file` reads and writes :class:`str` objects. |
|
... |
... |
Glossary |
966 |
970 |
A :term:`file object` able to read and write :class:`str` objects. |
A :term:`file object` able to read and write :class:`str` objects. |
967 |
971 |
Often, a text file actually accesses a byte-oriented datastream |
Often, a text file actually accesses a byte-oriented datastream |
968 |
972 |
and handles the :term:`text encoding` automatically. |
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`. |
969 |
976 |
|
|
970 |
977 |
.. seealso:: |
.. seealso:: |
971 |
978 |
A :term:`binary file` reads and write :class:`bytes` objects. |
A :term:`binary file` reads and write :class:`bytes` objects. |
File Doc/library/marshal.rst changed (mode: 100644) (index 1ffc6effc7..d65afc2004) |
... |
... |
For format *version* lower than 3, recursive lists, sets and dictionaries cannot |
49 |
49 |
be written (see below). |
be written (see below). |
50 |
50 |
|
|
51 |
51 |
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 |
52 |
|
strings. |
|
|
52 |
|
bytes-like objects. |
53 |
53 |
|
|
54 |
54 |
The module defines these functions: |
The module defines these functions: |
55 |
55 |
|
|
|
... |
... |
The module defines these functions: |
57 |
57 |
.. function:: dump(value, file[, version]) |
.. function:: dump(value, file[, version]) |
58 |
58 |
|
|
59 |
59 |
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 |
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`. |
63 |
61 |
|
|
64 |
62 |
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 |
65 |
63 |
:exc:`ValueError` exception is raised --- but garbage data will also be written |
:exc:`ValueError` exception is raised --- but garbage data will also be written |
|
... |
... |
The module defines these functions: |
74 |
72 |
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 |
75 |
73 |
(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 |
76 |
74 |
format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The |
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`. |
79 |
76 |
|
|
80 |
77 |
.. note:: |
.. note:: |
81 |
78 |
|
|
|
... |
... |
The module defines these functions: |
85 |
82 |
|
|
86 |
83 |
.. function:: dumps(value[, version]) |
.. function:: dumps(value[, version]) |
87 |
84 |
|
|
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 |
89 |
86 |
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 |
90 |
87 |
has (or contains an object that has) an unsupported type. |
has (or contains an object that has) an unsupported type. |
91 |
88 |
|
|
|
... |
... |
The module defines these functions: |
93 |
90 |
(see below). |
(see below). |
94 |
91 |
|
|
95 |
92 |
|
|
96 |
|
.. function:: loads(string) |
|
|
93 |
|
.. function:: loads(bytes) |
97 |
94 |
|
|
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. |
101 |
98 |
|
|
102 |
99 |
|
|
103 |
100 |
In addition, the following constants are defined: |
In addition, the following constants are defined: |
File Python/marshal.c changed (mode: 100644) (index d71d3c2b9d..a0fb0f3d8a) |
... |
... |
w_complex_object(PyObject *v, char flag, WFILE *p) |
549 |
549 |
w_object(co->co_lnotab, p); |
w_object(co->co_lnotab, p); |
550 |
550 |
} |
} |
551 |
551 |
else if (PyObject_CheckBuffer(v)) { |
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 */ |
553 |
553 |
Py_buffer view; |
Py_buffer view; |
554 |
554 |
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) { |
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) { |
555 |
555 |
w_byte(TYPE_UNKNOWN, p); |
w_byte(TYPE_UNKNOWN, p); |
|
... |
... |
r_object(RFILE *p) |
1086 |
1086 |
if (PyErr_Occurred()) |
if (PyErr_Occurred()) |
1087 |
1087 |
break; |
break; |
1088 |
1088 |
if (n < 0 || n > SIZE32_MAX) { |
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)"); |
1090 |
1090 |
break; |
break; |
1091 |
1091 |
} |
} |
1092 |
1092 |
v = PyBytes_FromStringAndSize((char *)NULL, n); |
v = PyBytes_FromStringAndSize((char *)NULL, n); |
|
... |
... |
r_object(RFILE *p) |
1110 |
1110 |
if (PyErr_Occurred()) |
if (PyErr_Occurred()) |
1111 |
1111 |
break; |
break; |
1112 |
1112 |
if (n < 0 || n > SIZE32_MAX) { |
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)"); |
1114 |
1114 |
break; |
break; |
1115 |
1115 |
} |
} |
1116 |
1116 |
goto _read_ascii; |
goto _read_ascii; |
|
... |
... |
r_object(RFILE *p) |
1150 |
1150 |
if (PyErr_Occurred()) |
if (PyErr_Occurred()) |
1151 |
1151 |
break; |
break; |
1152 |
1152 |
if (n < 0 || n > SIZE32_MAX) { |
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)"); |
1154 |
1154 |
break; |
break; |
1155 |
1155 |
} |
} |
1156 |
1156 |
if (n != 0) { |
if (n != 0) { |
|
... |
... |
PyMarshal_WriteObjectToString(PyObject *x, int version) |
1612 |
1612 |
if (wf.ptr - base > PY_SSIZE_T_MAX) { |
if (wf.ptr - base > PY_SSIZE_T_MAX) { |
1613 |
1613 |
Py_DECREF(wf.str); |
Py_DECREF(wf.str); |
1614 |
1614 |
PyErr_SetString(PyExc_OverflowError, |
PyErr_SetString(PyExc_OverflowError, |
1615 |
|
"too much marshal data for a string"); |
|
|
1615 |
|
"too much marshal data for a bytes object"); |
1616 |
1616 |
return NULL; |
return NULL; |
1617 |
1617 |
} |
} |
1618 |
1618 |
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0) |
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0) |
|
... |
... |
PyDoc_STRVAR(dump_doc, |
1658 |
1658 |
"dump(value, file[, version])\n\ |
"dump(value, file[, version])\n\ |
1659 |
1659 |
\n\ |
\n\ |
1660 |
1660 |
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\ |
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\ |
1663 |
1662 |
\n\ |
\n\ |
1664 |
1663 |
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\ |
1665 |
1664 |
ValueError exception is raised - but garbage data will also be written\n\ |
ValueError exception is raised - but garbage data will also be written\n\ |
|
... |
... |
PyDoc_STRVAR(load_doc, |
1715 |
1714 |
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\ |
1716 |
1715 |
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\ |
1717 |
1716 |
incompatible marshal format), raise EOFError, ValueError or TypeError.\n\ |
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\ |
1720 |
1718 |
\n\ |
\n\ |
1721 |
1719 |
Note: If an object containing an unsupported type was marshalled with\n\ |
Note: If an object containing an unsupported type was marshalled with\n\ |
1722 |
1720 |
dump(), load() will substitute None for the unmarshallable type."); |
dump(), load() will substitute None for the unmarshallable type."); |
|
... |
... |
marshal_dumps(PyObject *self, PyObject *args) |
1735 |
1733 |
PyDoc_STRVAR(dumps_doc, |
PyDoc_STRVAR(dumps_doc, |
1736 |
1734 |
"dumps(value[, version])\n\ |
"dumps(value[, version])\n\ |
1737 |
1735 |
\n\ |
\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\ |
1739 |
1737 |
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\ |
1740 |
1738 |
value has (or contains an object that has) an unsupported type.\n\ |
value has (or contains an object that has) an unsupported type.\n\ |
1741 |
1739 |
\n\ |
\n\ |
|
... |
... |
marshal_loads(PyObject *self, PyObject *args) |
1771 |
1769 |
PyDoc_STRVAR(loads_doc, |
PyDoc_STRVAR(loads_doc, |
1772 |
1770 |
"loads(bytes)\n\ |
"loads(bytes)\n\ |
1773 |
1771 |
\n\ |
\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\ |
1776 |
1774 |
ignored."); |
ignored."); |
1777 |
1775 |
|
|
1778 |
1776 |
static PyMethodDef marshal_methods[] = { |
static PyMethodDef marshal_methods[] = { |
|
... |
... |
Functions:\n\ |
1810 |
1808 |
\n\ |
\n\ |
1811 |
1809 |
dump() -- write value to a file\n\ |
dump() -- write value to a file\n\ |
1812 |
1810 |
load() -- read value from a file\n\ |
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"); |
1815 |
1813 |
|
|
1816 |
1814 |
|
|
1817 |
1815 |
|
|