Debian Codegraph @

Modules/_zstd/compressor.c

raw symbols SCIP
1 /* Low level interface to the Zstandard algorithm & the zstd library. */
2
3 /* ZstdCompressor class definitions */
4
5 /*[clinic input]
6 module _zstd
7 class _zstd.ZstdCompressor "ZstdCompressor *" "&zstd_compressor_type_spec"
8 [clinic start generated code]*/
9 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7166021db1ef7df8]*/
10
11 #ifndef Py_BUILD_CORE_BUILTIN
12 # define Py_BUILD_CORE_MODULE 1
13 #endif
14
15 #include "Python.h"
16
17 #include "_zstdmodule.h"
18 #include "buffer.h"
19 #include "internal/pycore_lock.h" // PyMutex_IsLocked
20
21 #include <stddef.h> // offsetof()
22 #include <zstd.h> // ZSTD_*()
23
24 typedef struct {
25 PyObject_HEAD
26
27 /* Compression context */
28 ZSTD_CCtx *cctx;
29
30 /* ZstdDict object in use */
31 PyObject *dict;
32
33 /* Last mode, initialized to ZSTD_e_end */
34 int last_mode;
35
36 /* (nbWorker >= 1) ? 1 : 0 */
37 int use_multithread;
38
39 /* Compression level */
40 int compression_level;
41
42 /* Lock to protect the compression context */
43 PyMutex lock;
44 } ZstdCompressor;
45
46 #define ZstdCompressor_CAST(op) ((ZstdCompressor *)op)
47
48 /*[python input]
49
50 class zstd_contentsize_converter(CConverter):
51 type = 'unsigned long long'
52 converter = 'zstd_contentsize_converter'
53
54 [python start generated code]*/
55 /*[python end generated code: output=da39a3ee5e6b4b0d input=0932c350d633c7de]*/
56
57
58 static int
59 zstd_contentsize_converter(PyObject *size, unsigned long long *p)
60 {
61 // None means the user indicates the size is unknown.
62 if (size == Py_None) {
63 *p = ZSTD_CONTENTSIZE_UNKNOWN;
64 }
65 else {
66 /* ZSTD_CONTENTSIZE_UNKNOWN is 0ULL - 1
67 ZSTD_CONTENTSIZE_ERROR is 0ULL - 2
68 Users should only pass values < ZSTD_CONTENTSIZE_ERROR */
69 unsigned long long pledged_size = PyLong_AsUnsignedLongLong(size);
70 /* Here we check for (unsigned long long)-1 as a sign of an error in
71 PyLong_AsUnsignedLongLong */
72 if (pledged_size == (unsigned long long)-1 && PyErr_Occurred()) {
73 *p = ZSTD_CONTENTSIZE_ERROR;
74 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
75 PyErr_Format(PyExc_ValueError,
76 "size argument should be a positive int less "
77 "than %llu", ZSTD_CONTENTSIZE_ERROR);
78 return 0;
79 }
80 return 0;
81 }
82 if (pledged_size >= ZSTD_CONTENTSIZE_ERROR) {
83 *p = ZSTD_CONTENTSIZE_ERROR;
84 PyErr_Format(PyExc_ValueError,
85 "size argument should be a positive int less "
86 "than %llu", ZSTD_CONTENTSIZE_ERROR);
87 return 0;
88 }
89 *p = pledged_size;
90 }
91 return 1;
92 }
93
94 #include "clinic/compressor.c.h"
95
96 static int
97 _zstd_set_c_level(ZstdCompressor *self, int level)
98 {
99 /* Set integer compression level */
100 int min_level = ZSTD_minCLevel();
101 int max_level = ZSTD_maxCLevel();
102 if (level < min_level || level > max_level) {
103 PyErr_Format(PyExc_ValueError,
104 "illegal compression level %d; the valid range is [%d, %d]",
105 level, min_level, max_level);
106 return -1;
107 }
108
109 /* Save for generating ZSTD_CDICT */
110 self->compression_level = level;
111
112 /* Set compressionLevel to compression context */
113 size_t zstd_ret = ZSTD_CCtx_setParameter(
114 self->cctx, ZSTD_c_compressionLevel, level);
115
116 /* Check error */
117 if (ZSTD_isError(zstd_ret)) {
118 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
119 set_zstd_error(mod_state, ERR_SET_C_LEVEL, zstd_ret);
120 return -1;
121 }
122 return 0;
123 }
124
125 static int
126 _zstd_set_c_parameters(ZstdCompressor *self, PyObject *options)
127 {
128 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
129 if (mod_state == NULL) {
130 return -1;
131 }
132
133 if (!PyDict_Check(options)) {
134 PyErr_Format(PyExc_TypeError,
135 "ZstdCompressor() argument 'options' must be dict, not %T",
136 options);
137 return -1;
138 }
139
140 Py_ssize_t pos = 0;
141 PyObject *key, *value;
142 while (PyDict_Next(options, &pos, &key, &value)) {
143 /* Check key type */
144 if (Py_TYPE(key) == mod_state->DParameter_type) {
145 PyErr_SetString(PyExc_TypeError,
146 "compression options dictionary key must not be a "
147 "DecompressionParameter attribute");
148 return -1;
149 }
150
151 Py_INCREF(key);
152 Py_INCREF(value);
153 int key_v = PyLong_AsInt(key);
154 Py_DECREF(key);
155 if (key_v == -1 && PyErr_Occurred()) {
156 Py_DECREF(value);
157 return -1;
158 }
159
160 int value_v = PyLong_AsInt(value);
161 Py_DECREF(value);
162 if (value_v == -1 && PyErr_Occurred()) {
163 return -1;
164 }
165
166 if (key_v == ZSTD_c_compressionLevel) {
167 if (_zstd_set_c_level(self, value_v) < 0) {
168 return -1;
169 }
170 continue;
171 }
172 if (key_v == ZSTD_c_nbWorkers) {
173 /* From the zstd library docs:
174 1. When nbWorkers >= 1, triggers asynchronous mode when
175 used with ZSTD_compressStream2().
176 2, Default value is `0`, aka "single-threaded mode" : no
177 worker is spawned, compression is performed inside
178 caller's thread, all invocations are blocking. */
179 if (value_v != 0) {
180 self->use_multithread = 1;
181 }
182 }
183
184 /* Set parameter to compression context */
185 size_t zstd_ret = ZSTD_CCtx_setParameter(self->cctx, key_v, value_v);
186
187 /* Check error */
188 if (ZSTD_isError(zstd_ret)) {
189 set_parameter_error(1, key_v, value_v);
190 return -1;
191 }
192 }
193 return 0;
194 }
195
196 static void
197 capsule_free_cdict(PyObject *capsule)
198 {
199 ZSTD_CDict *cdict = PyCapsule_GetPointer(capsule, NULL);
200 ZSTD_freeCDict(cdict);
201 }
202
203 ZSTD_CDict *
204 _get_CDict(ZstdDict *self, int compressionLevel)
205 {
206 assert(PyMutex_IsLocked(&self->lock));
207 PyObject *level = NULL;
208 PyObject *capsule = NULL;
209 ZSTD_CDict *cdict;
210 int ret;
211
212
213 /* int level object */
214 level = PyLong_FromLong(compressionLevel);
215 if (level == NULL) {
216 goto error;
217 }
218
219 /* Get PyCapsule object from self->c_dicts */
220 ret = PyDict_GetItemRef(self->c_dicts, level, &capsule);
221 if (ret < 0) {
222 goto error;
223 }
224 if (capsule == NULL) {
225 /* Create ZSTD_CDict instance */
226 Py_BEGIN_ALLOW_THREADS
227 cdict = ZSTD_createCDict(self->dict_buffer, self->dict_len,
228 compressionLevel);
229 Py_END_ALLOW_THREADS
230
231 if (cdict == NULL) {
232 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
233 if (mod_state != NULL) {
234 PyErr_SetString(mod_state->ZstdError,
235 "Failed to create a ZSTD_CDict instance from "
236 "Zstandard dictionary content.");
237 }
238 goto error;
239 }
240
241 /* Put ZSTD_CDict instance into PyCapsule object */
242 capsule = PyCapsule_New(cdict, NULL, capsule_free_cdict);
243 if (capsule == NULL) {
244 ZSTD_freeCDict(cdict);
245 goto error;
246 }
247
248 /* Add PyCapsule object to self->c_dicts */
249 ret = PyDict_SetItem(self->c_dicts, level, capsule);
250 if (ret < 0) {
251 goto error;
252 }
253 }
254 else {
255 /* ZSTD_CDict instance already exists */
256 cdict = PyCapsule_GetPointer(capsule, NULL);
257 }
258 goto success;
259
260 error:
261 cdict = NULL;
262 success:
263 Py_XDECREF(level);
264 Py_XDECREF(capsule);
265 return cdict;
266 }
267
268 static int
269 _zstd_load_impl(ZstdCompressor *self, ZstdDict *zd,
270 _zstd_state *mod_state, int type)
271 {
272 size_t zstd_ret;
273 if (type == DICT_TYPE_DIGESTED) {
274 /* Get ZSTD_CDict */
275 ZSTD_CDict *c_dict = _get_CDict(zd, self->compression_level);
276 if (c_dict == NULL) {
277 return -1;
278 }
279 /* Reference a prepared dictionary.
280 It overrides some compression context's parameters. */
281 zstd_ret = ZSTD_CCtx_refCDict(self->cctx, c_dict);
282 }
283 else if (type == DICT_TYPE_UNDIGESTED) {
284 /* Load a dictionary.
285 It doesn't override compression context's parameters. */
286 zstd_ret = ZSTD_CCtx_loadDictionary(self->cctx, zd->dict_buffer,
287 zd->dict_len);
288 }
289 else if (type == DICT_TYPE_PREFIX) {
290 /* Load a prefix */
291 zstd_ret = ZSTD_CCtx_refPrefix(self->cctx, zd->dict_buffer,
292 zd->dict_len);
293 }
294 else {
295 Py_UNREACHABLE();
296 }
297
298 /* Check error */
299 if (ZSTD_isError(zstd_ret)) {
300 set_zstd_error(mod_state, ERR_LOAD_C_DICT, zstd_ret);
301 return -1;
302 }
303 return 0;
304 }
305
306 static int
307 _zstd_load_c_dict(ZstdCompressor *self, PyObject *dict)
308 {
309 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
310 /* When compressing, use undigested dictionary by default. */
311 int type = DICT_TYPE_UNDIGESTED;
312 ZstdDict *zd = _Py_parse_zstd_dict(mod_state, dict, &type);
313 if (zd == NULL) {
314 return -1;
315 }
316 int ret;
317 PyMutex_Lock(&zd->lock);
318 ret = _zstd_load_impl(self, zd, mod_state, type);
319 PyMutex_Unlock(&zd->lock);
320 return ret;
321 }
322
323 /*[clinic input]
324 @classmethod
325 _zstd.ZstdCompressor.__new__ as _zstd_ZstdCompressor_new
326 level: object = None
327 The compression level to use. Defaults to COMPRESSION_LEVEL_DEFAULT.
328 options: object = None
329 A dict object that contains advanced compression parameters.
330 zstd_dict: object = None
331 A ZstdDict object, a pre-trained Zstandard dictionary.
332
333 Create a compressor object for compressing data incrementally.
334
335 Thread-safe at method level. For one-shot compression, use the
336 compress() function instead.
337 [clinic start generated code]*/
338
339 static PyObject *
340 _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
341 PyObject *options, PyObject *zstd_dict)
342 /*[clinic end generated code: output=cdef61eafecac3d7 input=bbfeeaa06fd3bd4d]*/
343 {
344 ZstdCompressor* self = PyObject_GC_New(ZstdCompressor, type);
345 if (self == NULL) {
346 goto error;
347 }
348
349 self->use_multithread = 0;
350 self->compression_level = ZSTD_CLEVEL_DEFAULT;
351 self->dict = NULL;
352 self->lock = (PyMutex){0};
353
354 /* Compression context */
355 self->cctx = ZSTD_createCCtx();
356 if (self->cctx == NULL) {
357 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
358 if (mod_state != NULL) {
359 PyErr_SetString(mod_state->ZstdError,
360 "Unable to create ZSTD_CCtx instance.");
361 }
362 goto error;
363 }
364
365 /* Last mode */
366 self->last_mode = ZSTD_e_end;
367
368 if (level != Py_None && options != Py_None) {
369 PyErr_SetString(PyExc_TypeError,
370 "Only one of level or options should be used.");
371 goto error;
372 }
373
374 /* Set compression level */
375 if (level != Py_None) {
376 if (!PyLong_Check(level)) {
377 PyErr_SetString(PyExc_TypeError,
378 "invalid type for level, expected int");
379 goto error;
380 }
381 int level_v = PyLong_AsInt(level);
382 if (level_v == -1 && PyErr_Occurred()) {
383 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
384 PyErr_Format(PyExc_ValueError,
385 "illegal compression level; the valid range is [%d, %d]",
386 ZSTD_minCLevel(), ZSTD_maxCLevel());
387 }
388 goto error;
389 }
390 if (_zstd_set_c_level(self, level_v) < 0) {
391 goto error;
392 }
393 }
394
395 /* Set options dictionary */
396 if (options != Py_None) {
397 if (_zstd_set_c_parameters(self, options) < 0) {
398 goto error;
399 }
400 }
401
402 /* Load Zstandard dictionary to compression context */
403 if (zstd_dict != Py_None) {
404 if (_zstd_load_c_dict(self, zstd_dict) < 0) {
405 goto error;
406 }
407 Py_INCREF(zstd_dict);
408 self->dict = zstd_dict;
409 }
410
411 // We can only start GC tracking once self->dict is set.
412 PyObject_GC_Track(self);
413
414 return (PyObject*)self;
415
416 error:
417 Py_XDECREF(self);
418 return NULL;
419 }
420
421 static void
422 ZstdCompressor_dealloc(PyObject *ob)
423 {
424 ZstdCompressor *self = ZstdCompressor_CAST(ob);
425
426 PyObject_GC_UnTrack(self);
427
428 /* Free compression context */
429 if (self->cctx) {
430 ZSTD_freeCCtx(self->cctx);
431 }
432
433 assert(!PyMutex_IsLocked(&self->lock));
434
435 /* Py_XDECREF the dict after free the compression context */
436 Py_CLEAR(self->dict);
437
438 PyTypeObject *tp = Py_TYPE(self);
439 PyObject_GC_Del(ob);
440 Py_DECREF(tp);
441 }
442
443 static PyObject *
444 compress_lock_held(ZstdCompressor *self, Py_buffer *data,
445 ZSTD_EndDirective end_directive)
446 {
447 assert(PyMutex_IsLocked(&self->lock));
448 ZSTD_inBuffer in;
449 ZSTD_outBuffer out;
450 _BlocksOutputBuffer buffer = {.list = NULL};
451 size_t zstd_ret;
452 PyObject *ret;
453
454 /* Prepare input & output buffers */
455 if (data != NULL) {
456 in.src = data->buf;
457 in.size = data->len;
458 in.pos = 0;
459 }
460 else {
461 in.src = &in;
462 in.size = 0;
463 in.pos = 0;
464 }
465
466 /* Calculate output buffer's size */
467 size_t output_buffer_size = ZSTD_compressBound(in.size);
468 if (output_buffer_size > (size_t) PY_SSIZE_T_MAX) {
469 PyErr_NoMemory();
470 goto error;
471 }
472
473 if (_OutputBuffer_InitWithSize(&buffer, &out, -1,
474 (Py_ssize_t) output_buffer_size) < 0) {
475 goto error;
476 }
477
478
479 /* Zstandard stream compress */
480 while (1) {
481 Py_BEGIN_ALLOW_THREADS
482 zstd_ret = ZSTD_compressStream2(self->cctx, &out, &in, end_directive);
483 Py_END_ALLOW_THREADS
484
485 /* Check error */
486 if (ZSTD_isError(zstd_ret)) {
487 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
488 set_zstd_error(mod_state, ERR_COMPRESS, zstd_ret);
489 goto error;
490 }
491
492 /* Finished */
493 if (zstd_ret == 0) {
494 break;
495 }
496
497 /* Output buffer should be exhausted, grow the buffer. */
498 assert(out.pos == out.size);
499 if (out.pos == out.size) {
500 if (_OutputBuffer_Grow(&buffer, &out) < 0) {
501 goto error;
502 }
503 }
504 }
505
506 /* Return a bytes object */
507 ret = _OutputBuffer_Finish(&buffer, &out);
508 if (ret != NULL) {
509 return ret;
510 }
511
512 error:
513 _OutputBuffer_OnError(&buffer);
514 return NULL;
515 }
516
517 #ifndef NDEBUG
518 static inline int
519 mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out)
520 {
521 return in->size == in->pos && out->size != out->pos;
522 }
523 #endif
524
525 static PyObject *
526 compress_mt_continue_lock_held(ZstdCompressor *self, Py_buffer *data)
527 {
528 assert(PyMutex_IsLocked(&self->lock));
529 ZSTD_inBuffer in;
530 ZSTD_outBuffer out;
531 _BlocksOutputBuffer buffer = {.list = NULL};
532 size_t zstd_ret;
533 PyObject *ret;
534
535 /* Prepare input & output buffers */
536 in.src = data->buf;
537 in.size = data->len;
538 in.pos = 0;
539
540 if (_OutputBuffer_InitAndGrow(&buffer, &out, -1) < 0) {
541 goto error;
542 }
543
544 /* Zstandard stream compress */
545 while (1) {
546 Py_BEGIN_ALLOW_THREADS
547 do {
548 zstd_ret = ZSTD_compressStream2(self->cctx, &out, &in,
549 ZSTD_e_continue);
550 } while (out.pos != out.size
551 && in.pos != in.size
552 && !ZSTD_isError(zstd_ret));
553 Py_END_ALLOW_THREADS
554
555 /* Check error */
556 if (ZSTD_isError(zstd_ret)) {
557 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
558 set_zstd_error(mod_state, ERR_COMPRESS, zstd_ret);
559 goto error;
560 }
561
562 /* Like compress_lock_held(), output as much as possible. */
563 if (out.pos == out.size) {
564 if (_OutputBuffer_Grow(&buffer, &out) < 0) {
565 goto error;
566 }
567 }
568 else if (in.pos == in.size) {
569 /* Finished */
570 assert(mt_continue_should_break(&in, &out));
571 break;
572 }
573 }
574
575 /* Return a bytes object */
576 ret = _OutputBuffer_Finish(&buffer, &out);
577 if (ret != NULL) {
578 return ret;
579 }
580
581 error:
582 _OutputBuffer_OnError(&buffer);
583 return NULL;
584 }
585
586 /*[clinic input]
587 _zstd.ZstdCompressor.compress
588
589 data: Py_buffer
590 mode: int(c_default="ZSTD_e_continue") = ZstdCompressor.CONTINUE
591 Can be these 3 values ZstdCompressor.CONTINUE,
592 ZstdCompressor.FLUSH_BLOCK, ZstdCompressor.FLUSH_FRAME
593
594 Provide data to the compressor object.
595
596 Return a chunk of compressed data if possible, or b'' otherwise.
597 When you have finished providing data to the compressor, call the
598 flush() method to finish the compression process.
599 [clinic start generated code]*/
600
601 static PyObject *
602 _zstd_ZstdCompressor_compress_impl(ZstdCompressor *self, Py_buffer *data,
603 int mode)
604 /*[clinic end generated code: output=ed7982d1cf7b4f98 input=11726dff64d7b2f9]*/
605 {
606 PyObject *ret;
607
608 /* Check mode value */
609 if (mode != ZSTD_e_continue &&
610 mode != ZSTD_e_flush &&
611 mode != ZSTD_e_end)
612 {
613 PyErr_SetString(PyExc_ValueError,
614 "mode argument wrong value, it should be one of "
615 "ZstdCompressor.CONTINUE, ZstdCompressor.FLUSH_BLOCK, "
616 "ZstdCompressor.FLUSH_FRAME.");
617 return NULL;
618 }
619
620 /* Thread-safe code */
621 PyMutex_Lock(&self->lock);
622
623 /* Compress */
624 if (self->use_multithread && mode == ZSTD_e_continue) {
625 ret = compress_mt_continue_lock_held(self, data);
626 }
627 else {
628 ret = compress_lock_held(self, data, mode);
629 }
630
631 if (ret) {
632 self->last_mode = mode;
633 }
634 else {
635 self->last_mode = ZSTD_e_end;
636
637 /* Resetting cctx's session never fail */
638 ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only);
639 }
640 PyMutex_Unlock(&self->lock);
641
642 return ret;
643 }
644
645 /*[clinic input]
646 _zstd.ZstdCompressor.flush
647
648 mode: int(c_default="ZSTD_e_end") = ZstdCompressor.FLUSH_FRAME
649 Can be these 2 values ZstdCompressor.FLUSH_FRAME,
650 ZstdCompressor.FLUSH_BLOCK
651
652 Finish the compression process.
653
654 Flush any remaining data left in internal buffers. Since Zstandard
655 data consists of one or more independent frames, the compressor
656 object can still be used after this method is called.
657 [clinic start generated code]*/
658
659 static PyObject *
660 _zstd_ZstdCompressor_flush_impl(ZstdCompressor *self, int mode)
661 /*[clinic end generated code: output=b7cf2c8d64dcf2e3 input=130e0b1eddf0f498]*/
662 {
663 PyObject *ret;
664
665 /* Check mode value */
666 if (mode != ZSTD_e_end && mode != ZSTD_e_flush) {
667 PyErr_SetString(PyExc_ValueError,
668 "mode argument wrong value, it should be "
669 "ZstdCompressor.FLUSH_FRAME or "
670 "ZstdCompressor.FLUSH_BLOCK.");
671 return NULL;
672 }
673
674 /* Thread-safe code */
675 PyMutex_Lock(&self->lock);
676
677 ret = compress_lock_held(self, NULL, mode);
678
679 if (ret) {
680 self->last_mode = mode;
681 }
682 else {
683 self->last_mode = ZSTD_e_end;
684
685 /* Resetting cctx's session never fail */
686 ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only);
687 }
688 PyMutex_Unlock(&self->lock);
689
690 return ret;
691 }
692
693
694 /*[clinic input]
695 _zstd.ZstdCompressor.set_pledged_input_size
696
697 size: zstd_contentsize
698 The size of the uncompressed data to be provided to the compressor.
699 /
700
701 Set the uncompressed content size to be written into the frame header.
702
703 This method can be used to ensure the header of the frame about to
704 be written includes the size of the data, unless the
705 CompressionParameter.content_size_flag is set to False.
706 If last_mode != FLUSH_FRAME, then a RuntimeError is raised.
707
708 It is important to ensure that the pledged data size matches the
709 actual data size. If they do not match the compressed output data
710 may be corrupted and the final chunk written may be lost.
711 [clinic start generated code]*/
712
713 static PyObject *
714 _zstd_ZstdCompressor_set_pledged_input_size_impl(ZstdCompressor *self,
715 unsigned long long size)
716 /*[clinic end generated code: output=3a09e55cc0e3b4f9 input=d6d1669171af3da4]*/
717 {
718 // Error occured while converting argument, should be unreachable
719 assert(size != ZSTD_CONTENTSIZE_ERROR);
720
721 /* Thread-safe code */
722 PyMutex_Lock(&self->lock);
723
724 /* Check the current mode */
725 if (self->last_mode != ZSTD_e_end) {
726 PyErr_SetString(PyExc_ValueError,
727 "set_pledged_input_size() method must be called "
728 "when last_mode == FLUSH_FRAME");
729 PyMutex_Unlock(&self->lock);
730 return NULL;
731 }
732
733 /* Set pledged content size */
734 size_t zstd_ret = ZSTD_CCtx_setPledgedSrcSize(self->cctx, size);
735 PyMutex_Unlock(&self->lock);
736 if (ZSTD_isError(zstd_ret)) {
737 _zstd_state* mod_state = PyType_GetModuleState(Py_TYPE(self));
738 set_zstd_error(mod_state, ERR_SET_PLEDGED_INPUT_SIZE, zstd_ret);
739 return NULL;
740 }
741
742 Py_RETURN_NONE;
743 }
744
745 static PyMethodDef ZstdCompressor_methods[] = {
746 _ZSTD_ZSTDCOMPRESSOR_COMPRESS_METHODDEF
747 _ZSTD_ZSTDCOMPRESSOR_FLUSH_METHODDEF
748 _ZSTD_ZSTDCOMPRESSOR_SET_PLEDGED_INPUT_SIZE_METHODDEF
749 {NULL, NULL}
750 };
751
752 PyDoc_STRVAR(ZstdCompressor_last_mode_doc,
753 "The last mode used to this compressor object, its value can be .CONTINUE,\n"
754 ".FLUSH_BLOCK, .FLUSH_FRAME. Initialized to .FLUSH_FRAME.\n\n"
755 "It can be used to get the current state of a compressor, such as, data\n"
756 "flushed, or a frame ended.");
757
758 static PyMemberDef ZstdCompressor_members[] = {
759 {"last_mode", Py_T_INT, offsetof(ZstdCompressor, last_mode),
760 Py_READONLY, ZstdCompressor_last_mode_doc},
761 {NULL}
762 };
763
764 static int
765 ZstdCompressor_traverse(PyObject *ob, visitproc visit, void *arg)
766 {
767 ZstdCompressor *self = ZstdCompressor_CAST(ob);
768 Py_VISIT(self->dict);
769 return 0;
770 }
771
772 static int
773 ZstdCompressor_clear(PyObject *ob)
774 {
775 ZstdCompressor *self = ZstdCompressor_CAST(ob);
776 Py_CLEAR(self->dict);
777 return 0;
778 }
779
780 static PyType_Slot zstdcompressor_slots[] = {
781 {Py_tp_new, _zstd_ZstdCompressor_new},
782 {Py_tp_dealloc, ZstdCompressor_dealloc},
783 {Py_tp_methods, ZstdCompressor_methods},
784 {Py_tp_members, ZstdCompressor_members},
785 {Py_tp_doc, (void *)_zstd_ZstdCompressor_new__doc__},
786 {Py_tp_traverse, ZstdCompressor_traverse},
787 {Py_tp_clear, ZstdCompressor_clear},
788 {0, 0}
789 };
790
791 PyType_Spec zstd_compressor_type_spec = {
792 .name = "compression.zstd.ZstdCompressor",
793 .basicsize = sizeof(ZstdCompressor),
794 // Py_TPFLAGS_IMMUTABLETYPE is not used here as several
795 // associated constants need to be added to the type.
796 // PyType_Freeze is called later to set the flag.
797 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
798 .slots = zstdcompressor_slots,
799 };