| 1 | #include "parts.h" |
| 2 | #include "util.h" |
| 3 | |
| 4 | |
| 5 | static PyObject * |
| 6 | object_getoptionalattr(PyObject *self, PyObject *args) |
| 7 | { |
| 8 | PyObject *obj, *attr_name, *value = UNINITIALIZED_PTR; |
| 9 | if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) { |
| 10 | return NULL; |
| 11 | } |
| 12 | NULLABLE(obj); |
| 13 | NULLABLE(attr_name); |
| 14 | |
| 15 | switch (PyObject_GetOptionalAttr(obj, attr_name, &value)) { |
| 16 | case -1: |
| 17 | assert(value == NULL); |
| 18 | return NULL; |
| 19 | case 0: |
| 20 | assert(value == NULL); |
| 21 | return Py_NewRef(PyExc_AttributeError); |
| 22 | case 1: |
| 23 | return value; |
| 24 | default: |
| 25 | Py_FatalError("PyObject_GetOptionalAttr() returned invalid code"); |
| 26 | Py_UNREACHABLE(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | static PyObject * |
| 31 | object_getoptionalattrstring(PyObject *self, PyObject *args) |
| 32 | { |
| 33 | PyObject *obj, *value = UNINITIALIZED_PTR; |
| 34 | const char *attr_name; |
| 35 | Py_ssize_t size; |
| 36 | if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) { |
| 37 | return NULL; |
| 38 | } |
| 39 | NULLABLE(obj); |
| 40 | |
| 41 | switch (PyObject_GetOptionalAttrString(obj, attr_name, &value)) { |
| 42 | case -1: |
| 43 | assert(value == NULL); |
| 44 | return NULL; |
| 45 | case 0: |
| 46 | assert(value == NULL); |
| 47 | return Py_NewRef(PyExc_AttributeError); |
| 48 | case 1: |
| 49 | return value; |
| 50 | default: |
| 51 | Py_FatalError("PyObject_GetOptionalAttrString() returned invalid code"); |
| 52 | Py_UNREACHABLE(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static PyObject * |
| 57 | object_hasattrwitherror(PyObject *self, PyObject *args) |
| 58 | { |
| 59 | PyObject *obj, *attr_name; |
| 60 | if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) { |
| 61 | return NULL; |
| 62 | } |
| 63 | NULLABLE(obj); |
| 64 | NULLABLE(attr_name); |
| 65 | RETURN_INT(PyObject_HasAttrWithError(obj, attr_name)); |
| 66 | } |
| 67 | |
| 68 | static PyObject * |
| 69 | object_hasattrstringwitherror(PyObject *self, PyObject *args) |
| 70 | { |
| 71 | PyObject *obj; |
| 72 | const char *attr_name; |
| 73 | Py_ssize_t size; |
| 74 | if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) { |
| 75 | return NULL; |
| 76 | } |
| 77 | NULLABLE(obj); |
| 78 | RETURN_INT(PyObject_HasAttrStringWithError(obj, attr_name)); |
| 79 | } |
| 80 | |
| 81 | static PyObject * |
| 82 | mapping_getoptionalitemstring(PyObject *self, PyObject *args) |
| 83 | { |
| 84 | PyObject *obj, *value = UNINITIALIZED_PTR; |
| 85 | const char *attr_name; |
| 86 | Py_ssize_t size; |
| 87 | if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) { |
| 88 | return NULL; |
| 89 | } |
| 90 | NULLABLE(obj); |
| 91 | |
| 92 | switch (PyMapping_GetOptionalItemString(obj, attr_name, &value)) { |
| 93 | case -1: |
| 94 | assert(value == NULL); |
| 95 | return NULL; |
| 96 | case 0: |
| 97 | assert(value == NULL); |
| 98 | return Py_NewRef(PyExc_KeyError); |
| 99 | case 1: |
| 100 | return value; |
| 101 | default: |
| 102 | Py_FatalError("PyMapping_GetOptionalItemString() returned invalid code"); |
| 103 | Py_UNREACHABLE(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static PyObject * |
| 108 | mapping_getoptionalitem(PyObject *self, PyObject *args) |
| 109 | { |
| 110 | PyObject *obj, *attr_name, *value = UNINITIALIZED_PTR; |
| 111 | if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) { |
| 112 | return NULL; |
| 113 | } |
| 114 | NULLABLE(obj); |
| 115 | NULLABLE(attr_name); |
| 116 | |
| 117 | switch (PyMapping_GetOptionalItem(obj, attr_name, &value)) { |
| 118 | case -1: |
| 119 | assert(value == NULL); |
| 120 | return NULL; |
| 121 | case 0: |
| 122 | assert(value == NULL); |
| 123 | return Py_NewRef(PyExc_KeyError); |
| 124 | case 1: |
| 125 | return value; |
| 126 | default: |
| 127 | Py_FatalError("PyMapping_GetOptionalItem() returned invalid code"); |
| 128 | Py_UNREACHABLE(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static PyObject * |
| 133 | pyiter_next(PyObject *self, PyObject *iter) |
| 134 | { |
| 135 | PyObject *item = PyIter_Next(iter); |
| 136 | if (item == NULL && !PyErr_Occurred()) { |
| 137 | Py_RETURN_NONE; |
| 138 | } |
| 139 | return item; |
| 140 | } |
| 141 | |
| 142 | static PyObject * |
| 143 | pyiter_nextitem(PyObject *self, PyObject *iter) |
| 144 | { |
| 145 | PyObject *item; |
| 146 | int rc = PyIter_NextItem(iter, &item); |
| 147 | if (rc < 0) { |
| 148 | assert(PyErr_Occurred()); |
| 149 | assert(item == NULL); |
| 150 | return NULL; |
| 151 | } |
| 152 | assert(!PyErr_Occurred()); |
| 153 | if (item == NULL) { |
| 154 | Py_RETURN_NONE; |
| 155 | } |
| 156 | return item; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static PyObject * |
| 161 | sequence_fast_get_size(PyObject *self, PyObject *obj) |
| 162 | { |
| 163 | NULLABLE(obj); |
| 164 | return PyLong_FromSsize_t(PySequence_Fast_GET_SIZE(obj)); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static PyObject * |
| 169 | sequence_fast_get_item(PyObject *self, PyObject *args) |
| 170 | { |
| 171 | PyObject *obj; |
| 172 | Py_ssize_t index; |
| 173 | if (!PyArg_ParseTuple(args, "On", &obj, &index)) { |
| 174 | return NULL; |
| 175 | } |
| 176 | NULLABLE(obj); |
| 177 | return PySequence_Fast_GET_ITEM(obj, index); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | static PyMethodDef test_methods[] = { |
| 182 | {"object_getoptionalattr", object_getoptionalattr, METH_VARARGS}, |
| 183 | {"object_getoptionalattrstring", object_getoptionalattrstring, METH_VARARGS}, |
| 184 | {"object_hasattrwitherror", object_hasattrwitherror, METH_VARARGS}, |
| 185 | {"object_hasattrstringwitherror", object_hasattrstringwitherror, METH_VARARGS}, |
| 186 | {"mapping_getoptionalitem", mapping_getoptionalitem, METH_VARARGS}, |
| 187 | {"mapping_getoptionalitemstring", mapping_getoptionalitemstring, METH_VARARGS}, |
| 188 | |
| 189 | {"PyIter_Next", pyiter_next, METH_O}, |
| 190 | {"PyIter_NextItem", pyiter_nextitem, METH_O}, |
| 191 | |
| 192 | {"sequence_fast_get_size", sequence_fast_get_size, METH_O}, |
| 193 | {"sequence_fast_get_item", sequence_fast_get_item, METH_VARARGS}, |
| 194 | {NULL}, |
| 195 | }; |
| 196 | |
| 197 | int |
| 198 | _PyTestCapi_Init_Abstract(PyObject *m) |
| 199 | { |
| 200 | if (PyModule_AddFunctions(m, test_methods) < 0) { |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |