Zpugdccherry 101105081729-phpapp01

21
Producing Python Wrappers for C++ Libraries Using SWIG Joshua L. Cherry, Ph.D. National Center for Biotechnology Information National Library of Medicine National Institutes of Health [email protected]

Transcript of Zpugdccherry 101105081729-phpapp01

Page 1: Zpugdccherry 101105081729-phpapp01

Producing Python Wrappers for C++ Libraries Using SWIG

Joshua L. Cherry, Ph.D.

National Center for Biotechnology InformationNational Library of MedicineNational Institutes of [email protected]

Page 2: Zpugdccherry 101105081729-phpapp01

SWIG

Simplified Wrapper and Interface Generator

www.swig.org

Page 3: Zpugdccherry 101105081729-phpapp01

Two Ways to Extend Python

• Write functions, classes, etc. in Python

• Write C/C++– Use Python C API– Usually turn into shared library (dll)

Page 4: Zpugdccherry 101105081729-phpapp01

Why C/C++?

• Performance

• Do things that couldn’t otherwise be done

• Use already existing C/C++ code

Page 5: Zpugdccherry 101105081729-phpapp01

double foo(int n);

Page 6: Zpugdccherry 101105081729-phpapp01

double foo(int n);

#include <Python.h>

PyObject *wrap_foo(PyObject *dummy, PyObject *args) {

...

}

Page 7: Zpugdccherry 101105081729-phpapp01

double foo(int n);

#include <Python.h>

PyObject *wrap_foo(PyObject *dummy, PyObject *args) {

PyObject *resultobj;

int n;

double result;

PyArg_ParseTuple(args, “i", &n);

result = foo(n);

resultobj = PyFloat_FromDouble(result)

return resultobj;

}

Page 8: Zpugdccherry 101105081729-phpapp01

double foo(int n);

int bar(int n, int m);

#include <Python.h>

PyObject *wrap_foo(PyObject *dummy, PyObject *args) {

...

}

PyObject *wrap_bar(PyObject *dummy, PyObject *args) {

PyObject *resultobj;

int n, m;

int result;

PyArg_ParseTuple(args, “ii", &n, &m);

result = bar(n, m);

resultobj = PyInt_FromLong(result)

return resultobj;

}

Page 9: Zpugdccherry 101105081729-phpapp01

Some Things SWIG Does for You

• Full support for structs and classes, including inheritance

• Support for overloaded functions and functions with default arguments

• Exception handling that you specify

• Numerous customization capabilities

Page 10: Zpugdccherry 101105081729-phpapp01

SharedLibrary

C++Code

PythonCode

Compile,link

SWIGInterface Files

(point atC++ headers)

SWIG

Page 11: Zpugdccherry 101105081729-phpapp01

Simple SWIG Interface File

%module example

%include my_header1.hpp

%include my_header2.hpp

void some_function(double x, int n);

%{

#include “my_header1.hpp”

#include “my_header2.hpp

void some_function(double x, int n);

%}

Page 12: Zpugdccherry 101105081729-phpapp01

Some Customizations and Features

• Simple customization with %rename, %ignore, %extend, %pythoncode, etc.

• Specialize C++ classes in Python with “directors”

• Customize behavior by writing “typemaps”

• SWIG Library: very useful library of SWIG code, e.g., exception handling, typemaps, stl support

Page 13: Zpugdccherry 101105081729-phpapp01

%module example

%include stl.i%template(vector_int) std::vector<int>;

int prod(const std::vector<int>& vec);

>>> import example>>> vec = example.vector_int()>>> vec.append(42) # append mapped to vector::push_back>>> vec.append(98)>>> vec.append(47)>>> len(vec) # __len__ mapped to vector::size3>>> example.prod(vec) # normal class usage193452>>> >>> example.prod([42, 98, 47]) # works because of "in" typemap193452>>> vec2 = example.vector_int([42, 98, 47])

STL Support Example

Page 14: Zpugdccherry 101105081729-phpapp01

NCBI C++ Toolkit

• C++ for speed and programmer control

• Vast: about 3200 classes, 44,000 functions– General-purpose functionality– Mission-specific functionality– Includes handling of ASN.1 objects

Page 15: Zpugdccherry 101105081729-phpapp01

Scripting Languages

• E.g., Perl and Python

• “High-level”

• No (explicit) compilation necessary

• Usually lots of functionality included, and more available separately

Page 16: Zpugdccherry 101105081729-phpapp01

Why Make Scripting Interfaces?

• Large number of existing programmers

• Easy to learn

• Easy to use

• Allow interactive use

• Ability to combine Toolkit functions with scripting language functions

• Embedding scripting languages in applications can make them customizable

Page 17: Zpugdccherry 101105081729-phpapp01

Uses of Scripting Language Interfaces

• Writing end-product programs

• Prototyping C++ programs

• Interactive use for accomplishing things

• Interactive use for exploring the C++ Toolkit

• Introspection

Page 18: Zpugdccherry 101105081729-phpapp01

The Interfaces

• SWIG generates code based on C++ headers and other information

• Ancillary scripts necessary for proper behavior and customizations

• Perl and Python supported

• Interfaces largely mirror C++ API, but with important enhancements and customizations

Page 19: Zpugdccherry 101105081729-phpapp01

Issues

• Languages are different

• C++ templates must be instantiated at build time

• SWIG far from perfect

• Problems with the NCBI C++ Toolkit

• Large size of monolithic wrapper

Page 20: Zpugdccherry 101105081729-phpapp01

Enhancements and Customizations

• Easier ASN.1/XML reading and writing

• Easy access to NCBI documentation

• Transparent reference counting for reference-counted objects

• Other add-on features

Page 21: Zpugdccherry 101105081729-phpapp01

Josh Cherry

[email protected]

SWIG

www.swig.org

NCBI C++ Toolkit Wrappers

www.ncbi.nlm.nih.gov/cvsweb/index.cgi/internal/c++/src/wrappers/