• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
marshal_dump(PyObject * module,PyObject * const * args,Py_ssize_t nargs)30 marshal_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
31 {
32     PyObject *return_value = NULL;
33     PyObject *value;
34     PyObject *file;
35     int version = Py_MARSHAL_VERSION;
36 
37     if (!_PyArg_CheckPositional("dump", nargs, 2, 3)) {
38         goto exit;
39     }
40     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     }
54 skip_optional:
55     return_value = marshal_dump_impl(module, value, file, version);
56 
57 exit:
58     return return_value;
59 }
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 *
marshal_dumps(PyObject * module,PyObject * const * args,Py_ssize_t nargs)101 marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
102 {
103     PyObject *return_value = NULL;
104     PyObject *value;
105     int version = Py_MARSHAL_VERSION;
106 
107     if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) {
108         goto exit;
109     }
110     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     }
123 skip_optional:
124     return_value = marshal_dumps_impl(module, value, version);
125 
126 exit:
127     return return_value;
128 }
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 *
marshal_loads(PyObject * module,PyObject * arg)146 marshal_loads(PyObject *module, PyObject *arg)
147 {
148     PyObject *return_value = NULL;
149     Py_buffer bytes = {NULL, NULL};
150 
151     if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) {
152         goto exit;
153     }
154     if (!PyBuffer_IsContiguous(&bytes, 'C')) {
155         _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg);
156         goto exit;
157     }
158     return_value = marshal_loads_impl(module, &bytes);
159 
160 exit:
161     /* Cleanup for bytes */
162     if (bytes.obj) {
163        PyBuffer_Release(&bytes);
164     }
165 
166     return return_value;
167 }
168 /*[clinic end generated code: output=a859dabe8b0afeb6 input=a9049054013a1b77]*/
169