8.8.1. Python: Embedded into Lush

Author(s): Fu Jie Huang

Python can be embedded. You can just take the library file (libpython), link it with your C program, and access the Python functionalities through its C API.

In our case, we dynamically load the libpython file into Lush, and then interactively access its API, like:

(setq pdict (PyDict_New))
(PyDict_SetItemString pdict "__builtins__" (PyEval_GetBuiltins))
(PyDict_SetItemString pdict "Y" (PyInt_FromLong 2))
(PyDict_SetItemString pdict "X" (PyInt_FromLong 3))
(PyRun_String "X=X+Y" 257 pdict pdict)
(PyRun_String "print X" 257 pdict pdict)
(Py_DECREF pdict)

Before try anything, you need to browse through the Python/C API Reference Manual written by Guido van Rossum. This document can be found at: http://www.python.org/doc/current/api/api.html Installation

The Lush interface requires the shared library libpython2.x.so and associated files. It's built and tested with version 2.2.1 .

Most of Linux distributions install Python by default. The only problem with version 2.x is that only the static library libpython2.x.a is available, not the shared library libpython2.x.so. Even if you compile from the source code, you won't get the shared library. It's a known problem, indicated by the FAQ item 3.30 on Python's website. The FAQ item also gives out a workaround. Basically, you need to create a shared library yourself.

This interface does not work with Python version 2.3, yet.

Start Lush, and type:

? (libload "python/python")