User Tools

Site Tools


info:swig

SWIG

General Notes

  • By default, SWIG only parses files that you explicitly include (with %include)
  • %feature("docstring", "New docstring") ClassName::MethodName; appends to autodoc; replace "docstring" with "autodoc" to replace (supposedly)
  • If I were writing a new interface, it seems like it might make some things easier to have swig generate a rough interface and manually write a Python wrapper for that to smooth out the edges; on the other hand, that seems like a lot of wrappers (maybe have SWIG skip its pure-Python module?)

PySequence_Fast API

PyObject auto_ptr

See http://code.activestate.com/recipes/577985-automatic-python-pyobject-ref-count-management-in-/

/*
 * auto_pyptr.h
 *
 * Originally from
 * http://code.activestate.com/recipes/528875-automatic-ref-count-management-in-c-using-a-smart-/
 */
 
#ifndef AUTO_PYPTR_H_
#define AUTO_PYPTR_H_
 
#include <Python.h>
#include <memory>
 
typedef std::auto_ptr<PyObject> auto_pyptr_base;
 
/**
 * An auto_ptr that, instead of deleting, decrements the reference count
 * of a PyObject pointer.
 *
 * Make sure to only use this when you get a *new* reference (Py_INCREF or
 * getting the result of any function that says it returns a new reference
 * to a PyObject), NOT for "borrowed" references.
 */
class auto_pyptr : public auto_pyptr_base {
public:
    auto_pyptr(PyObject * obj = NULL) : auto_pyptr_base(obj) {
    }
    ~auto_pyptr() {
        reset();
    }
    void reset(PyObject * obj = NULL) {
        if(obj != get()) {
            PyObject * old = release(); // Avoid the delete call
            Py_XDECREF(old);
            auto_pyptr_base::reset(obj);
        }
    }
    void inc() {
        PyObject * ptr = get();
        if(ptr)
            Py_INCREF(ptr);
    }
 
    /*
     * Implement cast to PyObject pointer so you don't have to call var.get()
     * every time you use the object.
     *
     * You still have to use get() in certain cases, notably varargs
     * (i.e. "..."). GCC will warn you that this will abort at runtime.
     */
    operator PyObject*() {
        return this->get();
    }
};
 
#endif /* AUTO_PYPTR_H_ */

Stack Overflow posts

Typemaps

  • Comments included inside of typemap blocks gets included verbatim in the generated wrapper code. This can be potentially useful if you're trying to figure out why something isn't working as you expect.

Problems Encountered

I was able to generate simple bindings without issue, but I'd done it before.


When trying to call an overloaded method:

NotImplementedError: No matching function for overloaded 'HelloSubclass_Greet'

This was fixed when I included the SWIG std::string library (it wasn't recognizing the type):

%include "std_string.i"

"Directors" must be explicitly enabled to allow "cross language polymorphism".

When trying to override a virtual method with a Python subclass:

terminate called after throwing an instance of 'Swig::DirectorTypeMismatchException'

This was also fixed when I included the SWIG std::string library.


Typemaps for a string output parameter: string didn't work; I had to explicitly use std::string.

info/swig.txt · Last modified: 2012-02-08 22:00 by sam