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