Debian Codegraph @

test cases/python/9 extmodule limited api/not_limited.c

raw
1 #include <Python.h>
2 #include <stdio.h>
3
4 #ifdef Py_LIMITED_API
5 #error Py_LIMITED_API must not be defined.
6 #endif
7
8 /* This function explicitly calls functions whose declaration is elided when
9 * Py_LIMITED_API is defined. This is to test that the linker is actually
10 * linking to the right version of the library on Windows. */
11 static PyObject *meth_not_limited(PyObject *self, PyObject *args)
12 {
13 PyObject *list;
14 Py_ssize_t size;
15
16 if (!PyArg_ParseTuple(args, "o", & list))
17 return NULL;
18
19 if (!PyList_Check(list)) {
20 PyErr_Format(PyExc_TypeError, "expected 'list'");
21 return NULL;
22 }
23
24 /* PyList_GET_SIZE and PyList_GET_ITEM are only available if Py_LIMITED_API
25 * is not defined. It seems likely that they will remain excluded from the
26 * limited API as their checked counterparts (PyList_GetSize and
27 * PyList_GetItem) are made available in that mode instead. */
28 size = PyList_GET_SIZE(list);
29 for(Py_ssize_t i = 0; i < size; ++i) {
30 PyObject *element = PyList_GET_ITEM(list, i);
31 if (element == NULL) {
32 return NULL;
33 }
34
35 if(PyObject_Print(element, stdout, Py_PRINT_RAW) == -1) {
36 return NULL;
37 }
38 }
39
40 Py_RETURN_NONE;
41 }
42
43 static struct PyMethodDef not_limited_methods[] = {
44 { "not_limited", meth_not_limited, METH_VARARGS,
45 "Calls functions whose declaration is elided by Py_LIMITED_API" },
46 { NULL, NULL, 0, NULL }
47 };
48
49 static struct PyModuleDef not_limited_module = {
50 PyModuleDef_HEAD_INIT,
51 "not_limited_api_test",
52 NULL,
53 -1,
54 not_limited_methods
55 };
56
57 PyMODINIT_FUNC PyInit_not_limited(void) {
58 return PyModule_Create(&not_limited_module);
59 }