Skip to content

Commit 727b00d

Browse files
glandiumindygreg
authored andcommitted
Use PyType_Spec to define types instead of PyTypeObject: ZstdBufferWithSegmentsCollection
1 parent e4d2b18 commit 727b00d

File tree

4 files changed

+28
-61
lines changed

4 files changed

+28
-61
lines changed

c-ext/bufferutil.c

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -502,64 +502,31 @@ BufferWithSegmentsCollection_item(ZstdBufferWithSegmentsCollection *self,
502502
return NULL;
503503
}
504504

505-
static PySequenceMethods BufferWithSegmentsCollection_sq = {
506-
(lenfunc)BufferWithSegmentsCollection_length, /* sq_length */
507-
0, /* sq_concat */
508-
0, /* sq_repeat */
509-
(ssizeargfunc)BufferWithSegmentsCollection_item, /* sq_item */
510-
0, /* sq_ass_item */
511-
0, /* sq_contains */
512-
0, /* sq_inplace_concat */
513-
0 /* sq_inplace_repeat */
514-
};
515-
516505
static PyMethodDef BufferWithSegmentsCollection_methods[] = {
517506
{"size", (PyCFunction)BufferWithSegmentsCollection_size, METH_NOARGS,
518507
PyDoc_STR("total size in bytes of all segments")},
519508
{NULL, NULL}};
520509

521-
PyTypeObject ZstdBufferWithSegmentsCollectionType = {
522-
PyVarObject_HEAD_INIT(NULL,
523-
0) "zstd.BufferWithSegmentsCollection", /* tp_name */
524-
sizeof(ZstdBufferWithSegmentsCollection), /* tp_basicsize */
525-
0, /* tp_itemsize */
526-
(destructor)BufferWithSegmentsCollection_dealloc, /* tp_dealloc */
527-
0, /* tp_print */
528-
0, /* tp_getattr */
529-
0, /* tp_setattr */
530-
0, /* tp_compare */
531-
0, /* tp_repr */
532-
0, /* tp_as_number */
533-
&BufferWithSegmentsCollection_sq, /* tp_as_sequence */
534-
0, /* tp_as_mapping */
535-
0, /* tp_hash */
536-
0, /* tp_call */
537-
0, /* tp_str */
538-
0, /* tp_getattro */
539-
0, /* tp_setattro */
540-
0, /* tp_as_buffer */
541-
Py_TPFLAGS_DEFAULT, /* tp_flags */
542-
0, /* tp_doc */
543-
0, /* tp_traverse */
544-
0, /* tp_clear */
545-
0, /* tp_richcompare */
546-
0, /* tp_weaklistoffset */
547-
/* TODO implement iterator for performance. */
548-
0, /* tp_iter */
549-
0, /* tp_iternext */
550-
BufferWithSegmentsCollection_methods, /* tp_methods */
551-
0, /* tp_members */
552-
0, /* tp_getset */
553-
0, /* tp_base */
554-
0, /* tp_dict */
555-
0, /* tp_descr_get */
556-
0, /* tp_descr_set */
557-
0, /* tp_dictoffset */
558-
(initproc)BufferWithSegmentsCollection_init, /* tp_init */
559-
0, /* tp_alloc */
560-
PyType_GenericNew, /* tp_new */
510+
PyType_Slot ZstdBufferWithSegmentsCollectionSlots[] = {
511+
{Py_tp_dealloc, BufferWithSegmentsCollection_dealloc},
512+
{Py_sq_length, BufferWithSegmentsCollection_length},
513+
{Py_sq_item, BufferWithSegmentsCollection_item},
514+
{Py_tp_methods, BufferWithSegmentsCollection_methods},
515+
{Py_tp_init, BufferWithSegmentsCollection_init},
516+
{Py_tp_new, PyType_GenericNew},
517+
{0, NULL},
561518
};
562519

520+
PyType_Spec ZstdBufferWithSegmentsCollectionSpec = {
521+
"zstd.BufferWithSegmentsCollection",
522+
sizeof(ZstdBufferWithSegmentsCollection),
523+
0,
524+
Py_TPFLAGS_DEFAULT,
525+
ZstdBufferWithSegmentsCollectionSlots,
526+
};
527+
528+
PyTypeObject *ZstdBufferWithSegmentsCollectionType;
529+
563530
void bufferutil_module_init(PyObject *mod) {
564531
ZstdBufferWithSegmentsType =
565532
(PyTypeObject *)PyType_FromSpec(&ZstdBufferWithSegmentsSpec);
@@ -599,12 +566,13 @@ void bufferutil_module_init(PyObject *mod) {
599566
Py_INCREF(ZstdBufferSegmentType);
600567
PyModule_AddObject(mod, "BufferSegment", (PyObject *)ZstdBufferSegmentType);
601568

602-
Py_SET_TYPE(&ZstdBufferWithSegmentsCollectionType, &PyType_Type);
603-
if (PyType_Ready(&ZstdBufferWithSegmentsCollectionType) < 0) {
569+
ZstdBufferWithSegmentsCollectionType =
570+
(PyTypeObject *)PyType_FromSpec(&ZstdBufferWithSegmentsCollectionSpec);
571+
if (PyType_Ready(ZstdBufferWithSegmentsCollectionType) < 0) {
604572
return;
605573
}
606574

607-
Py_INCREF(&ZstdBufferWithSegmentsCollectionType);
575+
Py_INCREF(ZstdBufferWithSegmentsCollectionType);
608576
PyModule_AddObject(mod, "BufferWithSegmentsCollection",
609-
(PyObject *)&ZstdBufferWithSegmentsCollectionType);
577+
(PyObject *)ZstdBufferWithSegmentsCollectionType);
610578
}

c-ext/compressor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ compress_from_datasources(ZstdCompressor *compressor, DataSources *sources,
12981298
}
12991299

13001300
result = (ZstdBufferWithSegmentsCollection *)PyObject_CallObject(
1301-
(PyObject *)&ZstdBufferWithSegmentsCollectionType, segmentsArg);
1301+
(PyObject *)ZstdBufferWithSegmentsCollectionType, segmentsArg);
13021302

13031303
finally:
13041304
Py_CLEAR(segmentsArg);
@@ -1392,7 +1392,7 @@ ZstdCompressor_multi_compress_to_buffer(ZstdCompressor *self, PyObject *args,
13921392

13931393
sources.sourcesSize = buffer->segmentCount;
13941394
}
1395-
else if (PyObject_TypeCheck(data, &ZstdBufferWithSegmentsCollectionType)) {
1395+
else if (PyObject_TypeCheck(data, ZstdBufferWithSegmentsCollectionType)) {
13961396
Py_ssize_t j;
13971397
Py_ssize_t offset = 0;
13981398
ZstdBufferWithSegments *buffer;

c-ext/decompressor.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ decompress_from_framesources(ZstdDecompressor *decompressor,
14131413
}
14141414

14151415
result = (ZstdBufferWithSegmentsCollection *)PyObject_CallObject(
1416-
(PyObject *)&ZstdBufferWithSegmentsCollectionType, resultArg);
1416+
(PyObject *)ZstdBufferWithSegmentsCollectionType, resultArg);
14171417

14181418
finally:
14191419
Py_CLEAR(resultArg);
@@ -1549,8 +1549,7 @@ Decompressor_multi_decompress_to_buffer(ZstdDecompressor *self, PyObject *args,
15491549
framePointers[i].destSize = (size_t)decompressedSize;
15501550
}
15511551
}
1552-
else if (PyObject_TypeCheck(frames,
1553-
&ZstdBufferWithSegmentsCollectionType)) {
1552+
else if (PyObject_TypeCheck(frames, ZstdBufferWithSegmentsCollectionType)) {
15541553
Py_ssize_t offset = 0;
15551554
ZstdBufferWithSegments *buffer;
15561555
ZstdBufferWithSegmentsCollection *collection =

c-ext/python-zstandard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ typedef struct {
366366
Py_ssize_t *firstElements;
367367
} ZstdBufferWithSegmentsCollection;
368368

369-
extern PyTypeObject ZstdBufferWithSegmentsCollectionType;
369+
extern PyTypeObject *ZstdBufferWithSegmentsCollectionType;
370370

371371
int set_parameter(ZSTD_CCtx_params *params, ZSTD_cParameter param, int value);
372372
int set_parameters(ZSTD_CCtx_params *params,

0 commit comments

Comments
 (0)