| 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_BadArgument() |
| 11 |
|
| 12 |
PyDoc_STRVAR(_bz2_BZ2Compressor_compress__doc__, |
| 13 |
"compress($self, data, /)\n" |
| 14 |
"--\n" |
| 15 |
"\n" |
| 16 |
"Provide data to the compressor object.\n" |
| 17 |
"\n" |
| 18 |
"Returns a chunk of compressed data if possible, or b\'\' otherwise.\n" |
| 19 |
"\n" |
| 20 |
"When you have finished providing data to the compressor, call the\n" |
| 21 |
"flush() method to finish the compression process."); |
| 22 |
|
| 23 |
#define _BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF \ |
| 24 |
{"compress", (PyCFunction)_bz2_BZ2Compressor_compress, METH_O, _bz2_BZ2Compressor_compress__doc__}, |
| 25 |
|
| 26 |
static PyObject * |
| 27 |
_bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data); |
| 28 |
|
| 29 |
static PyObject * |
| 30 |
_bz2_BZ2Compressor_compress(PyObject *self, PyObject *arg) |
| 31 |
{ |
| 32 |
PyObject *return_value = NULL; |
| 33 |
Py_buffer data = {NULL, NULL}; |
| 34 |
|
| 35 |
if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) { |
| 36 |
goto exit; |
| 37 |
} |
| 38 |
return_value = _bz2_BZ2Compressor_compress_impl((BZ2Compressor *)self, &data); |
| 39 |
|
| 40 |
exit: |
| 41 |
/* Cleanup for data */ |
| 42 |
if (data.obj) { |
| 43 |
PyBuffer_Release(&data); |
| 44 |
} |
| 45 |
|
| 46 |
return return_value; |
| 47 |
} |
| 48 |
|
| 49 |
PyDoc_STRVAR(_bz2_BZ2Compressor_flush__doc__, |
| 50 |
"flush($self, /)\n" |
| 51 |
"--\n" |
| 52 |
"\n" |
| 53 |
"Finish the compression process.\n" |
| 54 |
"\n" |
| 55 |
"Returns the compressed data left in internal buffers.\n" |
| 56 |
"\n" |
| 57 |
"The compressor object may not be used after this method is called."); |
| 58 |
|
| 59 |
#define _BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF \ |
| 60 |
{"flush", (PyCFunction)_bz2_BZ2Compressor_flush, METH_NOARGS, _bz2_BZ2Compressor_flush__doc__}, |
| 61 |
|
| 62 |
static PyObject * |
| 63 |
_bz2_BZ2Compressor_flush_impl(BZ2Compressor *self); |
| 64 |
|
| 65 |
static PyObject * |
| 66 |
_bz2_BZ2Compressor_flush(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 67 |
{ |
| 68 |
return _bz2_BZ2Compressor_flush_impl((BZ2Compressor *)self); |
| 69 |
} |
| 70 |
|
| 71 |
PyDoc_STRVAR(_bz2_BZ2Compressor__doc__, |
| 72 |
"BZ2Compressor(compresslevel=9, /)\n" |
| 73 |
"--\n" |
| 74 |
"\n" |
| 75 |
"Create a compressor object for compressing data incrementally.\n" |
| 76 |
"\n" |
| 77 |
" compresslevel\n" |
| 78 |
" Compression level, as a number between 1 and 9.\n" |
| 79 |
"\n" |
| 80 |
"For one-shot compression, use the compress() function instead."); |
| 81 |
|
| 82 |
static PyObject * |
| 83 |
_bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel); |
| 84 |
|
| 85 |
static PyObject * |
| 86 |
_bz2_BZ2Compressor(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 87 |
{ |
| 88 |
PyObject *return_value = NULL; |
| 89 |
PyTypeObject *base_tp = clinic_state()->bz2_compressor_type; |
| 90 |
int compresslevel = 9; |
| 91 |
|
| 92 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 93 |
!_PyArg_NoKeywords("BZ2Compressor", kwargs)) { |
| 94 |
goto exit; |
| 95 |
} |
| 96 |
if (!_PyArg_CheckPositional("BZ2Compressor", PyTuple_GET_SIZE(args), 0, 1)) { |
| 97 |
goto exit; |
| 98 |
} |
| 99 |
if (PyTuple_GET_SIZE(args) < 1) { |
| 100 |
goto skip_optional; |
| 101 |
} |
| 102 |
compresslevel = PyLong_AsInt(PyTuple_GET_ITEM(args, 0)); |
| 103 |
if (compresslevel == -1 && PyErr_Occurred()) { |
| 104 |
goto exit; |
| 105 |
} |
| 106 |
skip_optional: |
| 107 |
return_value = _bz2_BZ2Compressor_impl(type, compresslevel); |
| 108 |
|
| 109 |
exit: |
| 110 |
return return_value; |
| 111 |
} |
| 112 |
|
| 113 |
PyDoc_STRVAR(_bz2_BZ2Decompressor_decompress__doc__, |
| 114 |
"decompress($self, /, data, max_length=-1)\n" |
| 115 |
"--\n" |
| 116 |
"\n" |
| 117 |
"Decompress *data*, returning uncompressed data as bytes.\n" |
| 118 |
"\n" |
| 119 |
"If *max_length* is nonnegative, returns at most *max_length* bytes\n" |
| 120 |
"of decompressed data. If this limit is reached and further output\n" |
| 121 |
"can be produced, *self.needs_input* will be set to ``False``. In\n" |
| 122 |
"this case, the next call to *decompress()* may provide *data* as b\'\'\n" |
| 123 |
"to obtain more of the output.\n" |
| 124 |
"\n" |
| 125 |
"If all of the input data was decompressed and returned (either\n" |
| 126 |
"because this was less than *max_length* bytes, or because\n" |
| 127 |
"*max_length* was negative), *self.needs_input* will be set to True.\n" |
| 128 |
"\n" |
| 129 |
"Attempting to decompress data after the end of stream is reached\n" |
| 130 |
"raises an EOFError. Any data found after the end of the stream is\n" |
| 131 |
"ignored and saved in the unused_data attribute."); |
| 132 |
|
| 133 |
#define _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF \ |
| 134 |
{"decompress", _PyCFunction_CAST(_bz2_BZ2Decompressor_decompress), METH_FASTCALL|METH_KEYWORDS, _bz2_BZ2Decompressor_decompress__doc__}, |
| 135 |
|
| 136 |
static PyObject * |
| 137 |
_bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data, |
| 138 |
Py_ssize_t max_length); |
| 139 |
|
| 140 |
static PyObject * |
| 141 |
_bz2_BZ2Decompressor_decompress(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 142 |
{ |
| 143 |
PyObject *return_value = NULL; |
| 144 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 145 |
|
| 146 |
#define NUM_KEYWORDS 2 |
| 147 |
static struct { |
| 148 |
PyGC_Head _this_is_not_used; |
| 149 |
PyObject_VAR_HEAD |
| 150 |
Py_hash_t ob_hash; |
| 151 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 152 |
} _kwtuple = { |
| 153 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 154 |
.ob_hash = -1, |
| 155 |
.ob_item = { &_Py_ID(data), &_Py_ID(max_length), }, |
| 156 |
}; |
| 157 |
#undef NUM_KEYWORDS |
| 158 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 159 |
|
| 160 |
#else // !Py_BUILD_CORE |
| 161 |
# define KWTUPLE NULL |
| 162 |
#endif // !Py_BUILD_CORE |
| 163 |
|
| 164 |
static const char * const _keywords[] = {"data", "max_length", NULL}; |
| 165 |
static _PyArg_Parser _parser = { |
| 166 |
.keywords = _keywords, |
| 167 |
.fname = "decompress", |
| 168 |
.kwtuple = KWTUPLE, |
| 169 |
}; |
| 170 |
#undef KWTUPLE |
| 171 |
PyObject *argsbuf[2]; |
| 172 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
| 173 |
Py_buffer data = {NULL, NULL}; |
| 174 |
Py_ssize_t max_length = -1; |
| 175 |
|
| 176 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 177 |
/*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 178 |
if (!args) { |
| 179 |
goto exit; |
| 180 |
} |
| 181 |
if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 182 |
goto exit; |
| 183 |
} |
| 184 |
if (!noptargs) { |
| 185 |
goto skip_optional_pos; |
| 186 |
} |
| 187 |
{ |
| 188 |
Py_ssize_t ival = -1; |
| 189 |
PyObject *iobj = _PyNumber_Index(args[1]); |
| 190 |
if (iobj != NULL) { |
| 191 |
ival = PyLong_AsSsize_t(iobj); |
| 192 |
Py_DECREF(iobj); |
| 193 |
} |
| 194 |
if (ival == -1 && PyErr_Occurred()) { |
| 195 |
goto exit; |
| 196 |
} |
| 197 |
max_length = ival; |
| 198 |
} |
| 199 |
skip_optional_pos: |
| 200 |
return_value = _bz2_BZ2Decompressor_decompress_impl((BZ2Decompressor *)self, &data, max_length); |
| 201 |
|
| 202 |
exit: |
| 203 |
/* Cleanup for data */ |
| 204 |
if (data.obj) { |
| 205 |
PyBuffer_Release(&data); |
| 206 |
} |
| 207 |
|
| 208 |
return return_value; |
| 209 |
} |
| 210 |
|
| 211 |
PyDoc_STRVAR(_bz2_BZ2Decompressor__doc__, |
| 212 |
"BZ2Decompressor()\n" |
| 213 |
"--\n" |
| 214 |
"\n" |
| 215 |
"Create a decompressor object for decompressing data incrementally.\n" |
| 216 |
"\n" |
| 217 |
"For one-shot decompression, use the decompress() function instead."); |
| 218 |
|
| 219 |
static PyObject * |
| 220 |
_bz2_BZ2Decompressor_impl(PyTypeObject *type); |
| 221 |
|
| 222 |
static PyObject * |
| 223 |
_bz2_BZ2Decompressor(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 224 |
{ |
| 225 |
PyObject *return_value = NULL; |
| 226 |
PyTypeObject *base_tp = clinic_state()->bz2_decompressor_type; |
| 227 |
|
| 228 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 229 |
!_PyArg_NoPositional("BZ2Decompressor", args)) { |
| 230 |
goto exit; |
| 231 |
} |
| 232 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 233 |
!_PyArg_NoKeywords("BZ2Decompressor", kwargs)) { |
| 234 |
goto exit; |
| 235 |
} |
| 236 |
return_value = _bz2_BZ2Decompressor_impl(type); |
| 237 |
|
| 238 |
exit: |
| 239 |
return return_value; |
| 240 |
} |
| 241 |
/*[clinic end generated code: output=1dce5396d592bad7 input=a9049054013a1b77]*/ |