From: SourceForge.net <no...@so...> - 2009-08-27 17:28:41
|
Bugs item #2819361, was opened at 2009-07-10 02:57 Message generated for change (Comment added) made by simupop You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=101645&aid=2819361&group_id=1645 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: python Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Bo Peng (simupop) Assigned to: Nobody/Anonymous (nobody) Summary: Failed to pass None to classes that are implicitly converted Initial Comment: Current, None passed from Python is treated as NULL and is not passed to the constructor of a class even if the class is set to be implicitly converted (Lib/python/pyrun.swg): 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 (ptr) *ptr = 0; return SWIG_OK; } else { However, a NULL pointer can be a valid input for a class constructor (and in my case used as an indicator for invalid input) and should be allowed. For example, ================== 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 *). The fix appear to be extremely simple, although I do not know if it would work for all cases. The following patch makes func(None) work as expected. 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 && !(flags & SWIG_POINTER_IMPLICIT_CONV)) { if (ptr) *ptr = 0; return SWIG_OK; } else { ---------------------------------------------------------------------- >Comment By: Bo Peng (simupop) Date: 2009-08-27 17:28 Message: I would like to explain why this feature is important to me. Many functions of my Python module accept Python lists with corresponding C++ type std::vector<apple>. In addition to inputs such as apples 1 and 2 (param=[1, 2]), I need a special value for 'all available apples' regardless of actual number of apples. I defined a special class that wraps around vector<apple> but I could not use None to indicate this special case because None cannot be passed to the constructor of this class. My Python module now defines special values AllApples, AllOranges etc for these purposes but the use of None will greatly simplify the interface. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=101645&aid=2819361&group_id=1645 |