blob: 05d4830c4ab313c73636d0438cab50f63e0d5274 [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 }
45 if (PyFloat_Check(args[2])) {
46 PyErr_SetString(PyExc_TypeError,
47 "integer argument expected, got float" );
48 goto exit;
49 }
50 version = _PyLong_AsInt(args[2]);
51 if (version == -1 && PyErr_Occurred()) {
52 goto exit;
53 }
54skip_optional:
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020055 return_value = marshal_dump_impl(module, value, file, version);
56
57exit:
58 return return_value;
59}
60
61PyDoc_STRVAR(marshal_load__doc__,
62"load($module, file, /)\n"
63"--\n"
64"\n"
65"Read one value from the open file and return it.\n"
66"\n"
67" file\n"
68" Must be readable binary file.\n"
69"\n"
70"If no valid value is read (e.g. because the data has a different Python\n"
71"version\'s incompatible marshal format), raise EOFError, ValueError or\n"
72"TypeError.\n"
73"\n"
74"Note: If an object containing an unsupported type was marshalled with\n"
75"dump(), load() will substitute None for the unmarshallable type.");
76
77#define MARSHAL_LOAD_METHODDEF \
78 {"load", (PyCFunction)marshal_load, METH_O, marshal_load__doc__},
79
80PyDoc_STRVAR(marshal_dumps__doc__,
81"dumps($module, value, version=version, /)\n"
82"--\n"
83"\n"
84"Return the bytes object that would be written to a file by dump(value, file).\n"
85"\n"
86" value\n"
87" Must be a supported type.\n"
88" version\n"
89" Indicates the data format that dumps should use.\n"
90"\n"
91"Raise a ValueError exception if value has (or contains an object that has) an\n"
92"unsupported type.");
93
94#define MARSHAL_DUMPS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020095 {"dumps", (PyCFunction)(void(*)(void))marshal_dumps, METH_FASTCALL, marshal_dumps__doc__},
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020096
97static PyObject *
98marshal_dumps_impl(PyObject *module, PyObject *value, int version);
99
100static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200101marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0767ad42017-03-12 09:20:15 +0200102{
103 PyObject *return_value = NULL;
104 PyObject *value;
105 int version = Py_MARSHAL_VERSION;
106
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200107 if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) {
Serhiy Storchaka0767ad42017-03-12 09:20:15 +0200108 goto exit;
109 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200110 value = args[0];
111 if (nargs < 2) {
112 goto skip_optional;
113 }
114 if (PyFloat_Check(args[1])) {
115 PyErr_SetString(PyExc_TypeError,
116 "integer argument expected, got float" );
117 goto exit;
118 }
119 version = _PyLong_AsInt(args[1]);
120 if (version == -1 && PyErr_Occurred()) {
121 goto exit;
122 }
123skip_optional:
Serhiy Storchaka0767ad42017-03-12 09:20:15 +0200124 return_value = marshal_dumps_impl(module, value, version);
125
126exit:
127 return return_value;
128}
129
130PyDoc_STRVAR(marshal_loads__doc__,
131"loads($module, bytes, /)\n"
132"--\n"
133"\n"
134"Convert the bytes-like object to a value.\n"
135"\n"
136"If no valid value is found, raise EOFError, ValueError or TypeError. Extra\n"
137"bytes in the input are ignored.");
138
139#define MARSHAL_LOADS_METHODDEF \
140 {"loads", (PyCFunction)marshal_loads, METH_O, marshal_loads__doc__},
141
142static PyObject *
143marshal_loads_impl(PyObject *module, Py_buffer *bytes);
144
145static PyObject *
146marshal_loads(PyObject *module, PyObject *arg)
147{
148 PyObject *return_value = NULL;
149 Py_buffer bytes = {NULL, NULL};
150
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200151 if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) {
152 goto exit;
153 }
154 if (!PyBuffer_IsContiguous(&bytes, 'C')) {
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300155 _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg);
Serhiy Storchaka0767ad42017-03-12 09:20:15 +0200156 goto exit;
157 }
158 return_value = marshal_loads_impl(module, &bytes);
159
160exit:
161 /* Cleanup for bytes */
162 if (bytes.obj) {
163 PyBuffer_Release(&bytes);
164 }
165
166 return return_value;
167}
Serhiy Storchaka96631dc2019-08-29 18:29:59 +0300168/*[clinic end generated code: output=a859dabe8b0afeb6 input=a9049054013a1b77]*/