Line data Source code
1 : /*[clinic input]
2 : preserve
3 : [clinic start generated code]*/
4 :
5 : PyDoc_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 :
25 : static PyObject *
26 : marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file,
27 : int version);
28 :
29 : static PyObject *
30 1003 : marshal_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
31 : {
32 1003 : PyObject *return_value = NULL;
33 : PyObject *value;
34 : PyObject *file;
35 1003 : int version = Py_MARSHAL_VERSION;
36 :
37 1003 : if (!_PyArg_CheckPositional("dump", nargs, 2, 3)) {
38 0 : goto exit;
39 : }
40 1003 : value = args[0];
41 1003 : file = args[1];
42 1003 : if (nargs < 3) {
43 805 : goto skip_optional;
44 : }
45 198 : version = _PyLong_AsInt(args[2]);
46 198 : if (version == -1 && PyErr_Occurred()) {
47 0 : goto exit;
48 : }
49 198 : skip_optional:
50 1003 : return_value = marshal_dump_impl(module, value, file, version);
51 :
52 1003 : exit:
53 1003 : return return_value;
54 : }
55 :
56 : PyDoc_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 :
75 : PyDoc_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 :
92 : static PyObject *
93 : marshal_dumps_impl(PyObject *module, PyObject *value, int version);
94 :
95 : static PyObject *
96 8735 : marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
97 : {
98 8735 : PyObject *return_value = NULL;
99 : PyObject *value;
100 8735 : int version = Py_MARSHAL_VERSION;
101 :
102 8735 : if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) {
103 0 : goto exit;
104 : }
105 8735 : value = args[0];
106 8735 : if (nargs < 2) {
107 8424 : goto skip_optional;
108 : }
109 311 : version = _PyLong_AsInt(args[1]);
110 311 : if (version == -1 && PyErr_Occurred()) {
111 0 : goto exit;
112 : }
113 311 : skip_optional:
114 8735 : return_value = marshal_dumps_impl(module, value, version);
115 :
116 8735 : exit:
117 8735 : return return_value;
118 : }
119 :
120 : PyDoc_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 :
132 : static PyObject *
133 : marshal_loads_impl(PyObject *module, Py_buffer *bytes);
134 :
135 : static PyObject *
136 203533 : marshal_loads(PyObject *module, PyObject *arg)
137 : {
138 203533 : PyObject *return_value = NULL;
139 203533 : Py_buffer bytes = {NULL, NULL};
140 :
141 203533 : if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) {
142 1 : goto exit;
143 : }
144 203532 : if (!PyBuffer_IsContiguous(&bytes, 'C')) {
145 0 : _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg);
146 0 : goto exit;
147 : }
148 203532 : return_value = marshal_loads_impl(module, &bytes);
149 :
150 203533 : exit:
151 : /* Cleanup for bytes */
152 203533 : if (bytes.obj) {
153 203532 : PyBuffer_Release(&bytes);
154 : }
155 :
156 203533 : return return_value;
157 : }
158 : /*[clinic end generated code: output=b9e838edee43fe87 input=a9049054013a1b77]*/
|