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 \ |
| 23 | {"dump", (PyCFunction)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 * |
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 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 37 | if (!_PyArg_ParseStack(args, nargs, "OO|i:dump", |
| 38 | &value, &file, &version)) { |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 39 | goto exit; |
| 40 | } |
| 41 | return_value = marshal_dump_impl(module, value, file, version); |
| 42 | |
| 43 | exit: |
| 44 | return return_value; |
| 45 | } |
| 46 | |
| 47 | PyDoc_STRVAR(marshal_load__doc__, |
| 48 | "load($module, file, /)\n" |
| 49 | "--\n" |
| 50 | "\n" |
| 51 | "Read one value from the open file and return it.\n" |
| 52 | "\n" |
| 53 | " file\n" |
| 54 | " Must be readable binary file.\n" |
| 55 | "\n" |
| 56 | "If no valid value is read (e.g. because the data has a different Python\n" |
| 57 | "version\'s incompatible marshal format), raise EOFError, ValueError or\n" |
| 58 | "TypeError.\n" |
| 59 | "\n" |
| 60 | "Note: If an object containing an unsupported type was marshalled with\n" |
| 61 | "dump(), load() will substitute None for the unmarshallable type."); |
| 62 | |
| 63 | #define MARSHAL_LOAD_METHODDEF \ |
| 64 | {"load", (PyCFunction)marshal_load, METH_O, marshal_load__doc__}, |
| 65 | |
| 66 | PyDoc_STRVAR(marshal_dumps__doc__, |
| 67 | "dumps($module, value, version=version, /)\n" |
| 68 | "--\n" |
| 69 | "\n" |
| 70 | "Return the bytes object that would be written to a file by dump(value, file).\n" |
| 71 | "\n" |
| 72 | " value\n" |
| 73 | " Must be a supported type.\n" |
| 74 | " version\n" |
| 75 | " Indicates the data format that dumps should use.\n" |
| 76 | "\n" |
| 77 | "Raise a ValueError exception if value has (or contains an object that has) an\n" |
| 78 | "unsupported type."); |
| 79 | |
| 80 | #define MARSHAL_DUMPS_METHODDEF \ |
| 81 | {"dumps", (PyCFunction)marshal_dumps, METH_FASTCALL, marshal_dumps__doc__}, |
| 82 | |
| 83 | static PyObject * |
| 84 | marshal_dumps_impl(PyObject *module, PyObject *value, int version); |
| 85 | |
| 86 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 87 | marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 88 | { |
| 89 | PyObject *return_value = NULL; |
| 90 | PyObject *value; |
| 91 | int version = Py_MARSHAL_VERSION; |
| 92 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 93 | if (!_PyArg_ParseStack(args, nargs, "O|i:dumps", |
| 94 | &value, &version)) { |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 95 | goto exit; |
| 96 | } |
| 97 | return_value = marshal_dumps_impl(module, value, version); |
| 98 | |
| 99 | exit: |
| 100 | return return_value; |
| 101 | } |
| 102 | |
| 103 | PyDoc_STRVAR(marshal_loads__doc__, |
| 104 | "loads($module, bytes, /)\n" |
| 105 | "--\n" |
| 106 | "\n" |
| 107 | "Convert the bytes-like object to a value.\n" |
| 108 | "\n" |
| 109 | "If no valid value is found, raise EOFError, ValueError or TypeError. Extra\n" |
| 110 | "bytes in the input are ignored."); |
| 111 | |
| 112 | #define MARSHAL_LOADS_METHODDEF \ |
| 113 | {"loads", (PyCFunction)marshal_loads, METH_O, marshal_loads__doc__}, |
| 114 | |
| 115 | static PyObject * |
| 116 | marshal_loads_impl(PyObject *module, Py_buffer *bytes); |
| 117 | |
| 118 | static PyObject * |
| 119 | marshal_loads(PyObject *module, PyObject *arg) |
| 120 | { |
| 121 | PyObject *return_value = NULL; |
| 122 | Py_buffer bytes = {NULL, NULL}; |
| 123 | |
| 124 | if (!PyArg_Parse(arg, "y*:loads", &bytes)) { |
| 125 | goto exit; |
| 126 | } |
| 127 | return_value = marshal_loads_impl(module, &bytes); |
| 128 | |
| 129 | exit: |
| 130 | /* Cleanup for bytes */ |
| 131 | if (bytes.obj) { |
| 132 | PyBuffer_Release(&bytes); |
| 133 | } |
| 134 | |
| 135 | return return_value; |
| 136 | } |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 137 | /*[clinic end generated code: output=584eb2222d86fdc3 input=a9049054013a1b77]*/ |