@@ -46,12 +46,21 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content,
46
46
int is_raw )
47
47
/*[clinic end generated code: output=685b7406a48b0949 input=9e8c493e31c98383]*/
48
48
{
49
+ /* All dictionaries must be at least 8 bytes */
50
+ if (dict_content -> len < 8 ) {
51
+ PyErr_SetString (PyExc_ValueError ,
52
+ "Zstandard dictionary content should at least "
53
+ "8 bytes." );
54
+ gotoerror ;
55
+ }
56
+
49
57
ZstdDict * self = PyObject_GC_New (ZstdDict ,type );
50
58
if (self == NULL ) {
51
59
return NULL ;
52
60
}
53
61
54
62
self -> d_dict = NULL ;
63
+ self -> dict_buffer = NULL ;
55
64
self -> dict_id = 0 ;
56
65
self -> lock = (PyMutex ){0 };
57
66
@@ -61,36 +70,20 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content,
61
70
gotoerror ;
62
71
}
63
72
64
- /* Check dict_content's type */
65
- if (dict_content == NULL ) {
66
- PyErr_SetString (PyExc_TypeError ,
67
- "dict_content argument should be bytes-like object." );
68
- gotoerror ;
69
- }
70
-
71
- self -> dict_buffer = PyMem_RawMalloc (dict_content -> len );
73
+ self -> dict_buffer = PyMem_Malloc (dict_content -> len );
72
74
if (!self -> dict_buffer ) {
75
+ Py_DECREF (self );
73
76
return PyErr_NoMemory ();
74
77
}
75
78
memcpy (self -> dict_buffer ,dict_content -> buf ,dict_content -> len );
76
79
self -> dict_len = dict_content -> len ;
77
80
78
- /* Both ordinary and "raw content" dictionaries must be 8 bytes minimum */
79
- if (self -> dict_len < 8 ) {
80
- PyErr_SetString (PyExc_ValueError ,
81
- "Zstandard dictionary content should at least "
82
- "8 bytes." );
83
- gotoerror ;
84
- }
85
-
86
81
/* Get dict_id, 0 means "raw content" dictionary. */
87
- self -> dict_id = ZSTD_getDictID_fromDict (
88
- self -> dict_buffer ,self -> dict_len );
82
+ self -> dict_id = ZSTD_getDictID_fromDict (self -> dict_buffer ,self -> dict_len );
89
83
90
84
/* Check validity for ordinary dictionary */
91
85
if (!is_raw && self -> dict_id == 0 ) {
92
- char * msg = "Invalid Zstandard dictionary and is_raw not set.\n" ;
93
- PyErr_SetString (PyExc_ValueError ,msg );
86
+ PyErr_SetString (PyExc_ValueError ,"invalid Zstandard dictionary." );
94
87
gotoerror ;
95
88
}
96
89
@@ -100,7 +93,6 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content,
100
93
101
94
error :
102
95
Py_XDECREF (self );
103
- PyObject_GC_Del (self );
104
96
return NULL ;
105
97
}
106
98
@@ -118,8 +110,8 @@ ZstdDict_dealloc(PyObject *ob)
118
110
119
111
assert (!PyMutex_IsLocked (& self -> lock ));
120
112
121
- /* Release dict_buffer afterFree ZSTD_CDict/ZSTD_DDict instances */
122
- PyMem_RawFree (self -> dict_buffer );
113
+ /* Release dict_buffer afterfreeing ZSTD_CDict/ZSTD_DDict instances */
114
+ PyMem_Free (self -> dict_buffer );
123
115
Py_CLEAR (self -> c_dicts );
124
116
125
117
PyTypeObject * tp = Py_TYPE (self );
@@ -135,11 +127,11 @@ PyDoc_STRVAR(ZstdDict_dictid_doc,
135
127
"without any restrictions on format or content." );
136
128
137
129
static PyObject *
138
- ZstdDict_str (PyObject * ob )
130
+ ZstdDict_repr (PyObject * ob )
139
131
{
140
132
ZstdDict * dict = ZstdDict_CAST (ob );
141
133
return PyUnicode_FromFormat ("<ZstdDict dict_id=%u dict_size=%zd>" ,
142
- dict -> dict_id ,dict -> dict_len );
134
+ ( unsigned int ) dict -> dict_id ,dict -> dict_len );
143
135
}
144
136
145
137
static PyMemberDef ZstdDict_members []= {
@@ -252,15 +244,24 @@ ZstdDict_traverse(PyObject *ob, visitproc visit, void *arg)
252
244
return 0 ;
253
245
}
254
246
247
+ static int
248
+ ZstdDict_clear (PyObject * ob )
249
+ {
250
+ ZstdDict * self = ZstdDict_CAST (ob );
251
+ Py_CLEAR (self -> c_dicts );
252
+ return 0 ;
253
+ }
254
+
255
255
static PyType_Slot zstddict_slots []= {
256
256
{Py_tp_members ,ZstdDict_members },
257
257
{Py_tp_getset ,ZstdDict_getset },
258
258
{Py_tp_new ,_zstd_ZstdDict_new },
259
259
{Py_tp_dealloc ,ZstdDict_dealloc },
260
- {Py_tp_str , ZstdDict_str },
260
+ {Py_tp_repr , ZstdDict_repr },
261
261
{Py_tp_doc , (void * )_zstd_ZstdDict_new__doc__ },
262
262
{Py_sq_length ,ZstdDict_length },
263
263
{Py_tp_traverse ,ZstdDict_traverse },
264
+ {Py_tp_clear ,ZstdDict_clear },
264
265
{0 ,0 }
265
266
};
266
267