diff --git a/Examples/test-suite/python/Makefile.in b/Examples/test-suite/python/Makefile.in index 6be3f5012..7546d3bae 100644 --- a/Examples/test-suite/python/Makefile.in +++ b/Examples/test-suite/python/Makefile.in @@ -20,6 +20,7 @@ CPP_TEST_CASES += \ file_test \ implicittest \ inout \ + input \ inplaceadd \ kwargs \ li_std_except \ diff --git a/Examples/test-suite/python/input.i b/Examples/test-suite/python/input.i new file mode 100644 index 000000000..8626773a3 --- /dev/null +++ b/Examples/test-suite/python/input.i @@ -0,0 +1,53 @@ +%module input + +%apply int *INPUT {int *bar}; + +%typemap(out) int * Foo::foo { + if ($1) { + $result = PyInt_FromLong(*$1); + } else { + Py_INCREF(Py_None); + $result = Py_None; + } +} + +%inline +{ + struct Foo { + int *foo(int *bar) { + if (bar) { + *bar *= 2; + } + return (bar) ? bar : 0; + } + }; +} + + + +%include std_string.i +%apply std::string *INPUT {std::string *bar}; +%typemap(out) int * sfoo { + if ($1) { + $result = PyInt_FromLong(*$1); + } else { + Py_INCREF(Py_None); + $result = Py_None; + } +} + +%typemap(out, fragment=SWIG_From_frag(std::string)) std::string * sfoo { + if ($1) { + $result = SWIG_From(std::string)(*$1); + } else { + Py_INCREF(Py_None); + $result = Py_None; + } +} + +%inline %{ + std::string *sfoo(std::string *bar) { + if (bar) *bar += " world"; + return (bar) ? bar : 0; + } +%} diff --git a/Examples/test-suite/python/input_runme.py b/Examples/test-suite/python/input_runme.py new file mode 100644 index 000000000..d4778ff8a --- /dev/null +++ b/Examples/test-suite/python/input_runme.py @@ -0,0 +1,15 @@ +from input import * + +f = Foo() +if f.foo(2) != 4: + raise RuntimeError + +if f.foo(None)!= None: + raise RuntimeError + +if sfoo("Hello") != "Hello world": + raise RuntimeError + +if sfoo(None) != None: + raise RuntimeError + diff --git a/Lib/python/pyinout.swg b/Lib/python/pyinout.swg index e0623487d..93d13c81a 100644 --- a/Lib/python/pyinout.swg +++ b/Lib/python/pyinout.swg @@ -80,11 +80,7 @@ or you can use the %apply directive : res = asptr_meth($input, &$1); if (!res) { SWIG_type_error("$basetype", $input); - } else { - if (!$1) { - SWIG_null_ref("$basetype"); - } - } + } if (SWIG_arg_fail($argnum)) SWIG_fail; }