From aadff06f45b0a865dc91d60dffa705da6b135eac Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Wed, 19 Oct 2005 10:52:50 +0000 Subject: [PATCH] more typemaps unification and fixes for valgrind git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7684 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/carrays.i | 4 - Lib/python/argcargv.i | 2 +- Lib/python/carrays.i | 2 + Lib/python/cmalloc.i | 1 + Lib/python/cpointer.i | 1 + Lib/python/pycontainer.swg | 6 +- Lib/python/pystdcommon.swg | 26 +++--- Lib/python/pystrings.swg | 24 +++++- Lib/python/pywstrings.swg | 1 + Lib/python/std_pair.i | 2 +- Lib/ruby/carrays.i | 2 + Lib/ruby/cmalloc.i | 1 + Lib/ruby/cpointer.i | 1 + Lib/ruby/rubyrun.swg | 2 +- Lib/std/std_common.i | 2 +- Lib/tcl/carrays.i | 2 + Lib/tcl/cmalloc.i | 1 + Lib/tcl/cpointer.i | 1 + Lib/typemaps/carrays.swg | 111 +++++++++++++++++++++++++ Lib/typemaps/cmalloc.swg | 114 ++++++++++++++++++++++++++ Lib/typemaps/cpointer.swg | 160 +++++++++++++++++++++++++++++++++++++ Lib/typemaps/cstring.swg | 18 +++-- Lib/typemaps/implicit.swg | 2 +- Lib/typemaps/ptrtypes.swg | 61 +++++++------- Lib/typemaps/strings.swg | 9 +-- Lib/typemaps/swigtype.swg | 2 +- 26 files changed, 484 insertions(+), 74 deletions(-) create mode 100644 Lib/python/carrays.i create mode 100644 Lib/python/cmalloc.i create mode 100644 Lib/python/cpointer.i create mode 100644 Lib/ruby/carrays.i create mode 100644 Lib/ruby/cmalloc.i create mode 100644 Lib/ruby/cpointer.i create mode 100644 Lib/tcl/carrays.i create mode 100644 Lib/tcl/cmalloc.i create mode 100644 Lib/tcl/cpointer.i create mode 100644 Lib/typemaps/carrays.swg create mode 100644 Lib/typemaps/cmalloc.swg create mode 100644 Lib/typemaps/cpointer.swg diff --git a/Lib/carrays.i b/Lib/carrays.i index 3a9092f00..7eac65e4a 100644 --- a/Lib/carrays.i +++ b/Lib/carrays.i @@ -80,10 +80,6 @@ typedef TYPE NAME; typedef struct NAME { /* Put language specific enhancements here */ -#if defined(SWIGPYTHON) || defined(SWIGRUBY) - %rename(__getitem__) getitem; - %rename(__setitem__) setitem; -#endif } NAME; %extend NAME { diff --git a/Lib/python/argcargv.i b/Lib/python/argcargv.i index 2b26a3690..8e068d00f 100644 --- a/Lib/python/argcargv.i +++ b/Lib/python/argcargv.i @@ -65,7 +65,7 @@ SWIG_AsArgcArgv(PyObject* input, } } -%typemap(freearg) (int ARGC, char **ARGV) { +%typemap(freearg,noblock=1) (int ARGC, char **ARGV) { if (owner$argnum) { size_t i = argc$argnum; while (i) { diff --git a/Lib/python/carrays.i b/Lib/python/carrays.i new file mode 100644 index 000000000..8be67abce --- /dev/null +++ b/Lib/python/carrays.i @@ -0,0 +1,2 @@ +%include + diff --git a/Lib/python/cmalloc.i b/Lib/python/cmalloc.i new file mode 100644 index 000000000..248f06b96 --- /dev/null +++ b/Lib/python/cmalloc.i @@ -0,0 +1 @@ +%include diff --git a/Lib/python/cpointer.i b/Lib/python/cpointer.i new file mode 100644 index 000000000..d824792fa --- /dev/null +++ b/Lib/python/cpointer.i @@ -0,0 +1 @@ +%include diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 1da325cd6..fd735eadf 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -338,10 +338,10 @@ namespace swig snprintf(msg, sizeof(msg), "in sequence element %d of type %s", i, swig::type_name()); SWIG_set_errmsg(SWIG_TypeError, msg); } - return 0; + return false; } } - return 1; + return true; } private: @@ -488,7 +488,7 @@ namespace swig if (SWIG_ConvertPtr(obj,(void**)&p, swig::type_info(),0) == SWIG_OK) { if (seq) *seq = p; - return 1; + return SWIG_OLDOBJ; } } if (seq) { diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index 5bb4f6c98..a266906cb 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -67,7 +67,7 @@ namespace swig { template struct traits_asval { - static bool asval(PyObject *obj, Type *val) { + static int asval(PyObject *obj, Type *val) { if (val) { Type *p = 0; int res = traits_asptr::asptr(obj, &p); @@ -75,12 +75,12 @@ namespace swig { typedef typename noconst_traits::noconst_type noconst_type; *(const_cast(val)) = *p; if (res == SWIG_NEWOBJ) SWIG_delete(p); - return true; + return SWIG_OK; } else { - return false; + return SWIG_ERROR; } } else { - return traits_asptr::asptr(obj, (Type **)(0)) ? true : false; + return traits_asptr::asptr(obj, (Type **)(0)) ? SWIG_OK : SWIG_ERROR; } } }; @@ -93,26 +93,26 @@ namespace swig { int res = traits_asptr::asptr(obj, &p); if (res) { *(const_cast(val)) = p; - return true; + return SWIG_OK; } else { - return false; + return SWIG_ERROR; } } else { - return traits_asptr::asptr(obj, (Type **)(0)) ? true : false; + return traits_asptr::asptr(obj, (Type **)(0)) ? SWIG_OK : SWIG_ERROR; } } }; template - inline bool asval(PyObject *obj, Type *val) { - return traits_asval::asval(obj, val) ? true : false; + inline int asval(PyObject *obj, Type *val) { + return traits_asval::asval(obj, val); } template struct traits_as { static Type as(PyObject *obj, bool throw_error) { Type v; - if (!obj || !asval(obj, &v)) { + if (!obj || (asval(obj, &v) != SWIG_OK)) { if (!PyErr_Occurred()) { SWIG_Python_SetErrorMsg(SWIG_Python_ErrorType(SWIG_TypeError), swig::type_name()); } @@ -173,7 +173,7 @@ namespace swig { template struct traits_check { static bool check(PyObject *obj) { - return obj && asval(obj, (Type *)(0)); + return obj && (asval(obj, (Type *)(0)) == SWIG_OK); } }; @@ -229,9 +229,9 @@ namespace swig { static int asval(PyObject *obj, value_type *val) { if (Check(obj)) { *val = As(obj); - return 1; + return SWIG_OK; } - return 0; + return SWIG_ERROR; } }; template <> struct traits_from { diff --git a/Lib/python/pystrings.swg b/Lib/python/pystrings.swg index cf4dbd362..9ea99df46 100644 --- a/Lib/python/pystrings.swg +++ b/Lib/python/pystrings.swg @@ -20,12 +20,30 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) PyString_AsStringAndSize(obj, &cstr, &len); if (cptr) { if (alloc) { - if (*alloc == SWIG_NEWOBJ) { - *cptr = SWIG_new_copy_array(cstr, len + 1, char); - } else { + /* + In python the user should not be able to modify the inner + string representation, so, by default we return a copy of + the buffer, as in the wstring case. + + But, if you use the -DSWIG_PYTHON_UNSAFE_CSTR at + compilation time, you will get, at your own risk, the + inner string pointer instead, when possible. + */ +#ifndef SWIG_PYTHON_UNSAFE_CSTR + if (*alloc != SWIG_OLDOBJ) +#else + if (*alloc == SWIG_NEWOBJ) +#endif + { + *cptr = SWIG_new_copy_array(cstr, len + 1, char); + *alloc = SWIG_NEWOBJ; + } + else { *cptr = cstr; *alloc = SWIG_OLDOBJ; } + } else { + *cptr = cstr; } } if (psize) *psize = len + 1; diff --git a/Lib/python/pywstrings.swg b/Lib/python/pywstrings.swg index bd0dea188..319ee02b2 100644 --- a/Lib/python/pywstrings.swg +++ b/Lib/python/pywstrings.swg @@ -26,6 +26,7 @@ SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc) int len = PyUnicode_GetSize(obj); if (cptr) { *cptr = SWIG_new_array(len + 1, wchar_t); + memset(*cptr, 0, (len + 1)*sizeof(wchar_t)); PyUnicode_AsWideChar((PyUnicodeObject *)obj, *cptr, len); (*cptr)[len] = 0; } diff --git a/Lib/python/std_pair.i b/Lib/python/std_pair.i index d0505bf0a..fa1f35dc4 100644 --- a/Lib/python/std_pair.i +++ b/Lib/python/std_pair.i @@ -20,7 +20,7 @@ pfirst = &((*val)->first); psecond = &((*val)->second); } - if (swig::asval(first,pfirst) && swig::asval(second,psecond)) { + if ((swig::asval(first,pfirst) == SWIG_OK) && (swig::asval(second,psecond) == SWIG_OK)) { return SWIG_NEWOBJ; } else { SWIG_delete(*val); diff --git a/Lib/ruby/carrays.i b/Lib/ruby/carrays.i new file mode 100644 index 000000000..8be67abce --- /dev/null +++ b/Lib/ruby/carrays.i @@ -0,0 +1,2 @@ +%include + diff --git a/Lib/ruby/cmalloc.i b/Lib/ruby/cmalloc.i new file mode 100644 index 000000000..248f06b96 --- /dev/null +++ b/Lib/ruby/cmalloc.i @@ -0,0 +1 @@ +%include diff --git a/Lib/ruby/cpointer.i b/Lib/ruby/cpointer.i new file mode 100644 index 000000000..d824792fa --- /dev/null +++ b/Lib/ruby/cpointer.i @@ -0,0 +1 @@ +%include diff --git a/Lib/ruby/rubyrun.swg b/Lib/ruby/rubyrun.swg index 227edae17..db6d05776 100644 --- a/Lib/ruby/rubyrun.swg +++ b/Lib/ruby/rubyrun.swg @@ -42,7 +42,7 @@ /* Error manipulation */ #define SWIG_ERROR -1 -#define SWIG_fail return Qnil +#define SWIG_fail goto fail #define SWIG_var_fail return Qnil #define SWIG_error(code, msg) SWIG_Ruby_SetErrorMsg(SWIG_Ruby_ErrorType(code), msg) diff --git a/Lib/std/std_common.i b/Lib/std/std_common.i index a83861226..b8a915a2b 100644 --- a/Lib/std/std_common.i +++ b/Lib/std/std_common.i @@ -142,7 +142,7 @@ namespace swig { template <> struct traits_asval { typedef Type value_type; static int asval(PyObject *obj, value_type *val) { - return SWIG_AsVal(Type)(obj, val) == SWIG_OK; + return SWIG_AsVal(Type)(obj, val); } }; template <> struct traits_from { diff --git a/Lib/tcl/carrays.i b/Lib/tcl/carrays.i new file mode 100644 index 000000000..8be67abce --- /dev/null +++ b/Lib/tcl/carrays.i @@ -0,0 +1,2 @@ +%include + diff --git a/Lib/tcl/cmalloc.i b/Lib/tcl/cmalloc.i new file mode 100644 index 000000000..248f06b96 --- /dev/null +++ b/Lib/tcl/cmalloc.i @@ -0,0 +1 @@ +%include diff --git a/Lib/tcl/cpointer.i b/Lib/tcl/cpointer.i new file mode 100644 index 000000000..d824792fa --- /dev/null +++ b/Lib/tcl/cpointer.i @@ -0,0 +1 @@ +%include diff --git a/Lib/typemaps/carrays.swg b/Lib/typemaps/carrays.swg new file mode 100644 index 000000000..b52f01ae7 --- /dev/null +++ b/Lib/typemaps/carrays.swg @@ -0,0 +1,111 @@ +/* ----------------------------------------------------------------------------- + * carrays.i + * + * Author(s): David Beazley (beazley@cs.uchicago.edu) + * + * This library file contains macros that can be used to manipulate simple + * pointers as arrays. + * + * $Header$ + * ----------------------------------------------------------------------------- */ + +/* ----------------------------------------------------------------------------- + * %array_functions(TYPE,NAME) + * + * Generates functions for creating and accessing elements of a C array + * (as pointers). Creates the following functions: + * + * TYPE *new_NAME(int nelements) + * void delete_NAME(TYPE *); + * TYPE NAME_getitem(TYPE *, int index); + * void NAME_setitem(TYPE *, int index, TYPE value); + * + * ----------------------------------------------------------------------------- */ + +%define %array_functions(TYPE,NAME) +%{ + static TYPE *new_##NAME(size_t nelements) { + return SWIG_new_array(nelements, TYPE); + } + + static void delete_##NAME(TYPE *ary) { + SWIG_delete_array(ary); + } + + static TYPE NAME##_getitem(TYPE *ary, size_t index) { + return ary[index]; + } + static void NAME##_setitem(TYPE *ary, size_t index, TYPE value) { + ary[index] = value; + } +%} + +TYPE *new_##NAME(size_t nelements); +void delete_##NAME(TYPE *ary); +TYPE NAME##_getitem(TYPE *ary, size_t index); +void NAME##_setitem(TYPE *ary, size_t index, TYPE value); + +%enddef + + +/* ----------------------------------------------------------------------------- + * %array_class(TYPE,NAME) + * + * Generates a class wrapper around a C array. The class has the following + * interface: + * + * struct NAME { + * NAME(int nelements); + * ~NAME(); + * TYPE getitem(int index); + * void setitem(int index, TYPE value); + * TYPE * cast(); + * static NAME *frompointer(TYPE *t); + * } + * + * ----------------------------------------------------------------------------- */ + +%define %array_class(TYPE,NAME) +%{ +typedef TYPE NAME; +%} +typedef struct NAME { + /* Put language specific enhancements here */ + +#if defined(SWIGPYTHON) || defined(SWIGRUBY) + %rename(__getitem__) getitem; + %rename(__setitem__) setitem; +#endif +} NAME; + +%extend NAME { + + NAME(size_t nelements) { + return SWIG_new_array(nelements, TYPE); + } + + ~NAME() { + SWIG_delete_array(self); + } + + TYPE getitem(size_t index) { + return self[index]; + } + + void setitem(size_t index, TYPE value) { + self[index] = value; + } + + TYPE * cast() { + return self; + } + + static NAME *frompointer(TYPE *t) { + return SWIG_static_cast(t, NAME *); + } +}; + +%types(NAME = TYPE); + +%enddef + diff --git a/Lib/typemaps/cmalloc.swg b/Lib/typemaps/cmalloc.swg new file mode 100644 index 000000000..4f6c56a70 --- /dev/null +++ b/Lib/typemaps/cmalloc.swg @@ -0,0 +1,114 @@ +/* ----------------------------------------------------------------------------- + * cmalloc.i + * + * Author(s): David Beazley (beazley@cs.uchicago.edu) + * + * This library file contains macros that can be used to create objects using + * the C malloc function. + * + * $Header$ + * ----------------------------------------------------------------------------- */ + +%{ +#include +%} + +/* %malloc(TYPE [, NAME = TYPE]) + %calloc(TYPE [, NAME = TYPE]) + %realloc(TYPE [, NAME = TYPE]) + %free(TYPE [, NAME = TYPE]) + %allocators(TYPE [,NAME = TYPE]) + + Creates functions for allocating/reallocating memory. + + TYPE *malloc_NAME(size_t nbytes = sizeof(TYPE); + TYPE *calloc_NAME(size_t nobj=1, size_t size=sizeof(TYPE)); + TYPE *realloc_NAME(TYPE *ptr, size_t nbytes); + void free_NAME(TYPE *ptr); + +*/ + +%define %malloc(TYPE,NAME...) +#if #NAME != "" +%rename(malloc_##NAME) ::malloc(size_t nbytes); +#else +%rename(malloc_##TYPE) ::malloc(size_t nbytes); +#endif + +#if #TYPE != "void" +%typemap(default) size_t nbytes "$1 = (size_t) sizeof(TYPE);" +#endif +TYPE *malloc(size_t nbytes); +%typemap(default) size_t nbytes; +%enddef + +%define %calloc(TYPE,NAME...) +#if #NAME != "" +%rename(calloc_##NAME) ::calloc(size_t nobj, size_t sz); +#else +%rename(calloc_##TYPE) ::calloc(size_t nobj, size_t sz); +#endif +#if #TYPE != "void" +%typemap(default) size_t sz "$1 = (size_t) sizeof(TYPE);" +#else +%typemap(default) size_t sz "$1 = 1;" +#endif +%typemap(default) size_t nobj "$1 = 1;" +TYPE *calloc(size_t nobj, size_t sz); +%typemap(default) size_t sz; +%typemap(default) size_t nobj; +%enddef + +%define %realloc(TYPE,NAME...) +%insert("header") { +#if #NAME != "" +TYPE *realloc_##NAME(TYPE *ptr, size_t nitems) +#else +TYPE *realloc_##TYPE(TYPE *ptr, size_t nitems) +#endif +{ +#if #TYPE != "void" +return (TYPE *) realloc(ptr, nitems*sizeof(TYPE)); +#else +return (TYPE *) realloc(ptr, nitems); +#endif +} +} +#if #NAME != "" +TYPE *realloc_##NAME(TYPE *ptr, size_t nitems); +#else +TYPE *realloc_##TYPE(TYPE *ptr, size_t nitems); +#endif +%enddef + +%define %free(TYPE,NAME...) +#if #NAME != "" +%rename(free_##NAME) ::free(TYPE *ptr); +#else +%rename(free_##TYPE) ::free(TYPE *ptr); +#endif +void free(TYPE *ptr); +%enddef + +%define %sizeof(TYPE,NAME...) +#if #NAME != "" +%constant size_t sizeof_##NAME = sizeof(TYPE); +#else +%constant size_t sizeof_##TYPE = sizeof(TYPE); +#endif +%enddef + +%define %allocators(TYPE,NAME...) +%malloc(TYPE,NAME) +%calloc(TYPE,NAME) +%realloc(TYPE,NAME) +%free(TYPE,NAME) +#if #TYPE != "void" +%sizeof(TYPE,NAME) +#endif +%enddef + + + + + diff --git a/Lib/typemaps/cpointer.swg b/Lib/typemaps/cpointer.swg new file mode 100644 index 000000000..bfd2d2a67 --- /dev/null +++ b/Lib/typemaps/cpointer.swg @@ -0,0 +1,160 @@ +/* ----------------------------------------------------------------------------- + * cpointer.i + * + * Author(s): David Beazley (beazley@cs.uchicago.edu) + * + * This library file contains macros that can be used to manipulate simple + * pointer objects. + * + * $Header$ + * ----------------------------------------------------------------------------- */ + +/* ----------------------------------------------------------------------------- + * %pointer_class(type,name) + * + * Places a simple proxy around a simple type like 'int', 'float', or whatever. + * The proxy provides this interface: + * + * class type { + * public: + * type(); + * ~type(); + * type value(); + * void assign(type value); + * }; + * + * Example: + * + * %pointer_class(int, intp); + * + * int add(int *x, int *y) { return *x + *y; } + * + * In python (with proxies) + * + * >>> a = intp() + * >>> a.assign(10) + * >>> a.value() + * 10 + * >>> b = intp() + * >>> b.assign(20) + * >>> print add(a,b) + * 30 + * + * As a general rule, this macro should not be used on class/structures that + * are already defined in the interface. + * ----------------------------------------------------------------------------- */ + + +%define %pointer_class(TYPE, NAME) +%{ +typedef TYPE NAME; +%} + +typedef struct { +} NAME; + +%extend NAME { + NAME() { + return SWIG_new(TYPE); + } + ~NAME() { + if (self) SWIG_delete(self); + } +} + +%extend NAME { + + void assign(TYPE value) { + *self = value; + } + TYPE value() { + return *self; + } + TYPE * cast() { + return self; + } + static NAME * frompointer(TYPE *t) { + return (NAME *) t; + } +} + +%types(NAME = TYPE); + +%enddef + +/* ----------------------------------------------------------------------------- + * %pointer_functions(type,name) + * + * Create functions for allocating/deallocating pointers. This can be used + * if you don't want to create a proxy class or if the pointer is complex. + * + * %pointer_functions(int, intp) + * + * int add(int *x, int *y) { return *x + *y; } + * + * In python (with proxies) + * + * >>> a = copy_intp(10) + * >>> intp_value(a) + * 10 + * >>> b = new_intp() + * >>> intp_assign(b,20) + * >>> print add(a,b) + * 30 + * >>> delete_intp(a) + * >>> delete_intp(b) + * + * ----------------------------------------------------------------------------- */ + +%define %pointer_functions(TYPE,NAME) +%{ + static TYPE *new_##NAME() { + return SWIG_new(TYPE); + } + + static TYPE *copy_##NAME(TYPE value) { + return SWIG_new_copy(value, TYPE); + } + + static void delete_##NAME(TYPE *self) { + if (self) SWIG_delete(self); + } + + static void NAME ##_assign(TYPE *self, TYPE value) { + *self = value; + } + + static TYPE NAME ##_value(TYPE *self) { + return *self; + } +%} + +TYPE *new_##NAME(); +TYPE *copy_##NAME(TYPE value); +void delete_##NAME(TYPE *self); +void NAME##_assign(TYPE *self, TYPE value); +TYPE NAME##_value(TYPE *self); + +%enddef + +/* ----------------------------------------------------------------------------- + * %pointer_cast(type1,type2,name) + * + * Generates a pointer casting function. + * ----------------------------------------------------------------------------- */ + +%define %pointer_cast(TYPE1,TYPE2,NAME) +%inline %{ +TYPE2 NAME(TYPE1 x) { + return SWIG_static_cast(x, TYPE2); +} +%} +%enddef + + + + + + + + diff --git a/Lib/typemaps/cstring.swg b/Lib/typemaps/cstring.swg index 0801da86d..2bc9d0665 100644 --- a/Lib/typemaps/cstring.swg +++ b/Lib/typemaps/cstring.swg @@ -29,14 +29,14 @@ %define Name ## _input_binary(TYPEMAP, SIZE) %typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) (TYPEMAP, SIZE) - (Char *buf, size_t size, int alloc) { + (Char *buf = 0, size_t size = 0, int alloc = 0) { if (SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc) != SWIG_OK) { SWIG_arg_fail(SWIG_TypeError, "(TYPEMAP, SIZE)", $argnum); } $1 = ($1_ltype) buf; $2 = ($2_ltype) size - 1; } -%typemap(freearg) (TYPEMAP, SIZE) { +%typemap(freearg,noblock=1) (TYPEMAP, SIZE) { if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); } %enddef @@ -59,7 +59,7 @@ %typemap(in,noblock=1,numinputs=0) TYPEMAP (Char temp[MAX+1]) { $1 = ($1_ltype) temp; } -%typemap(freearg) TYPEMAP ""; +%typemap(freearg,noblock=1) TYPEMAP ""; %typemap(argout,noblock=1,fragment= #SWIG_FromCharPtr ) TYPEMAP { $1[MAX] = 0; SWIG_append_result(SWIG_FromCharPtr($1)); @@ -84,7 +84,7 @@ %typemap(in,noblock=1,numinputs=0) TYPEMAP(Char temp[SIZE]) { $1 = ($1_ltype) temp; } -%typemap(freearg) TYPEMAP ""; +%typemap(freearg,noblock=1) TYPEMAP ""; %typemap(argout,noblock=1,fragment= #SWIG_FromCharPtrAndSize) TYPEMAP { SWIG_append_result(SWIG_FromCharPtrAndSize($1,SIZE)); } @@ -108,7 +108,8 @@ %define Name ## _bounded_mutable(TYPEMAP,MAX) -%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP (Char temp[MAX+1], Char *t, size_t n, int alloc) { +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP + (Char temp[MAX+1], Char *t = 0, size_t n = 0, int alloc = 0) { if (SWIG_AsCharPtrAndSize($input, &t, &n, &alloc) != SWIG_OK) { SWIG_arg_fail(SWIG_TypeError, "TYPEMAP", $argnum); } @@ -118,7 +119,7 @@ temp[n - 1] = 0; $1 = ($1_ltype) temp; } -%typemap(freearg) TYPEMAP ""; +%typemap(freearg,noblock=1) TYPEMAP ""; %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP { $1[MAX] = 0; SWIG_append_result(SWIG_FromCharPtr($1)); @@ -143,7 +144,8 @@ */ %define Name ## _mutable(TYPEMAP,EXP...) -%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP (Char* t, size_t n, int alloc, size_t expansion = 0) { +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP + (Char* t = 0, size_t n = 0, int alloc = 0, size_t expansion = 0) { #if #EXP != "" expansion += EXP; #endif @@ -155,7 +157,7 @@ if (alloc == SWIG_NEWOBJ) SWIG_delete_array(t); $1[n-1] = 0; } -%typemap(freearg) TYPEMAP ""; +%typemap(freearg,noblock=1) TYPEMAP ""; %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP { SWIG_append_result(SWIG_FromCharPtr($1)); SWIG_delete_array($1); diff --git a/Lib/typemaps/implicit.swg b/Lib/typemaps/implicit.swg index 7fbe09d55..6025a1962 100644 --- a/Lib/typemaps/implicit.swg +++ b/Lib/typemaps/implicit.swg @@ -46,7 +46,7 @@ %define %implicit_frag(Type...) ,fragment=SWIG_Traits_frag(Type) %enddef %define %implicit_code(Type...) - if (swig::asval(obj, 0)) { + if (swig::asval(obj, 0) == SWIG_OK) { Type _v; swig::asval(obj, &_v); if (val) *val = new value_type(_v); diff --git a/Lib/typemaps/ptrtypes.swg b/Lib/typemaps/ptrtypes.swg index d8eac2c60..c6400dfb6 100644 --- a/Lib/typemaps/ptrtypes.swg +++ b/Lib/typemaps/ptrtypes.swg @@ -41,47 +41,44 @@ /* directorout */ %define SWIG_PTR_DIRECTOROUT_TYPEMAP(asptr_meth,frag,Type...) - %typemap(directorargout,fragment=frag) Type *DIRECTOROUT ($*1_ltype temp) { - Type *ptr = 0; - int res = $input ? asptr_meth($input, &ptr) : 0; - if (!res || !ptr) { + %typemap(directorargout,noblock=1,fragment=frag) Type *DIRECTOROUT ($*ltype temp) { + Type *optr = 0; + int ores = $input ? asptr_meth($input, &optr) : 0; + if (!ores || !optr) { SWIG_dout_fail(SWIG_TypeError,"$type"); - } else { - temp = *ptr; - $result = &temp; - if (res == SWIG_NEWOBJ) SWIG_delete(ptr); } + temp = *optr; + $result = &temp; + if (ores == SWIG_NEWOBJ) SWIG_delete(optr); } - %typemap(directorout,fragment=frag) Type { - Type *ptr = 0; - int res = asptr_meth($input, &ptr); - if (!res || !ptr) { + %typemap(directorout,noblock=1,fragment=frag) Type { + Type *optr = 0; + int ores = asptr_meth($input, &optr); + if (!ores || !optr) { SWIG_dout_fail(SWIG_TypeError,"$type"); - } else { - $result = *ptr; - if (res == SWIG_NEWOBJ) SWIG_delete(ptr); } + $result = *optr; + if (ores == SWIG_NEWOBJ) SWIG_delete(optr); } - %typemap(directorout,fragment=frag,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const Type& { - Type *ptr = 0; - int res = asptr_meth($input, &ptr); - if (!res) { + %typemap(directorout,noblock=1,fragment=frag,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const Type& { + Type *optr = 0; + int ores = asptr_meth($input, &optr); + if (!ores) { SWIG_dout_fail(SWIG_TypeError,"$type"); } else { - if (!ptr) { + if (!optr) { SWIG_dout_nullref("$type"); - } else { - if (res == SWIG_NEWOBJ) { - /* Possible thread/reentrant problem here! */ - static $*ltype temp = *ptr; - $result = &temp; - SWIG_delete(ptr); - } else { - $result = ptr; - } - } + } + } + if (ores == SWIG_NEWOBJ) { + /* Possible thread/reentrant problem here! */ + static $*ltype temp = *optr; + $result = &temp; + SWIG_delete(optr); + } else { + $result = optr; } } @@ -115,12 +112,12 @@ { Type *v = (Type *)0; int res = SWIG_AsPtr(Type)(obj, &v); - if (!res || !v) return 0; + if (!res || !v) return SWIG_ERROR; if (val) { *val = *v; if (res == SWIG_NEWOBJ) SWIG_delete(v); } - return 1; + return SWIG_OK; } %} %fragment(SWIG_As_frag(Type),"header", diff --git a/Lib/typemaps/strings.swg b/Lib/typemaps/strings.swg index 898682e2f..c5e961bb4 100644 --- a/Lib/typemaps/strings.swg +++ b/Lib/typemaps/strings.swg @@ -39,8 +39,7 @@ if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); } -%typemap(in,noblock=1,fragment=#SWIG_AsCharPtr) Char const*& (Char *buf, int alloc) -{ +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtr) Char const*& (Char *buf = 0, int alloc = 0) { if (SWIG_AsCharPtr($input, &buf, &alloc) != SWIG_OK) { SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); } @@ -149,7 +148,7 @@ /* directorin */ -%typemap(directorin,fragment=#SWIG_FromCharPtr,noblock=1) +%typemap(directorin,noblock=1,fragment=#SWIG_FromCharPtr) Char *, Char const*, Char *const, Char const *const, Char const *&, Char *const &, Char const *const & { $input = SWIG_FromCharPtr($1_name); @@ -158,14 +157,14 @@ /* directorout */ -%typemap(directorout,fragment=#SWIG_AsCharPtr,noblock=1) Char * (Char* buf, int alloc) { +%typemap(directorout,noblock=1,fragment=#SWIG_AsCharPtr) Char * (Char* buf = 0, int alloc = 0) { if (SWIG_AsCharPtr($input, &buf, &alloc) != SWIG_OK) { SWIG_dout_fail(SWIG_TypeError, "$type"); } $result = buf; } -%typemap(directorout,fragment=#SWIG_AsCharPtr) Char * const& (Char* buf, int alloc) { +%typemap(directorout,noblock=1,fragment=#SWIG_AsCharPtr) Char * const& (Char* buf = 0, int alloc = 0) { if (SWIG_AsCharPtr($input, &buf, &alloc) != SWIG_OK) { SWIG_dout_fail(SWIG_TypeError, "$type"); } diff --git a/Lib/typemaps/swigtype.swg b/Lib/typemaps/swigtype.swg index f3295b17d..b8fb71a8b 100644 --- a/Lib/typemaps/swigtype.swg +++ b/Lib/typemaps/swigtype.swg @@ -4,7 +4,7 @@ /* Pointers and arrays */ %typemap(in, noblock=1) SWIGTYPE *, SWIGTYPE [] { - if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&$1), $descriptor, $disown) != SWIG_OK) { + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&$1),$descriptor, $disown) != SWIG_OK) { SWIG_arg_fail(SWIG_TypeError, "$type", $argnum); } }