From 3ead254d42deeff22dab9dd73808876705ec0496 Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Sat, 10 Dec 2005 20:20:19 +0000 Subject: [PATCH] fix extra calls to TypeQuery for 'char *' and 'wchar_t *' git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7968 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Lib/perl5/perlstrings.swg | 46 ++++++++++++++-------------- SWIG/Lib/python/pycontainer.swg | 21 ++++++++----- SWIG/Lib/python/pystrings.swg | 17 +++++------ SWIG/Lib/python/pythreads.swg | 26 +++++++--------- SWIG/Lib/python/pywstrings.swg | 14 ++++----- SWIG/Lib/ruby/rubystrings.swg | 51 ++++++++++++++++---------------- SWIG/Lib/typemaps/string.swg | 18 ++++++++++- SWIG/Lib/typemaps/swigmacros.swg | 1 + SWIG/Lib/typemaps/wstring.swg | 17 +++++++++++ SWIG/Source/Modules/python.cxx | 1 + 10 files changed, 123 insertions(+), 89 deletions(-) diff --git a/SWIG/Lib/perl5/perlstrings.swg b/SWIG/Lib/perl5/perlstrings.swg index 61ed59bdf..9362f1917 100644 --- a/SWIG/Lib/perl5/perlstrings.swg +++ b/SWIG/Lib/perl5/perlstrings.swg @@ -2,37 +2,37 @@ * utility methods for char strings * ------------------------------------------------------------ */ -%fragment("SWIG_AsCharPtrAndSize","header") { +%fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { SWIGINTERN int SWIG_AsCharPtrAndSize(SV *obj, char** cptr, size_t* psize, int *alloc) { - static swig_type_info* pchar_info = 0; - char* vptr = 0; - if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); - if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) { - if (cptr) *cptr = vptr; - if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; - if (alloc) *alloc = SWIG_OLDOBJ; - return SWIG_OK; - } else { - if (SvPOK(obj)) { - STRLEN len = 0; - char *cstr = SvPV(obj, len); - size_t size = len + 1; - if (cptr) { - if (alloc) { - if (*alloc == SWIG_NEWOBJ) { - *cptr = %new_copy_array(cstr, size, char); - } else { - *cptr = cstr; - *alloc = SWIG_OLDOBJ; - } + if (SvPOK(obj)) { + STRLEN len = 0; + char *cstr = SvPV(obj, len); + size_t size = len + 1; + if (cptr) { + if (alloc) { + if (*alloc == SWIG_NEWOBJ) { + *cptr = %new_copy_array(cstr, size, char); + } else { + *cptr = cstr; + *alloc = SWIG_OLDOBJ; } } - if (psize) *psize = size; + } + if (psize) *psize = size; + return SWIG_OK; + } else { + char* vptr = 0; + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + if (pchar_descriptor && (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_descriptor, 0) == SWIG_OK)) { + if (cptr) *cptr = vptr; + if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; + if (alloc) *alloc = SWIG_OLDOBJ; return SWIG_OK; } } + return SWIG_TypeError; } } diff --git a/SWIG/Lib/python/pycontainer.swg b/SWIG/Lib/python/pycontainer.swg index 46b3ddbd0..8c835389b 100644 --- a/SWIG/Lib/python/pycontainer.swg +++ b/SWIG/Lib/python/pycontainer.swg @@ -367,8 +367,13 @@ namespace swig { return x.distance(*this); } - static swig_type_info* desc() { - static swig_type_info* desc = SWIG_TypeQuery("swig::PySequence_OutputIterator *"); + static swig_type_info* descriptor() { + static int init = 0; + static swig_type_info* desc = 0; + if (!init) { + desc = SWIG_TypeQuery("swig::PySequence_OutputIterator *"); + init = 1; + } return desc; } }; @@ -714,15 +719,15 @@ namespace swig %typemap(out,noblock=1,fragment="PySequence_Cont") iterator, reverse_iterator, const_iterator, const_reverse_iterator { $result = SWIG_NewPointerObj(swig::make_output_iterator((const $type &)$1), - swig::PySequence_OutputIterator::desc(),SWIG_POINTER_OWN); + swig::PySequence_OutputIterator::descriptor(),SWIG_POINTER_OWN); } %typemap(out,noblock=1,fragment="PySequence_Cont") std::pair, std::pair { $result = PyTuple_New(2); PyTuple_SetItem($result,0,SWIG_NewPointerObj(swig::make_output_iterator(%static_cast($1,const $type &).first), - swig::PySequence_OutputIterator::desc(),SWIG_POINTER_OWN)); + swig::PySequence_OutputIterator::descriptor(),SWIG_POINTER_OWN)); PyTuple_SetItem($result,1,SWIG_NewPointerObj(swig::make_output_iterator(%static_cast($1,const $type &).second), - swig::PySequence_OutputIterator::desc(),SWIG_POINTER_OWN)); + swig::PySequence_OutputIterator::descriptor(),SWIG_POINTER_OWN)); } %fragment("PyPairBoolOutputIterator","header",fragment=SWIG_From_frag(bool),fragment="PySequence_Cont") {} @@ -731,7 +736,7 @@ namespace swig std::pair, std::pair { $result = PyTuple_New(2); PyTuple_SetItem($result,0,SWIG_NewPointerObj(swig::make_output_iterator(%static_cast($1,const $type &).first), - swig::PySequence_OutputIterator::desc(),SWIG_POINTER_OWN)); + swig::PySequence_OutputIterator::descriptor(),SWIG_POINTER_OWN)); PyTuple_SetItem($result,1,SWIG_From(bool)(%static_cast($1,const $type &).second)); } @@ -740,7 +745,7 @@ namespace swig reverse_iterator(swig::PySequence_OutputIterator *iter), const_iterator(swig::PySequence_OutputIterator *iter), const_reverse_iterator(swig::PySequence_OutputIterator *iter) { - if (SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::PySequence_OutputIterator::desc(), 0) != SWIG_OK) { + if (SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::PySequence_OutputIterator::descriptor(), 0) != SWIG_OK) { %argument_fail(SWIG_TypeError, "$type", $argnum); } if (iter) { @@ -756,7 +761,7 @@ namespace swig iterator, reverse_iterator, const_iterator, const_reverse_iterator { swig::PySequence_OutputIterator *iter = 0; $1 = ((SWIG_ConvertPtr($input, %as_voidptrptr(&iter), - swig::PySequence_OutputIterator::desc(), 0) == SWIG_OK) + swig::PySequence_OutputIterator::descriptor(), 0) == SWIG_OK) && iter && (dynamic_cast *>(iter) != 0)); } diff --git a/SWIG/Lib/python/pystrings.swg b/SWIG/Lib/python/pystrings.swg index 53b55224f..d2f4fdc8c 100644 --- a/SWIG/Lib/python/pystrings.swg +++ b/SWIG/Lib/python/pystrings.swg @@ -1,8 +1,7 @@ /* ------------------------------------------------------------ * utility methods for char strings * ------------------------------------------------------------ */ - -%fragment("SWIG_AsCharPtrAndSize","header") { +%fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { SWIGINTERN int SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) { @@ -40,10 +39,9 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) if (psize) *psize = len + 1; return SWIG_OK; } else { - char* vptr = 0; - static swig_type_info* pchar_info = 0; - if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); - if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) { + char* vptr = 0; + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + if (pchar_descriptor && (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_descriptor, 0) == SWIG_OK)) { if (cptr) *cptr = vptr; if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; if (alloc) *alloc = SWIG_OLDOBJ; @@ -54,14 +52,15 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) } } -%fragment("SWIG_FromCharPtrAndSize","header") { +%fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { SWIGINTERNINLINE PyObject * SWIG_FromCharPtrAndSize(const char* carray, size_t size) { if (carray) { if (size > INT_MAX) { - return SWIG_NewPointerObj(%const_cast(carray,char *), - SWIG_TypeQuery("char *"), 0); + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + return pchar_descriptor ? + SWIG_NewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : SWIG_Py_Void(); } else { return PyString_FromStringAndSize(carray, %numeric_cast(size,int)); } diff --git a/SWIG/Lib/python/pythreads.swg b/SWIG/Lib/python/pythreads.swg index 4660aea57..a7552f1fe 100644 --- a/SWIG/Lib/python/pythreads.swg +++ b/SWIG/Lib/python/pythreads.swg @@ -23,28 +23,22 @@ ~SWIG_Python_Thread_Block() { end(); } }; class SWIG_Python_Thread_Allow { + bool status; PyThreadState *save; public: - SWIG_Python_Thread_Allow() : save(PyEval_SaveThread()) {} - ~SWIG_Python_Thread_Allow() { PyEval_RestoreThread(save); } + void end() { if (status) { PyEval_RestoreThread(save); status = false; }} + SWIG_Python_Thread_Allow() : status(true), save(PyEval_SaveThread()) {} + ~SWIG_Python_Thread_Allow() { end(); } }; -# if !defined(SWIG_PYTHON_THREAD_BEGIN_BLOCK) -# define SWIG_PYTHON_THREAD_BEGIN_BLOCK SWIG_Python_Thread_Block _swig_thread_block -# endif -# if !defined(SWIG_PYTHON_THREAD_END_BLOCK) -# define SWIG_PYTHON_THREAD_END_BLOCK _swig_thread_block.end() -# endif -# if !defined(SWIG_PYTHON_THREAD_BEGIN_ALLOW) -# define SWIG_PYTHON_THREAD_BEGIN_ALLOW { SWIG_Python_Thread_Allow _swig_thread_allow -# endif -# if !defined(SWIG_PYTHON_THREAD_END_ALLOW) -# define SWIG_PYTHON_THREAD_END_ALLOW } -# endif +# define SWIG_PYTHON_THREAD_BEGIN_BLOCK SWIG_Python_Thread_Block _swig_thread_block +# define SWIG_PYTHON_THREAD_END_BLOCK _swig_thread_block.end() +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW SWIG_Python_Thread_Allow _swig_thread_allow +# define SWIG_PYTHON_THREAD_END_ALLOW _swig_thread_allow.end() # else /* C code */ # define SWIG_PYTHON_THREAD_BEGIN_BLOCK PyGILState_STATE _swig_thread_block = PyGILState_Ensure() # define SWIG_PYTHON_THREAD_END_BLOCK PyGILState_Release(_swig_thread_block) -# define SWIG_PYTHON_THREAD_BEGIN_ALLOW { PyThreadState *_swig_thread_allow = PyEval_SaveThread() -# define SWIG_PYTHON_THREAD_END_ALLOW PyEval_RestoreThread(_swig_thread_allow); } +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW PyThreadState *_swig_thread_allow = PyEval_SaveThread() +# define SWIG_PYTHON_THREAD_END_ALLOW PyEval_RestoreThread(_swig_thread_allow) # endif # else /* Old thread way, not implemented, user must provide it */ # if !defined(SWIG_PYTHON_INITIALIZE_THREADS) diff --git a/SWIG/Lib/python/pywstrings.swg b/SWIG/Lib/python/pywstrings.swg index 915875e95..2fecbbf0c 100644 --- a/SWIG/Lib/python/pywstrings.swg +++ b/SWIG/Lib/python/pywstrings.swg @@ -2,7 +2,7 @@ * utility methods for wchar_t strings * ------------------------------------------------------------ */ -%fragment("SWIG_AsWCharPtrAndSize","header",fragment="") { +%fragment("SWIG_AsWCharPtrAndSize","header",fragment="",fragment="SWIG_pwchar_descriptor") { SWIGINTERN int SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc) { @@ -26,10 +26,9 @@ SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc) if (tmp) Py_DECREF(tmp); return SWIG_OK; } else { - static swig_type_info* pwchar_info = 0; + swig_type_info* pwchar_descriptor = SWIG_pwchar_descriptor(); wchar_t * vptr = 0; - if (!pwchar_info) pwchar_info = SWIG_TypeQuery("wchar_t *"); - if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) == SWIG_OK) { + if (pwchar_descriptor && (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_descriptor, 0) == SWIG_OK)) { if (cptr) *cptr = vptr; if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0; return SWIG_OK; @@ -39,14 +38,15 @@ SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc) } } -%fragment("SWIG_FromWCharPtrAndSize","header",fragment="") { +%fragment("SWIG_FromWCharPtrAndSize","header",fragment="",fragment="SWIG_pwchar_descriptor") { SWIGINTERNINLINE PyObject * SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size) { if (carray) { if (size > INT_MAX) { - return SWIG_NewPointerObj(%const_cast(carray,wchar_t *), - SWIG_TypeQuery("wchar_t *"), 0); + swig_type_info* pwchar_descriptor = SWIG_pwchar_descriptor(); + return pwchar_descriptor ? + SWIG_NewPointerObj(%const_cast(carray,wchar_t *), pwchar_descriptor, 0) : SWIG_Py_Void(); } else { return PyUnicode_FromWideChar(carray, %numeric_cast(size,int)); } diff --git a/SWIG/Lib/ruby/rubystrings.swg b/SWIG/Lib/ruby/rubystrings.swg index cbe20a712..06254b681 100644 --- a/SWIG/Lib/ruby/rubystrings.swg +++ b/SWIG/Lib/ruby/rubystrings.swg @@ -2,47 +2,48 @@ * utility methods for char strings * ------------------------------------------------------------ */ -%fragment("SWIG_AsCharPtrAndSize","header") { +%fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { SWIGINTERN int SWIG_AsCharPtrAndSize(VALUE obj, char** cptr, size_t* psize, int *alloc) { - static swig_type_info* pchar_info = 0; - char* vptr = 0; - if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); - if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) { - if (cptr) *cptr = vptr; - if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; - if (alloc) *alloc = SWIG_OLDOBJ; - return SWIG_OK; - } else { - if (TYPE(obj) == T_STRING) { - char *cstr = rb_string_value_ptr(&(obj)); - size_t size = RSTRING(obj)->len + 1; - if (cptr) { - if (alloc) { - if (*alloc == SWIG_NEWOBJ) { - *cptr = %new_copy_array(cstr, size, char); - } else { - *cptr = cstr; - *alloc = SWIG_OLDOBJ; - } + if (TYPE(obj) == T_STRING) { + char *cstr = rb_string_value_ptr(&(obj)); + size_t size = RSTRING(obj)->len + 1; + if (cptr) { + if (alloc) { + if (*alloc == SWIG_NEWOBJ) { + *cptr = %new_copy_array(cstr, size, char); + } else { + *cptr = cstr; + *alloc = SWIG_OLDOBJ; } } - if (psize) *psize = size; + } + if (psize) *psize = size; + return SWIG_OK; + } else { + char* vptr = 0; + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + if (pchar_descriptor && (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_descriptor, 0) == SWIG_OK)) { + if (cptr) *cptr = vptr; + if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; + if (alloc) *alloc = SWIG_OLDOBJ; return SWIG_OK; } - } + } return SWIG_TypeError; } } -%fragment("SWIG_FromCharPtrAndSize","header") { +%fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { SWIGINTERNINLINE VALUE SWIG_FromCharPtrAndSize(const char* carray, size_t size) { if (carray) { if (size > LONG_MAX) { - return SWIG_NewPointerObj(%const_cast(carray,char *), SWIG_TypeQuery("char *"), 0); + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + return pchar_descriptor ? + SWIG_NewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : Qnil; } else { return rb_str_new(carray, %numeric_cast(size,long)); } diff --git a/SWIG/Lib/typemaps/string.swg b/SWIG/Lib/typemaps/string.swg index 3220d6805..77a082378 100644 --- a/SWIG/Lib/typemaps/string.swg +++ b/SWIG/Lib/typemaps/string.swg @@ -1,7 +1,23 @@ - %ensure_fragment(SWIG_AsCharPtrAndSize) %ensure_fragment(SWIG_FromCharPtrAndSize) +%types(char *); + +%fragment("SWIG_pchar_descriptor","header") { +SWIGINTERN swig_type_info* +SWIG_pchar_descriptor() +{ + static int init = 0; + static swig_type_info* info = 0; + if (!init) { + info = SWIG_TypeQuery("_p_char"); + init = 1; + } + return info; +} +} + + %include %typemaps_string(char, Char, SWIG_AsCharPtrAndSize, SWIG_FromCharPtrAndSize, strlen, "", CHAR_MIN, CHAR_MAX) diff --git a/SWIG/Lib/typemaps/swigmacros.swg b/SWIG/Lib/typemaps/swigmacros.swg index 7840b4b2d..75e790f14 100644 --- a/SWIG/Lib/typemaps/swigmacros.swg +++ b/SWIG/Lib/typemaps/swigmacros.swg @@ -166,6 +166,7 @@ nocppval * ----------------------------------------------------------------------------- */ #define %mangle(Type...) #@Type +#define %descriptor(Type...) SWIGTYPE_ ## #@Type #define %string_name(Name) "SWIG_" %str(Name) #define %symbol_name(Name, Type...) SWIG_ ## Name ## _ #@Type #define %checkcode(Code) SWIG_TYPECHECK_ ## Code diff --git a/SWIG/Lib/typemaps/wstring.swg b/SWIG/Lib/typemaps/wstring.swg index e842bc45d..13e318acb 100644 --- a/SWIG/Lib/typemaps/wstring.swg +++ b/SWIG/Lib/typemaps/wstring.swg @@ -1,6 +1,23 @@ %ensure_fragment(SWIG_AsWCharPtrAndSize) %ensure_fragment(SWIG_FromWCharPtrAndSize) + +%types(wchar_t *); + +%fragment("SWIG_pwchar_descriptor","header") { +SWIGINTERN swig_type_info* +SWIG_pwchar_descriptor() +{ + static int init = 0; + static swig_type_info* info = 0; + if (!init) { + info = SWIG_TypeQuery("_p_wchar_t"); + init = 1; + } + return info; +} +} + %include %typemaps_string(wchar_t, WChar, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtrAndSize, wcslen, "", WCHAR_MIN, WCHAR_MAX) diff --git a/SWIG/Source/Modules/python.cxx b/SWIG/Source/Modules/python.cxx index 5cb8ba378..c442698c3 100644 --- a/SWIG/Source/Modules/python.cxx +++ b/SWIG/Source/Modules/python.cxx @@ -2122,6 +2122,7 @@ public: Printf(f_directors_h," msg += method_name;\n"); Printf(f_directors_h," Swig::DirectorMethodException::raise(msg.c_str());\n"); Printf(f_directors_h," }\n"); + Printf(f_directors_h," Py_DECREF(swig_get_self());\n"); Printf(f_directors_h," vtable[method_index] = method;\n"); Printf(f_directors_h," };\n"); Printf(f_directors_h," return method;\n");