blob: 1ae9332fd3f772579b1d4f305d61f526de2a2266 [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 *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020030marshal_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020031{
32 PyObject *return_value = NULL;
33 PyObject *value;
34 PyObject *file;
35 int version = Py_MARSHAL_VERSION;
36
Sylvain74453812017-06-10 06:51:48 +020037 if (!_PyArg_ParseStack(args, nargs, "OO|i:dump",
38 &value, &file, &version)) {
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020039 goto exit;
40 }
41 return_value = marshal_dump_impl(module, value, file, version);
42
43exit:
44 return return_value;
45}
46
47PyDoc_STRVAR(marshal_load__doc__,
48"load($module, file, /)\n"
49"--\n"
50"\n"
51"Read one value from the open file and return it.\n"
52"\n"
53" file\n"
54" Must be readable binary file.\n"
55"\n"
56"If no valid value is read (e.g. because the data has a different Python\n"
57"version\'s incompatible marshal format), raise EOFError, ValueError or\n"
58"TypeError.\n"
59"\n"
60"Note: If an object containing an unsupported type was marshalled with\n"
61"dump(), load() will substitute None for the unmarshallable type.");
62
63#define MARSHAL_LOAD_METHODDEF \
64 {"load", (PyCFunction)marshal_load, METH_O, marshal_load__doc__},
65
66PyDoc_STRVAR(marshal_dumps__doc__,
67"dumps($module, value, version=version, /)\n"
68"--\n"
69"\n"
70"Return the bytes object that would be written to a file by dump(value, file).\n"
71"\n"
72" value\n"
73" Must be a supported type.\n"
74" version\n"
75" Indicates the data format that dumps should use.\n"
76"\n"
77"Raise a ValueError exception if value has (or contains an object that has) an\n"
78"unsupported type.");
79
80#define MARSHAL_DUMPS_METHODDEF \
81 {"dumps", (PyCFunction)marshal_dumps, METH_FASTCALL, marshal_dumps__doc__},
82
83static PyObject *
84marshal_dumps_impl(PyObject *module, PyObject *value, int version);
85
86static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020087marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020088{
89 PyObject *return_value = NULL;
90 PyObject *value;
91 int version = Py_MARSHAL_VERSION;
92
Sylvain74453812017-06-10 06:51:48 +020093 if (!_PyArg_ParseStack(args, nargs, "O|i:dumps",
94 &value, &version)) {
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020095 goto exit;
96 }
97 return_value = marshal_dumps_impl(module, value, version);
98
99exit:
100 return return_value;
101}
102
103PyDoc_STRVAR(marshal_loads__doc__,
104"loads($module, bytes, /)\n"
105"--\n"
106"\n"
107"Convert the bytes-like object to a value.\n"
108"\n"
109"If no valid value is found, raise EOFError, ValueError or TypeError. Extra\n"
110"bytes in the input are ignored.");
111
112#define MARSHAL_LOADS_METHODDEF \
113 {"loads", (PyCFunction)marshal_loads, METH_O, marshal_loads__doc__},
114
115static PyObject *
116marshal_loads_impl(PyObject *module, Py_buffer *bytes);
117
118static PyObject *
119marshal_loads(PyObject *module, PyObject *arg)
120{
121 PyObject *return_value = NULL;
122 Py_buffer bytes = {NULL, NULL};
123
124 if (!PyArg_Parse(arg, "y*:loads", &bytes)) {
125 goto exit;
126 }
127 return_value = marshal_loads_impl(module, &bytes);
128
129exit:
130 /* Cleanup for bytes */
131 if (bytes.obj) {
132 PyBuffer_Release(&bytes);
133 }
134
135 return return_value;
136}
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200137/*[clinic end generated code: output=584eb2222d86fdc3 input=a9049054013a1b77]*/