blob: ae2c8990a6c1392c50c3bce27b94f0e9ab9ba2c1 [file] [log] [blame]
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(marshal_dump__doc__,
6"dump($module, value, file, version=version, /)\n"
7"--\n"
8"\n"
9"Write the value on the open file.\n"
10"\n"
11" value\n"
12" Must be a supported type.\n"
13" file\n"
14" Must be a writeable binary file.\n"
15" version\n"
16" Indicates the data format that dump should use.\n"
17"\n"
18"If the value has (or contains an object that has) an unsupported type, a\n"
19"ValueError exception is raised - but garbage data will also be written\n"
20"to the file. The object will not be properly read back by load().");
21
22#define MARSHAL_DUMP_METHODDEF \
23 {"dump", (PyCFunction)marshal_dump, METH_FASTCALL, marshal_dump__doc__},
24
25static PyObject *
26marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file,
27 int version);
28
29static PyObject *
30marshal_dump(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
31{
32 PyObject *return_value = NULL;
33 PyObject *value;
34 PyObject *file;
35 int version = Py_MARSHAL_VERSION;
36
37 if (!_PyArg_ParseStack(args, nargs, "OO|i:dump",
38 &value, &file, &version)) {
39 goto exit;
40 }
41
42 if (!_PyArg_NoStackKeywords("dump", kwnames)) {
43 goto exit;
44 }
45 return_value = marshal_dump_impl(module, value, file, version);
46
47exit:
48 return return_value;
49}
50
51PyDoc_STRVAR(marshal_load__doc__,
52"load($module, file, /)\n"
53"--\n"
54"\n"
55"Read one value from the open file and return it.\n"
56"\n"
57" file\n"
58" Must be readable binary file.\n"
59"\n"
60"If no valid value is read (e.g. because the data has a different Python\n"
61"version\'s incompatible marshal format), raise EOFError, ValueError or\n"
62"TypeError.\n"
63"\n"
64"Note: If an object containing an unsupported type was marshalled with\n"
65"dump(), load() will substitute None for the unmarshallable type.");
66
67#define MARSHAL_LOAD_METHODDEF \
68 {"load", (PyCFunction)marshal_load, METH_O, marshal_load__doc__},
69
70PyDoc_STRVAR(marshal_dumps__doc__,
71"dumps($module, value, version=version, /)\n"
72"--\n"
73"\n"
74"Return the bytes object that would be written to a file by dump(value, file).\n"
75"\n"
76" value\n"
77" Must be a supported type.\n"
78" version\n"
79" Indicates the data format that dumps should use.\n"
80"\n"
81"Raise a ValueError exception if value has (or contains an object that has) an\n"
82"unsupported type.");
83
84#define MARSHAL_DUMPS_METHODDEF \
85 {"dumps", (PyCFunction)marshal_dumps, METH_FASTCALL, marshal_dumps__doc__},
86
87static PyObject *
88marshal_dumps_impl(PyObject *module, PyObject *value, int version);
89
90static PyObject *
91marshal_dumps(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
92{
93 PyObject *return_value = NULL;
94 PyObject *value;
95 int version = Py_MARSHAL_VERSION;
96
97 if (!_PyArg_ParseStack(args, nargs, "O|i:dumps",
98 &value, &version)) {
99 goto exit;
100 }
101
102 if (!_PyArg_NoStackKeywords("dumps", kwnames)) {
103 goto exit;
104 }
105 return_value = marshal_dumps_impl(module, value, version);
106
107exit:
108 return return_value;
109}
110
111PyDoc_STRVAR(marshal_loads__doc__,
112"loads($module, bytes, /)\n"
113"--\n"
114"\n"
115"Convert the bytes-like object to a value.\n"
116"\n"
117"If no valid value is found, raise EOFError, ValueError or TypeError. Extra\n"
118"bytes in the input are ignored.");
119
120#define MARSHAL_LOADS_METHODDEF \
121 {"loads", (PyCFunction)marshal_loads, METH_O, marshal_loads__doc__},
122
123static PyObject *
124marshal_loads_impl(PyObject *module, Py_buffer *bytes);
125
126static PyObject *
127marshal_loads(PyObject *module, PyObject *arg)
128{
129 PyObject *return_value = NULL;
130 Py_buffer bytes = {NULL, NULL};
131
132 if (!PyArg_Parse(arg, "y*:loads", &bytes)) {
133 goto exit;
134 }
135 return_value = marshal_loads_impl(module, &bytes);
136
137exit:
138 /* Cleanup for bytes */
139 if (bytes.obj) {
140 PyBuffer_Release(&bytes);
141 }
142
143 return return_value;
144}
145/*[clinic end generated code: output=9dec2158b8c5d975 input=a9049054013a1b77]*/