| 1 | // gh-91321: Very basic C++ test extension to check that the Python C API is |
| 2 | // compatible with C++ and does not emit C++ compiler warnings. |
| 3 | // |
| 4 | // The code is only built, not executed. |
| 5 | |
| 6 | // Always enable assertions |
| 7 | #undef NDEBUG |
| 8 | |
| 9 | #include "Python.h" |
| 10 | |
| 11 | #if __cplusplus >= 201103 |
| 12 | # define NAME _testcpp11ext |
| 13 | #else |
| 14 | # define NAME _testcpp03ext |
| 15 | #endif |
| 16 | |
| 17 | #define _STR(NAME) #NAME |
| 18 | #define STR(NAME) _STR(NAME) |
| 19 | |
| 20 | PyDoc_STRVAR(_testcppext_add_doc, |
| 21 | "add(x, y)\n" |
| 22 | "\n" |
| 23 | "Return the sum of two integers: x + y."); |
| 24 | |
| 25 | static PyObject * |
| 26 | _testcppext_add(PyObject *Py_UNUSED(module), PyObject *args) |
| 27 | { |
| 28 | long i, j; |
| 29 | if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) { |
| 30 | return _Py_NULL; |
| 31 | } |
| 32 | long res = i + j; |
| 33 | return PyLong_FromLong(res); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | // Class to test operator casting an object to PyObject* |
| 38 | class StrongRef |
| 39 | { |
| 40 | public: |
| 41 | StrongRef(PyObject *obj) : m_obj(obj) { |
| 42 | Py_INCREF(this->m_obj); |
| 43 | } |
| 44 | |
| 45 | ~StrongRef() { |
| 46 | Py_DECREF(this->m_obj); |
| 47 | } |
| 48 | |
| 49 | // Cast to PyObject*: get a borrowed reference |
| 50 | inline operator PyObject*() const { return this->m_obj; } |
| 51 | |
| 52 | private: |
| 53 | PyObject *m_obj; // Strong reference |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | static PyObject * |
| 58 | test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 59 | { |
| 60 | PyObject *obj = Py_BuildValue("(ii)", 1, 2); |
| 61 | if (obj == _Py_NULL) { |
| 62 | return _Py_NULL; |
| 63 | } |
| 64 | Py_ssize_t refcnt = Py_REFCNT(obj); |
| 65 | assert(refcnt >= 1); |
| 66 | |
| 67 | // gh-92138: For backward compatibility, functions of Python C API accepts |
| 68 | // "const PyObject*". Check that using it does not emit C++ compiler |
| 69 | // warnings. |
| 70 | const PyObject *const_obj = obj; |
| 71 | Py_INCREF(const_obj); |
| 72 | Py_DECREF(const_obj); |
| 73 | PyTypeObject *type = Py_TYPE(const_obj); |
| 74 | assert(Py_REFCNT(const_obj) == refcnt); |
| 75 | assert(type == &PyTuple_Type); |
| 76 | assert(PyTuple_GET_SIZE(const_obj) == 2); |
| 77 | PyObject *one = PyTuple_GET_ITEM(const_obj, 0); |
| 78 | assert(PyLong_AsLong(one) == 1); |
| 79 | |
| 80 | // gh-92898: StrongRef doesn't inherit from PyObject but has an operator to |
| 81 | // cast to PyObject*. |
| 82 | StrongRef strong_ref(obj); |
| 83 | assert(Py_TYPE(strong_ref) == &PyTuple_Type); |
| 84 | assert(Py_REFCNT(strong_ref) == (refcnt + 1)); |
| 85 | Py_INCREF(strong_ref); |
| 86 | Py_DECREF(strong_ref); |
| 87 | |
| 88 | // gh-93442: Pass 0 as NULL for PyObject* |
| 89 | Py_XINCREF(0); |
| 90 | Py_XDECREF(0); |
| 91 | #if _cplusplus >= 201103 |
| 92 | // Test nullptr passed as PyObject* |
| 93 | Py_XINCREF(nullptr); |
| 94 | Py_XDECREF(nullptr); |
| 95 | #endif |
| 96 | |
| 97 | Py_DECREF(obj); |
| 98 | Py_RETURN_NONE; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static PyObject * |
| 103 | test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 104 | { |
| 105 | PyObject *str = PyUnicode_FromString("abc"); |
| 106 | if (str == _Py_NULL) { |
| 107 | return _Py_NULL; |
| 108 | } |
| 109 | |
| 110 | assert(PyUnicode_Check(str)); |
| 111 | assert(PyUnicode_GET_LENGTH(str) == 3); |
| 112 | |
| 113 | // gh-92800: test PyUnicode_READ() |
| 114 | const void* data = PyUnicode_DATA(str); |
| 115 | assert(data != _Py_NULL); |
| 116 | int kind = PyUnicode_KIND(str); |
| 117 | assert(kind == PyUnicode_1BYTE_KIND); |
| 118 | assert(PyUnicode_READ(kind, data, 0) == 'a'); |
| 119 | |
| 120 | // gh-92800: test PyUnicode_READ() casts |
| 121 | const void* const_data = PyUnicode_DATA(str); |
| 122 | unsigned int ukind = static_cast<unsigned int>(kind); |
| 123 | assert(PyUnicode_READ(ukind, const_data, 2) == 'c'); |
| 124 | |
| 125 | assert(PyUnicode_READ_CHAR(str, 1) == 'b'); |
| 126 | |
| 127 | Py_DECREF(str); |
| 128 | Py_RETURN_NONE; |
| 129 | } |
| 130 | |
| 131 | /* Test a `new`-allocated object with a virtual method. |
| 132 | * (https://github.com/python/cpython/issues/94731) */ |
| 133 | |
| 134 | class VirtualPyObject : public PyObject { |
| 135 | public: |
| 136 | VirtualPyObject(); |
| 137 | virtual ~VirtualPyObject() { |
| 138 | delete [] internal_data; |
| 139 | --instance_count; |
| 140 | } |
| 141 | virtual void set_internal_data() { |
| 142 | internal_data[0] = 1; |
| 143 | } |
| 144 | static void dealloc(PyObject* o) { |
| 145 | delete static_cast<VirtualPyObject*>(o); |
| 146 | } |
| 147 | |
| 148 | // Number of "living" instances |
| 149 | static int instance_count; |
| 150 | private: |
| 151 | // buffer that can get corrupted |
| 152 | int* internal_data; |
| 153 | }; |
| 154 | |
| 155 | int VirtualPyObject::instance_count = 0; |
| 156 | |
| 157 | PyType_Slot VirtualPyObject_Slots[] = { |
| 158 | {Py_tp_free, (void*)VirtualPyObject::dealloc}, |
| 159 | {0, _Py_NULL}, |
| 160 | }; |
| 161 | |
| 162 | PyType_Spec VirtualPyObject_Spec = { |
| 163 | /* .name */ STR(NAME) ".VirtualPyObject", |
| 164 | /* .basicsize */ sizeof(VirtualPyObject), |
| 165 | /* .itemsize */ 0, |
| 166 | /* .flags */ Py_TPFLAGS_DEFAULT, |
| 167 | /* .slots */ VirtualPyObject_Slots, |
| 168 | }; |
| 169 | |
| 170 | VirtualPyObject::VirtualPyObject() { |
| 171 | // Create a temporary type (just so we don't need to store it) |
| 172 | PyObject *type = PyType_FromSpec(&VirtualPyObject_Spec); |
| 173 | // no good way to signal failure from a C++ constructor, so use assert |
| 174 | // for error handling |
| 175 | assert(type); |
| 176 | assert(PyObject_Init(this, (PyTypeObject *)type)); |
| 177 | Py_DECREF(type); |
| 178 | internal_data = new int[50]; |
| 179 | ++instance_count; |
| 180 | } |
| 181 | |
| 182 | static PyObject * |
| 183 | test_virtual_object(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 184 | { |
| 185 | VirtualPyObject* obj = new VirtualPyObject(); |
| 186 | obj->set_internal_data(); |
| 187 | Py_DECREF(obj); |
| 188 | if (VirtualPyObject::instance_count != 0) { |
| 189 | return PyErr_Format( |
| 190 | PyExc_AssertionError, |
| 191 | "instance_count should be 0, got %d", |
| 192 | VirtualPyObject::instance_count); |
| 193 | } |
| 194 | Py_RETURN_NONE; |
| 195 | } |
| 196 | |
| 197 | static PyMethodDef _testcppext_methods[] = { |
| 198 | {"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc}, |
| 199 | {"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL}, |
| 200 | {"test_unicode", test_unicode, METH_NOARGS, _Py_NULL}, |
| 201 | {"test_virtual_object", test_virtual_object, METH_NOARGS, _Py_NULL}, |
| 202 | // Note: _testcppext_exec currently runs all test functions directly. |
| 203 | // When adding a new one, add a call there. |
| 204 | |
| 205 | {_Py_NULL, _Py_NULL, 0, _Py_NULL} /* sentinel */ |
| 206 | }; |
| 207 | |
| 208 | |
| 209 | static int |
| 210 | _testcppext_exec(PyObject *module) |
| 211 | { |
| 212 | if (PyModule_AddIntMacro(module, __cplusplus) < 0) { |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | PyObject *result; |
| 217 | |
| 218 | result = PyObject_CallMethod(module, "test_api_casts", ""); |
| 219 | if (!result) return -1; |
| 220 | Py_DECREF(result); |
| 221 | |
| 222 | result = PyObject_CallMethod(module, "test_unicode", ""); |
| 223 | if (!result) return -1; |
| 224 | Py_DECREF(result); |
| 225 | |
| 226 | result = PyObject_CallMethod(module, "test_virtual_object", ""); |
| 227 | if (!result) return -1; |
| 228 | Py_DECREF(result); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static PyModuleDef_Slot _testcppext_slots[] = { |
| 234 | {Py_mod_exec, reinterpret_cast<void*>(_testcppext_exec)}, |
| 235 | {0, _Py_NULL} |
| 236 | }; |
| 237 | |
| 238 | |
| 239 | PyDoc_STRVAR(_testcppext_doc, "C++ test extension."); |
| 240 | |
| 241 | static struct PyModuleDef _testcppext_module = { |
| 242 | PyModuleDef_HEAD_INIT, // m_base |
| 243 | STR(NAME), // m_name |
| 244 | _testcppext_doc, // m_doc |
| 245 | 0, // m_size |
| 246 | _testcppext_methods, // m_methods |
| 247 | _testcppext_slots, // m_slots |
| 248 | _Py_NULL, // m_traverse |
| 249 | _Py_NULL, // m_clear |
| 250 | _Py_NULL, // m_free |
| 251 | }; |
| 252 | |
| 253 | #define _FUNC_NAME(NAME) PyInit_ ## NAME |
| 254 | #define FUNC_NAME(NAME) _FUNC_NAME(NAME) |
| 255 | |
| 256 | PyMODINIT_FUNC |
| 257 | FUNC_NAME(NAME)(void) |
| 258 | { |
| 259 | return PyModuleDef_Init(&_testcppext_module); |
| 260 | } |