1/*[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_CAST(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 *const *args, Py_ssize_t nargs)
31{
32    PyObject *return_value = NULL;
33    PyObject *value;
34    PyObject *file;
35    int version = Py_MARSHAL_VERSION;
36
37    if (!_PyArg_CheckPositional("dump", nargs, 2, 3)) {
38        goto exit;
39    }
40    value = args[0];
41    file = args[1];
42    if (nargs < 3) {
43        goto skip_optional;
44    }
45    version = _PyLong_AsInt(args[2]);
46    if (version == -1 && PyErr_Occurred()) {
47        goto exit;
48    }
49skip_optional:
50    return_value = marshal_dump_impl(module, value, file, version);
51
52exit:
53    return return_value;
54}
55
56PyDoc_STRVAR(marshal_load__doc__,
57"load($module, file, /)\n"
58"--\n"
59"\n"
60"Read one value from the open file and return it.\n"
61"\n"
62"  file\n"
63"    Must be readable binary file.\n"
64"\n"
65"If no valid value is read (e.g. because the data has a different Python\n"
66"version\'s incompatible marshal format), raise EOFError, ValueError or\n"
67"TypeError.\n"
68"\n"
69"Note: If an object containing an unsupported type was marshalled with\n"
70"dump(), load() will substitute None for the unmarshallable type.");
71
72#define MARSHAL_LOAD_METHODDEF    \
73    {"load", (PyCFunction)marshal_load, METH_O, marshal_load__doc__},
74
75PyDoc_STRVAR(marshal_dumps__doc__,
76"dumps($module, value, version=version, /)\n"
77"--\n"
78"\n"
79"Return the bytes object that would be written to a file by dump(value, file).\n"
80"\n"
81"  value\n"
82"    Must be a supported type.\n"
83"  version\n"
84"    Indicates the data format that dumps should use.\n"
85"\n"
86"Raise a ValueError exception if value has (or contains an object that has) an\n"
87"unsupported type.");
88
89#define MARSHAL_DUMPS_METHODDEF    \
90    {"dumps", _PyCFunction_CAST(marshal_dumps), METH_FASTCALL, marshal_dumps__doc__},
91
92static PyObject *
93marshal_dumps_impl(PyObject *module, PyObject *value, int version);
94
95static PyObject *
96marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
97{
98    PyObject *return_value = NULL;
99    PyObject *value;
100    int version = Py_MARSHAL_VERSION;
101
102    if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) {
103        goto exit;
104    }
105    value = args[0];
106    if (nargs < 2) {
107        goto skip_optional;
108    }
109    version = _PyLong_AsInt(args[1]);
110    if (version == -1 && PyErr_Occurred()) {
111        goto exit;
112    }
113skip_optional:
114    return_value = marshal_dumps_impl(module, value, version);
115
116exit:
117    return return_value;
118}
119
120PyDoc_STRVAR(marshal_loads__doc__,
121"loads($module, bytes, /)\n"
122"--\n"
123"\n"
124"Convert the bytes-like object to a value.\n"
125"\n"
126"If no valid value is found, raise EOFError, ValueError or TypeError.  Extra\n"
127"bytes in the input are ignored.");
128
129#define MARSHAL_LOADS_METHODDEF    \
130    {"loads", (PyCFunction)marshal_loads, METH_O, marshal_loads__doc__},
131
132static PyObject *
133marshal_loads_impl(PyObject *module, Py_buffer *bytes);
134
135static PyObject *
136marshal_loads(PyObject *module, PyObject *arg)
137{
138    PyObject *return_value = NULL;
139    Py_buffer bytes = {NULL, NULL};
140
141    if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) {
142        goto exit;
143    }
144    if (!PyBuffer_IsContiguous(&bytes, 'C')) {
145        _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg);
146        goto exit;
147    }
148    return_value = marshal_loads_impl(module, &bytes);
149
150exit:
151    /* Cleanup for bytes */
152    if (bytes.obj) {
153       PyBuffer_Release(&bytes);
154    }
155
156    return return_value;
157}
158/*[clinic end generated code: output=b9e838edee43fe87 input=a9049054013a1b77]*/
159