Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge _zstd_ZstdCompressor___init__ into _zstd_ZstdCompressor_new
  • Loading branch information
AA-Turner committed May 11, 2025
commit 98ee61ae4f59af9ea526305d81f56b7d2957de6f
43 changes: 10 additions & 33 deletions Modules/_zstd/clinic/compressor.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 46 additions & 67 deletions Modules/_zstd/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ typedef struct {

/* Compression level */
int compression_level;

/* __init__ has been called, 0 or 1. */
bool initialized;
} ZstdCompressor;

#define ZstdCompressor_CAST(op) ((ZstdCompressor *)op)
Expand Down Expand Up @@ -321,18 +318,30 @@ _zstd_load_c_dict(ZstdCompressor *self, PyObject *dict)
/*[clinic input]
@classmethod
_zstd.ZstdCompressor.__new__ as _zstd_ZstdCompressor_new

level: object = None
The compression level to use, defaults to ZSTD_CLEVEL_DEFAULT.
options: object = None
A dict object that contains advanced compression parameters.
zstd_dict: object = None
A ZstdDict object, a pre-trained zstd dictionary.

Create a compressor object for compressing data incrementally.

Thread-safe at method level. For one-shot compression, use the compress()
function instead.
[clinic start generated code]*/

static PyObject *
_zstd_ZstdCompressor_new_impl(PyTypeObject *type)
/*[clinic end generated code: output=d1f5f14b617789e4 input=9a6b2592cd79b822]*/
_zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
PyObject *options, PyObject *zstd_dict)
/*[clinic end generated code: output=cdef61eafecac3d7 input=a9e9d73f246d6588]*/
{
ZstdCompressor* self = PyObject_GC_New(ZstdCompressor, type);
if (self == NULL) {
goto error;
}

self->initialized = 0;
self->dict = NULL;
self->use_multithread = 0;

Expand All @@ -351,82 +360,28 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type)
/* Last mode */
self->last_mode = ZSTD_e_end;

return (PyObject*)self;

error:
if (self != NULL) {
PyObject_GC_Del(self);
}
return NULL;
}

static void
ZstdCompressor_dealloc(PyObject *ob)
{
ZstdCompressor *self = ZstdCompressor_CAST(ob);

PyObject_GC_UnTrack(self);

/* Free compression context */
ZSTD_freeCCtx(self->cctx);

/* Py_XDECREF the dict after free the compression context */
Py_CLEAR(self->dict);

PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_Del(ob);
Py_DECREF(tp);
}

/*[clinic input]
_zstd.ZstdCompressor.__init__

level: object = None
The compression level to use, defaults to ZSTD_CLEVEL_DEFAULT.
options: object = None
A dict object that contains advanced compression parameters.
zstd_dict: object = None
A ZstdDict object, a pre-trained zstd dictionary.

Create a compressor object for compressing data incrementally.

Thread-safe at method level. For one-shot compression, use the compress()
function instead.
[clinic start generated code]*/

static int
_zstd_ZstdCompressor___init___impl(ZstdCompressor *self, PyObject *level,
PyObject *options, PyObject *zstd_dict)
/*[clinic end generated code: output=215e6c4342732f96 input=9f79b0d8d34c8ef0]*/
{
if (self->initialized) {
PyErr_SetString(PyExc_RuntimeError, "reinitialization not supported");
return -1;
}
self->initialized = 1;

if (level != Py_None && options != Py_None) {
PyErr_SetString(PyExc_RuntimeError, "Only one of level or options should be used.");
return -1;
goto error;
}

/* Set compressLevel/options to compression context */
if (level != Py_None) {
if (_zstd_set_c_parameters(self, level, "level", "int") < 0) {
return -1;
goto error;
}
}

if (options != Py_None) {
if (_zstd_set_c_parameters(self, options, "options", "dict") < 0) {
return -1;
goto error;
}
}

/* Load dictionary to compression context */
if (zstd_dict != Py_None) {
if (_zstd_load_c_dict(self, zstd_dict) < 0) {
return -1;
goto error;
}

/* Py_INCREF the dict */
Expand All @@ -436,7 +391,32 @@ _zstd_ZstdCompressor___init___impl(ZstdCompressor *self, PyObject *level,

// We can only start tracking self with the GC once self->dict is set.
PyObject_GC_Track(self);
return 0;

return (PyObject*)self;

error:
if (self != NULL) {
PyObject_GC_Del(self);
}
return NULL;
}

static void
ZstdCompressor_dealloc(PyObject *ob)
{
ZstdCompressor *self = ZstdCompressor_CAST(ob);

PyObject_GC_UnTrack(self);

/* Free compression context */
ZSTD_freeCCtx(self->cctx);

/* Py_XDECREF the dict after free the compression context */
Py_CLEAR(self->dict);

PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_Del(ob);
Py_DECREF(tp);
}

static PyObject *
Expand Down Expand Up @@ -722,10 +702,9 @@ ZstdCompressor_clear(PyObject *ob)
static PyType_Slot zstdcompressor_slots[] = {
{Py_tp_new, _zstd_ZstdCompressor_new},
{Py_tp_dealloc, ZstdCompressor_dealloc},
{Py_tp_init, _zstd_ZstdCompressor___init__},
{Py_tp_methods, ZstdCompressor_methods},
{Py_tp_members, ZstdCompressor_members},
{Py_tp_doc, (char*)_zstd_ZstdCompressor___init____doc__},
{Py_tp_doc, (void *)_zstd_ZstdCompressor_new__doc__},
{Py_tp_traverse, ZstdCompressor_traverse},
{Py_tp_clear, ZstdCompressor_clear},
{0, 0}
Expand Down