Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-132983: Convert dict_content to take Py_buffer#133924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
42782b8
7c0d002
30ac9d5
e40de15
feb034d
c36f62d
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -26,7 +26,7 @@ class _zstd.ZstdDict "ZstdDict *" "&zstd_dict_type_spec" | ||
/*[clinic input] | ||
@classmethod | ||
_zstd.ZstdDict.__new__ as _zstd_ZstdDict_new | ||
dict_content:Py_buffer | ||
The content of a Zstandard dictionary as a bytes-like object. | ||
/ | ||
* | ||
@@ -42,17 +42,25 @@ by multiple ZstdCompressor or ZstdDecompressor objects. | ||
[clinic start generated code]*/ | ||
static PyObject * | ||
_zstd_ZstdDict_new_impl(PyTypeObject *type,Py_buffer *dict_content, | ||
int is_raw) | ||
/*[clinic end generated code: output=685b7406a48b0949 input=9e8c493e31c98383]*/ | ||
{ | ||
/* All dictionaries must be at least 8 bytes */ | ||
if (dict_content->len < 8) { | ||
PyErr_SetString(PyExc_ValueError, | ||
"Zstandard dictionary content too short " | ||
"(must have at least eight bytes)"); | ||
return NULL; | ||
} | ||
ZstdDict* self = PyObject_GC_New(ZstdDict, type); | ||
if (self == NULL) { | ||
return NULL; | ||
} | ||
self->d_dict = NULL; | ||
AA-Turner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
self->dict_buffer = NULL; | ||
self->dict_id = 0; | ||
self->lock = (PyMutex){0}; | ||
@@ -62,39 +70,26 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, PyObject *dict_content, | ||
goto error; | ||
} | ||
self->dict_buffer = PyMem_Malloc(dict_content->len); | ||
if (!self->dict_buffer) { | ||
PyErr_NoMemory(); | ||
goto error; | ||
} | ||
memcpy(self->dict_buffer, dict_content->buf, dict_content->len); | ||
self->dict_len = dict_content->len; | ||
/* Get dict_id, 0 means "raw content" dictionary. */ | ||
self->dict_id = ZSTD_getDictID_fromDict(self->dict_buffer, self->dict_len); | ||
/* Check validity for ordinary dictionary */ | ||
if (!is_raw && self->dict_id == 0) { | ||
PyErr_SetString(PyExc_ValueError, "invalid Zstandard dictionary"); | ||
goto error; | ||
} | ||
PyObject_GC_Track(self); | ||
return (PyObject*)self; | ||
error: | ||
Py_XDECREF(self); | ||
@@ -115,12 +110,12 @@ ZstdDict_dealloc(PyObject *ob) | ||
assert(!PyMutex_IsLocked(&self->lock)); | ||
/* Releasedict_buffer afterfreeing ZSTD_CDict/ZSTD_DDict instances */ | ||
PyMem_Free(self->dict_buffer); | ||
Py_CLEAR(self->c_dicts); | ||
PyTypeObject *tp = Py_TYPE(self); | ||
tp->tp_free(self); | ||
Py_DECREF(tp); | ||
} | ||
@@ -131,25 +126,33 @@ PyDoc_STRVAR(ZstdDict_dictid_doc, | ||
"The special value '0' means a 'raw content' dictionary," | ||
"without any restrictions on format or content."); | ||
static PyObject * | ||
ZstdDict_repr(PyObject *ob) | ||
{ | ||
ZstdDict *dict = ZstdDict_CAST(ob); | ||
return PyUnicode_FromFormat("<ZstdDict dict_id=%u dict_size=%zd>", | ||
(unsigned int)dict->dict_id, dict->dict_len); | ||
} | ||
static PyMemberDef ZstdDict_members[] = { | ||
{"dict_id", Py_T_UINT, offsetof(ZstdDict, dict_id), Py_READONLY, ZstdDict_dictid_doc}, | ||
{NULL} | ||
}; | ||
/*[clinic input] | ||
@getter | ||
_zstd.ZstdDict.dict_content | ||
The content of a Zstandard dictionary, as a bytes object. | ||
[clinic start generated code]*/ | ||
static PyObject * | ||
_zstd_ZstdDict_dict_content_get_impl(ZstdDict *self) | ||
/*[clinic end generated code: output=0d05caa5b550eabb input=4ed526d1c151c596]*/ | ||
{ | ||
return PyBytes_FromStringAndSize(self->dict_buffer, self->dict_len); | ||
} | ||
/*[clinic input] | ||
@getter | ||
_zstd.ZstdDict.as_digested_dict | ||
@@ -219,6 +222,7 @@ _zstd_ZstdDict_as_prefix_get_impl(ZstdDict *self) | ||
} | ||
static PyGetSetDef ZstdDict_getset[] = { | ||
_ZSTD_ZSTDDICT_DICT_CONTENT_GETSETDEF | ||
_ZSTD_ZSTDDICT_AS_DIGESTED_DICT_GETSETDEF | ||
_ZSTD_ZSTDDICT_AS_UNDIGESTED_DICT_GETSETDEF | ||
_ZSTD_ZSTDDICT_AS_PREFIX_GETSETDEF | ||
@@ -229,24 +233,22 @@ static Py_ssize_t | ||
ZstdDict_length(PyObject *ob) | ||
{ | ||
ZstdDict *self = ZstdDict_CAST(ob); | ||
return self->dict_len; | ||
} | ||
static int | ||
ZstdDict_traverse(PyObject *ob, visitproc visit, void *arg) | ||
{ | ||
ZstdDict *self = ZstdDict_CAST(ob); | ||
Py_VISIT(self->c_dicts); | ||
return 0; | ||
} | ||
static int | ||
ZstdDict_clear(PyObject *ob) | ||
{ | ||
ZstdDict *self = ZstdDict_CAST(ob); | ||
Py_CLEAR(self->c_dicts); | ||
return 0; | ||
} | ||
@@ -255,7 +257,7 @@ static PyType_Slot zstddict_slots[] = { | ||
{Py_tp_getset, ZstdDict_getset}, | ||
{Py_tp_new, _zstd_ZstdDict_new}, | ||
{Py_tp_dealloc, ZstdDict_dealloc}, | ||
{Py_tp_repr, ZstdDict_repr}, | ||
{Py_tp_doc, (void *)_zstd_ZstdDict_new__doc__}, | ||
{Py_sq_length, ZstdDict_length}, | ||
{Py_tp_traverse, ZstdDict_traverse}, | ||
Uh oh!
There was an error while loading.Please reload this page.