gh-95534: Convert `ZlibDecompressor.__new__` to AC (#137923)

This commit is contained in:
Adam Turner 2025-08-19 09:52:13 +01:00 committed by GitHub
parent 06dd63501a
commit bb75dec87f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 118 additions and 46 deletions

View file

@ -898,7 +898,7 @@ exit:
return return_value; return return_value;
} }
PyDoc_STRVAR(zlib_ZlibDecompressor_decompress__doc__, PyDoc_STRVAR(zlib__ZlibDecompressor_decompress__doc__,
"decompress($self, /, data, max_length=-1)\n" "decompress($self, /, data, max_length=-1)\n"
"--\n" "--\n"
"\n" "\n"
@ -917,15 +917,16 @@ PyDoc_STRVAR(zlib_ZlibDecompressor_decompress__doc__,
"EOFError. Any data found after the end of the stream is ignored and saved in\n" "EOFError. Any data found after the end of the stream is ignored and saved in\n"
"the unused_data attribute."); "the unused_data attribute.");
#define ZLIB_ZLIBDECOMPRESSOR_DECOMPRESS_METHODDEF \ #define ZLIB__ZLIBDECOMPRESSOR_DECOMPRESS_METHODDEF \
{"decompress", _PyCFunction_CAST(zlib_ZlibDecompressor_decompress), METH_FASTCALL|METH_KEYWORDS, zlib_ZlibDecompressor_decompress__doc__}, {"decompress", _PyCFunction_CAST(zlib__ZlibDecompressor_decompress), METH_FASTCALL|METH_KEYWORDS, zlib__ZlibDecompressor_decompress__doc__},
static PyObject * static PyObject *
zlib_ZlibDecompressor_decompress_impl(ZlibDecompressor *self, zlib__ZlibDecompressor_decompress_impl(ZlibDecompressor *self,
Py_buffer *data, Py_ssize_t max_length); Py_buffer *data,
Py_ssize_t max_length);
static PyObject * static PyObject *
zlib_ZlibDecompressor_decompress(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) zlib__ZlibDecompressor_decompress(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
@ -984,7 +985,7 @@ zlib_ZlibDecompressor_decompress(PyObject *self, PyObject *const *args, Py_ssize
max_length = ival; max_length = ival;
} }
skip_optional_pos: skip_optional_pos:
return_value = zlib_ZlibDecompressor_decompress_impl((ZlibDecompressor *)self, &data, max_length); return_value = zlib__ZlibDecompressor_decompress_impl((ZlibDecompressor *)self, &data, max_length);
exit: exit:
/* Cleanup for data */ /* Cleanup for data */
@ -995,6 +996,86 @@ exit:
return return_value; return return_value;
} }
PyDoc_STRVAR(zlib__ZlibDecompressor__doc__,
"_ZlibDecompressor(wbits=MAX_WBITS, zdict=b\'\')\n"
"--\n"
"\n"
"Create a decompressor object for decompressing data incrementally.\n"
"\n"
" zdict\n"
" The predefined compression dictionary. This is a sequence of bytes\n"
" (such as a bytes object) containing subsequences that are expected\n"
" to occur frequently in the data that is to be compressed. Those\n"
" subsequences that are expected to be most common should come at the\n"
" end of the dictionary. This must be the same dictionary as used by the\n"
" compressor that produced the input data.");
static PyObject *
zlib__ZlibDecompressor_impl(PyTypeObject *type, int wbits, PyObject *zdict);
static PyObject *
zlib__ZlibDecompressor(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 2
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
Py_hash_t ob_hash;
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
.ob_item = { &_Py_ID(wbits), &_Py_ID(zdict), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"wbits", "zdict", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "_ZlibDecompressor",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[2];
PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
int wbits = MAX_WBITS;
PyObject *zdict = NULL;
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
/*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
if (!fastargs) {
goto exit;
}
if (!noptargs) {
goto skip_optional_pos;
}
if (fastargs[0]) {
wbits = PyLong_AsInt(fastargs[0]);
if (wbits == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
zdict = fastargs[1];
skip_optional_pos:
return_value = zlib__ZlibDecompressor_impl(type, wbits, zdict);
exit:
return return_value;
}
PyDoc_STRVAR(zlib_adler32__doc__, PyDoc_STRVAR(zlib_adler32__doc__,
"adler32($module, data, value=1, /)\n" "adler32($module, data, value=1, /)\n"
"--\n" "--\n"
@ -1311,4 +1392,4 @@ exit:
#ifndef ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF #ifndef ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
#define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF #define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
#endif /* !defined(ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF) */ #endif /* !defined(ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF) */
/*[clinic end generated code: output=3054c8894aa44568 input=a9049054013a1b77]*/ /*[clinic end generated code: output=59184b81fea41d3d input=a9049054013a1b77]*/

View file

@ -1369,9 +1369,9 @@ typedef struct {
} ZlibDecompressor; } ZlibDecompressor;
/*[clinic input] /*[clinic input]
class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType" class zlib._ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
[clinic start generated code]*/ [clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=0658178ab94645df]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=49151d1d703e6bcc]*/
static void static void
ZlibDecompressor_dealloc(PyObject *op) ZlibDecompressor_dealloc(PyObject *op)
@ -1670,7 +1670,7 @@ decompress(ZlibDecompressor *self, uint8_t *data,
/*[clinic input] /*[clinic input]
@permit_long_docstring_body @permit_long_docstring_body
zlib.ZlibDecompressor.decompress zlib._ZlibDecompressor.decompress
data: Py_buffer data: Py_buffer
max_length: Py_ssize_t=-1 max_length: Py_ssize_t=-1
@ -1692,9 +1692,10 @@ the unused_data attribute.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
zlib_ZlibDecompressor_decompress_impl(ZlibDecompressor *self, zlib__ZlibDecompressor_decompress_impl(ZlibDecompressor *self,
Py_buffer *data, Py_ssize_t max_length) Py_buffer *data,
/*[clinic end generated code: output=990d32787b775f85 input=fcf9f974de5d02b1]*/ Py_ssize_t max_length)
/*[clinic end generated code: output=ac00dcf73e843e99 input=c9278e791be1152b]*/
{ {
PyObject *result = NULL; PyObject *result = NULL;
@ -1710,38 +1711,28 @@ zlib_ZlibDecompressor_decompress_impl(ZlibDecompressor *self,
return result; return result;
} }
PyDoc_STRVAR(ZlibDecompressor__new____doc__, /*[clinic input]
"_ZlibDecompressor(wbits=15, zdict=b\'\')\n" @classmethod
"--\n" zlib._ZlibDecompressor.__new__
"\n"
"Create a decompressor object for decompressing data incrementally.\n" wbits: int(c_default='MAX_WBITS') = MAX_WBITS
"\n" zdict: object(c_default='NULL') = b''
" wbits = 15\n" The predefined compression dictionary. This is a sequence of bytes
" zdict\n" (such as a bytes object) containing subsequences that are expected
" The predefined compression dictionary. This is a sequence of bytes\n" to occur frequently in the data that is to be compressed. Those
" (such as a bytes object) containing subsequences that are expected\n" subsequences that are expected to be most common should come at the
" to occur frequently in the data that is to be compressed. Those\n" end of the dictionary. This must be the same dictionary as used by the
" subsequences that are expected to be most common should come at the\n" compressor that produced the input data.
" end of the dictionary. This must be the same dictionary as used by the\n"
" compressor that produced the input data.\n" Create a decompressor object for decompressing data incrementally.
"\n"); [clinic start generated code]*/
static PyObject * static PyObject *
ZlibDecompressor__new__(PyTypeObject *cls, zlib__ZlibDecompressor_impl(PyTypeObject *type, int wbits, PyObject *zdict)
PyObject *args, /*[clinic end generated code: output=1065607df0d33baa input=9ebad0be6de226e2]*/
PyObject *kwargs)
{ {
static char *keywords[] = {"wbits", "zdict", NULL}; zlibstate *state = PyType_GetModuleState(type);
static const char * const format = "|iO:_ZlibDecompressor"; ZlibDecompressor *self = PyObject_New(ZlibDecompressor, type);
int wbits = MAX_WBITS;
PyObject *zdict = NULL;
zlibstate *state = PyType_GetModuleState(cls);
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, format, keywords, &wbits, &zdict)) {
return NULL;
}
ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls);
if (self == NULL) { if (self == NULL) {
return NULL; return NULL;
} }
@ -1817,7 +1808,7 @@ static PyMethodDef Decomp_methods[] =
}; };
static PyMethodDef ZlibDecompressor_methods[] = { static PyMethodDef ZlibDecompressor_methods[] = {
ZLIB_ZLIBDECOMPRESSOR_DECOMPRESS_METHODDEF ZLIB__ZLIBDECOMPRESSOR_DECOMPRESS_METHODDEF
{NULL} {NULL}
}; };
@ -2052,8 +2043,8 @@ static PyType_Spec Decomptype_spec = {
static PyType_Slot ZlibDecompressor_type_slots[] = { static PyType_Slot ZlibDecompressor_type_slots[] = {
{Py_tp_dealloc, ZlibDecompressor_dealloc}, {Py_tp_dealloc, ZlibDecompressor_dealloc},
{Py_tp_members, ZlibDecompressor_members}, {Py_tp_members, ZlibDecompressor_members},
{Py_tp_new, ZlibDecompressor__new__}, {Py_tp_new, zlib__ZlibDecompressor},
{Py_tp_doc, (char *)ZlibDecompressor__new____doc__}, {Py_tp_doc, (char *)zlib__ZlibDecompressor__doc__},
{Py_tp_methods, ZlibDecompressor_methods}, {Py_tp_methods, ZlibDecompressor_methods},
{0, 0}, {0, 0},
}; };