Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 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 \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 23 | {"dump", (PyCFunction)(void(*)(void))marshal_dump, METH_FASTCALL, marshal_dump__doc__}, |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 24 | |
| 25 | static PyObject * |
| 26 | marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file, |
| 27 | int version); |
| 28 | |
| 29 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 30 | marshal_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 31 | { |
| 32 | PyObject *return_value = NULL; |
| 33 | PyObject *value; |
| 34 | PyObject *file; |
| 35 | int version = Py_MARSHAL_VERSION; |
| 36 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 37 | if (!_PyArg_CheckPositional("dump", nargs, 2, 3)) { |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 38 | goto exit; |
| 39 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 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: |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 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 \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 95 | {"dumps", (PyCFunction)(void(*)(void))marshal_dumps, METH_FASTCALL, marshal_dumps__doc__}, |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 96 | |
| 97 | static PyObject * |
| 98 | marshal_dumps_impl(PyObject *module, PyObject *value, int version); |
| 99 | |
| 100 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 101 | marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 102 | { |
| 103 | PyObject *return_value = NULL; |
| 104 | PyObject *value; |
| 105 | int version = Py_MARSHAL_VERSION; |
| 106 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 107 | if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) { |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 108 | goto exit; |
| 109 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 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: |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 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 * |
| 146 | marshal_loads(PyObject *module, PyObject *arg) |
| 147 | { |
| 148 | PyObject *return_value = NULL; |
| 149 | Py_buffer bytes = {NULL, NULL}; |
| 150 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 151 | if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) { |
| 152 | goto exit; |
| 153 | } |
| 154 | if (!PyBuffer_IsContiguous(&bytes, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 155 | _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg); |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 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 | } |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 168 | /*[clinic end generated code: output=a859dabe8b0afeb6 input=a9049054013a1b77]*/ |