See All Titles |
![]() ![]() Compiling and Linking Extension ModulesTwo options are available for building Python extension modules. The first one compiles and links the module into the interpreter. This option makes the module always available to the interpreter. The second option doesn't require that you recompile the interpreter because it dynamically links the modules to the system. Linking Static Extensions to the InterpreterBefore starting, make sure that you have already compiled the interpreter's source code (refer to Chapter 17, "Development Tools," for more details). Building and installing Python before adding new modules is essential to have the libraries and other files in the right places. Static Extensions on UNIXOn UNIX, Python modules written in C are easily identified by looking at the /usr/lib/Python1.5 directory. Most of the time, they are the shared library files with the .so extension. Although, if you are using HPUX, the extension is .sl, and on some others it is just .o. The next few steps show how to create static extensions on UNIX.
For example, hello /mnt/hda/python/helloworldmodule.c If your extension module requires additional libraries, add the argument -llibraryname at the end of the line. For example, hello /mnt/hda/python/helloworldmodule.c -l/mnt/hda/python/auxmodule.c The *static* flag builds the modules as static modules. The other option is to use the *shared* flag, which means that they have to be built as shared modules (known as DLLs on Windows). The last step is to recompile Python as normal to include the extra module by typing ./configure and make in the top of the Python Source tree. The Python interpreter is rebuilt after that. To execute the new interpreter and test your new extension module, just call it like this: ./python Static Extensions on WindowsThe following instructions are based on the use of Microsoft Visual C++ version 5. First, you need to inform Python's include path. To do that, go to Tools, Options, Directories (see Figure 6.2). Figure 6.2. You need to inform the include path.![]() It is also necessary to inform the library's location (see Figure 6.3). You need to add the python15.lib directory to your Tools, Options, Directories, Library files. Figure 6.3. You need to inform the python15.lib path.![]() Now, the rest is easy.
By default, the EXE file and the DLLs will be saved in your /Pcbuild/ directory. Linking Dynamic Extensions to the InterpreterNow look at what you should do in order to create dynamic extension modules. Dynamic Extensions on UNIXThe next few steps show how to build Dynamic extensions on UNIX.
This process creates a helloworldmodule.so file. You could also try gcc -c -I/usr/local/include/python1.5 helloworldmodule.c gcc -shared helloworldmodule.o -o helloworldmodule.so Dynamic Extension on WindowsNext, how you can build a Dynamic Extension on Windows is illustrated.
A subdirectory was created underneath your working directory. This subdirectory, called Release, contains your modulename.dll. A tool created by David Ascher is very useful to create Python extension modules. It uses a UNIX Setup.in file to generate and build a Microsoft Visual C++ project. This tool is called compile.py. To use it, you just need to put your C module and the compile.py file in the same directory, and execute the tool. When fired, the program creates a MS Visual C++ project (.dsp extension) and the workspace (.dsw extension). Along with those files, it also creates a subdirectory called /pyds in which it stores the python extension module (.pyd extension). In order to use this extension in your application, the interpreter needs to be able to locate the .pyd file by looking at the sys.path's variable. compile.py is available at http://starship.python.net:9673/crew/da/Code/compile Installing and Using Dynamic ModulesYou have four simple choices:
You won't find any difference while running dynamic modules. They act exactly the same way as the static modules that are linked to the interpreter. Accessing Generic DLLsSam Rushing has created an extension module called calldll that enables Python to call any function that is part of a Windows DLL. It doesn't matter whether the DLL is a Python extension. The problem to remember is that errors caused by non-Python extension DLLs don't return exception codes but error messages. With this module you can call any function in any DLL. This means that you can do just about anything on Win32. This module includes a library that gives access to lots of the system GUI features, and a 'callback'generator for i386, which lets external functions call back into Python as if it were C. (Much of the Win32 API uses callbacks.) Along with that, you can access ODBC by directly calling functions in odbc32.dll using a wrapper module called odbc.py. The ODBC module is implemented using calldll, and it has a few extra practical pieces; code for managing data sources, installing ODBC itself, and creating and maintaining Jet (Microsoft Access) databases. It has also been tested with ODBC drivers from Oracle and Objectivity. Of course, using calldll destroys any platform or architecture independence your program may have had. You can see more details at http://www.nightmare.com/software.html.
|
© 2002, O'Reilly & Associates, Inc. |