From 4e4d8679f14a87bd5ca0c74ca67634c3b2d12f15 Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Tue, 7 Sep 2004 09:26:23 +0000 Subject: [PATCH] move tuple helper to its own file, and allow the user to redefine fragments through the fragments.i file git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6234 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Lib/python/cstring.i | 2 +- SWIG/Lib/python/fragments.i | 47 +++++++++++++++------------------- SWIG/Lib/python/pyinout.swg | 4 +-- SWIG/Lib/python/python.swg | 8 ++++++ SWIG/Lib/python/pytuplehlp.swg | 28 ++++++++++++++++++++ 5 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 SWIG/Lib/python/pytuplehlp.swg diff --git a/SWIG/Lib/python/cstring.i b/SWIG/Lib/python/cstring.i index 061480e1a..8ddcbab71 100644 --- a/SWIG/Lib/python/cstring.i +++ b/SWIG/Lib/python/cstring.i @@ -10,7 +10,7 @@ * some way. */ -%include "fragments.i" +%include "pytuplehlp.swg" /* %cstring_input_binary(TYPEMAP, SIZE) * diff --git a/SWIG/Lib/python/fragments.i b/SWIG/Lib/python/fragments.i index 5d5b18896..4c93bd50d 100644 --- a/SWIG/Lib/python/fragments.i +++ b/SWIG/Lib/python/fragments.i @@ -1,28 +1,23 @@ -/* Helper function to return tuples */ +/* + + Create a file with this name, 'fragments.i', in your working + directory and add all the %fragments you want to take precedence + over the ones defined by default by swig. + + For example, if you add: + + %fragment(SWIG_AsVal_frag(int),"header") { + SWIGSTATICINLINE(int) + SWIG_AsVal(int)(PyObject *obj, int *val) + { + ; + } + } + + this will replace the code used to retreive an integer value for all + the typemaps that need it, including: + + int, std::vector, std::list >, etc. -%fragment("t_output_helper","header") %{ - static PyObject* t_output_helper(PyObject* target, PyObject* o) { - PyObject* o2; - PyObject* o3; - if (!target) { - target = o; - } else if (target == Py_None) { - Py_DECREF(Py_None); - target = o; - } else { - o2 = target; - target = PyTuple_New(1); - PyTuple_SetItem(target, 0, o2); - - o3 = PyTuple_New(1); - PyTuple_SetItem(o3, 0, o); - - o2 = target; - target = PySequence_Concat(o2, o3); - Py_DECREF(o2); - Py_DECREF(o3); - } - return target; -} -%} +*/ diff --git a/SWIG/Lib/python/pyinout.swg b/SWIG/Lib/python/pyinout.swg index 8f38330f6..cb28ca1ca 100644 --- a/SWIG/Lib/python/pyinout.swg +++ b/SWIG/Lib/python/pyinout.swg @@ -123,7 +123,7 @@ output values. // Author: Robin Dunn //---------------------------------------------------------------------- -%include fragments.i +%include pytuplehlp.swg %define _PYVAL_OUTPUT_TYPEMAP(from_meth, from_frag, Type) %typemap(in,numinputs=0) Type *OUTPUT ($*1_ltype temp, int res = 0), @@ -193,7 +193,7 @@ phased out in future releases. %enddef #ifndef SWIG_INOUT_NODEF -#define PYVAL_INPUT_TYPEMAP(code,_a,_c,_af,_cf,...) \ +#define PYVAL_INPUT_TYPEMAP(code,_a,_c,_af,_cf,...) \ _PYVAL_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_c), \ SWIG_arg(_af),SWIG_arg(_cf),SWIG_arg(__VA_ARGS__)) diff --git a/SWIG/Lib/python/python.swg b/SWIG/Lib/python/python.swg index 0ab994252..a1c41b82d 100644 --- a/SWIG/Lib/python/python.swg +++ b/SWIG/Lib/python/python.swg @@ -22,6 +22,14 @@ %include "pymacros.swg" +/* ----------------------------------------------------------------------------- + * Look for user fragments.i file, if not, include empty system one. + * ----------------------------------------------------------------------------- */ + + +%include "fragments.i" + + /* ----------------------------------------------------------------------------- * SWIGTYPE typemaps * ----------------------------------------------------------------------------- */ diff --git a/SWIG/Lib/python/pytuplehlp.swg b/SWIG/Lib/python/pytuplehlp.swg new file mode 100644 index 000000000..5d5b18896 --- /dev/null +++ b/SWIG/Lib/python/pytuplehlp.swg @@ -0,0 +1,28 @@ +/* Helper function to return tuples */ + +%fragment("t_output_helper","header") %{ + static PyObject* t_output_helper(PyObject* target, PyObject* o) { + PyObject* o2; + PyObject* o3; + + if (!target) { + target = o; + } else if (target == Py_None) { + Py_DECREF(Py_None); + target = o; + } else { + o2 = target; + target = PyTuple_New(1); + PyTuple_SetItem(target, 0, o2); + + o3 = PyTuple_New(1); + PyTuple_SetItem(o3, 0, o); + + o2 = target; + target = PySequence_Concat(o2, o3); + Py_DECREF(o2); + Py_DECREF(o3); + } + return target; +} +%}