| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Add :c:func:`PyDict_Pop` and :c:func:`PyDict_PopString` functions: remove a key | ||
| from a dictionary and optionally return the removed value. This is similar to | ||
| :meth:`dict.pop`, but without the default value and not raising :exc:`KeyError` | ||
| if the key missing. Patch by Stefan Behnel and Victor Stinner. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| Expand Up | @@ -331,6 +331,88 @@ dict_mergefromseq2(PyObject *self, PyObject *args) | |||||
| } | ||||||
|
|
||||||
|
|
||||||
| static PyObject * | ||||||
| dict_pop(PyObject *self, PyObject *args) | ||||||
|
Comment thread
vstinner marked this conversation as resolved.
|
||||||
| { | ||||||
| // Test PyDict_Pop(dict, key, &value) | ||||||
| PyObject *dict, *key; | ||||||
| if (!PyArg_ParseTuple(args, "OO", &dict, &key)) { | ||||||
| return NULL; | ||||||
| } | ||||||
| NULLABLE(dict); | ||||||
| NULLABLE(key); | ||||||
| PyObject *result = UNINITIALIZED_PTR; | ||||||
| int res = PyDict_Pop(dict, key, &result); | ||||||
| if (res < 0) { | ||||||
| assert(result == NULL); | ||||||
| return NULL; | ||||||
|
Comment thread
vstinner marked this conversation as resolved.
|
||||||
| } | ||||||
| if (res == 0) { | ||||||
| assert(result == NULL); | ||||||
| result = Py_NewRef(Py_None); | ||||||
|
Comment thread
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI prefer to return (0, None) to make the tests written in Python closer to what the C API returns. In test_capi.test_dict, you can see that as (0, NULL).
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt can be confused with actual None. For example dict.get() return an actual none, but several corresponding C API functions return NULL. In other tests I made them returning AttributeError or KeyError, as it is less chance to confuse with real value, but I think that it would be better to use a special singleton _testcapi.MISSING in future.
Sorry, something went wrong.
All reactions
|
||||||
| } | ||||||
| else { | ||||||
| assert(result != NULL); | ||||||
| } | ||||||
| return Py_BuildValue("iN", res, result); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| static PyObject * | ||||||
| dict_pop_null(PyObject *self, PyObject *args) | ||||||
| { | ||||||
| // Test PyDict_Pop(dict, key, NULL) | ||||||
| PyObject *dict, *key; | ||||||
| if (!PyArg_ParseTuple(args, "OO", &dict, &key)) { | ||||||
| return NULL; | ||||||
| } | ||||||
| NULLABLE(dict); | ||||||
| NULLABLE(key); | ||||||
| RETURN_INT(PyDict_Pop(dict, key, NULL)); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| static PyObject * | ||||||
| dict_popstring(PyObject *self, PyObject *args) | ||||||
|
Comment thread
vstinner marked this conversation as resolved.
|
||||||
| { | ||||||
| PyObject *dict; | ||||||
| const char *key; | ||||||
| Py_ssize_t key_size; | ||||||
| if (!PyArg_ParseTuple(args, "Oz#", &dict, &key, &key_size)) { | ||||||
| return NULL; | ||||||
| } | ||||||
| NULLABLE(dict); | ||||||
| PyObject *result = UNINITIALIZED_PTR; | ||||||
| int res = PyDict_PopString(dict, key, &result); | ||||||
| if (res < 0) { | ||||||
| assert(result == NULL); | ||||||
| return NULL; | ||||||
| } | ||||||
| if (res == 0) { | ||||||
| assert(result == NULL); | ||||||
| result = Py_NewRef(Py_None); | ||||||
| } | ||||||
| else { | ||||||
| assert(result != NULL); | ||||||
| } | ||||||
| return Py_BuildValue("iN", res, result); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| static PyObject * | ||||||
| dict_popstring_null(PyObject *self, PyObject *args) | ||||||
| { | ||||||
| PyObject *dict; | ||||||
| const char *key; | ||||||
| Py_ssize_t key_size; | ||||||
| if (!PyArg_ParseTuple(args, "Oz#", &dict, &key, &key_size)) { | ||||||
| return NULL; | ||||||
| } | ||||||
| NULLABLE(dict); | ||||||
| RETURN_INT(PyDict_PopString(dict, key, NULL)); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| static PyMethodDef test_methods[] = { | ||||||
| {"dict_check", dict_check, METH_O}, | ||||||
| {"dict_checkexact", dict_checkexact, METH_O}, | ||||||
| Expand Down Expand Up | @@ -358,7 +440,10 @@ static PyMethodDef test_methods[] = { | |||||
| {"dict_merge", dict_merge, METH_VARARGS}, | ||||||
| {"dict_update", dict_update, METH_VARARGS}, | ||||||
| {"dict_mergefromseq2", dict_mergefromseq2, METH_VARARGS}, | ||||||
|
|
||||||
| {"dict_pop", dict_pop, METH_VARARGS}, | ||||||
| {"dict_pop_null", dict_pop_null, METH_VARARGS}, | ||||||
| {"dict_popstring", dict_popstring, METH_VARARGS}, | ||||||
| {"dict_popstring_null", dict_popstring_null, METH_VARARGS}, | ||||||
| {NULL}, | ||||||
| }; | ||||||
|
|
||||||
| Expand Down | ||||||
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.