/src/Python-3.8.3/Python/clinic/marshal.c.h
Line | Count | Source (jump to first uncovered line) |
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)(void(*)(void))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 | | marshal_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
31 | 0 | { |
32 | 0 | PyObject *return_value = NULL; |
33 | 0 | PyObject *value; |
34 | 0 | PyObject *file; |
35 | 0 | int version = Py_MARSHAL_VERSION; |
36 | |
|
37 | 0 | if (!_PyArg_CheckPositional("dump", nargs, 2, 3)) { |
38 | 0 | goto exit; |
39 | 0 | } |
40 | 0 | value = args[0]; |
41 | 0 | file = args[1]; |
42 | 0 | if (nargs < 3) { |
43 | 0 | goto skip_optional; |
44 | 0 | } |
45 | 0 | if (PyFloat_Check(args[2])) { |
46 | 0 | PyErr_SetString(PyExc_TypeError, |
47 | 0 | "integer argument expected, got float" ); |
48 | 0 | goto exit; |
49 | 0 | } |
50 | 0 | version = _PyLong_AsInt(args[2]); |
51 | 0 | if (version == -1 && PyErr_Occurred()) { |
52 | 0 | goto exit; |
53 | 0 | } |
54 | 0 | skip_optional: |
55 | 0 | return_value = marshal_dump_impl(module, value, file, version); |
56 | |
|
57 | 0 | exit: |
58 | 0 | return return_value; |
59 | 0 | } |
60 | | |
61 | | PyDoc_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 | | |
80 | | PyDoc_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 \ |
95 | | {"dumps", (PyCFunction)(void(*)(void))marshal_dumps, METH_FASTCALL, marshal_dumps__doc__}, |
96 | | |
97 | | static PyObject * |
98 | | marshal_dumps_impl(PyObject *module, PyObject *value, int version); |
99 | | |
100 | | static PyObject * |
101 | | marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
102 | 0 | { |
103 | 0 | PyObject *return_value = NULL; |
104 | 0 | PyObject *value; |
105 | 0 | int version = Py_MARSHAL_VERSION; |
106 | |
|
107 | 0 | if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) { |
108 | 0 | goto exit; |
109 | 0 | } |
110 | 0 | value = args[0]; |
111 | 0 | if (nargs < 2) { |
112 | 0 | goto skip_optional; |
113 | 0 | } |
114 | 0 | if (PyFloat_Check(args[1])) { |
115 | 0 | PyErr_SetString(PyExc_TypeError, |
116 | 0 | "integer argument expected, got float" ); |
117 | 0 | goto exit; |
118 | 0 | } |
119 | 0 | version = _PyLong_AsInt(args[1]); |
120 | 0 | if (version == -1 && PyErr_Occurred()) { |
121 | 0 | goto exit; |
122 | 0 | } |
123 | 0 | skip_optional: |
124 | 0 | return_value = marshal_dumps_impl(module, value, version); |
125 | |
|
126 | 0 | exit: |
127 | 0 | return return_value; |
128 | 0 | } |
129 | | |
130 | | PyDoc_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 | | |
142 | | static PyObject * |
143 | | marshal_loads_impl(PyObject *module, Py_buffer *bytes); |
144 | | |
145 | | static PyObject * |
146 | | marshal_loads(PyObject *module, PyObject *arg) |
147 | 235 | { |
148 | 235 | PyObject *return_value = NULL; |
149 | 235 | Py_buffer bytes = {NULL, NULL}; |
150 | | |
151 | 235 | if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) { |
152 | 0 | goto exit; |
153 | 0 | } |
154 | 235 | if (!PyBuffer_IsContiguous(&bytes, 'C')) { |
155 | 0 | _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg); |
156 | 0 | goto exit; |
157 | 0 | } |
158 | 235 | return_value = marshal_loads_impl(module, &bytes); |
159 | | |
160 | 235 | exit: |
161 | | /* Cleanup for bytes */ |
162 | 235 | if (bytes.obj) { |
163 | 235 | PyBuffer_Release(&bytes); |
164 | 235 | } |
165 | | |
166 | 235 | return return_value; |
167 | 235 | } |
168 | | /*[clinic end generated code: output=a859dabe8b0afeb6 input=a9049054013a1b77]*/ |