Skip to content

Commit

Permalink
Use PyType_Spec to define types instead of PyTypeObject: ZstdBufferWi…
Browse files Browse the repository at this point in the history
…thSegmentsCollection
  • Loading branch information
glandium authored and indygreg committed Feb 21, 2023
1 parent e4d2b18 commit 727b00d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 61 deletions.
78 changes: 23 additions & 55 deletions c-ext/bufferutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,64 +502,31 @@ BufferWithSegmentsCollection_item(ZstdBufferWithSegmentsCollection *self,
return NULL;
}

static PySequenceMethods BufferWithSegmentsCollection_sq = {
(lenfunc)BufferWithSegmentsCollection_length, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
(ssizeargfunc)BufferWithSegmentsCollection_item, /* sq_item */
0, /* sq_ass_item */
0, /* sq_contains */
0, /* sq_inplace_concat */
0 /* sq_inplace_repeat */
};

static PyMethodDef BufferWithSegmentsCollection_methods[] = {
{"size", (PyCFunction)BufferWithSegmentsCollection_size, METH_NOARGS,
PyDoc_STR("total size in bytes of all segments")},
{NULL, NULL}};

PyTypeObject ZstdBufferWithSegmentsCollectionType = {
PyVarObject_HEAD_INIT(NULL,
0) "zstd.BufferWithSegmentsCollection", /* tp_name */
sizeof(ZstdBufferWithSegmentsCollection), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)BufferWithSegmentsCollection_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
&BufferWithSegmentsCollection_sq, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
/* TODO implement iterator for performance. */
0, /* tp_iter */
0, /* tp_iternext */
BufferWithSegmentsCollection_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)BufferWithSegmentsCollection_init, /* tp_init */
0, /* tp_alloc */
PyType_GenericNew, /* tp_new */
PyType_Slot ZstdBufferWithSegmentsCollectionSlots[] = {
{Py_tp_dealloc, BufferWithSegmentsCollection_dealloc},
{Py_sq_length, BufferWithSegmentsCollection_length},
{Py_sq_item, BufferWithSegmentsCollection_item},
{Py_tp_methods, BufferWithSegmentsCollection_methods},
{Py_tp_init, BufferWithSegmentsCollection_init},
{Py_tp_new, PyType_GenericNew},
{0, NULL},
};

PyType_Spec ZstdBufferWithSegmentsCollectionSpec = {
"zstd.BufferWithSegmentsCollection",
sizeof(ZstdBufferWithSegmentsCollection),
0,
Py_TPFLAGS_DEFAULT,
ZstdBufferWithSegmentsCollectionSlots,
};

PyTypeObject *ZstdBufferWithSegmentsCollectionType;

void bufferutil_module_init(PyObject *mod) {
ZstdBufferWithSegmentsType =
(PyTypeObject *)PyType_FromSpec(&ZstdBufferWithSegmentsSpec);
Expand Down Expand Up @@ -599,12 +566,13 @@ void bufferutil_module_init(PyObject *mod) {
Py_INCREF(ZstdBufferSegmentType);
PyModule_AddObject(mod, "BufferSegment", (PyObject *)ZstdBufferSegmentType);

Py_SET_TYPE(&ZstdBufferWithSegmentsCollectionType, &PyType_Type);
if (PyType_Ready(&ZstdBufferWithSegmentsCollectionType) < 0) {
ZstdBufferWithSegmentsCollectionType =
(PyTypeObject *)PyType_FromSpec(&ZstdBufferWithSegmentsCollectionSpec);
if (PyType_Ready(ZstdBufferWithSegmentsCollectionType) < 0) {
return;
}

Py_INCREF(&ZstdBufferWithSegmentsCollectionType);
Py_INCREF(ZstdBufferWithSegmentsCollectionType);
PyModule_AddObject(mod, "BufferWithSegmentsCollection",
(PyObject *)&ZstdBufferWithSegmentsCollectionType);
(PyObject *)ZstdBufferWithSegmentsCollectionType);
}
4 changes: 2 additions & 2 deletions c-ext/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ compress_from_datasources(ZstdCompressor *compressor, DataSources *sources,
}

result = (ZstdBufferWithSegmentsCollection *)PyObject_CallObject(
(PyObject *)&ZstdBufferWithSegmentsCollectionType, segmentsArg);
(PyObject *)ZstdBufferWithSegmentsCollectionType, segmentsArg);

finally:
Py_CLEAR(segmentsArg);
Expand Down Expand Up @@ -1392,7 +1392,7 @@ ZstdCompressor_multi_compress_to_buffer(ZstdCompressor *self, PyObject *args,

sources.sourcesSize = buffer->segmentCount;
}
else if (PyObject_TypeCheck(data, &ZstdBufferWithSegmentsCollectionType)) {
else if (PyObject_TypeCheck(data, ZstdBufferWithSegmentsCollectionType)) {
Py_ssize_t j;
Py_ssize_t offset = 0;
ZstdBufferWithSegments *buffer;
Expand Down
5 changes: 2 additions & 3 deletions c-ext/decompressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ decompress_from_framesources(ZstdDecompressor *decompressor,
}

result = (ZstdBufferWithSegmentsCollection *)PyObject_CallObject(
(PyObject *)&ZstdBufferWithSegmentsCollectionType, resultArg);
(PyObject *)ZstdBufferWithSegmentsCollectionType, resultArg);

finally:
Py_CLEAR(resultArg);
Expand Down Expand Up @@ -1549,8 +1549,7 @@ Decompressor_multi_decompress_to_buffer(ZstdDecompressor *self, PyObject *args,
framePointers[i].destSize = (size_t)decompressedSize;
}
}
else if (PyObject_TypeCheck(frames,
&ZstdBufferWithSegmentsCollectionType)) {
else if (PyObject_TypeCheck(frames, ZstdBufferWithSegmentsCollectionType)) {
Py_ssize_t offset = 0;
ZstdBufferWithSegments *buffer;
ZstdBufferWithSegmentsCollection *collection =
Expand Down
2 changes: 1 addition & 1 deletion c-ext/python-zstandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ typedef struct {
Py_ssize_t *firstElements;
} ZstdBufferWithSegmentsCollection;

extern PyTypeObject ZstdBufferWithSegmentsCollectionType;
extern PyTypeObject *ZstdBufferWithSegmentsCollectionType;

int set_parameter(ZSTD_CCtx_params *params, ZSTD_cParameter param, int value);
int set_parameters(ZSTD_CCtx_params *params,
Expand Down

0 comments on commit 727b00d

Please sign in to comment.