8000 Reorder code · python/cpython@6a5cdfd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a5cdfd

Browse files
committed
Reorder code
1 parent 1e13a89 commit 6a5cdfd

File tree

1 file changed

+79
-69
lines changed

1 file changed

+79
-69
lines changed

Modules/zlibmodule.c

Lines changed: 79 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,36 +1351,6 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
13511351
return RetVal;
13521352
}
13531353

1354-
#include "clinic/zlibmodule.c.h"
1355-
1356-
static PyMethodDef comp_methods[] =
1357< 8000 /td>-
{
1358-
ZLIB_COMPRESS_COMPRESS_METHODDEF
1359-
ZLIB_COMPRESS_FLUSH_METHODDEF
1360-
ZLIB_COMPRESS_COPY_METHODDEF
1361-
ZLIB_COMPRESS___COPY___METHODDEF
1362-
ZLIB_COMPRESS___DEEPCOPY___METHODDEF
1363-
{NULL, NULL}
1364-
};
1365-
1366-
static PyMethodDef Decomp_methods[] =
1367-
{
1368-
ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
1369-
ZLIB_DECOMPRESS_FLUSH_METHODDEF
1370-
ZLIB_DECOMPRESS_COPY_METHODDEF
1371-
ZLIB_DECOMPRESS___COPY___METHODDEF
1372-
ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
1373-
{NULL, NULL}
1374-
};
1375-
1376-
#define COMP_OFF(x) offsetof(compobject, x)
1377-
static PyMemberDef Decomp_members[] = {
1378-
{"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1379-
{"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
1380-
{"eof", T_BOOL, COMP_OFF(eof), READONLY},
1381-
{NULL},
1382-
};
1383-
13841354

13851355
typedef struct {
13861356
PyObject_HEAD
@@ -1484,6 +1454,7 @@ decompress_buf(ZlibDecompressor *self, Py_ssize_t max_length)
14841454
PyObject *RetVal = NULL;
14851455
Py_ssize_t hard_limit;
14861456
Py_ssize_t obuflen;
1457+
zlibstate *state = PyType_GetModuleState(Py_TYPE(self));
14871458

14881459
int err;
14891460

@@ -1680,77 +1651,116 @@ decompress(ZlibDecompressor *self, PyTypeObject *cls, uint8_t *data,
16801651
return NULL;
16811652
}
16821653

1683-
PyDoc_STRVAR(igzip_lib_IgzipDecompressor___init____doc__,
1684-
"IgzipDecompressor(flag=0, hist_bits=15, zdict=b\'\')\n"
1654+
PyDoc_STRVAR(ZlibDecompressor__new____doc__,
1655+
"_ZlibDecompressor(wbits=15, zdict=b\'\')\n"
16851656
"--\n"
16861657
"\n"
16871658
"Create a decompressor object for decompressing data incrementally.\n"
16881659
"\n"
1689-
" wbits\n"
1690-
" The lookback distance is 2 ^ hist_bits.\n"
1660+
" wbits = 15\n"
16911661
" zdict\n"
1692-
" Dictionary used for decompressing the data\n"
1693-
"\n"
1694-
"For one-shot decompression, use the decompress() function instead.");
1662+
" The predefined compression dictionary. This must be the same\n"
1663+
" dictionary as used by the compressor that produced the input data.\n"
1664+
"\n");
16951665

16961666
static PyObject *
1697-
igzip_lib_IgzipDecompressor__new__(PyTypeObject *type,
1698-
PyObject *args,
1699-
PyObject *kwargs)
1667+
ZlibDecompressor__new__(PyTypeObject *cls,
1668+
PyObject *args,
1669+
PyObject *kwargs)
17001670
{
1701-
static char *keywords[] = {"flag", "hist_bits", "zdict", NULL};
1702-
static char *format = "|iiO:IgzipDecompressor";
1703-
int flag = ISAL_DEFLATE;
1704-
int hist_bits = ISAL_DEF_MAX_HIST_BITS;
1671+
static char *keywords[] = {"wbits", "zdict", NULL};
1672+
static char *format = "|iO:IgzipDecompressor";
1673+
int wbits = MAX_WBITS;
17051674
PyObject *zdict = NULL;
1675+
zlibstate *state = PyType_GetModuleState(cls);
17061676

17071677
if (!PyArg_ParseTupleAndKeywords(
1708-
args, kwargs, format, keywords, &flag, &hist_bits, &zdict)) {
1678+
args, kwargs, format, keywords, &wbits, &zdict)) {
17091679
return NULL;
17101680
}
1711-
IgzipDecompressor *self = PyObject_New(IgzipDecompressor, type);
1681+
ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls);
17121682
int err;
17131683
self->eof = 0;
17141684
self->needs_input = 1;
17151685
self->avail_in_real = 0;
17161686
self->input_buffer = NULL;
17171687
self->input_buffer_size = 0;
17181688
self->zdict = zdict;
1689+
self->zst.opaque = NULL;
1690+
self->zst.zalloc = PyZlib_Malloc;
1691+
self->zst.zfree = PyZlib_Free;
17191692
self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
17201693
if (self->unused_data == NULL) {
17211694
Py_CLEAR(self);
17221695
return NULL;
17231696
}
1724-
isal_inflate_init(&(self->state));
1725-
self->state.hist_bits = hist_bits;
1726-
self->state.crc_flag = flag;
1727-
if (self->zdict != NULL){
1728-
Py_buffer zdict_buf;
1729-
if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
1730-
Py_CLEAR(self);
1697+
int err = inflateInit2(&(self->zst), wbits);
1698+
switch (err) {
1699+
case Z_OK:
1700+
self->is_initialised = 1;
1701+
if (self->zdict != NULL && wbits < 0) {
1702+
#ifdef AT_LEAST_ZLIB_1_2_2_1
1703+
if (set_inflate_zdict(state, self) < 0) {
1704+
Py_DECREF(self);
17311705
return NULL;
1732-
}
1733-
if ((size_t)zdict_buf.len > UINT32_MAX) {
1734-
PyErr_SetString(PyExc_OverflowError,
1735-
"zdict length does not fit in an unsigned 32-bits int");
1736-
PyBuffer_Release(&zdict_buf);
1737-
Py_CLEAR(self);
1706+
}
1707+
#else
1708+
PyErr_Format(state->ZlibError,
1709+
"zlib version %s does not allow raw inflate with dictionary",
1710+
ZLIB_VERSION);
1711+
Py_DECREF(self);
17381712
return NULL;
1713+
#endif
17391714
}
1740-
err = isal_inflate_set_dict(&(self->state), zdict_buf.buf,
1741-
(uint32_t)zdict_buf.len);
1742-
PyBuffer_Release(&zdict_buf);
1743-
if (err != ISAL_DECOMP_OK) {
1744-
isal_inflate_error(err);
1745-
Py_CLEAR(self);
1746-
return NULL;
1747-
}
1715+
return (PyObject *)self;
1716+
case Z_STREAM_ERROR:
1717+
Py_DECREF(self);
1718+
PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
1719+
return NULL;
1720+
case Z_MEM_ERROR:
1721+
Py_DECREF(self);
1722+
PyErr_SetString(PyExc_MemoryError,
1723+
"Can't allocate memory for decompression object");
1724+
return NULL;
1725+
default:
1726+
zlib_error(state, self->zst, err, "while creating decompression object");
1727+
Py_DECREF(self);
1728+
return NULL;
17481729
}
1749-
return (PyObject *)self;
17501730
}
17511731

1732+
#include "clinic/zlibmodule.c.h"
1733+
1734+
static PyMethodDef comp_methods[] =
1735+
{
1736+
ZLIB_COMPRESS_COMPRESS_METHODDEF
1737+
ZLIB_COMPRESS_FLUSH_METHODDEF
1738+
ZLIB_COMPRESS_COPY_METHODDEF
1739+
ZLIB_COMPRESS___COPY___METHODDEF
1740+
ZLIB_COMPRESS___DEEPCOPY___METHODDEF
1741+
{NULL, NULL}
1742+
};
1743+
1744+
static PyMethodDef Decomp_methods[] =
1745+
{
1746+
ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
1747+
ZLIB_DECOMPRESS_FLUSH_METHODDEF
1748+
ZLIB_DECOMPRESS_COPY_METHODDEF
1749+
ZLIB_DECOMPRESS___COPY___METHODDEF
1750+
ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
1751+
{NULL, NULL}
1752+
};
1753+
1754+
#define COMP_OFF(x) offsetof(compobject, x)
1755+
static PyMemberDef Decomp_members[] = {
1756+
{"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1757+
{"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
1758+
{"eof", T_BOOL, COMP_OFF(eof), READONLY},
1759+
{NULL},
1760+
};
1761+
17521762
static PyMethodDef IgzipDecompressor_methods[] = {
1753-
IGZIP_LIB_IGZIPDECOMPRESSOR_DECOMPRESS_METHODDEF,
1763+
ZLIBCOMPRESSOR_DECOMPRESS_METHODDEF,
17541764
{NULL}
17551765
};
17561766

0 commit comments

Comments
 (0)
0