Page 113
example.c
This is the combined code from the chapter - the json files are as for example given ealier
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject *tuple1(PyObject *self, PyObject *args)
{
PyObject *myTuple = PyTuple_New(10);
for (int i = 0; i < 10; i++)
{
PyObject *item = PyLong_FromLong(i);
PyTuple_SET_ITEM(myTuple, i, item);
}
return myTuple;
}
static PyObject *list1(PyObject *self, PyObject *args)
{
PyObject *mylist = PyList_New(10);
for (int i = 0; i < 10; i++)
{
PyObject *item = PyLong_FromLong(i);
PyList_SetItem(mylist, i, item);
}
return mylist;
}
static PyObject *list2(PyObject *self, PyObject *args)
{
PyObject *itemlist = PyList_New(0);
for (int i = 0; i < 10; i++)
{
PyObject *item = PyLong_FromLong(i);
PyList_Append(itemlist, PyLong_FromLong(i));
Py_DECREF(PyList_GetItem(itemlist, i));
}
return itemlist;
}
static PyObject *dict1(PyObject *self, PyObject *args)
{
PyObject *mydict = PyDict_New();
PyObject *val = PyLong_FromLong(42333);
PyObject *key = PyUnicode_DecodeUTF8("Item0", 5, NULL);
PyDict_SetItem(mydict, key, val);
Py_DECREF(val);
Py_DECREF(key);
return mydict;
}
static PyObject *tuple2(PyObject *self, PyObject *args)
{
PyObject *myTuple;
if (!PyArg_ParseTuple(args, "O", &myTuple))
return NULL;
int myarray[10];
for (int i = 0; i < 10; i++)
{
PyObject *item = PyTuple_GetItem(myTuple, i);
myarray[i] = PyLong_AsLong(item);
printf("%d ", myarray[i]);
}
printf("\n");
Py_RETURN_NONE;
}
static PyObject *list3(PyObject *self, PyObject *args)
{
PyObject *mylist;
if (!PyArg_ParseTuple(args, "O", &mylist))
return NULL;
PyObject *itemlist = PyList_New(4);
PyList_SetItem(itemlist, 0, PyLong_FromLong(10));
PyList_SetItem(itemlist, 1, PyLong_FromLong(11));
PyList_SetItem(itemlist, 2, PyLong_FromLong(12));
PyList_SetItem(itemlist, 3, PyLong_FromLong(13));
PyList_SetSlice(mylist, 3, 5, itemlist);
Py_DECREF(itemlist);
Py_RETURN_NONE;
}
static PyObject *listIterator(PyObject *self, PyObject *args)
{
PyObject *mylist;
if (!PyArg_ParseTuple(args, "O", &mylist))
return NULL;
PyObject *myIter = PyObject_GetIter(mylist);
printf("%d ", PyLong_AsLong((PyIter_Next(myIter))));
printf("%d ", PyLong_AsLong((PyIter_Next(myIter))));
printf("%d ", PyLong_AsLong((PyIter_Next(myIter))));
printf("%d ", PyLong_AsLong((PyIter_Next(myIter))));
printf("\n");
Py_DECREF(myIter);
Py_RETURN_NONE;
}
static PyObject *listFor(PyObject *self, PyObject *args)
{
PyObject *myList;
if (!PyArg_ParseTuple(args, "O", &myList))
return NULL;
PyObject *myIter = PyObject_GetIter(myList);
for (PyObject *item = PyIter_Next(myIter); item != NULL; item = PyIter_Next(myIter))
{
printf("%d ", PyLong_AsLong(item));
Py_DECREF(item);
}
printf("\n");
Py_DECREF(myIter);
Py_RETURN_NONE;
}
static PyObject *listWhile(PyObject *self, PyObject *args)
{
PyObject *myList;
if (!PyArg_ParseTuple(args, "O", &myList))
return NULL;
PyObject *myIter = PyObject_GetIter(myList);
PyObject *item;
while (item = PyIter_Next(myIter))
{
printf("%d ", PyLong_AsLong(item));
Py_DECREF(item);
}
Py_DECREF(myIter);
printf("\n");
Py_DECREF(myIter);
Py_RETURN_NONE;
}
static PyMethodDef AddMethods[] = {
{"tuple1", tuple1, METH_VARARGS, "an example"},
{"list1", list1, METH_VARARGS, "an example"},
{"list2", list2, METH_VARARGS, "an example"},
{"dict1", dict1, METH_VARARGS, "an example"},
{"tuple2", tuple2, METH_VARARGS, "an example"},
{"list3", list3, METH_VARARGS, "an example"},
{"listIterator", listIterator, METH_VARARGS, "an example"},
{"listFor", listFor, METH_VARARGS, "an example"},
{"listWhile", listWhile, METH_VARARGS, "an example"},
{NULL, NULL, 0, NULL} // sentinel
};
static struct PyModuleDef addmodule = {
PyModuleDef_HEAD_INIT,
"example",
"C library to test API",
-1,
AddMethods};
PyMODINIT_FUNC PyInit_example(void)
{
return PyModule_Create(&addmodule);
}
test.py
import example
import sys
#print(example.tuple1())
print(example.list1())
myList=example.list2()
print(myList)
print("ref count=",sys.getrefcount(myList[0])-1)
myDict=example.dict1()
print(myDict)
print("ref count=",sys.getrefcount(myDict["Item0"])-1)
print("ref count=",sys.getrefcount(myDict)-1)
myTuple=example.tuple1()
print(myTuple)
example.tuple2(myTuple)
mylist=[0,1,2,3,4,5,6,7,8,9]
example.list3(mylist)
print(myList)
mylist=[0,1,2,3,4,5,6,7,8,9]
example.listIterator(mylist)
example.listFor(mylist)
example.listWhile(mylist)