blob: f80d5ef31f29c74957a17150e20c51bd8f7cd5af [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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020023 {"dump", (PyCFunction)(void(*)(void))marshal_dump, METH_FASTCALL, marshal_dump__doc__},
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020024
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
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020037 if (!_PyArg_CheckPositional("dump", nargs, 2, 3)) {
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020038 goto exit;
39 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020040 value = args[0];
41 file = args[1];
42 if (nargs < 3) {
43 goto skip_optional;
44 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020045 version = _PyLong_AsInt(args[2]);
46 if (version == -1 && PyErr_Occurred()) {
47 goto exit;
48 }
49skip_optional:
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020050 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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020090 {"dumps", (PyCFunction)(void(*)(void))marshal_dumps, METH_FASTCALL, marshal_dumps__doc__},
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020091
92static PyObject *
93marshal_dumps_impl(PyObject *module, PyObject *value, int version);
94
95static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020096marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020097{
98 PyObject *return_value = NULL;
99 PyObject *value;
100 int version = Py_MARSHAL_VERSION;
101
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200102 if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) {
Serhiy Storchaka0767ad42017-03-12 09:20:15 +0200103 goto exit;
104 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200105 value = args[0];
106 if (nargs < 2) {
107 goto skip_optional;
108 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200109 version = _PyLong_AsInt(args[1]);
110 if (version == -1 && PyErr_Occurred()) {
111 goto exit;
112 }
113skip_optional:
Serhiy Storchaka0767ad42017-03-12 09:20:15 +0200114 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
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200141 if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) {
142 goto exit;
143 }
144 if (!PyBuffer_IsContiguous(&bytes, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200145 _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg);
Serhiy Storchaka0767ad42017-03-12 09:20:15 +0200146 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}
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300158/*[clinic end generated code: output=68b78f38bfe0c06d input=a9049054013a1b77]*/