2023-08-07 12:51:43 -03:00
|
|
|
#include "parts.h"
|
2023-09-01 15:42:42 -03:00
|
|
|
#include "util.h"
|
2023-08-07 12:51:43 -03:00
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
object_getoptionalattr(PyObject *self, PyObject *args)
|
|
|
|
{
|
2023-09-06 16:02:01 -03:00
|
|
|
PyObject *obj, *attr_name, *value = UNINITIALIZED_PTR;
|
2023-08-07 12:51:43 -03:00
|
|
|
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)
|
|
|
|
{
|
2023-09-06 16:02:01 -03:00
|
|
|
PyObject *obj, *value = UNINITIALIZED_PTR;
|
2023-08-07 12:51:43 -03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 08:23:31 -03:00
|
|
|
static PyObject *
|
|
|
|
object_hasattrwitherror(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_HasAttrWithError(obj, attr_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
object_hasattrstringwitherror(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_HasAttrStringWithError(obj, attr_name));
|
|
|
|
}
|
|
|
|
|
2023-08-07 12:51:43 -03:00
|
|
|
static PyObject *
|
2024-03-19 07:44:13 -03:00
|
|
|
mapping_getoptionalitemstring(PyObject *self, PyObject *args)
|
2023-08-07 12:51:43 -03:00
|
|
|
{
|
2024-03-19 07:44:13 -03:00
|
|
|
PyObject *obj, *value = UNINITIALIZED_PTR;
|
2023-08-07 12:51:43 -03:00
|
|
|
const char *attr_name;
|
|
|
|
Py_ssize_t size;
|
|
|
|
if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
NULLABLE(obj);
|
|
|
|
|
2024-03-19 07:44:13 -03:00
|
|
|
switch (PyMapping_GetOptionalItemString(obj, attr_name, &value)) {
|
2023-08-07 12:51:43 -03:00
|
|
|
case -1:
|
|
|
|
assert(value == NULL);
|
|
|
|
return NULL;
|
|
|
|
case 0:
|
|
|
|
assert(value == NULL);
|
|
|
|
return Py_NewRef(PyExc_KeyError);
|
|
|
|
case 1:
|
|
|
|
return value;
|
|
|
|
default:
|
2024-03-19 07:44:13 -03:00
|
|
|
Py_FatalError("PyMapping_GetOptionalItemString() returned invalid code");
|
2023-08-07 12:51:43 -03:00
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2024-03-19 07:44:13 -03:00
|
|
|
mapping_getoptionalitem(PyObject *self, PyObject *args)
|
2023-08-07 12:51:43 -03:00
|
|
|
{
|
2024-03-19 07:44:13 -03:00
|
|
|
PyObject *obj, *attr_name, *value = UNINITIALIZED_PTR;
|
|
|
|
if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
|
2023-08-07 12:51:43 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
NULLABLE(obj);
|
2024-03-19 07:44:13 -03:00
|
|
|
NULLABLE(attr_name);
|
2023-08-07 12:51:43 -03:00
|
|
|
|
2024-03-19 07:44:13 -03:00
|
|
|
switch (PyMapping_GetOptionalItem(obj, attr_name, &value)) {
|
2023-08-07 12:51:43 -03:00
|
|
|
case -1:
|
|
|
|
assert(value == NULL);
|
|
|
|
return NULL;
|
|
|
|
case 0:
|
|
|
|
assert(value == NULL);
|
|
|
|
return Py_NewRef(PyExc_KeyError);
|
|
|
|
case 1:
|
|
|
|
return value;
|
|
|
|
default:
|
2024-03-19 07:44:13 -03:00
|
|
|
Py_FatalError("PyMapping_GetOptionalItem() returned invalid code");
|
2023-08-07 12:51:43 -03:00
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef test_methods[] = {
|
|
|
|
{"object_getoptionalattr", object_getoptionalattr, METH_VARARGS},
|
|
|
|
{"object_getoptionalattrstring", object_getoptionalattrstring, METH_VARARGS},
|
2023-09-17 08:23:31 -03:00
|
|
|
{"object_hasattrwitherror", object_hasattrwitherror, METH_VARARGS},
|
|
|
|
{"object_hasattrstringwitherror", object_hasattrstringwitherror, METH_VARARGS},
|
2023-08-07 12:51:43 -03:00
|
|
|
{"mapping_getoptionalitem", mapping_getoptionalitem, METH_VARARGS},
|
|
|
|
{"mapping_getoptionalitemstring", mapping_getoptionalitemstring, METH_VARARGS},
|
|
|
|
|
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyTestCapi_Init_Abstract(PyObject *m)
|
|
|
|
{
|
|
|
|
if (PyModule_AddFunctions(m, test_methods) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|