8000 gh-106672: C API: Report indiscriminately ignored errors by serhiy-storchaka · Pull Request #106674 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

8000 gh-106672: C API: Report indiscriminately ignored errors #106674

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into capi-write-unraisable-for-ignored-errors
  • Loading branch information
serhiy-storchaka committed Aug 26, 2023
commit c681ce2b1ba56ddf9a8b040dcafc77ed9d98ab37
1 change: 1 addition & 0 deletions Lib/test/test_capi/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def test_dict_getitemstring(self):
self.assertRegex(str(cm.unraisable.exc_value),
"'utf-8' codec can't decode")

self.assertIs(getitemstring({}, INVALID_UTF8), KeyError)
self.assertIs(getitemstring(42, b'a'), KeyError)
self.assertIs(getitemstring([], b'a'), KeyError)
# CRASHES getitemstring({}, NULL)
Expand Down
186 changes: 0 additions & 186 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3298,192 +3298,6 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
}


static PyObject *
test_dict_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
assert(!PyErr_Occurred());

PyObject *dict= NULL, *key = NULL, *missing_key = NULL, *value = NULL;
PyObject *invalid_key = NULL;
int res;

// test PyDict_New()
dict = PyDict_New();
if (dict == NULL) {
goto error;
}

key = PyUnicode_FromString("key");
if (key == NULL) {
goto error;
}

missing_key = PyUnicode_FromString("missing_key");
if (missing_key == NULL) {
goto error;
}

value = PyUnicode_FromString("value");
if (value == NULL) {
goto error;
}

// test PyDict_SetItem()
Py_ssize_t key_refcnt = Py_REFCNT(key);
Py_ssize_t value_refcnt = Py_REFCNT(value);
res = PyDict_SetItem(dict, key, value);
if (res < 0) {
goto error;
}
assert(res == 0);
assert(Py_REFCNT(key) == (key_refcnt + 1));
assert(Py_REFCNT(value) == (value_refcnt + 1));

// test PyDict_SetItemString()
res = PyDict_SetItemString(dict, "key", value);
if (res < 0) {
goto error;
}
assert(res == 0);
assert(Py_REFCNT(key) == (key_refcnt + 1));
assert(Py_REFCNT(value) == (value_refcnt + 1));

// test PyDict_Size()
assert(PyDict_Size(dict) == 1);

// test PyDict_Contains(), key is present
assert(PyDict_Contains(dict, key) == 1);

// test PyDict_GetItem(), key is present
assert(PyDict_GetItem(dict, key) == value);

// test PyDict_GetItemString(), key is present
assert(PyDict_GetItemString(dict, "key") == value);

// test PyDict_GetItemWithError(), key is present
assert(PyDict_GetItemWithError(dict, key) == value);
assert(!PyErr_Occurred());

// test PyDict_GetItemRef(), key is present
PyObject *get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemRef(dict, key, &get_value) == 1);
assert(get_value == value);
Py_DECREF(get_value);

// test PyDict_GetItemStringRef(), key is present
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemStringRef(dict, "key", &get_value) == 1);
assert(get_value == value);
Py_DECREF(get_value);

// test PyDict_Contains(), missing key
assert(PyDict_Contains(dict, missing_key) == 0);

// test PyDict_GetItem(), missing key
assert(PyDict_GetItem(dict, missing_key) == NULL);
assert(!PyErr_Occurred());

// test PyDict_GetItemString(), missing key
assert(PyDict_GetItemString(dict, "missing_key") == NULL);
assert(!PyErr_Occurred());

// test PyDict_GetItemWithError(), missing key
assert(PyDict_GetItem(dict, missing_key) == NULL);
assert(!PyErr_Occurred());

// test PyDict_GetItemRef(), missing key
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemRef(dict, missing_key, &get_value) == 0);
assert(!PyErr_Occurred());
assert(get_value == NULL);

// test PyDict_GetItemStringRef(), missing key
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemStringRef(dict, "missing_key", &get_value) == 0);
assert(!PyErr_Occurred());
assert(get_value == NULL);

// test PyDict_GetItem(), invalid dict
PyObject *invalid_dict = key; // borrowed reference
assert(PyDict_GetItem(invalid_dict, key) == NULL);
assert(!PyErr_Occurred());

// test PyDict_GetItemWithError(), invalid dict
assert(PyDict_GetItemWithError(invalid_dict, key) == NULL);
assert(PyErr_ExceptionMatches(PyExc_SystemError));
PyErr_Clear();

// test PyDict_GetItemRef(), invalid dict
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemRef(invalid_dict, key, &get_value) == -1);
assert(PyErr_ExceptionMatches(PyExc_SystemError));
PyErr_Clear();
assert(get_value == NULL);

// test PyDict_GetItemStringRef(), invalid dict
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemStringRef(invalid_dict, "key", &get_value) == -1);
assert(PyErr_ExceptionMatches(PyExc_SystemError));
PyErr_Clear();
assert(get_value == NULL);

invalid_key = PyList_New(0);
if (invalid_key == NULL) {
goto error;
}

// test PyDict_Contains(), invalid key
assert(PyDict_Contains(dict, invalid_key) == -1);
assert(PyErr_ExceptionMatches(PyExc_TypeError));
PyErr_Clear();

// test PyDict_GetItemWithError(), invalid key
assert(PyDict_GetItemWithError(dict, invalid_key) == NULL);
assert(PyErr_ExceptionMatches(PyExc_TypeError));
PyErr_Clear();

// test PyDict_GetItemRef(), invalid key
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemRef(dict, invalid_key, &get_value) == -1);
assert(PyErr_ExceptionMatches(PyExc_TypeError));
PyErr_Clear();
assert(get_value == NULL);

// test PyDict_DelItem(), key is present
assert(PyDict_DelItem(dict, key) == 0);
assert(PyDict_Size(dict) == 0);

// test PyDict_DelItem(), missing key
assert(PyDict_DelItem(dict, missing_key) == -1);
assert(PyErr_ExceptionMatches(PyExc_KeyError));
PyErr_Clear();

// test PyDict_DelItem(), invalid key
assert(PyDict_DelItem(dict, invalid_key) == -1);
assert(PyErr_ExceptionMatches(PyExc_TypeError));
PyErr_Clear();

// test PyDict_Clear()
PyDict_Clear(dict);

Py_DECREF(dict);
Py_DECREF(key);
Py_DECREF(missing_key);
Py_DECREF(value);
Py_DECREF(invalid_key);

Py_RETURN_NONE;

error:
Py_XDECREF(dict);
Py_XDECREF(key);
Py_XDECREF(missing_key);
Py_XDECREF(value);
Py_XDECREF(invalid_key);
return NULL;
}


static PyObject *
sys_getobject(PyObject *Py_UNUSED(module), PyObject *arg)
{
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0