« Return to Thread: Accepting None as a valid input for the constructor of a C++ class.

Re: Accepting None as a valid input for the constructor of a C++ class.

by Bo Peng-2 :: Rate this Message:

Reply to Author | View in Thread

> But do you agree that None/Null should be passed to the constructor of
> a class for which implicitconv is defined? Please refer to my previous
> email for an example.

I just realized that 'my previous email' was sent only to Josh so I am
resending it to the list:

Thank you very much for your reply. You are right that SWIG treats
None as NULL pointer so it can only be used for pointer types. In this
case, either NULL (None) or the pointer of a real object is passed
from Python to C++. My problem might be solved by switching from a
reference type to a pointer type and check for None from where my
uintList object is received, not from uintList itself.

However, I do think there is a bug here. If a type is set to be
converted implicitly (%implicitconv), its constructor might accept a
pointer as its input type and a NULL pointer should be allowed. Using
a silly 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 *).

Cheers,
Bo

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

 « Return to Thread: Accepting None as a valid input for the constructor of a C++ class.