2

I have read that it is possible to extend Python by writing a shared library that the Python interpreter can load as part of an import statement.

Could anyone point me to a simple example or tutorial where such an endeavour is accomplished by the use of Chicken Scheme instead of C/C++? Chicken is said to produce "portable and efficient C" - hence it should be suitable for such a task, no?

Cheers!

wuschel
  • 21
  • 1
  • Perhaps you can use http://wiki.call-cc.org/man/4/Extensions#find-header to load the Python.h headers needed in standard c/c++ development of a python extension? – VooDooNOFX Nov 08 '13 at 00:18
  • Thanks for the idea! I send an email to the chicken-users mailing list, pointing to this thread. Let us see what comes out if it. – wuschel Nov 08 '13 at 23:58

1 Answers1

3

This is an interesting question. What you are suggesting looks like a good idea, however I don't think anyone has attempted it before and got round to writing a tutorial. There is an interface to call python from chicken scheme, see pyffi, this looks like a wrapper round the python C interface. Pyffi doesn't allow calls to be made in the other direction as asked in the question.

Creating a module for python in C involves using quite a lot of python API to manipulate python objects, because python is dynamically typed all parameters based into the C code are of type PyObject this goes for built in types, user defined types and collections. The C API then provides functions to manipulate these objects, perform arithmetic, add them to and from collections, etc. In addition manual reference counting must be carried out on all pyObjects, to ensure garbage collection can be carried out correctly in python. Documentation for this can be seen here.

It is possible to convert a PyObject into a C types such as int, double, etc, within the C code, which can make writing the module easier, if a function is large, or native C collections are being used. Then the output must be wrapped back into PyObjects to return data back to python code.

Creating python modules in chicken could be much easier than C, as both are dynamically typed languages, removing the need of explicitly manipulating PyObjects, with the additional benefit of creating efficient C code. It looks like pyffi has gone some way towards mapping the types, although further work would be required to allow this to write modules.

Henry Florence
  • 2,848
  • 19
  • 16
  • Hi, thanks for elaborating the problems connected to linking C code with Python. I would give a an upvote for your answer, but I lack the reputation to do so for now. – wuschel Nov 08 '13 at 23:57
  • There are other solutions such as cython, although this doesn't use scheme. – Henry Florence Nov 09 '13 at 00:08