%include <pyptrtypes.swg>
%fragment("PyObject_var","header")
PyObject_var(PyObject* obj = 0) : ptr(obj) { }
~PyObject_var() { if (ptr) Py_DECREF(ptr); }
operator PyObject*() { return ptr; }
PyObject* operator->() const { return ptr; }
%fragment("StdTraits","header",fragment="StdTraitsCommon")
Traits that provides the from method
template <class Type> struct traits_from_ptr {
static PyObject *from(Type *val, int owner = 0) {
return SWIG_NewPointerObj(val, type_info<Type>(), owner);
template <class Type> struct traits_from {
static PyObject *from(const Type& val) {
return traits_from_ptr<Type>::from(new Type(val), 1);
template <class Type> struct traits_from<Type *> {
static PyObject *from(Type* val) {
return traits_from_ptr<Type>::from(val, 0);
inline PyObject *from(const Type& val) {
return traits_from<Type>::from(val);
inline PyObject *from_ptr(Type* val, int owner) {
return traits_from_ptr<Type>::from(val, owner);
Traits that provides the asval/as/check method
static int asptr(PyObject *obj, Type **val) {
int res = (SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0) != -1)
SWIG_type_error(type_name<Type>(), obj);
inline int asptr(PyObject *obj, Type **vptr) {
return traits_asptr<Type>::asptr(obj, vptr);
static bool asval(PyObject *obj, Type *val) {
int res = traits_asptr<Type>::asptr(obj, &p);
typedef typename noconst_traits<Type>::noconst_type noconst_type;
*(const_cast<noconst_type*>(val)) = *p;
if (res == SWIG_NEWOBJ) delete p;
return traits_asptr<Type>::asptr(obj, (Type **)(0)) ? true : false;
template <class Type> struct traits_asval<Type*> {
static bool asval(PyObject *obj, Type **val) {
typedef typename noconst_traits<Type>::noconst_type noconst_type;
int res = traits_asptr<noconst_type>::asptr(obj, &p);
*(const_cast<noconst_type**>(val)) = p;
return traits_asptr<Type>::asptr(obj, (Type **)(0)) ? true : false;
inline bool asval(PyObject *obj, Type *val) {
return traits_asval<Type>::asval(obj, val) ? true : false;
struct traits_as<Type, value_category> {
static Type as(PyObject *obj, bool throw_error) {
if (!obj || !asval(obj, &v)) {
SWIG_type_error(swig::type_name<Type>(), obj);
if (throw_error) throw std::invalid_argument("bad type");
struct traits_as<Type, pointer_category> {
static Type as(PyObject *obj, bool throw_error) {
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : 0);
if (res == SWIG_NEWOBJ) {
// Uninitialized return value, no Type() constructor required.
static Type *v_def = (Type*) malloc(sizeof(Type));
SWIG_type_error(swig::type_name<Type>(), obj);
if (throw_error) throw std::invalid_argument("bad type");
memset(v_def,0,sizeof(Type));
struct traits_as<Type*, pointer_category> {
static Type* as(PyObject *obj, bool throw_error) {
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : 0);
SWIG_type_error(swig::type_name<Type>(), obj);
if (throw_error) throw std::invalid_argument("bad type");
inline Type as(PyObject *obj, bool te = false) {
return traits_as<Type, typename traits<Type>::category>::as(obj, te);
struct traits_check<Type, value_category> {
static bool check(PyObject *obj) {
return obj && asval(obj, (Type *)(0));
struct traits_check<Type, pointer_category> {
static bool check(PyObject *obj) {
return obj && asptr(obj, (Type **)(0));
inline bool check(PyObject *obj) {
return traits_check<Type, typename traits<Type>::category>::check(obj);
// Backward compatibility
#ifdef SWIG_PYTHON_BACKWARD_COMP
PyObject* SwigInt_FromBool(bool b) {
return PyInt_FromLong(b ? 1L : 0L);
double SwigNumber_Check(PyObject* o) {
return PyFloat_Check(o) || PyInt_Check(o) || PyLong_Check(o);
double SwigNumber_AsDouble(PyObject* o) {
return PyFloat_Check(o) ? PyFloat_AsDouble(o)
: (PyInt_Check(o) ? double(PyInt_AsLong(o))
: double(PyLong_AsLong(o)));
PyObject* SwigString_FromString(const std::string& s) {
return PyString_FromStringAndSize(s.data(),s.size());
std::string SwigString_AsString(PyObject* o) {
return std::string(PyString_AsString(o));
%define %specialize_std_container(Type,Check,As,From)
template <> struct traits_asval<Type > {
static int asval(PyObject *obj, value_type *val) {
template <> struct traits_from<Type > {
static PyObject *from(const value_type& val) {
struct traits_check<Type, value_category> {
static bool check(PyObject *obj) {
return obj && Check(obj);
#define specialize_std_vector(Type,Check,As,From) %specialize_std_container(SWIG_arg(Type),Check,As,From)
#define specialize_std_list(Type,Check,As,From) %specialize_std_container(SWIG_arg(Type),Check,As,From)
#define specialize_std_deque(Type,Check,As,From) %specialize_std_container(SWIG_arg(Type),Check,As,From)
#define specialize_std_set(Type,Check,As,From) %specialize_std_container(SWIG_arg(Type),Check,As,From)
#define specialize_std_multiset(Type,Check,As,From) %specialize_std_container(SWIG_arg(Type),Check,As,From)