Joris Gillis edited this page Jun 11, 2011 · 6 revisions


A typical input typemap consists of three basic parts: typemap(in): contains the actual conversion typemap(typecheck): necessary when you have a method with multiple signatures typemap(freearg): free memory that was allocated in typemap(in) , if any.

The general pattern is:

%typemap(in) singleargumentpattern  (localvariables)? {
  $1 = f($input);

or even more general:

%typemap(in) (singleargumentpattern+)  (localvariables)? {
  $1 = f($input);
  $2 = g($input);

Defining argumentpattern = singleargumentpattern | (singleargumentpattern+), we have the following typical typemaps:

%typemap(typecheck,precedence=...) argumentpattern {
if (my_check($input)) {
    } else {
%typemap(freearg) argumentpattern {
if ($1) 
    delete $1;


Passing data between typemaps

A typemap(in) can allocate variables that are available for all typemaps with the same argumentpattern. Typically, localvariables would contain PyObject *temp and the typemap(freearg) would contain the code delete temp$argnum

Throwing Exceptions

Use the macro SWIG_exception_fail, not SWIG_Error, inside typemaps. Example: SWIG_exception_fail(SWIG_TypeError, "Wrong type of argument");

Accessing called objects

When writing a typemap for a member function, you can access the object with arg1.


A typemap is not always the best solution. You can also add code in the target language verbatim:

%extend Matrix<double> {
%pythoncode %{
def toMatrix(self):
import numpy as n
return n.matrix(self.getArray())

See: typemaps_development for more in-depth information of how we structure typemaps.