libPythonize and the Pythonize class simplify embedding, loading and communicating with the Python interpreter. Pythonize is a class with methods for handling the interpreter and is included in libpythonize. Also in libpythonize is a C wrapper for the C++ Pythonize class. There is also a standalone C library libpythonizec, written completely in C. All 3 versions (C++, C and C wrapper) use the same methods/functions and the same args - the only major difference is in initialization and finalization.
The method descriptions here apply to both C and C++ versions unless noted.
Methods are either called directly in C or as class members in C++. In C, int variables/values are used instead of bool.
C++: Pythonize *pyize = Pythonize (); ... bool result = pyize->appendToSysPath ("/usr/local/foo"); C: initialize (); ... int result = appendToSysPath ("/usr/local/foo"); |
Constructor/initializer - loads and initializaes the interpreter and acquires the global interpreter lock. Sets the value of pythonInit to true on success or false if construction/initialization fails.
Returns the result of initializing/constructing the interpreter object. If the return value is true, the interpreter should be usable. If it returns false, an error occurred and the interpreter will not be useful. Most methods/functions check the value of pythonInit and won't run if it's false
Destructor/finalizer - shuts down the interpreter and destroys the class Sets pythonInit to false.
Appends newPath to sys.path
Imports a module into the interpreter. Same as "import
Returns a reference to an object in an already loaded module (use importModule to load the module or get a reference to it) Returns NULL on failure.
Returns a reference to the Python sys module. Returns NULL on failure.
Returns a reference to the Python main module. Returns NULL on failure.
Sets internal value of main module
Uses Py_XDECREF to decrement the reference count of an object
Runs an arbitrary script in the interpreter and returns Python's result.
Runs an arbitrary string in the interpreter (indentation required for mulit-line strings) and returns Python's result.
If object is callable, runs object with args and returns the results of the function call as a PyObject. Returns NULL on failure.
Releases the global interpreter lock using PyEval_ReleaseLock
Acquires the global interpreter lock using PyEval_AcquireLock
Include the pythonize.h header file (requires Python.h) and link to libpythonize.so - that's pretty much it. There are unit tests in both C and C++ in the pythonize/tests/ subdirectory. An example of libpythonize usage is in pykpanelapplet.cpp in the PyKDE distribution (pykpanelapplet/ directory).
At present only the C++/C wrapper versions are available, and they're distributed with PyKDE.
The build process for libpythonize uses TrollTech's qmake (available as part of Qt), but is simple enough to be included in most make files. See the pythonize.pro file for more information - most of the additional libs required are required by libpython, and may vary by platform.
You can contact me at this address: Jim Bublitz <jbublitz@nwinternet.com>
Prev | Home | Next |
Applet Installer | Module Reference Documentation |