aboutsummaryrefslogtreecommitdiff
path: root/test cases/python
diff options
context:
space:
mode:
authorAndrew McNulty <amcn102@gmail.com>2023-04-24 09:52:28 +0200
committerEli Schwartz <eschwartz93@gmail.com>2023-08-14 20:02:09 -0400
commitc7308076966c1c55bc117ce9f7a7f49ac96acfa6 (patch)
tree826fcf546090c3a5155c1d730d34033563038d98 /test cases/python
parent9d323020321893093492bc7d538c311c61398a1e (diff)
downloadmeson-c7308076966c1c55bc117ce9f7a7f49ac96acfa6.zip
meson-c7308076966c1c55bc117ce9f7a7f49ac96acfa6.tar.gz
meson-c7308076966c1c55bc117ce9f7a7f49ac96acfa6.tar.bz2
Python: Add 'limited_api' kwarg to extension_module
This commit adds a new keyword arg to extension_module() that enables a user to target the Python Limited API, declaring the version of the limited API that they wish to target. Two new unittests have been added to test this functionality.
Diffstat (limited to 'test cases/python')
-rw-r--r--test cases/python/10 extmodule limited api disabled/meson.build10
-rw-r--r--test cases/python/10 extmodule limited api disabled/module.c17
-rw-r--r--test cases/python/9 extmodule limited api/limited.c19
-rw-r--r--test cases/python/9 extmodule limited api/meson.build16
-rw-r--r--test cases/python/9 extmodule limited api/not_limited.c59
-rw-r--r--test cases/python/9 extmodule limited api/test.json8
6 files changed, 129 insertions, 0 deletions
diff --git a/test cases/python/10 extmodule limited api disabled/meson.build b/test cases/python/10 extmodule limited api disabled/meson.build
new file mode 100644
index 0000000..42cd618
--- /dev/null
+++ b/test cases/python/10 extmodule limited api disabled/meson.build
@@ -0,0 +1,10 @@
+project('Python limited api disabled', 'c',
+ default_options : ['buildtype=release', 'werror=true', 'python.allow_limited_api=false'])
+
+py_mod = import('python')
+py = py_mod.find_installation()
+
+module = py.extension_module('my_module',
+ 'module.c',
+ limited_api: '3.7',
+)
diff --git a/test cases/python/10 extmodule limited api disabled/module.c b/test cases/python/10 extmodule limited api disabled/module.c
new file mode 100644
index 0000000..a5d3a87
--- /dev/null
+++ b/test cases/python/10 extmodule limited api disabled/module.c
@@ -0,0 +1,17 @@
+#include <Python.h>
+
+#if defined(Py_LIMITED_API)
+#error "Py_LIMITED_API's definition by Meson should have been disabled."
+#endif
+
+static struct PyModuleDef my_module = {
+ PyModuleDef_HEAD_INIT,
+ "my_module",
+ NULL,
+ -1,
+ NULL
+};
+
+PyMODINIT_FUNC PyInit_my_module(void) {
+ return PyModule_Create(&my_module);
+}
diff --git a/test cases/python/9 extmodule limited api/limited.c b/test cases/python/9 extmodule limited api/limited.c
new file mode 100644
index 0000000..0d1c718
--- /dev/null
+++ b/test cases/python/9 extmodule limited api/limited.c
@@ -0,0 +1,19 @@
+#include <Python.h>
+
+#ifndef Py_LIMITED_API
+#error Py_LIMITED_API must be defined.
+#elif Py_LIMITED_API != 0x03070000
+#error Wrong value for Py_LIMITED_API
+#endif
+
+static struct PyModuleDef limited_module = {
+ PyModuleDef_HEAD_INIT,
+ "limited_api_test",
+ NULL,
+ -1,
+ NULL
+};
+
+PyMODINIT_FUNC PyInit_limited(void) {
+ return PyModule_Create(&limited_module);
+}
diff --git a/test cases/python/9 extmodule limited api/meson.build b/test cases/python/9 extmodule limited api/meson.build
new file mode 100644
index 0000000..68afc96
--- /dev/null
+++ b/test cases/python/9 extmodule limited api/meson.build
@@ -0,0 +1,16 @@
+project('Python limited api', 'c',
+ default_options : ['buildtype=release', 'werror=true'])
+
+py_mod = import('python')
+py = py_mod.find_installation()
+
+ext_mod_limited = py.extension_module('limited',
+ 'limited.c',
+ limited_api: '3.7',
+ install: true,
+)
+
+ext_mod = py.extension_module('not_limited',
+ 'not_limited.c',
+ install: true,
+)
diff --git a/test cases/python/9 extmodule limited api/not_limited.c b/test cases/python/9 extmodule limited api/not_limited.c
new file mode 100644
index 0000000..105dbb8
--- /dev/null
+++ b/test cases/python/9 extmodule limited api/not_limited.c
@@ -0,0 +1,59 @@
+#include <Python.h>
+#include <stdio.h>
+
+#ifdef Py_LIMITED_API
+#error Py_LIMITED_API must not be defined.
+#endif
+
+/* This function explicitly calls functions whose declaration is elided when
+ * Py_LIMITED_API is defined. This is to test that the linker is actually
+ * linking to the right version of the library on Windows. */
+static PyObject *meth_not_limited(PyObject *self, PyObject *args)
+{
+ PyObject *list;
+ Py_ssize_t size;
+
+ if (!PyArg_ParseTuple(args, "o", & list))
+ return NULL;
+
+ if (!PyList_Check(list)) {
+ PyErr_Format(PyExc_TypeError, "expected 'list'");
+ return NULL;
+ }
+
+ /* PyList_GET_SIZE and PyList_GET_ITEM are only available if Py_LIMITED_API
+ * is not defined. It seems likely that they will remain excluded from the
+ * limited API as their checked counterparts (PyList_GetSize and
+ * PyList_GetItem) are made available in that mode instead. */
+ size = PyList_GET_SIZE(list);
+ for(Py_ssize_t i = 0; i < size; ++i) {
+ PyObject *element = PyList_GET_ITEM(list, i);
+ if (element == NULL) {
+ return NULL;
+ }
+
+ if(PyObject_Print(element, stdout, Py_PRINT_RAW) == -1) {
+ return NULL;
+ }
+ }
+
+ Py_RETURN_NONE;
+}
+
+static struct PyMethodDef not_limited_methods[] = {
+ { "not_limited", meth_not_limited, METH_VARARGS,
+ "Calls functions whose declaration is elided by Py_LIMITED_API" },
+ { NULL, NULL, 0, NULL }
+};
+
+static struct PyModuleDef not_limited_module = {
+ PyModuleDef_HEAD_INIT,
+ "not_limited_api_test",
+ NULL,
+ -1,
+ not_limited_methods
+};
+
+PyMODINIT_FUNC PyInit_not_limited(void) {
+ return PyModule_Create(&not_limited_module);
+}
diff --git a/test cases/python/9 extmodule limited api/test.json b/test cases/python/9 extmodule limited api/test.json
new file mode 100644
index 0000000..06a1706
--- /dev/null
+++ b/test cases/python/9 extmodule limited api/test.json
@@ -0,0 +1,8 @@
+{
+ "installed": [
+ {"type": "python_limited_lib", "file": "usr/@PYTHON_PLATLIB@/limited"},
+ {"type": "py_limited_implib", "file": "usr/@PYTHON_PLATLIB@/limited"},
+ {"type": "python_lib", "file": "usr/@PYTHON_PLATLIB@/not_limited"},
+ {"type": "py_implib", "file": "usr/@PYTHON_PLATLIB@/not_limited"}
+ ]
+}