From: Bo P. <be...@gm...> - 2009-09-01 14:05:58
|
Dear SWIG developers, I am writing to see if you could kindly have a look at the following patch, which, instead of treating None always as NULL, passes NULL to the constructor of a class when %implicitconv is defined. The motivation of this patch is that I would like to make None a valid input of a customized type but the way SWIG handles None makes it impossible to pass None to the constructor of the class. I have attached a simple example that demonstrates the current behavior. The patch seems to work but I do not know any side effect of it. Thank you very much. Bo Index: Lib/python/pyrun.swg =================================================================== --- Lib/python/pyrun.swg (revision 11674) +++ Lib/python/pyrun.swg (working copy) @@ -1065,7 +1065,7 @@ SWIGRUNTIME int SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int flags, int *own) { if (!obj) return SWIG_ERROR; - if (obj == Py_None) { + if (obj == Py_None && !(flags & SWIG_POINTER_IMPLICIT_CONV)) { if (ptr) *ptr = 0; return SWIG_OK; } else { ================== test.h ======== class Foo { public: Foo(int * i) : num(i == NULL ? 0 : 1) {} private: int num; }; void func(Foo f) { } ============= test.i ================= %module test %{ #include "test.h" %} %implicitconv Foo; %include "test.h" =============== command ============= % swig -c++ -python -shadow test.i % g++ test_wrap.cxx -shared -o _test.so -fPIC -I/usr/include/python2.4 I get a module test with type Foo that cannot be implicitly converted: >>> from test import * >>> func(Foo(None)) >>> func(None) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid null reference in method 'func', argument 1 of type 'Foo' For the last command, I would expect that None is passed to Foo(int *). |