|
1 | | -// FIXME: test PyObject_CallNoArgs() |
2 | | -// FIXME: test PyModule_AddType() |
3 | | -// FIXME: test PyObject_GC_IsTracked() |
4 | | -// FIXME: test PyObject_GC_IsFinalized() |
5 | | - |
6 | 1 | // Always enable assertions |
7 | 2 | #undef NDEBUG |
8 | 3 |
|
@@ -103,11 +98,82 @@ test_interpreter(PyObject *self, PyObject *ignored) |
103 | 98 | } |
104 | 99 |
|
105 | 100 |
|
| 101 | +static PyObject * |
| 102 | +test_calls(PyObject *self, PyObject *ignored) |
| 103 | +{ |
| 104 | + PyObject *func = (PyObject *)&PyUnicode_Type; |
| 105 | + |
| 106 | + // test PyObject_CallNoArgs(): str() returns an empty string |
| 107 | + PyObject *res = PyObject_CallNoArgs(func); |
| 108 | + if (res == NULL) { |
| 109 | + return NULL; |
| 110 | + } |
| 111 | + assert(PyUnicode_Check(res)); |
| 112 | + Py_DECREF(res); |
| 113 | + |
| 114 | + Py_RETURN_NONE; |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +static PyObject * |
| 119 | +test_gc(PyObject *self, PyObject *ignored) |
| 120 | +{ |
| 121 | + PyObject *tuple = PyTuple_New(1); |
| 122 | + Py_INCREF(Py_None); |
| 123 | + PyTuple_SET_ITEM(tuple, 0, Py_None); |
| 124 | + |
| 125 | + // test PyObject_GC_IsTracked() |
| 126 | + int tracked = PyObject_GC_IsTracked(tuple); |
| 127 | + assert(tracked); |
| 128 | + |
| 129 | + // test PyObject_GC_IsFinalized() |
| 130 | + int finalized = PyObject_GC_IsFinalized(tuple); |
| 131 | + assert(!finalized); |
| 132 | + |
| 133 | + Py_DECREF(tuple); |
| 134 | + Py_RETURN_NONE; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +static PyObject * |
| 139 | +test_module(PyObject *self, PyObject *ignored) |
| 140 | +{ |
| 141 | + PyObject *module = self; |
| 142 | + assert(PyModule_Check(module)); |
| 143 | + |
| 144 | + // test PyModule_AddType() |
| 145 | + PyTypeObject *type = &PyUnicode_Type; |
| 146 | + Py_ssize_t refcnt = Py_REFCNT(type); |
| 147 | + |
| 148 | + if (PyModule_AddType(module, type) < 0) { |
| 149 | + return NULL; |
| 150 | + } |
| 151 | + assert(Py_REFCNT(type) == refcnt + 1); |
| 152 | + |
| 153 | + PyObject *attr = PyObject_GetAttrString(module, "str"); |
| 154 | + if (attr == NULL) { |
| 155 | + return NULL; |
| 156 | + } |
| 157 | + assert(attr == (PyObject *)type); |
| 158 | + Py_DECREF(attr); |
| 159 | + |
| 160 | + if (PyObject_DelAttrString(module, "str") < 0) { |
| 161 | + return NULL; |
| 162 | + } |
| 163 | + assert(Py_REFCNT(type) == refcnt); |
| 164 | + |
| 165 | + Py_RETURN_NONE; |
| 166 | +} |
| 167 | + |
| 168 | + |
106 | 169 | static struct PyMethodDef methods[] = { |
107 | 170 | {"test_set_funcs", test_set_funcs, METH_NOARGS}, |
108 | 171 | {"test_frame", test_frame, METH_NOARGS}, |
109 | 172 | {"test_thread_state", test_thread_state, METH_NOARGS}, |
110 | 173 | {"test_interpreter", test_interpreter, METH_NOARGS}, |
| 174 | + {"test_calls", test_calls, METH_NOARGS}, |
| 175 | + {"test_gc", test_gc, METH_NOARGS}, |
| 176 | + {"test_module", test_module, METH_NOARGS}, |
111 | 177 | {NULL, NULL} |
112 | 178 | }; |
113 | 179 |
|
|
0 commit comments