@@ -155,8 +155,9 @@ _get_CDict(ZstdDict *self, int compressionLevel)
155
155
{
156
156
assert (PyMutex_IsLocked (& self -> lock ));
157
157
PyObject * level = NULL ;
158
- PyObject * capsule ;
158
+ PyObject * capsule = NULL ;
159
159
ZSTD_CDict * cdict ;
160
+ int ret ;
160
161
161
162
162
163
/* int level object */
@@ -166,11 +167,12 @@ _get_CDict(ZstdDict *self, int compressionLevel)
166
167
}
167
168
168
169
/* Get PyCapsule object from self->c_dicts */
169
- capsule = PyDict_GetItemWithError (self -> c_dicts , level );
170
+ ret = PyDict_GetItemRef (self -> c_dicts , level , & capsule );
171
+ if (ret < 0 ) {
172
+ Py_XDECREF (capsule );
173
+ goto error ;
174
+ }
170
175
if (capsule == NULL ) {
171
- if (PyErr_Occurred ()) {
172
- goto error ;
173
- }
174
176
/* Create ZSTD_CDict instance */
175
177
char * dict_buffer = PyBytes_AS_STRING (self -> dict_content );
176
178
Py_ssize_t dict_len = Py_SIZE (self -> dict_content );
@@ -197,9 +199,8 @@ _get_CDict(ZstdDict *self, int compressionLevel)
197
199
goto error ;
198
200
}
199
201
200
- /* Add PyCapsule object to self->c_dicts if not already inserted */
201
- int ret = PyDict_SetDefaultRef (self -> c_dicts , level , capsule , NULL );
202
- Py_DECREF (capsule );
202
+ /* Add PyCapsule object to self->c_dicts */
203
+ ret = PyDict_SetItem (self -> c_dicts , level , capsule );
203
204
if (ret < 0 ) {
204
205
goto error ;
205
206
}
@@ -214,6 +215,7 @@ _get_CDict(ZstdDict *self, int compressionLevel)
214
215
cdict = NULL ;
215
216
success :
216
217
Py_XDECREF (level );
218
+ Py_XDECREF (capsule );
217
219
return cdict ;
218
220
}
219
221
@@ -422,6 +424,7 @@ static PyObject *
422
424
compress_lock_held (ZstdCompressor * self , Py_buffer * data ,
423
425
ZSTD_EndDirective end_directive )
424
426
{
427
+ assert (PyMutex_IsLocked (& self -> lock ));
425
428
ZSTD_inBuffer in ;
426
429
ZSTD_outBuffer out ;
427
430
_BlocksOutputBuffer buffer = {.list = NULL };
@@ -504,6 +507,7 @@ mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out)
504
507
static PyObject *
505
508
compress_mt_continue_lock_held (ZstdCompressor * self , Py_buffer * data )
506
509
{
510
+ assert (PyMutex_IsLocked (& self -> lock ));
507
511
ZSTD_inBuffer in ;
508
512
ZSTD_outBuffer out ;
509
513
_BlocksOutputBuffer buffer = {.list = NULL };
0 commit comments