summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-08-07 15:51:43 (GMT)
committerGitHub <noreply@github.com>2023-08-07 15:51:43 (GMT)
commit16c9415fba4972743f1944ebc44946e475e68bc4 (patch)
tree3fcc78ed09fa2db6bc0b833fd29feabb619cb8c0 /Modules/_testcapi
parent85793278793708ad6b7132a54ac9fb4b2c5bcac1 (diff)
downloadcpython-16c9415fba4972743f1944ebc44946e475e68bc4.zip
cpython-16c9415fba4972743f1944ebc44946e475e68bc4.tar.gz
cpython-16c9415fba4972743f1944ebc44946e475e68bc4.tar.bz2
gh-107178: Add the C API tests for the Abstract Objects Layer (GH-107179)
Cover all the Mapping Protocol, almost all the Sequence Protocol (except PySequence_Fast) and a part of the Object Protocol. Move existing tests to Lib/test/test_capi/test_abstract.py and Modules/_testcapi/abstract.c. Add also tests for PyDict C API.
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/abstract.c640
-rw-r--r--Modules/_testcapi/dict.c376
-rw-r--r--Modules/_testcapi/parts.h2
3 files changed, 1018 insertions, 0 deletions
diff --git a/Modules/_testcapi/abstract.c b/Modules/_testcapi/abstract.c
new file mode 100644
index 0000000..10d7ff8
--- /dev/null
+++ b/Modules/_testcapi/abstract.c
@@ -0,0 +1,640 @@
+#include <stddef.h> // ptrdiff_t
+
+#include "parts.h"
+
+#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
+
+#define RETURN_INT(value) do { \
+ int _ret = (value); \
+ if (_ret == -1) { \
+ return NULL; \
+ } \
+ return PyLong_FromLong(_ret); \
+ } while (0)
+
+#define RETURN_SIZE(value) do { \
+ Py_ssize_t _ret = (value); \
+ if (_ret == -1) { \
+ return NULL; \
+ } \
+ return PyLong_FromSsize_t(_ret); \
+ } while (0)
+
+
+static PyObject *
+object_getattr(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *attr_name;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(attr_name);
+ return PyObject_GetAttr(obj, attr_name);
+}
+
+static PyObject *
+object_getattrstring(PyObject *self, PyObject *args)
+{
+ PyObject *obj;
+ const char *attr_name;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ return PyObject_GetAttrString(obj, attr_name);
+}
+
+static PyObject *
+object_getoptionalattr(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *attr_name, *value;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(attr_name);
+
+ switch (PyObject_GetOptionalAttr(obj, attr_name, &value)) {
+ case -1:
+ assert(value == NULL);
+ return NULL;
+ case 0:
+ assert(value == NULL);
+ return Py_NewRef(PyExc_AttributeError);
+ case 1:
+ return value;
+ default:
+ Py_FatalError("PyObject_GetOptionalAttr() returned invalid code");
+ Py_UNREACHABLE();
+ }
+}
+
+static PyObject *
+object_getoptionalattrstring(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *value;
+ const char *attr_name;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+
+ switch (PyObject_GetOptionalAttrString(obj, attr_name, &value)) {
+ case -1:
+ assert(value == NULL);
+ return NULL;
+ case 0:
+ assert(value == NULL);
+ return Py_NewRef(PyExc_AttributeError);
+ case 1:
+ return value;
+ default:
+ Py_FatalError("PyObject_GetOptionalAttrString() returned invalid code");
+ Py_UNREACHABLE();
+ }
+}
+
+static PyObject *
+object_hasattr(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *attr_name;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(attr_name);
+ return PyLong_FromLong(PyObject_HasAttr(obj, attr_name));
+}
+
+static PyObject *
+object_hasattrstring(PyObject *self, PyObject *args)
+{
+ PyObject *obj;
+ const char *attr_name;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ return PyLong_FromLong(PyObject_HasAttrString(obj, attr_name));
+}
+
+static PyObject *
+object_setattr(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *attr_name, *value;
+ if (!PyArg_ParseTuple(args, "OOO", &obj, &attr_name, &value)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(attr_name);
+ NULLABLE(value);
+ RETURN_INT(PyObject_SetAttr(obj, attr_name, value));
+}
+
+static PyObject *
+object_setattrstring(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *value;
+ const char *attr_name;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#O", &obj, &attr_name, &size, &value)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(value);
+ RETURN_INT(PyObject_SetAttrString(obj, attr_name, value));
+}
+
+static PyObject *
+object_delattr(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *attr_name;
+if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(attr_name);
+ RETURN_INT(PyObject_DelAttr(obj, attr_name));
+}
+
+static PyObject *
+object_delattrstring(PyObject *self, PyObject *args)
+{
+ PyObject *obj;
+ const char *attr_name;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ RETURN_INT(PyObject_DelAttrString(obj, attr_name));
+}
+
+
+static PyObject *
+mapping_check(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyLong_FromLong(PyMapping_Check(obj));
+}
+
+static PyObject *
+mapping_size(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_SIZE(PyMapping_Size(obj));
+}
+
+static PyObject *
+mapping_length(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_SIZE(PyMapping_Length(obj));
+}
+
+static PyObject *
+object_getitem(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ return PyObject_GetItem(mapping, key);
+}
+
+static PyObject *
+mapping_getitemstring(PyObject *self, PyObject *args)
+{
+ PyObject *mapping;
+ const char *key;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &mapping, &key, &size)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ return PyMapping_GetItemString(mapping, key);
+}
+
+static PyObject *
+mapping_getoptionalitem(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *attr_name, *value;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(attr_name);
+
+ switch (PyMapping_GetOptionalItem(obj, attr_name, &value)) {
+ case -1:
+ assert(value == NULL);
+ return NULL;
+ case 0:
+ assert(value == NULL);
+ return Py_NewRef(PyExc_KeyError);
+ case 1:
+ return value;
+ default:
+ Py_FatalError("PyMapping_GetOptionalItem() returned invalid code");
+ Py_UNREACHABLE();
+ }
+}
+
+static PyObject *
+mapping_getoptionalitemstring(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *value;
+ const char *attr_name;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+
+ switch (PyMapping_GetOptionalItemString(obj, attr_name, &value)) {
+ case -1:
+ assert(value == NULL);
+ return NULL;
+ case 0:
+ assert(value == NULL);
+ return Py_NewRef(PyExc_KeyError);
+ case 1:
+ return value;
+ default:
+ Py_FatalError("PyMapping_GetOptionalItemString() returned invalid code");
+ Py_UNREACHABLE();
+ }
+}
+
+static PyObject *
+mapping_haskey(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ return PyLong_FromLong(PyMapping_HasKey(mapping, key));
+}
+
+static PyObject *
+mapping_haskeystring(PyObject *self, PyObject *args)
+{
+ PyObject *mapping;
+ const char *key;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &mapping, &key, &size)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ return PyLong_FromLong(PyMapping_HasKeyString(mapping, key));
+}
+
+static PyObject *
+object_setitem(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key, *value;
+ if (!PyArg_ParseTuple(args, "OOO", &mapping, &key, &value)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ NULLABLE(value);
+ RETURN_INT(PyObject_SetItem(mapping, key, value));
+}
+
+static PyObject *
+mapping_setitemstring(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *value;
+ const char *key;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#O", &mapping, &key, &size, &value)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(value);
+ RETURN_INT(PyMapping_SetItemString(mapping, key, value));
+}
+
+static PyObject *
+object_delitem(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ RETURN_INT(PyObject_DelItem(mapping, key));
+}
+
+static PyObject *
+mapping_delitem(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ RETURN_INT(PyMapping_DelItem(mapping, key));
+}
+
+static PyObject *
+mapping_delitemstring(PyObject *self, PyObject *args)
+{
+ PyObject *mapping;
+ const char *key;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &mapping, &key, &size)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ RETURN_INT(PyMapping_DelItemString(mapping, key));
+}
+
+static PyObject *
+mapping_keys(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyMapping_Keys(obj);
+}
+
+static PyObject *
+mapping_values(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyMapping_Values(obj);
+}
+
+static PyObject *
+mapping_items(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyMapping_Items(obj);
+}
+
+
+static PyObject *
+sequence_check(PyObject* self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyLong_FromLong(PySequence_Check(obj));
+}
+
+static PyObject *
+sequence_size(PyObject* self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_SIZE(PySequence_Size(obj));
+}
+
+static PyObject *
+sequence_length(PyObject* self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_SIZE(PySequence_Length(obj));
+}
+
+static PyObject *
+sequence_concat(PyObject *self, PyObject *args)
+{
+ PyObject *seq1, *seq2;
+ if (!PyArg_ParseTuple(args, "OO", &seq1, &seq2)) {
+ return NULL;
+ }
+ NULLABLE(seq1);
+ NULLABLE(seq2);
+
+ return PySequence_Concat(seq1, seq2);
+}
+
+static PyObject *
+sequence_repeat(PyObject *self, PyObject *args)
+{
+ PyObject *seq;
+ Py_ssize_t count;
+ if (!PyArg_ParseTuple(args, "On", &seq, &count)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+
+ return PySequence_Repeat(seq, count);
+}
+
+static PyObject *
+sequence_inplaceconcat(PyObject *self, PyObject *args)
+{
+ PyObject *seq1, *seq2;
+ if (!PyArg_ParseTuple(args, "OO", &seq1, &seq2)) {
+ return NULL;
+ }
+ NULLABLE(seq1);
+ NULLABLE(seq2);
+
+ return PySequence_InPlaceConcat(seq1, seq2);
+}
+
+static PyObject *
+sequence_inplacerepeat(PyObject *self, PyObject *args)
+{
+ PyObject *seq;
+ Py_ssize_t count;
+ if (!PyArg_ParseTuple(args, "On", &seq, &count)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+
+ return PySequence_InPlaceRepeat(seq, count);
+}
+
+static PyObject *
+sequence_getitem(PyObject *self, PyObject *args)
+{
+ PyObject *seq;
+ Py_ssize_t i;
+ if (!PyArg_ParseTuple(args, "On", &seq, &i)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+
+ return PySequence_GetItem(seq, i);
+}
+
+static PyObject *
+sequence_setitem(PyObject *self, PyObject *args)
+{
+ Py_ssize_t i;
+ PyObject *seq, *val;
+ if (!PyArg_ParseTuple(args, "OnO", &seq, &i, &val)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+ NULLABLE(val);
+
+ RETURN_INT(PySequence_SetItem(seq, i, val));
+}
+
+
+static PyObject *
+sequence_delitem(PyObject *self, PyObject *args)
+{
+ Py_ssize_t i;
+ PyObject *seq;
+ if (!PyArg_ParseTuple(args, "On", &seq, &i)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+
+ RETURN_INT(PySequence_DelItem(seq, i));
+}
+
+static PyObject *
+sequence_setslice(PyObject* self, PyObject *args)
+{
+ PyObject *sequence, *obj;
+ Py_ssize_t i1, i2;
+ if (!PyArg_ParseTuple(args, "OnnO", &sequence, &i1, &i2, &obj)) {
+ return NULL;
+ }
+ NULLABLE(sequence);
+ NULLABLE(obj);
+
+ RETURN_INT(PySequence_SetSlice(sequence, i1, i2, obj));
+}
+
+static PyObject *
+sequence_delslice(PyObject *self, PyObject *args)
+{
+ PyObject *sequence;
+ Py_ssize_t i1, i2;
+ if (!PyArg_ParseTuple(args, "Onn", &sequence, &i1, &i2)) {
+ return NULL;
+ }
+ NULLABLE(sequence);
+
+ RETURN_INT(PySequence_DelSlice(sequence, i1, i2));
+}
+
+static PyObject *
+sequence_count(PyObject *self, PyObject *args)
+{
+ PyObject *seq, *value;
+ if (!PyArg_ParseTuple(args, "OO", &seq, &value)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+ NULLABLE(value);
+
+ RETURN_SIZE(PySequence_Count(seq, value));
+}
+
+static PyObject *
+sequence_contains(PyObject *self, PyObject *args)
+{
+ PyObject *seq, *value;
+ if (!PyArg_ParseTuple(args, "OO", &seq, &value)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+ NULLABLE(value);
+
+ RETURN_INT(PySequence_Contains(seq, value));
+}
+
+static PyObject *
+sequence_index(PyObject *self, PyObject *args)
+{
+ PyObject *seq, *value;
+ if (!PyArg_ParseTuple(args, "OO", &seq, &value)) {
+ return NULL;
+ }
+ NULLABLE(seq);
+ NULLABLE(value);
+
+ RETURN_SIZE(PySequence_Index(seq, value));
+}
+
+static PyObject *
+sequence_list(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PySequence_List(obj);
+}
+
+static PyObject *
+sequence_tuple(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PySequence_Tuple(obj);
+}
+
+
+static PyMethodDef test_methods[] = {
+ {"object_getattr", object_getattr, METH_VARARGS},
+ {"object_getattrstring", object_getattrstring, METH_VARARGS},
+ {"object_getoptionalattr", object_getoptionalattr, METH_VARARGS},
+ {"object_getoptionalattrstring", object_getoptionalattrstring, METH_VARARGS},
+ {"object_hasattr", object_hasattr, METH_VARARGS},
+ {"object_hasattrstring", object_hasattrstring, METH_VARARGS},
+ {"object_setattr", object_setattr, METH_VARARGS},
+ {"object_setattrstring", object_setattrstring, METH_VARARGS},
+ {"object_delattr", object_delattr, METH_VARARGS},
+ {"object_delattrstring", object_delattrstring, METH_VARARGS},
+
+ {"mapping_check", mapping_check, METH_O},
+ {"mapping_size", mapping_size, METH_O},
+ {"mapping_length", mapping_length, METH_O},
+ {"object_getitem", object_getitem, METH_VARARGS},
+ {"mapping_getitemstring", mapping_getitemstring, METH_VARARGS},
+ {"mapping_getoptionalitem", mapping_getoptionalitem, METH_VARARGS},
+ {"mapping_getoptionalitemstring", mapping_getoptionalitemstring, METH_VARARGS},
+ {"mapping_haskey", mapping_haskey, METH_VARARGS},
+ {"mapping_haskeystring", mapping_haskeystring, METH_VARARGS},
+ {"object_setitem", object_setitem, METH_VARARGS},
+ {"mapping_setitemstring", mapping_setitemstring, METH_VARARGS},
+ {"object_delitem", object_delitem, METH_VARARGS},
+ {"mapping_delitem", mapping_delitem, METH_VARARGS},
+ {"mapping_delitemstring", mapping_delitemstring, METH_VARARGS},
+ {"mapping_keys", mapping_keys, METH_O},
+ {"mapping_values", mapping_values, METH_O},
+ {"mapping_items", mapping_items, METH_O},
+
+ {"sequence_check", sequence_check, METH_O},
+ {"sequence_size", sequence_size, METH_O},
+ {"sequence_length", sequence_length, METH_O},
+ {"sequence_concat", sequence_concat, METH_VARARGS},
+ {"sequence_repeat", sequence_repeat, METH_VARARGS},
+ {"sequence_inplaceconcat", sequence_inplaceconcat, METH_VARARGS},
+ {"sequence_inplacerepeat", sequence_inplacerepeat, METH_VARARGS},
+ {"sequence_getitem", sequence_getitem, METH_VARARGS},
+ {"sequence_setitem", sequence_setitem, METH_VARARGS},
+ {"sequence_delitem", sequence_delitem, METH_VARARGS},
+ {"sequence_setslice", sequence_setslice, METH_VARARGS},
+ {"sequence_delslice", sequence_delslice, METH_VARARGS},
+ {"sequence_count", sequence_count, METH_VARARGS},
+ {"sequence_contains", sequence_contains, METH_VARARGS},
+ {"sequence_index", sequence_index, METH_VARARGS},
+ {"sequence_list", sequence_list, METH_O},
+ {"sequence_tuple", sequence_tuple, METH_O},
+
+ {NULL},
+};
+
+int
+_PyTestCapi_Init_Abstract(PyObject *m)
+{
+ if (PyModule_AddFunctions(m, test_methods) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/Modules/_testcapi/dict.c b/Modules/_testcapi/dict.c
new file mode 100644
index 0000000..b1dfcf4
--- /dev/null
+++ b/Modules/_testcapi/dict.c
@@ -0,0 +1,376 @@
+#include <stddef.h> // ptrdiff_t
+
+#include "parts.h"
+
+#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
+
+#define RETURN_INT(value) do { \
+ int _ret = (value); \
+ if (_ret == -1) { \
+ return NULL; \
+ } \
+ return PyLong_FromLong(_ret); \
+ } while (0)
+
+#define RETURN_SIZE(value) do { \
+ Py_ssize_t _ret = (value); \
+ if (_ret == -1) { \
+ return NULL; \
+ } \
+ return PyLong_FromSsize_t(_ret); \
+ } while (0)
+
+
+static PyObject *
+dict_check(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyLong_FromLong(PyDict_Check(obj));
+}
+
+static PyObject *
+dict_checkexact(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyLong_FromLong(PyDict_CheckExact(obj));
+}
+
+static PyObject *
+dict_new(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return PyDict_New();
+}
+
+static PyObject *
+dictproxy_new(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyDictProxy_New(obj);
+}
+
+static PyObject *
+dict_clear(PyObject *self, PyObject *obj)
+{
+ PyDict_Clear(obj);
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+dict_copy(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyDict_Copy(obj);
+}
+
+static PyObject *
+dict_contains(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *key;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &key)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(key);
+ RETURN_INT(PyDict_Contains(obj, key));
+}
+
+static PyObject *
+dict_size(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_SIZE(PyDict_Size(obj));
+}
+
+static PyObject *
+dict_getitem(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ PyObject *value = PyDict_GetItem(mapping, key);
+ if (value == NULL) {
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+ return Py_NewRef(PyExc_KeyError);
+ }
+ return Py_NewRef(value);
+}
+
+static PyObject *
+dict_getitemstring(PyObject *self, PyObject *args)
+{
+ PyObject *mapping;
+ const char *key;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &mapping, &key, &size)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ PyObject *value = PyDict_GetItemString(mapping, key);
+ if (value == NULL) {
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+ return Py_NewRef(PyExc_KeyError);
+ }
+ return Py_NewRef(value);
+}
+
+static PyObject *
+dict_getitemwitherror(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ PyObject *value = PyDict_GetItemWithError(mapping, key);
+ if (value == NULL) {
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+ return Py_NewRef(PyExc_KeyError);
+ }
+ return Py_NewRef(value);
+}
+
+
+static PyObject *
+dict_getitemref(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *attr_name, *value;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(attr_name);
+
+ switch (PyDict_GetItemRef(obj, attr_name, &value)) {
+ case -1:
+ assert(value == NULL);
+ return NULL;
+ case 0:
+ assert(value == NULL);
+ return Py_NewRef(PyExc_KeyError);
+ case 1:
+ return value;
+ default:
+ Py_FatalError("PyMapping_GetItemRef() returned invalid code");
+ Py_UNREACHABLE();
+ }
+}
+
+static PyObject *
+dict_getitemstringref(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *value;
+ const char *attr_name;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+
+ switch (PyDict_GetItemStringRef(obj, attr_name, &value)) {
+ case -1:
+ assert(value == NULL);
+ return NULL;
+ case 0:
+ assert(value == NULL);
+ return Py_NewRef(PyExc_KeyError);
+ case 1:
+ return value;
+ default:
+ Py_FatalError("PyDict_GetItemStringRef() returned invalid code");
+ Py_UNREACHABLE();
+ }
+}
+
+static PyObject *
+dict_setitem(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key, *value;
+ if (!PyArg_ParseTuple(args, "OOO", &mapping, &key, &value)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ NULLABLE(value);
+ RETURN_INT(PyDict_SetItem(mapping, key, value));
+}
+
+static PyObject *
+dict_setitemstring(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *value;
+ const char *key;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#O", &mapping, &key, &size, &value)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(value);
+ RETURN_INT(PyDict_SetItemString(mapping, key, value));
+}
+
+static PyObject *
+dict_setdefault(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key, *defaultobj;
+ if (!PyArg_ParseTuple(args, "OOO", &mapping, &key, &defaultobj)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ NULLABLE(defaultobj);
+ return PyDict_SetDefault(mapping, key, defaultobj);
+}
+
+static PyObject *
+dict_delitem(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &key)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(key);
+ RETURN_INT(PyDict_DelItem(mapping, key));
+}
+
+static PyObject *
+dict_delitemstring(PyObject *self, PyObject *args)
+{
+ PyObject *mapping;
+ const char *key;
+ Py_ssize_t size;
+ if (!PyArg_ParseTuple(args, "Oz#", &mapping, &key, &size)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ RETURN_INT(PyDict_DelItemString(mapping, key));
+}
+
+static PyObject *
+dict_keys(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyDict_Keys(obj);
+}
+
+static PyObject *
+dict_values(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyDict_Values(obj);
+}
+
+static PyObject *
+dict_items(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyDict_Items(obj);
+}
+
+static PyObject *
+dict_next(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *key, *value;
+ Py_ssize_t pos;
+ if (!PyArg_ParseTuple(args, "On", &mapping, &pos)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ int rc = PyDict_Next(mapping, &pos, &key, &value);
+ if (rc != 0) {
+ return Py_BuildValue("inOO", rc, pos, key, value);
+ }
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+dict_merge(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *mapping2;
+ int override;
+ if (!PyArg_ParseTuple(args, "OOi", &mapping, &mapping2, &override)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(mapping2);
+ RETURN_INT(PyDict_Merge(mapping, mapping2, override));
+}
+
+static PyObject *
+dict_update(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *mapping2;
+ if (!PyArg_ParseTuple(args, "OO", &mapping, &mapping2)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(mapping2);
+ RETURN_INT(PyDict_Update(mapping, mapping2));
+}
+
+static PyObject *
+dict_mergefromseq2(PyObject *self, PyObject *args)
+{
+ PyObject *mapping, *seq;
+ int override;
+ if (!PyArg_ParseTuple(args, "OOi", &mapping, &seq, &override)) {
+ return NULL;
+ }
+ NULLABLE(mapping);
+ NULLABLE(seq);
+ RETURN_INT(PyDict_MergeFromSeq2(mapping, seq, override));
+}
+
+
+static PyMethodDef test_methods[] = {
+ {"dict_check", dict_check, METH_O},
+ {"dict_checkexact", dict_checkexact, METH_O},
+ {"dict_new", dict_new, METH_NOARGS},
+ {"dictproxy_new", dictproxy_new, METH_O},
+ {"dict_clear", dict_clear, METH_O},
+ {"dict_copy", dict_copy, METH_O},
+ {"dict_size", dict_size, METH_O},
+ {"dict_getitem", dict_getitem, METH_VARARGS},
+ {"dict_getitemwitherror", dict_getitemwitherror, METH_VARARGS},
+ {"dict_getitemstring", dict_getitemstring, METH_VARARGS},
+ {"dict_getitemref", dict_getitemref, METH_VARARGS},
+ {"dict_getitemstringref", dict_getitemstringref, METH_VARARGS},
+ {"dict_contains", dict_contains, METH_VARARGS},
+ {"dict_setitem", dict_setitem, METH_VARARGS},
+ {"dict_setitemstring", dict_setitemstring, METH_VARARGS},
+ {"dict_delitem", dict_delitem, METH_VARARGS},
+ {"dict_delitemstring", dict_delitemstring, METH_VARARGS},
+ {"dict_setdefault", dict_setdefault, METH_VARARGS},
+ {"dict_keys", dict_keys, METH_O},
+ {"dict_values", dict_values, METH_O},
+ {"dict_items", dict_items, METH_O},
+ {"dict_next", dict_next, METH_VARARGS},
+ {"dict_merge", dict_merge, METH_VARARGS},
+ {"dict_update", dict_update, METH_VARARGS},
+ {"dict_mergefromseq2", dict_mergefromseq2, METH_VARARGS},
+
+ {NULL},
+};
+
+int
+_PyTestCapi_Init_Dict(PyObject *m)
+{
+ if (PyModule_AddFunctions(m, test_methods) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h
index aaec0a6..65ebf80 100644
--- a/Modules/_testcapi/parts.h
+++ b/Modules/_testcapi/parts.h
@@ -26,6 +26,7 @@
int _PyTestCapi_Init_Vectorcall(PyObject *module);
int _PyTestCapi_Init_Heaptype(PyObject *module);
+int _PyTestCapi_Init_Abstract(PyObject *module);
int _PyTestCapi_Init_Unicode(PyObject *module);
int _PyTestCapi_Init_GetArgs(PyObject *module);
int _PyTestCapi_Init_DateTime(PyObject *module);
@@ -34,6 +35,7 @@ int _PyTestCapi_Init_Mem(PyObject *module);
int _PyTestCapi_Init_Watchers(PyObject *module);
int _PyTestCapi_Init_Long(PyObject *module);
int _PyTestCapi_Init_Float(PyObject *module);
+int _PyTestCapi_Init_Dict(PyObject *module);
int _PyTestCapi_Init_Structmember(PyObject *module);
int _PyTestCapi_Init_Exceptions(PyObject *module);
int _PyTestCapi_Init_Code(PyObject *module);