Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Python Python and other languages Marcin Młotkowski 22nd May, 2013 Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms 1 Extending Python by C programs— Python/C API 2 Embedding Python in C 3 Other Python platforms Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Challenges of joining two languages Topics different data typed (lists, collections, strings); passing and returning values; new values creation; exception handling; memory management. Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms How to add a new function to Python Exercise A module with function computing the average value of elements of a list. Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms How to add a new function to Python Exercise A module with function computing the average value of elements of a list. Elements of implementation: a header file <Python.h>; a function implementation; a mapping of C function into "Python name"; an initializing function of the name initmodule_name. Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms A function implementation extern PyObject * mean(PyObject *, PyObject *); PyObject * mean(PyObject * self, PyObject * args) { int suma = 0, n, i; PyObject * res; PyObject * item; PyObject * lista; lista = PySequence_ITEM(args, 0); if (!PyList_Check(lista)) printf("This is not a list!\n"); n = PyList_Size(lista); Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Implementation, cont. the function, cont. for (i = 0; i < n; i++) { item = PyList_GetItem(lista, i); if (!PyInt_Check(item)) continue; suma += PyInt_AsLong(item); } res = Py_BuildValue("i", suma/n); Py_INCREF(res); return res; } Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Wrapping the function #include <python2.4/Python.h> extern PyObject * mean(PyObject *, PyObject *); PyObject * mean(PyObject * self, PyObject * args) { ... } Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Wrapping the function #include <python2.4/Python.h> extern PyObject * mean(PyObject *, PyObject *); PyObject * mean(PyObject * self, PyObject * args) { ... } static PyMethodDef modulik[ ] = { { "mean", mean, METH_VARARGS, "First function"}, { NULL, NULL, 0, NULL } }; PyMODINIT_FUNC initmodulik(void) { Py_InitModule("modulik", modulik); } Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Compilation and installation setup.py from distutils.core import setup, Extension module1 = Extension(’modulik’, sources = [’test.c’]) setup(name = ’MyPackage’, version = ’1.0’, description = ’Demonstration package’, ext_modules = [module1]) Compilation and installation $ python setup.py build $ python setup.py install Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Data types in Python Everything in Python is an object. Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms New values Creation new values PyObject* PyInt_FromLong(long ival) PyObject* Py_False PyObject* Py_True PyObject* PyList_New(Py_ssize_t len) PyObject* PyString_FromString(const char *v) PyObject *Py_BuildValue(char *format, ...); Py_BuildValue() Py_BuildValue("ss", "hello", "world") Py_BuildValue("[i,i]", 123, 456) Py_BuildValue("{s:i,s:i}", "abc", 123, "def", 456) Marcin Młotkowski Python None (’hello’, ’world’) [123, 456] {’abc’: 123, ’def’: 456} Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Lists Accessing lists PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index) int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item) Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Objects Accessing object fields PyObject* PyObject_GetItem(PyObject *o, PyObject *key) int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v) Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Standard multiarguments functions A header of the function PyObject * foo(PyObject * self, PyObject * args) Argument parsing int PyArg_ParseTuple(PyObject *args, const char *format, ...) Register the function in a function table { "mean", mean, METH_VARARGS, "First function"}, Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Memory management Memory management strategy Each object has a counter of references. If the counter is equal to zero, the object is removed. C programs must take care of objects’ counters. Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Counters Incrementing counter void Py_INCREF(PyObject *o) Decrementing counter void Py_DECREF(PyObject *o) Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Counters Incrementing counter void Py_INCREF(PyObject *o) Decrementing counter void Py_DECREF(PyObject *o) Remainder res = Py_BuildValue("i", suma/n); Py_INCREF(res); return res; Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Make our life easier Boost library: + joining Python with C++ + easier than C API - sometimes it is impossible to avoid C API, but there is promise... Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms How to execute Python programs Py_Initialize(); PyRun_SimpleString("i = 2"); PyRun_SimpleString("i = i*i\nprint i"); Py_Finalize(); Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Execution of programs in file Py_Initialize(); FILE * f = fopen("test.py", "r"); PyRun_SimpleFile(f, "test.py"); Py_Finalize(); Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Compilation gcc -lpython2.4 test.c Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Direct Python functions invocation Variable declarations PyObject *pName, *pModule, *pArgs, *pFunc, *pValue; Importing a Python module Py_Initialize(); pName = PyString_FromString("modulik"); pModule = PyImport_Import(pName); Drag a function form module pFunc = PyObject_GetAttrString(pModule, "foo"); Function invocation pValue = PyObject_CallObject(pFunc, pArgs); Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Jython Features of Jython implementation on Java Virtual Machine; compiles to .class files; access to Java library; full compatibility with standard implementation (the same regression tests). Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms IronPython Implementation for Mono framework; partially compatible with Python 2.4 Marcin Młotkowski Python Extending Python by C programs— Python/C API Embedding Python in C Other Python platforms Python for S60 Nokia implementation for mobile phones with Symbian 60 implements Python 2.2.2; access to hardware (SMS’s, power of signal, video recording, calling and receiving connections); support for GPRS and Bluetooth; access to 2D API and OpenGL. Marcin Młotkowski Python