| 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_modsupport.h" // _PyArg_UnpackKeywords() |
| 10 |
|
| 11 |
PyDoc_STRVAR(_zstd_ZstdCompressor_new__doc__, |
| 12 |
"ZstdCompressor(level=None, options=None, zstd_dict=None)\n" |
| 13 |
"--\n" |
| 14 |
"\n" |
| 15 |
"Create a compressor object for compressing data incrementally.\n" |
| 16 |
"\n" |
| 17 |
" level\n" |
| 18 |
" The compression level to use. Defaults to COMPRESSION_LEVEL_DEFAULT.\n" |
| 19 |
" options\n" |
| 20 |
" A dict object that contains advanced compression parameters.\n" |
| 21 |
" zstd_dict\n" |
| 22 |
" A ZstdDict object, a pre-trained Zstandard dictionary.\n" |
| 23 |
"\n" |
| 24 |
"Thread-safe at method level. For one-shot compression, use the compress()\n" |
| 25 |
"function instead."); |
| 26 |
|
| 27 |
static PyObject * |
| 28 |
_zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level, |
| 29 |
PyObject *options, PyObject *zstd_dict); |
| 30 |
|
| 31 |
static PyObject * |
| 32 |
_zstd_ZstdCompressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 33 |
{ |
| 34 |
PyObject *return_value = NULL; |
| 35 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 36 |
|
| 37 |
#define NUM_KEYWORDS 3 |
| 38 |
static struct { |
| 39 |
PyGC_Head _this_is_not_used; |
| 40 |
PyObject_VAR_HEAD |
| 41 |
Py_hash_t ob_hash; |
| 42 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 43 |
} _kwtuple = { |
| 44 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 45 |
.ob_hash = -1, |
| 46 |
.ob_item = { &_Py_ID(level), &_Py_ID(options), &_Py_ID(zstd_dict), }, |
| 47 |
}; |
| 48 |
#undef NUM_KEYWORDS |
| 49 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 50 |
|
| 51 |
#else // !Py_BUILD_CORE |
| 52 |
# define KWTUPLE NULL |
| 53 |
#endif // !Py_BUILD_CORE |
| 54 |
|
| 55 |
static const char * const _keywords[] = {"level", "options", "zstd_dict", NULL}; |
| 56 |
static _PyArg_Parser _parser = { |
| 57 |
.keywords = _keywords, |
| 58 |
.fname = "ZstdCompressor", |
| 59 |
.kwtuple = KWTUPLE, |
| 60 |
}; |
| 61 |
#undef KWTUPLE |
| 62 |
PyObject *argsbuf[3]; |
| 63 |
PyObject * const *fastargs; |
| 64 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 65 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
| 66 |
PyObject *level = Py_None; |
| 67 |
PyObject *options = Py_None; |
| 68 |
PyObject *zstd_dict = Py_None; |
| 69 |
|
| 70 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
| 71 |
/*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 72 |
if (!fastargs) { |
| 73 |
goto exit; |
| 74 |
} |
| 75 |
if (!noptargs) { |
| 76 |
goto skip_optional_pos; |
| 77 |
} |
| 78 |
if (fastargs[0]) { |
| 79 |
level = fastargs[0]; |
| 80 |
if (!--noptargs) { |
| 81 |
goto skip_optional_pos; |
| 82 |
} |
| 83 |
} |
| 84 |
if (fastargs[1]) { |
| 85 |
options = fastargs[1]; |
| 86 |
if (!--noptargs) { |
| 87 |
goto skip_optional_pos; |
| 88 |
} |
| 89 |
} |
| 90 |
zstd_dict = fastargs[2]; |
| 91 |
skip_optional_pos: |
| 92 |
return_value = _zstd_ZstdCompressor_new_impl(type, level, options, zstd_dict); |
| 93 |
|
| 94 |
exit: |
| 95 |
return return_value; |
| 96 |
} |
| 97 |
|
| 98 |
PyDoc_STRVAR(_zstd_ZstdCompressor_compress__doc__, |
| 99 |
"compress($self, /, data, mode=ZstdCompressor.CONTINUE)\n" |
| 100 |
"--\n" |
| 101 |
"\n" |
| 102 |
"Provide data to the compressor object.\n" |
| 103 |
"\n" |
| 104 |
" mode\n" |
| 105 |
" Can be these 3 values ZstdCompressor.CONTINUE,\n" |
| 106 |
" ZstdCompressor.FLUSH_BLOCK, ZstdCompressor.FLUSH_FRAME\n" |
| 107 |
"\n" |
| 108 |
"Return a chunk of compressed data if possible, or b\'\' otherwise. When you have\n" |
| 109 |
"finished providing data to the compressor, call the flush() method to finish\n" |
| 110 |
"the compression process."); |
| 111 |
|
| 112 |
#define _ZSTD_ZSTDCOMPRESSOR_COMPRESS_METHODDEF \ |
| 113 |
{"compress", _PyCFunction_CAST(_zstd_ZstdCompressor_compress), METH_FASTCALL|METH_KEYWORDS, _zstd_ZstdCompressor_compress__doc__}, |
| 114 |
|
| 115 |
static PyObject * |
| 116 |
_zstd_ZstdCompressor_compress_impl(ZstdCompressor *self, Py_buffer *data, |
| 117 |
int mode); |
| 118 |
|
| 119 |
static PyObject * |
| 120 |
_zstd_ZstdCompressor_compress(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 121 |
{ |
| 122 |
PyObject *return_value = NULL; |
| 123 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 124 |
|
| 125 |
#define NUM_KEYWORDS 2 |
| 126 |
static struct { |
| 127 |
PyGC_Head _this_is_not_used; |
| 128 |
PyObject_VAR_HEAD |
| 129 |
Py_hash_t ob_hash; |
| 130 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 131 |
} _kwtuple = { |
| 132 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 133 |
.ob_hash = -1, |
| 134 |
.ob_item = { &_Py_ID(data), &_Py_ID(mode), }, |
| 135 |
}; |
| 136 |
#undef NUM_KEYWORDS |
| 137 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 138 |
|
| 139 |
#else // !Py_BUILD_CORE |
| 140 |
# define KWTUPLE NULL |
| 141 |
#endif // !Py_BUILD_CORE |
| 142 |
|
| 143 |
static const char * const _keywords[] = {"data", "mode", NULL}; |
| 144 |
static _PyArg_Parser _parser = { |
| 145 |
.keywords = _keywords, |
| 146 |
.fname = "compress", |
| 147 |
.kwtuple = KWTUPLE, |
| 148 |
}; |
| 149 |
#undef KWTUPLE |
| 150 |
PyObject *argsbuf[2]; |
| 151 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
| 152 |
Py_buffer data = {NULL, NULL}; |
| 153 |
int mode = ZSTD_e_continue; |
| 154 |
|
| 155 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 156 |
/*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 157 |
if (!args) { |
| 158 |
goto exit; |
| 159 |
} |
| 160 |
if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 161 |
goto exit; |
| 162 |
} |
| 163 |
if (!noptargs) { |
| 164 |
goto skip_optional_pos; |
| 165 |
} |
| 166 |
mode = PyLong_AsInt(args[1]); |
| 167 |
if (mode == -1 && PyErr_Occurred()) { |
| 168 |
goto exit; |
| 169 |
} |
| 170 |
skip_optional_pos: |
| 171 |
return_value = _zstd_ZstdCompressor_compress_impl((ZstdCompressor *)self, &data, mode); |
| 172 |
|
| 173 |
exit: |
| 174 |
/* Cleanup for data */ |
| 175 |
if (data.obj) { |
| 176 |
PyBuffer_Release(&data); |
| 177 |
} |
| 178 |
|
| 179 |
return return_value; |
| 180 |
} |
| 181 |
|
| 182 |
PyDoc_STRVAR(_zstd_ZstdCompressor_flush__doc__, |
| 183 |
"flush($self, /, mode=ZstdCompressor.FLUSH_FRAME)\n" |
| 184 |
"--\n" |
| 185 |
"\n" |
| 186 |
"Finish the compression process.\n" |
| 187 |
"\n" |
| 188 |
" mode\n" |
| 189 |
" Can be these 2 values ZstdCompressor.FLUSH_FRAME,\n" |
| 190 |
" ZstdCompressor.FLUSH_BLOCK\n" |
| 191 |
"\n" |
| 192 |
"Flush any remaining data left in internal buffers. Since Zstandard data\n" |
| 193 |
"consists of one or more independent frames, the compressor object can still\n" |
| 194 |
"be used after this method is called."); |
| 195 |
|
| 196 |
#define _ZSTD_ZSTDCOMPRESSOR_FLUSH_METHODDEF \ |
| 197 |
{"flush", _PyCFunction_CAST(_zstd_ZstdCompressor_flush), METH_FASTCALL|METH_KEYWORDS, _zstd_ZstdCompressor_flush__doc__}, |
| 198 |
|
| 199 |
static PyObject * |
| 200 |
_zstd_ZstdCompressor_flush_impl(ZstdCompressor *self, int mode); |
| 201 |
|
| 202 |
static PyObject * |
| 203 |
_zstd_ZstdCompressor_flush(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 204 |
{ |
| 205 |
PyObject *return_value = NULL; |
| 206 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 207 |
|
| 208 |
#define NUM_KEYWORDS 1 |
| 209 |
static struct { |
| 210 |
PyGC_Head _this_is_not_used; |
| 211 |
PyObject_VAR_HEAD |
| 212 |
Py_hash_t ob_hash; |
| 213 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 214 |
} _kwtuple = { |
| 215 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 216 |
.ob_hash = -1, |
| 217 |
.ob_item = { &_Py_ID(mode), }, |
| 218 |
}; |
| 219 |
#undef NUM_KEYWORDS |
| 220 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 221 |
|
| 222 |
#else // !Py_BUILD_CORE |
| 223 |
# define KWTUPLE NULL |
| 224 |
#endif // !Py_BUILD_CORE |
| 225 |
|
| 226 |
static const char * const _keywords[] = {"mode", NULL}; |
| 227 |
static _PyArg_Parser _parser = { |
| 228 |
.keywords = _keywords, |
| 229 |
.fname = "flush", |
| 230 |
.kwtuple = KWTUPLE, |
| 231 |
}; |
| 232 |
#undef KWTUPLE |
| 233 |
PyObject *argsbuf[1]; |
| 234 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
| 235 |
int mode = ZSTD_e_end; |
| 236 |
|
| 237 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 238 |
/*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 239 |
if (!args) { |
| 240 |
goto exit; |
| 241 |
} |
| 242 |
if (!noptargs) { |
| 243 |
goto skip_optional_pos; |
| 244 |
} |
| 245 |
mode = PyLong_AsInt(args[0]); |
| 246 |
if (mode == -1 && PyErr_Occurred()) { |
| 247 |
goto exit; |
| 248 |
} |
| 249 |
skip_optional_pos: |
| 250 |
return_value = _zstd_ZstdCompressor_flush_impl((ZstdCompressor *)self, mode); |
| 251 |
|
| 252 |
exit: |
| 253 |
return return_value; |
| 254 |
} |
| 255 |
|
| 256 |
PyDoc_STRVAR(_zstd_ZstdCompressor_set_pledged_input_size__doc__, |
| 257 |
"set_pledged_input_size($self, size, /)\n" |
| 258 |
"--\n" |
| 259 |
"\n" |
| 260 |
"Set the uncompressed content size to be written into the frame header.\n" |
| 261 |
"\n" |
| 262 |
" size\n" |
| 263 |
" The size of the uncompressed data to be provided to the compressor.\n" |
| 264 |
"\n" |
| 265 |
"This method can be used to ensure the header of the frame about to be written\n" |
| 266 |
"includes the size of the data, unless the CompressionParameter.content_size_flag\n" |
| 267 |
"is set to False. If last_mode != FLUSH_FRAME, then a RuntimeError is raised.\n" |
| 268 |
"\n" |
| 269 |
"It is important to ensure that the pledged data size matches the actual data\n" |
| 270 |
"size. If they do not match the compressed output data may be corrupted and the\n" |
| 271 |
"final chunk written may be lost."); |
| 272 |
|
| 273 |
#define _ZSTD_ZSTDCOMPRESSOR_SET_PLEDGED_INPUT_SIZE_METHODDEF \ |
| 274 |
{"set_pledged_input_size", (PyCFunction)_zstd_ZstdCompressor_set_pledged_input_size, METH_O, _zstd_ZstdCompressor_set_pledged_input_size__doc__}, |
| 275 |
|
| 276 |
static PyObject * |
| 277 |
_zstd_ZstdCompressor_set_pledged_input_size_impl(ZstdCompressor *self, |
| 278 |
unsigned long long size); |
| 279 |
|
| 280 |
static PyObject * |
| 281 |
_zstd_ZstdCompressor_set_pledged_input_size(PyObject *self, PyObject *arg) |
| 282 |
{ |
| 283 |
PyObject *return_value = NULL; |
| 284 |
unsigned long long size; |
| 285 |
|
| 286 |
if (!zstd_contentsize_converter(arg, &size)) { |
| 287 |
goto exit; |
| 288 |
} |
| 289 |
return_value = _zstd_ZstdCompressor_set_pledged_input_size_impl((ZstdCompressor *)self, size); |
| 290 |
|
| 291 |
exit: |
| 292 |
return return_value; |
| 293 |
} |
| 294 |
/*[clinic end generated code: output=c1d5c2cf06a8becd input=a9049054013a1b77]*/ |