2828set_zstd_error (const  _zstd_state *  const  state ,
2929               const  error_type  type , const  size_t  zstd_ret )
3030{
31-     char  buf [128 ];
3231    char  * msg ;
3332    assert (ZSTD_isError (zstd_ret ));
3433
@@ -71,8 +70,7 @@ set_zstd_error(const _zstd_state* const state,
7170    default :
7271        Py_UNREACHABLE ();
7372    }
74-     PyOS_snprintf (buf , sizeof (buf ), msg , ZSTD_getErrorName (zstd_ret ));
75-     PyErr_SetString (state -> ZstdError , buf );
73+     PyErr_Format (state -> ZstdError , msg , ZSTD_getErrorName (zstd_ret ));
7674}
7775
7876typedef  struct  {
@@ -172,6 +170,14 @@ set_parameter_error(const _zstd_state* const state, int is_compress,
172170                 ZSTD_versionString (), 8 * (int )sizeof (Py_ssize_t ));
173171}
174172
173+ static  inline  _zstd_state * 
174+ get_zstd_state (PyObject  * module )
175+ {
176+     void  * state  =  PyModule_GetState (module );
177+     assert (state  !=  NULL );
178+     return  (_zstd_state  * )state ;
179+ }
180+ 
175181/* ------------------------- 
176182     Train dictionary code 
177183   ------------------------- */ 
@@ -216,7 +222,7 @@ _zstd__train_dict_impl(PyObject *module, PyBytesObject *samples_bytes,
216222    }
217223
218224    /* Prepare chunk_sizes */ 
219-     chunk_sizes  =  PyMem_Malloc ( chunks_number   *   sizeof ( size_t ) );
225+     chunk_sizes  =  PyMem_New ( size_t ,  chunks_number );
220226    if  (chunk_sizes  ==  NULL ) {
221227        PyErr_NoMemory ();
222228        goto error ;
@@ -256,10 +262,8 @@ _zstd__train_dict_impl(PyObject *module, PyBytesObject *samples_bytes,
256262
257263    /* Check zstd dict error */ 
258264    if  (ZDICT_isError (zstd_ret )) {
259-         _zstd_state *  const  _module_state  =  PyModule_GetState (module );
260-         if  (_module_state  !=  NULL ) {
261-             set_zstd_error (_module_state , ERR_TRAIN_DICT , zstd_ret );
262-         }
265+         _zstd_state *  const  _module_state  =  get_zstd_state (module );
266+         set_zstd_error (_module_state , ERR_TRAIN_DICT , zstd_ret );
263267        goto error ;
264268    }
265269
@@ -386,10 +390,8 @@ _zstd__finalize_dict_impl(PyObject *module, PyBytesObject *custom_dict_bytes,
386390
387391    /* Check zstd dict error */ 
388392    if  (ZDICT_isError (zstd_ret )) {
389-         _zstd_state *  const  _module_state  =  PyModule_GetState (module );
390-         if  (_module_state  !=  NULL ) {
391-             set_zstd_error (_module_state , ERR_FINALIZE_DICT , zstd_ret );
392-         }
393+         _zstd_state *  const  _module_state  =  get_zstd_state (module );
394+         set_zstd_error (_module_state , ERR_FINALIZE_DICT , zstd_ret );
393395        goto error ;
394396    }
395397
@@ -429,19 +431,15 @@ _zstd__get_param_bounds_impl(PyObject *module, int is_compress,
429431    if  (is_compress ) {
430432        bound  =  ZSTD_cParam_getBounds (parameter );
431433        if  (ZSTD_isError (bound .error )) {
432-             _zstd_state *  const  _module_state  =  PyModule_GetState (module );
433-             if  (_module_state  !=  NULL ) {
434-                 set_zstd_error (_module_state , ERR_GET_C_BOUNDS , bound .error );
435-             }
434+             _zstd_state *  const  _module_state  =  get_zstd_state (module );
435+             set_zstd_error (_module_state , ERR_GET_C_BOUNDS , bound .error );
436436            return  NULL ;
437437        }
438438    } else  {
439439        bound  =  ZSTD_dParam_getBounds (parameter );
440440        if  (ZSTD_isError (bound .error )) {
441-             _zstd_state *  const  _module_state  =  PyModule_GetState (module );
442-             if  (_module_state  !=  NULL ) {
443-                 set_zstd_error (_module_state , ERR_GET_D_BOUNDS , bound .error );
444-             }
441+             _zstd_state *  const  _module_state  =  get_zstd_state (module );
442+             set_zstd_error (_module_state , ERR_GET_D_BOUNDS , bound .error );
445443            return  NULL ;
446444        }
447445    }
@@ -470,15 +468,13 @@ _zstd_get_frame_size_impl(PyObject *module, Py_buffer *frame_buffer)
470468
471469    frame_size  =  ZSTD_findFrameCompressedSize (frame_buffer -> buf , frame_buffer -> len );
472470    if  (ZSTD_isError (frame_size )) {
473-         _zstd_state *  const  _module_state  =  PyModule_GetState (module );
474-         if  (_module_state  !=  NULL ) {
475-             PyErr_Format (_module_state -> ZstdError ,
476-                 "Error when finding the compressed size of a zstd frame. " 
477-                 "Make sure the frame_buffer argument starts from the " 
478-                 "beginning of a frame, and its length not less than this " 
479-                 "complete frame. Zstd error message: %s." ,
480-                 ZSTD_getErrorName (frame_size ));
481-         }
471+         _zstd_state *  const  _module_state  =  get_zstd_state (module );
472+         PyErr_Format (_module_state -> ZstdError ,
473+             "Error when finding the compressed size of a zstd frame. " 
474+             "Make sure the frame_buffer argument starts from the " 
475+             "beginning of a frame, and its length not less than this " 
476+             "complete frame. Zstd error message: %s." ,
477+             ZSTD_getErrorName (frame_size ));
482478        goto error ;
483479    }
484480
@@ -518,14 +514,12 @@ _zstd__get_frame_info_impl(PyObject *module, Py_buffer *frame_buffer)
518514    /* #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) 
519515       #define ZSTD_CONTENTSIZE_ERROR   (0ULL - 2) */ 
520516    if  (decompressed_size  ==  ZSTD_CONTENTSIZE_ERROR ) {
521-         _zstd_state *  const  _module_state  =  PyModule_GetState (module );
522-         if  (_module_state  !=  NULL ) {
523-             PyErr_SetString (_module_state -> ZstdError ,
524-                 "Error when getting information from the header of " 
525-                 "a zstd frame. Make sure the frame_buffer argument " 
526-                 "starts from the beginning of a frame, and its length " 
527-                 "not less than the frame header (6~18 bytes)." );
528-         }
517+         _zstd_state *  const  _module_state  =  get_zstd_state (module );
518+         PyErr_SetString (_module_state -> ZstdError ,
519+             "Error when getting information from the header of " 
520+             "a zstd frame. Make sure the frame_buffer argument " 
521+             "starts from the beginning of a frame, and its length " 
522+             "not less than the frame header (6~18 bytes)." );
529523        goto error ;
530524    }
531525
@@ -565,10 +559,7 @@ _zstd__set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type,
565559                                PyObject  * d_parameter_type )
566560/*[clinic end generated code: output=a13d4890ccbd2873 input=3e7d0d37c3a1045a]*/ 
567561{
568-     _zstd_state *  const  _module_state  =  PyModule_GetState (module );
569-     if  (_module_state  ==  NULL ) {
570-         return  NULL ;
571-     }
562+     _zstd_state *  const  _module_state  =  get_zstd_state (module );
572563
573564    if  (!PyType_Check (c_parameter_type ) ||  !PyType_Check (d_parameter_type )) {
574565        PyErr_SetString (PyExc_ValueError ,
@@ -791,10 +782,7 @@ add_constant_to_type(PyTypeObject *type, const char *name, const long value)
791782}
792783
793784static  int  _zstd_exec (PyObject  * module ) {
794-     _zstd_state *  const  _module_state  =  PyModule_GetState (module );
795-     if  (_module_state  ==  NULL ) {
796-         return  -1 ;
797-     }
785+     _zstd_state *  const  _module_state  =  get_zstd_state (module );
798786
799787    /* Reusable objects & variables */ 
800788    _module_state -> empty_bytes  =  PyBytes_FromStringAndSize (NULL , 0 );
@@ -886,10 +874,7 @@ static int _zstd_exec(PyObject *module) {
886874static  int 
887875_zstd_traverse (PyObject  * module , visitproc  visit , void  * arg )
888876{
889-     _zstd_state *  const  _module_state  =  PyModule_GetState (module );
890-     if  (_module_state  ==  NULL ) {
891-         return  -1 ;
892-     }
877+     _zstd_state *  const  _module_state  =  get_zstd_state (module );
893878
894879    Py_VISIT (_module_state -> empty_bytes );
895880    Py_VISIT (_module_state -> empty_readonly_memoryview );
@@ -913,10 +898,7 @@ _zstd_traverse(PyObject *module, visitproc visit, void *arg)
913898static  int 
914899_zstd_clear (PyObject  * module )
915900{
916-     _zstd_state *  const  _module_state  =  PyModule_GetState (module );
917-     if  (_module_state  ==  NULL ) {
918-         return  -1 ;
919-     }
901+     _zstd_state *  const  _module_state  =  get_zstd_state (module );
920902
921903    Py_CLEAR (_module_state -> empty_bytes );
922904    Py_CLEAR (_module_state -> empty_readonly_memoryview );
0 commit comments