@@ -46,6 +46,43 @@ typedef struct {
4646
4747#define ZstdCompressor_CAST (op ) ((ZstdCompressor *)op)
4848
49+ /*[python input]
50+
51+ class zstd_contentsize_converter(CConverter):
52+ type = 'unsigned long long'
53+ converter = 'zstd_contentsize_converter'
54+
55+ [python start generated code]*/
56+ /*[python end generated code: output=da39a3ee5e6b4b0d input=0932c350d633c7de]*/
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+ If they wish to pass ZSTD_CONTENTSIZE_UNKNOWN, they need to pass
70+ None (see above). */
71+ unsigned long long pledged_size = PyLong_AsUnsignedLongLong (size );
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 %ull" , ZSTD_CONTENTSIZE_ERROR );
78+ }
79+ return 0 ;
80+ }
81+ * p = pledged_size ;
82+ }
83+ return 1 ;
84+ }
85+
4986#include "clinic/compressor.c.h"
5087
5188static int
@@ -694,15 +731,15 @@ _zstd_ZstdCompressor_flush_impl(ZstdCompressor *self, int mode)
694731/*[clinic input]
695732_zstd.ZstdCompressor.set_pledged_input_size
696733
697- size: object
734+ size: zstd_contentsize
698735 The size of the uncompressed data to be provided to the compressor.
699736 /
700737
701738Set the uncompressed content size to be written into the frame header.
702739
703740This method can be used to ensure the header of the frame about to be written
704741includes the size of the data, unless CompressionParameter.content_size_flag is
705- set to False. If . last_mode != . FLUSH_FRAME, then a RuntimeError is raised.
742+ set to False. If last_mode != FLUSH_FRAME, then a RuntimeError is raised.
706743
707744It is important to ensure that the pledged data size matches the actual data
708745size. If they do not match the compressed output data may be corrupted and the
@@ -711,52 +748,36 @@ final chunk written may be lost.
711748
712749static PyObject *
713750_zstd_ZstdCompressor_set_pledged_input_size_impl (ZstdCompressor * self ,
714- PyObject * size )
715- /*[clinic end generated code: output=2fb57610ee586d42 input=272a7881a3526dd1 ]*/
751+ unsigned long long size )
752+ /*[clinic end generated code: output=3a09e55cc0e3b4f9 input=563b9a1ddd4facc3 ]*/
716753{
717- uint64_t pledged_size ;
718754 size_t zstd_ret ;
719755 PyObject * ret ;
720756
721- /* Get size value */
722- if (size == Py_None ) {
723- pledged_size = ZSTD_CONTENTSIZE_UNKNOWN ;
724- } else {
725- pledged_size = PyLong_AsUnsignedLongLong (size );
726- if (pledged_size == (uint64_t )-1 && PyErr_Occurred ()) {
727- if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
728- PyErr_Format (PyExc_ValueError ,
729- "size argument should be a positive int less "
730- "than %ull" , ZSTD_CONTENTSIZE_ERROR );
731- }
732- return NULL ;
733- }
734- }
757+ // Error occured while converting argument, should be unreachable
758+ assert (size != ZSTD_CONTENTSIZE_ERROR );
735759
736760 /* Thread-safe code */
737761 PyMutex_Lock (& self -> lock );
738762
739763 /* Check the current mode */
740764 if (self -> last_mode != ZSTD_e_end ) {
741765 PyErr_SetString (PyExc_RuntimeError ,
742- "._set_pledged_input_size () method must be called "
743- "when (. last_mode == . FLUSH_FRAME). " );
766+ "set_pledged_input_size () method must be called "
767+ "when (last_mode == FLUSH_FRAME)" );
744768 goto error ;
745769 }
746770
747771 /* Set pledged content size */
748- zstd_ret = ZSTD_CCtx_setPledgedSrcSize (self -> cctx , pledged_size );
772+ zstd_ret = ZSTD_CCtx_setPledgedSrcSize (self -> cctx , size );
749773 if (ZSTD_isError (zstd_ret )) {
750774 _zstd_state * mod_state = PyType_GetModuleState (Py_TYPE (self ));
751775 set_zstd_error (mod_state , ERR_SET_PLEDGED_INPUT_SIZE , zstd_ret );
752776 goto error ;
753777 }
754778
755- /* Return None */
756779 ret = Py_None ;
757- Py_INCREF (ret );
758780 goto success ;
759-
760781error :
761782 ret = NULL ;
762783success :
0 commit comments