Debian Codegraph @

Python/clinic/marshal.c.h

raw
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
10
11 PyDoc_STRVAR(marshal_dump__doc__,
12 "dump($module, value, file, version=version, /, *, allow_code=True)\n"
13 "--\n"
14 "\n"
15 "Write the value on the open file.\n"
16 "\n"
17 " value\n"
18 " Must be a supported type.\n"
19 " file\n"
20 " Must be a writeable binary file.\n"
21 " version\n"
22 " Indicates the data format that dump should use.\n"
23 " allow_code\n"
24 " Allow to write code objects.\n"
25 "\n"
26 "If the value has (or contains an object that has) an unsupported type, a\n"
27 "ValueError exception is raised - but garbage data will also be written\n"
28 "to the file. The object will not be properly read back by load().");
29
30 #define MARSHAL_DUMP_METHODDEF \
31 {"dump", _PyCFunction_CAST(marshal_dump), METH_FASTCALL|METH_KEYWORDS, marshal_dump__doc__},
32
33 static PyObject *
34 marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file,
35 int version, int allow_code);
36
37 static PyObject *
38 marshal_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
39 {
40 PyObject *return_value = NULL;
41 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
42
43 #define NUM_KEYWORDS 1
44 static struct {
45 PyGC_Head _this_is_not_used;
46 PyObject_VAR_HEAD
47 Py_hash_t ob_hash;
48 PyObject *ob_item[NUM_KEYWORDS];
49 } _kwtuple = {
50 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
51 .ob_hash = -1,
52 .ob_item = { &_Py_ID(allow_code), },
53 };
54 #undef NUM_KEYWORDS
55 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
56
57 #else // !Py_BUILD_CORE
58 # define KWTUPLE NULL
59 #endif // !Py_BUILD_CORE
60
61 static const char * const _keywords[] = {"", "", "", "allow_code", NULL};
62 static _PyArg_Parser _parser = {
63 .keywords = _keywords,
64 .fname = "dump",
65 .kwtuple = KWTUPLE,
66 };
67 #undef KWTUPLE
68 PyObject *argsbuf[4];
69 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
70 PyObject *value;
71 PyObject *file;
72 int version = Py_MARSHAL_VERSION;
73 int allow_code = 1;
74
75 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
76 /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
77 if (!args) {
78 goto exit;
79 }
80 value = args[0];
81 file = args[1];
82 if (nargs < 3) {
83 goto skip_optional_posonly;
84 }
85 noptargs--;
86 version = PyLong_AsInt(args[2]);
87 if (version == -1 && PyErr_Occurred()) {
88 goto exit;
89 }
90 skip_optional_posonly:
91 if (!noptargs) {
92 goto skip_optional_kwonly;
93 }
94 allow_code = PyObject_IsTrue(args[3]);
95 if (allow_code < 0) {
96 goto exit;
97 }
98 skip_optional_kwonly:
99 return_value = marshal_dump_impl(module, value, file, version, allow_code);
100
101 exit:
102 return return_value;
103 }
104
105 PyDoc_STRVAR(marshal_load__doc__,
106 "load($module, file, /, *, allow_code=True)\n"
107 "--\n"
108 "\n"
109 "Read one value from the open file and return it.\n"
110 "\n"
111 " file\n"
112 " Must be readable binary file.\n"
113 " allow_code\n"
114 " Allow to load code objects.\n"
115 "\n"
116 "If no valid value is read (e.g. because the data has a different Python\n"
117 "version\'s incompatible marshal format), raise EOFError, ValueError or\n"
118 "TypeError.\n"
119 "\n"
120 "Note: If an object containing an unsupported type was marshalled with\n"
121 "dump(), load() will substitute None for the unmarshallable type.");
122
123 #define MARSHAL_LOAD_METHODDEF \
124 {"load", _PyCFunction_CAST(marshal_load), METH_FASTCALL|METH_KEYWORDS, marshal_load__doc__},
125
126 static PyObject *
127 marshal_load_impl(PyObject *module, PyObject *file, int allow_code);
128
129 static PyObject *
130 marshal_load(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
131 {
132 PyObject *return_value = NULL;
133 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
134
135 #define NUM_KEYWORDS 1
136 static struct {
137 PyGC_Head _this_is_not_used;
138 PyObject_VAR_HEAD
139 Py_hash_t ob_hash;
140 PyObject *ob_item[NUM_KEYWORDS];
141 } _kwtuple = {
142 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
143 .ob_hash = -1,
144 .ob_item = { &_Py_ID(allow_code), },
145 };
146 #undef NUM_KEYWORDS
147 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
148
149 #else // !Py_BUILD_CORE
150 # define KWTUPLE NULL
151 #endif // !Py_BUILD_CORE
152
153 static const char * const _keywords[] = {"", "allow_code", NULL};
154 static _PyArg_Parser _parser = {
155 .keywords = _keywords,
156 .fname = "load",
157 .kwtuple = KWTUPLE,
158 };
159 #undef KWTUPLE
160 PyObject *argsbuf[2];
161 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
162 PyObject *file;
163 int allow_code = 1;
164
165 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
166 /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
167 if (!args) {
168 goto exit;
169 }
170 file = args[0];
171 if (!noptargs) {
172 goto skip_optional_kwonly;
173 }
174 allow_code = PyObject_IsTrue(args[1]);
175 if (allow_code < 0) {
176 goto exit;
177 }
178 skip_optional_kwonly:
179 return_value = marshal_load_impl(module, file, allow_code);
180
181 exit:
182 return return_value;
183 }
184
185 PyDoc_STRVAR(marshal_dumps__doc__,
186 "dumps($module, value, version=version, /, *, allow_code=True)\n"
187 "--\n"
188 "\n"
189 "Return the bytes object that would be written to a file by dump(value, file).\n"
190 "\n"
191 " value\n"
192 " Must be a supported type.\n"
193 " version\n"
194 " Indicates the data format that dumps should use.\n"
195 " allow_code\n"
196 " Allow to write code objects.\n"
197 "\n"
198 "Raise a ValueError exception if value has (or contains an object that\n"
199 "has) an unsupported type.");
200
201 #define MARSHAL_DUMPS_METHODDEF \
202 {"dumps", _PyCFunction_CAST(marshal_dumps), METH_FASTCALL|METH_KEYWORDS, marshal_dumps__doc__},
203
204 static PyObject *
205 marshal_dumps_impl(PyObject *module, PyObject *value, int version,
206 int allow_code);
207
208 static PyObject *
209 marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
210 {
211 PyObject *return_value = NULL;
212 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
213
214 #define NUM_KEYWORDS 1
215 static struct {
216 PyGC_Head _this_is_not_used;
217 PyObject_VAR_HEAD
218 Py_hash_t ob_hash;
219 PyObject *ob_item[NUM_KEYWORDS];
220 } _kwtuple = {
221 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
222 .ob_hash = -1,
223 .ob_item = { &_Py_ID(allow_code), },
224 };
225 #undef NUM_KEYWORDS
226 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
227
228 #else // !Py_BUILD_CORE
229 # define KWTUPLE NULL
230 #endif // !Py_BUILD_CORE
231
232 static const char * const _keywords[] = {"", "", "allow_code", NULL};
233 static _PyArg_Parser _parser = {
234 .keywords = _keywords,
235 .fname = "dumps",
236 .kwtuple = KWTUPLE,
237 };
238 #undef KWTUPLE
239 PyObject *argsbuf[3];
240 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
241 PyObject *value;
242 int version = Py_MARSHAL_VERSION;
243 int allow_code = 1;
244
245 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
246 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
247 if (!args) {
248 goto exit;
249 }
250 value = args[0];
251 if (nargs < 2) {
252 goto skip_optional_posonly;
253 }
254 noptargs--;
255 version = PyLong_AsInt(args[1]);
256 if (version == -1 && PyErr_Occurred()) {
257 goto exit;
258 }
259 skip_optional_posonly:
260 if (!noptargs) {
261 goto skip_optional_kwonly;
262 }
263 allow_code = PyObject_IsTrue(args[2]);
264 if (allow_code < 0) {
265 goto exit;
266 }
267 skip_optional_kwonly:
268 return_value = marshal_dumps_impl(module, value, version, allow_code);
269
270 exit:
271 return return_value;
272 }
273
274 PyDoc_STRVAR(marshal_loads__doc__,
275 "loads($module, bytes, /, *, allow_code=True)\n"
276 "--\n"
277 "\n"
278 "Convert the bytes-like object to a value.\n"
279 "\n"
280 " allow_code\n"
281 " Allow to load code objects.\n"
282 "\n"
283 "If no valid value is found, raise EOFError, ValueError or TypeError.\n"
284 "Extra bytes in the input are ignored.");
285
286 #define MARSHAL_LOADS_METHODDEF \
287 {"loads", _PyCFunction_CAST(marshal_loads), METH_FASTCALL|METH_KEYWORDS, marshal_loads__doc__},
288
289 static PyObject *
290 marshal_loads_impl(PyObject *module, Py_buffer *bytes, int allow_code);
291
292 static PyObject *
293 marshal_loads(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
294 {
295 PyObject *return_value = NULL;
296 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
297
298 #define NUM_KEYWORDS 1
299 static struct {
300 PyGC_Head _this_is_not_used;
301 PyObject_VAR_HEAD
302 Py_hash_t ob_hash;
303 PyObject *ob_item[NUM_KEYWORDS];
304 } _kwtuple = {
305 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
306 .ob_hash = -1,
307 .ob_item = { &_Py_ID(allow_code), },
308 };
309 #undef NUM_KEYWORDS
310 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
311
312 #else // !Py_BUILD_CORE
313 # define KWTUPLE NULL
314 #endif // !Py_BUILD_CORE
315
316 static const char * const _keywords[] = {"", "allow_code", NULL};
317 static _PyArg_Parser _parser = {
318 .keywords = _keywords,
319 .fname = "loads",
320 .kwtuple = KWTUPLE,
321 };
322 #undef KWTUPLE
323 PyObject *argsbuf[2];
324 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
325 Py_buffer bytes = {NULL, NULL};
326 int allow_code = 1;
327
328 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
329 /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
330 if (!args) {
331 goto exit;
332 }
333 if (PyObject_GetBuffer(args[0], &bytes, PyBUF_SIMPLE) != 0) {
334 goto exit;
335 }
336 if (!noptargs) {
337 goto skip_optional_kwonly;
338 }
339 allow_code = PyObject_IsTrue(args[1]);
340 if (allow_code < 0) {
341 goto exit;
342 }
343 skip_optional_kwonly:
344 return_value = marshal_loads_impl(module, &bytes, allow_code);
345
346 exit:
347 /* Cleanup for bytes */
348 if (bytes.obj) {
349 PyBuffer_Release(&bytes);
350 }
351
352 return return_value;
353 }
354 /*[clinic end generated code: output=a574570c3717f60e input=a9049054013a1b77]*/