| 1 |
#include "parts.h" |
| 2 |
#include "util.h" |
| 3 |
|
| 4 |
|
| 5 |
static PyType_Slot HeapTypeNameType_slots[] = { |
| 6 |
{0}, |
| 7 |
}; |
| 8 |
|
| 9 |
static PyType_Spec HeapTypeNameType_Spec = { |
| 10 |
.name = "_testcapi.HeapTypeNameType", |
| 11 |
.basicsize = sizeof(PyObject), |
| 12 |
.flags = Py_TPFLAGS_DEFAULT, |
| 13 |
.slots = HeapTypeNameType_slots, |
| 14 |
}; |
| 15 |
|
| 16 |
static PyObject * |
| 17 |
get_heaptype_for_name(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 18 |
{ |
| 19 |
return PyType_FromSpec(&HeapTypeNameType_Spec); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
static PyObject * |
| 24 |
get_type_name(PyObject *self, PyObject *type) |
| 25 |
{ |
| 26 |
assert(PyType_Check(type)); |
| 27 |
return PyType_GetName((PyTypeObject *)type); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
static PyObject * |
| 32 |
get_type_qualname(PyObject *self, PyObject *type) |
| 33 |
{ |
| 34 |
assert(PyType_Check(type)); |
| 35 |
return PyType_GetQualName((PyTypeObject *)type); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
static PyObject * |
| 40 |
get_type_fullyqualname(PyObject *self, PyObject *type) |
| 41 |
{ |
| 42 |
assert(PyType_Check(type)); |
| 43 |
return PyType_GetFullyQualifiedName((PyTypeObject *)type); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
static PyObject * |
| 48 |
get_type_module_name(PyObject *self, PyObject *type) |
| 49 |
{ |
| 50 |
assert(PyType_Check(type)); |
| 51 |
return PyType_GetModuleName((PyTypeObject *)type); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
static PyObject * |
| 56 |
test_get_type_dict(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 57 |
{ |
| 58 |
/* Test for PyType_GetDict */ |
| 59 |
|
| 60 |
// Assert ints have a `to_bytes` method |
| 61 |
PyObject *long_dict = PyType_GetDict(&PyLong_Type); |
| 62 |
assert(long_dict); |
| 63 |
assert(PyDict_GetItemString(long_dict, "to_bytes")); // borrowed ref |
| 64 |
Py_DECREF(long_dict); |
| 65 |
|
| 66 |
// Make a new type, add an attribute to it and assert it's there |
| 67 |
PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec); |
| 68 |
assert(HeapTypeNameType); |
| 69 |
assert(PyObject_SetAttrString( |
| 70 |
HeapTypeNameType, "new_attr", Py_NewRef(Py_None)) >= 0); |
| 71 |
PyObject *type_dict = PyType_GetDict((PyTypeObject*)HeapTypeNameType); |
| 72 |
assert(type_dict); |
| 73 |
assert(PyDict_GetItemString(type_dict, "new_attr")); // borrowed ref |
| 74 |
Py_DECREF(HeapTypeNameType); |
| 75 |
Py_DECREF(type_dict); |
| 76 |
Py_RETURN_NONE; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
static PyObject * |
| 81 |
test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 82 |
{ |
| 83 |
newfunc tp_new = PyType_GetSlot(&PyLong_Type, Py_tp_new); |
| 84 |
if (PyLong_Type.tp_new != tp_new) { |
| 85 |
PyErr_SetString(PyExc_AssertionError, "mismatch: tp_new of long"); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
reprfunc tp_repr = PyType_GetSlot(&PyLong_Type, Py_tp_repr); |
| 90 |
if (PyLong_Type.tp_repr != tp_repr) { |
| 91 |
PyErr_SetString(PyExc_AssertionError, "mismatch: tp_repr of long"); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
ternaryfunc tp_call = PyType_GetSlot(&PyLong_Type, Py_tp_call); |
| 96 |
if (tp_call != NULL) { |
| 97 |
PyErr_SetString(PyExc_AssertionError, "mismatch: tp_call of long"); |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
binaryfunc nb_add = PyType_GetSlot(&PyLong_Type, Py_nb_add); |
| 102 |
if (PyLong_Type.tp_as_number->nb_add != nb_add) { |
| 103 |
PyErr_SetString(PyExc_AssertionError, "mismatch: nb_add of long"); |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
lenfunc mp_length = PyType_GetSlot(&PyLong_Type, Py_mp_length); |
| 108 |
if (mp_length != NULL) { |
| 109 |
PyErr_SetString(PyExc_AssertionError, "mismatch: mp_length of long"); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
void *over_value = PyType_GetSlot(&PyLong_Type, Py_bf_releasebuffer + 1); |
| 114 |
if (over_value != NULL) { |
| 115 |
PyErr_SetString(PyExc_AssertionError, "mismatch: max+1 of long"); |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
|
| 119 |
tp_new = PyType_GetSlot(&PyLong_Type, 0); |
| 120 |
if (tp_new != NULL) { |
| 121 |
PyErr_SetString(PyExc_AssertionError, "mismatch: slot 0 of long"); |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
if (PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 125 |
// This is the right exception |
| 126 |
PyErr_Clear(); |
| 127 |
} |
| 128 |
else { |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
Py_RETURN_NONE; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
// Get type->tp_version_tag |
| 137 |
static PyObject * |
| 138 |
type_get_version(PyObject *self, PyObject *type) |
| 139 |
{ |
| 140 |
if (!PyType_Check(type)) { |
| 141 |
PyErr_SetString(PyExc_TypeError, "argument must be a type"); |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
PyObject *res = PyLong_FromUnsignedLong( |
| 145 |
((PyTypeObject *)type)->tp_version_tag); |
| 146 |
if (res == NULL) { |
| 147 |
assert(PyErr_Occurred()); |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
return res; |
| 151 |
} |
| 152 |
|
| 153 |
static PyObject * |
| 154 |
type_modified(PyObject *self, PyObject *arg) |
| 155 |
{ |
| 156 |
if (!PyType_Check(arg)) { |
| 157 |
PyErr_SetString(PyExc_TypeError, "argument must be a type"); |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
PyTypeObject *type = (PyTypeObject*)arg; |
| 161 |
|
| 162 |
PyType_Modified(type); |
| 163 |
Py_RETURN_NONE; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
static PyObject * |
| 168 |
type_assign_version(PyObject *self, PyObject *arg) |
| 169 |
{ |
| 170 |
if (!PyType_Check(arg)) { |
| 171 |
PyErr_SetString(PyExc_TypeError, "argument must be a type"); |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
PyTypeObject *type = (PyTypeObject*)arg; |
| 175 |
|
| 176 |
int res = PyUnstable_Type_AssignVersionTag(type); |
| 177 |
return PyLong_FromLong(res); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
static PyObject * |
| 182 |
type_get_tp_bases(PyObject *self, PyObject *arg) |
| 183 |
{ |
| 184 |
if (!PyType_Check(arg)) { |
| 185 |
PyErr_SetString(PyExc_TypeError, "argument must be a type"); |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
PyTypeObject *type = (PyTypeObject*)arg; |
| 189 |
|
| 190 |
PyObject *bases = type->tp_bases; |
| 191 |
if (bases == NULL) { |
| 192 |
Py_RETURN_NONE; |
| 193 |
} |
| 194 |
return Py_NewRef(bases); |
| 195 |
} |
| 196 |
|
| 197 |
static PyObject * |
| 198 |
type_get_tp_mro(PyObject *self, PyObject *arg) |
| 199 |
{ |
| 200 |
if (!PyType_Check(arg)) { |
| 201 |
PyErr_SetString(PyExc_TypeError, "argument must be a type"); |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
PyTypeObject *type = (PyTypeObject*)arg; |
| 205 |
|
| 206 |
PyObject *mro = ((PyTypeObject *)type)->tp_mro; |
| 207 |
if (mro == NULL) { |
| 208 |
Py_RETURN_NONE; |
| 209 |
} |
| 210 |
return Py_NewRef(mro); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static PyObject * |
| 215 |
type_freeze(PyObject *module, PyObject *arg) |
| 216 |
{ |
| 217 |
if (!PyType_Check(arg)) { |
| 218 |
PyErr_SetString(PyExc_TypeError, "argument must be a type"); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
PyTypeObject *type = (PyTypeObject*)arg; |
| 222 |
|
| 223 |
if (PyType_Freeze(type) < 0) { |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
Py_RETURN_NONE; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
static PyMethodDef test_methods[] = { |
| 231 |
{"get_heaptype_for_name", get_heaptype_for_name, METH_NOARGS}, |
| 232 |
{"get_type_name", get_type_name, METH_O}, |
| 233 |
{"get_type_qualname", get_type_qualname, METH_O}, |
| 234 |
{"get_type_fullyqualname", get_type_fullyqualname, METH_O}, |
| 235 |
{"get_type_module_name", get_type_module_name, METH_O}, |
| 236 |
{"test_get_type_dict", test_get_type_dict, METH_NOARGS}, |
| 237 |
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS}, |
| 238 |
{"type_get_version", type_get_version, METH_O, PyDoc_STR("type->tp_version_tag")}, |
| 239 |
{"type_modified", type_modified, METH_O, PyDoc_STR("PyType_Modified")}, |
| 240 |
{"type_assign_version", type_assign_version, METH_O, PyDoc_STR("PyUnstable_Type_AssignVersionTag")}, |
| 241 |
{"type_get_tp_bases", type_get_tp_bases, METH_O}, |
| 242 |
{"type_get_tp_mro", type_get_tp_mro, METH_O}, |
| 243 |
{"type_freeze", type_freeze, METH_O}, |
| 244 |
{NULL}, |
| 245 |
}; |
| 246 |
|
| 247 |
int |
| 248 |
_PyTestCapi_Init_Type(PyObject *m) |
| 249 |
{ |
| 250 |
return PyModule_AddFunctions(m, test_methods); |
| 251 |
} |