Crash Course in SIDL's Python Mapping

Tom Epperly <tepperly@llnl.gov>
Babel 0.8.x
2 August 2001, updated 21 January 2003

How to create a SIDL object in Python (once you've built the Python extension module)

You need to import the extension module and then calling a method to create an instance. If you have a class whose fully qualified name is x.y.z, you would say:
import x.y.z
obj = x.y.z.z()
The last part of the class name is repeated. The static methods of a class are available in the Python x.y.z name space.

In some cases, the Python extension module may be name zmodule.so instead of simply z.so. This might tempt you to say import x.y.zmodule instead of just import x.y.z; resist this temptation!

How to cast SIDL objects in Python

Let's say you have an object obj, and you would like to see if it is an instance of a SIDL class or interface whose fully qualified name is x.y.z. This is how you do it.
# assume obj already has its value
import x.y.z
zobj = x.y.z.z(obj)
if (zobj):
  # now you've got a handle to a x.y.z object/interface
else:
  # obj was not a handle to a x.y.z object/interface
Of course, you don't need the import if you know that x.y.z has already been imported.

Building Python Extension Modules

SIDL creates a Setup.in file that can be used to build the Python extension modules that you create. Copy Makefile.pre.in from your Python distribution into the directory containing Setup.in. There are three make variables you need to set when your building your Python extension module.
SIDLLIBDIR
This should be a path (absolute or relative) to the directory where the SIDL runtime library file (i.e. the shared library/dynamic link library) resides.
SIDLPYHDRS
This should be a path (absolute or relative) to the top directory in which the basic SIDL Python extensions are installed.
SIDLHDRS
This should be a path (absolute or relative) to the directory where the SIDL C header files are installed.
Here is a hypothetical example:
make -f Makefile.pre.in SIDLLIBDIR=/usr/local/lib \
   SIDLPYHDRS=/usr/local/include SIDLHDRS=/usr/local/include/SIDL
make SIDLLIBDIR=/usr/local/lib/libsidl.so \
  SIDLPYHDR=/usr/local/include SIDLHDRS=/usr/local/include/SIDL
It is unlikely that any installation actually uses those settings.

Setting up to run Python

Here I assume that you've installed BABEL in directories rooted at ${PREFIX}. You need to have ${PREFIX}/python in your PYTHONPATH environment variable in addition to the directory where you are doing your work.

On many systems, you will need ${PREFIX}/lib in your LD_LIBRARY_PATH (or whatever system setting controls which directories are searched for shared libraries/dynmic link libraries).

You will likely need to use SIDL_DLL_PATH (a semicolon separated path) to provide the path to the directory that holds the shared library/dynamic link library containing the implementation of the SIDL objects.

Notes

The Python binding for SIDL long is 32 bits instead of 64 bits at present.

What does this error message mean?

>>> import x.y.Zmodule
Traceback (innermost last):
  File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function (initZmodule)
Is the name of your SIDL interface/class x.y.Z or x.y.Zmodule, if it's the former, you should say import x.y.Z. If this isn't the problem, submit a bug report for BABEL. It might be informative to look at the symbol of the shared library/dynamic link library using a tool like nm. I suppose it's also worth checking the PYTHONPATH environment variable to make sure it's pointing to the right place.
>>> import x.y.Z
Fatal Python error: Cannot load implementation for SIDL class x.y.Z
Abort (core dumped)
This means that the Python stub code (the code that links Python to SIDL's independent object representation (IOR)) failed in its attempt to load the shared library or dynamic link library containing the implementation of SIDL class x.y.Z. Make sure the environment variable SIDL_DLL_PATH lists all the directories where the shared libraries/dynamic link libraries for your SIDL objects/interfaces are stored. SIDL_DLL_PATH is a semicolon separated list of directories where SIDL client stubs will search for shared libraries required for SIDL classes and interfaces. Make sure the directory in which the SIDL runtime resides is in the LD_LIBRARY_PATH (or whatever your machine's mechanism for locating shared library files is).
>>> import x.y.Z
Fatal Python error: Cannot load implementation for SIDL interface x.y.Z
Abort (core dumped)
This is the same problem for an interface as described immediately above for a class.