diff options
author | Victor Stinner <vstinner@python.org> | 2022-05-02 12:09:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-02 12:09:22 (GMT) |
commit | 79886e7b62f4879a4cb17526a9a809bd0f5ed5e3 (patch) | |
tree | 1dd063f0c01e651c936facbe5e5543c10d6d3843 /Lib/test/_testcppext.cpp | |
parent | 18b07d773e09a2719e69aeaa925d5abb7ba0c068 (diff) | |
download | cpython-79886e7b62f4879a4cb17526a9a809bd0f5ed5e3.zip cpython-79886e7b62f4879a4cb17526a9a809bd0f5ed5e3.tar.gz cpython-79886e7b62f4879a4cb17526a9a809bd0f5ed5e3.tar.bz2 |
gh-91321: Add _testcppext C++ extension (#32175)
Build a basic C++ test extension to check that the Python C API is
compatible with C++ and does not emit C++ compiler warnings.
* Add Modules/_testcppext.cpp: C++ extension
* Add Lib/test/test_cppext.py: test building the C++ extension.
Diffstat (limited to 'Lib/test/_testcppext.cpp')
-rw-r--r-- | Lib/test/_testcppext.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/Lib/test/_testcppext.cpp b/Lib/test/_testcppext.cpp new file mode 100644 index 0000000..14cd1dd --- /dev/null +++ b/Lib/test/_testcppext.cpp @@ -0,0 +1,62 @@ +// gh-91321: Very basic C++ test extension to check that the Python C API is +// compatible with C++ and does not emit C++ compiler warnings. + +#include "Python.h" + +PyDoc_STRVAR(_testcppext_add_doc, +"add(x, y)\n" +"\n" +"Return the sum of two integers: x + y."); + +static PyObject * +_testcppext_add(PyObject *Py_UNUSED(module), PyObject *args) +{ + long i, j; + if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) { + return nullptr; + } + long res = i + j; + return PyLong_FromLong(res); +} + + +static PyMethodDef _testcppext_methods[] = { + {"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc}, + {nullptr, nullptr, 0, nullptr} /* sentinel */ +}; + + +static int +_testcppext_exec(PyObject *module) +{ + if (PyModule_AddIntMacro(module, __cplusplus) < 0) { + return -1; + } + return 0; +} + +static PyModuleDef_Slot _testcppext_slots[] = { + {Py_mod_exec, reinterpret_cast<void*>(_testcppext_exec)}, + {0, NULL} +}; + + +PyDoc_STRVAR(_testcppext_doc, "C++ test extension."); + +static struct PyModuleDef _testcppext_module = { + PyModuleDef_HEAD_INIT, // m_base + "_testcppext", // m_name + _testcppext_doc, // m_doc + 0, // m_size + _testcppext_methods, // m_methods + _testcppext_slots, // m_slots + NULL, // m_traverse + NULL, // m_clear + nullptr, // m_free +}; + +PyMODINIT_FUNC +PyInit__testcppext(void) +{ + return PyModuleDef_Init(&_testcppext_module); +} |