| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 6 |
# include "pycore_gc.h" // PyGC_Head |
| 7 |
# include "pycore_runtime.h" // _Py_ID() |
| 8 |
#endif |
| 9 |
#include "pycore_abstract.h" // _PyNumber_Index() |
| 10 |
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
| 11 |
|
| 12 |
PyDoc_STRVAR(_zstd_ZstdDecompressor_new__doc__, |
| 13 |
"ZstdDecompressor(zstd_dict=None, options=None)\n" |
| 14 |
"--\n" |
| 15 |
"\n" |
| 16 |
"Create a decompressor object for decompressing data incrementally.\n" |
| 17 |
"\n" |
| 18 |
" zstd_dict\n" |
| 19 |
" A ZstdDict object, a pre-trained Zstandard dictionary.\n" |
| 20 |
" options\n" |
| 21 |
" A dict object that contains advanced decompression parameters.\n" |
| 22 |
"\n" |
| 23 |
"Thread-safe at method level. For one-shot decompression, use the decompress()\n" |
| 24 |
"function instead."); |
| 25 |
|
| 26 |
static PyObject * |
| 27 |
_zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict, |
| 28 |
PyObject *options); |
| 29 |
|
| 30 |
static PyObject * |
| 31 |
_zstd_ZstdDecompressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 32 |
{ |
| 33 |
PyObject *return_value = NULL; |
| 34 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 35 |
|
| 36 |
#define NUM_KEYWORDS 2 |
| 37 |
static struct { |
| 38 |
PyGC_Head _this_is_not_used; |
| 39 |
PyObject_VAR_HEAD |
| 40 |
Py_hash_t ob_hash; |
| 41 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 42 |
} _kwtuple = { |
| 43 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 44 |
.ob_hash = -1, |
| 45 |
.ob_item = { &_Py_ID(zstd_dict), &_Py_ID(options), }, |
| 46 |
}; |
| 47 |
#undef NUM_KEYWORDS |
| 48 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 49 |
|
| 50 |
#else // !Py_BUILD_CORE |
| 51 |
# define KWTUPLE NULL |
| 52 |
#endif // !Py_BUILD_CORE |
| 53 |
|
| 54 |
static const char * const _keywords[] = {"zstd_dict", "options", NULL}; |
| 55 |
static _PyArg_Parser _parser = { |
| 56 |
.keywords = _keywords, |
| 57 |
.fname = "ZstdDecompressor", |
| 58 |
.kwtuple = KWTUPLE, |
| 59 |
}; |
| 60 |
#undef KWTUPLE |
| 61 |
PyObject *argsbuf[2]; |
| 62 |
PyObject * const *fastargs; |
| 63 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 64 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
| 65 |
PyObject *zstd_dict = Py_None; |
| 66 |
PyObject *options = Py_None; |
| 67 |
|
| 68 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
| 69 |
/*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 70 |
if (!fastargs) { |
| 71 |
goto exit; |
| 72 |
} |
| 73 |
if (!noptargs) { |
| 74 |
goto skip_optional_pos; |
| 75 |
} |
| 76 |
if (fastargs[0]) { |
| 77 |
zstd_dict = fastargs[0]; |
| 78 |
if (!--noptargs) { |
| 79 |
goto skip_optional_pos; |
| 80 |
} |
| 81 |
} |
| 82 |
options = fastargs[1]; |
| 83 |
skip_optional_pos: |
| 84 |
return_value = _zstd_ZstdDecompressor_new_impl(type, zstd_dict, options); |
| 85 |
|
| 86 |
exit: |
| 87 |
return return_value; |
| 88 |
} |
| 89 |
|
| 90 |
PyDoc_STRVAR(_zstd_ZstdDecompressor_unused_data__doc__, |
| 91 |
"A bytes object of un-consumed input data.\n" |
| 92 |
"\n" |
| 93 |
"When ZstdDecompressor object stops after a frame is\n" |
| 94 |
"decompressed, unused input data after the frame. Otherwise this will be b\'\'."); |
| 95 |
#if defined(_zstd_ZstdDecompressor_unused_data_DOCSTR) |
| 96 |
# undef _zstd_ZstdDecompressor_unused_data_DOCSTR |
| 97 |
#endif |
| 98 |
#define _zstd_ZstdDecompressor_unused_data_DOCSTR _zstd_ZstdDecompressor_unused_data__doc__ |
| 99 |
|
| 100 |
#if !defined(_zstd_ZstdDecompressor_unused_data_DOCSTR) |
| 101 |
# define _zstd_ZstdDecompressor_unused_data_DOCSTR NULL |
| 102 |
#endif |
| 103 |
#if defined(_ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF) |
| 104 |
# undef _ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF |
| 105 |
# define _ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF {"unused_data", (getter)_zstd_ZstdDecompressor_unused_data_get, (setter)_zstd_ZstdDecompressor_unused_data_set, _zstd_ZstdDecompressor_unused_data_DOCSTR}, |
| 106 |
#else |
| 107 |
# define _ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF {"unused_data", (getter)_zstd_ZstdDecompressor_unused_data_get, NULL, _zstd_ZstdDecompressor_unused_data_DOCSTR}, |
| 108 |
#endif |
| 109 |
|
| 110 |
static PyObject * |
| 111 |
_zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self); |
| 112 |
|
| 113 |
static PyObject * |
| 114 |
_zstd_ZstdDecompressor_unused_data_get(PyObject *self, void *Py_UNUSED(context)) |
| 115 |
{ |
| 116 |
return _zstd_ZstdDecompressor_unused_data_get_impl((ZstdDecompressor *)self); |
| 117 |
} |
| 118 |
|
| 119 |
PyDoc_STRVAR(_zstd_ZstdDecompressor_decompress__doc__, |
| 120 |
"decompress($self, /, data, max_length=-1)\n" |
| 121 |
"--\n" |
| 122 |
"\n" |
| 123 |
"Decompress *data*, returning uncompressed bytes if possible, or b\'\' otherwise.\n" |
| 124 |
"\n" |
| 125 |
" data\n" |
| 126 |
" A bytes-like object, Zstandard data to be decompressed.\n" |
| 127 |
" max_length\n" |
| 128 |
" Maximum size of returned data. When it is negative, the size of\n" |
| 129 |
" output buffer is unlimited. When it is nonnegative, returns at\n" |
| 130 |
" most max_length bytes of decompressed data.\n" |
| 131 |
"\n" |
| 132 |
"If *max_length* is nonnegative, returns at most *max_length* bytes of\n" |
| 133 |
"decompressed data. If this limit is reached and further output can be\n" |
| 134 |
"produced, *self.needs_input* will be set to ``False``. In this case, the next\n" |
| 135 |
"call to *decompress()* may provide *data* as b\'\' to obtain more of the output.\n" |
| 136 |
"\n" |
| 137 |
"If all of the input data was decompressed and returned (either because this\n" |
| 138 |
"was less than *max_length* bytes, or because *max_length* was negative),\n" |
| 139 |
"*self.needs_input* will be set to True.\n" |
| 140 |
"\n" |
| 141 |
"Attempting to decompress data after the end of a frame is reached raises an\n" |
| 142 |
"EOFError. Any data found after the end of the frame is ignored and saved in\n" |
| 143 |
"the self.unused_data attribute."); |
| 144 |
|
| 145 |
#define _ZSTD_ZSTDDECOMPRESSOR_DECOMPRESS_METHODDEF \ |
| 146 |
{"decompress", _PyCFunction_CAST(_zstd_ZstdDecompressor_decompress), METH_FASTCALL|METH_KEYWORDS, _zstd_ZstdDecompressor_decompress__doc__}, |
| 147 |
|
| 148 |
static PyObject * |
| 149 |
_zstd_ZstdDecompressor_decompress_impl(ZstdDecompressor *self, |
| 150 |
Py_buffer *data, |
| 151 |
Py_ssize_t max_length); |
| 152 |
|
| 153 |
static PyObject * |
| 154 |
_zstd_ZstdDecompressor_decompress(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 155 |
{ |
| 156 |
PyObject *return_value = NULL; |
| 157 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 158 |
|
| 159 |
#define NUM_KEYWORDS 2 |
| 160 |
static struct { |
| 161 |
PyGC_Head _this_is_not_used; |
| 162 |
PyObject_VAR_HEAD |
| 163 |
Py_hash_t ob_hash; |
| 164 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 165 |
} _kwtuple = { |
| 166 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 167 |
.ob_hash = -1, |
| 168 |
.ob_item = { &_Py_ID(data), &_Py_ID(max_length), }, |
| 169 |
}; |
| 170 |
#undef NUM_KEYWORDS |
| 171 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 172 |
|
| 173 |
#else // !Py_BUILD_CORE |
| 174 |
# define KWTUPLE NULL |
| 175 |
#endif // !Py_BUILD_CORE |
| 176 |
|
| 177 |
static const char * const _keywords[] = {"data", "max_length", NULL}; |
| 178 |
static _PyArg_Parser _parser = { |
| 179 |
.keywords = _keywords, |
| 180 |
.fname = "decompress", |
| 181 |
.kwtuple = KWTUPLE, |
| 182 |
}; |
| 183 |
#undef KWTUPLE |
| 184 |
PyObject *argsbuf[2]; |
| 185 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
| 186 |
Py_buffer data = {NULL, NULL}; |
| 187 |
Py_ssize_t max_length = -1; |
| 188 |
|
| 189 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 190 |
/*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 191 |
if (!args) { |
| 192 |
goto exit; |
| 193 |
} |
| 194 |
if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 195 |
goto exit; |
| 196 |
} |
| 197 |
if (!noptargs) { |
| 198 |
goto skip_optional_pos; |
| 199 |
} |
| 200 |
{ |
| 201 |
Py_ssize_t ival = -1; |
| 202 |
PyObject *iobj = _PyNumber_Index(args[1]); |
| 203 |
if (iobj != NULL) { |
| 204 |
ival = PyLong_AsSsize_t(iobj); |
| 205 |
Py_DECREF(iobj); |
| 206 |
} |
| 207 |
if (ival == -1 && PyErr_Occurred()) { |
| 208 |
goto exit; |
| 209 |
} |
| 210 |
max_length = ival; |
| 211 |
} |
| 212 |
skip_optional_pos: |
| 213 |
return_value = _zstd_ZstdDecompressor_decompress_impl((ZstdDecompressor *)self, &data, max_length); |
| 214 |
|
| 215 |
exit: |
| 216 |
/* Cleanup for data */ |
| 217 |
if (data.obj) { |
| 218 |
PyBuffer_Release(&data); |
| 219 |
} |
| 220 |
|
| 221 |
return return_value; |
| 222 |
} |
| 223 |
/*[clinic end generated code: output=30c12ef047027ede input=a9049054013a1b77]*/ |