Eric Mahurin wrote:
> On 5/20/07, gga <ggarra@...> wrote:
>> Eric Mahurin wrote:
>> >
>> > Also, I just ran these benchmarks again with the fix I originally
>> > posted in this thread (make PySequence_Cont::check a nop and always
>> > return true) and all of these benchmarks work fine. In the test, I
>> > also added a big-O calculations (based on the largest 2 n's) and
>> > disabled garbage collection. Here are the results (python):
>> >
>>
>> That's probably not correct. The check is needed for verifying the
>> container does not have incompatible stuff in it. If you have a
>> std::vector<int>, for example, the test is needed.
>> For a proper fix, you should add a partial specialization to
>> swig::traits_asptr_stdseq where value_type = PyObject_ptr (as I
>> suggested earlier).
>> This is what I have now added for ruby (replace swig::GC_VALUE with
>> PyObject_ptr). This improves the copy constructors now:
>>
>> // Partial specialization for GC_VALUE's. No need to typecheck each
>> // element.
>> template< class Seq >
>> struct traits_asptr_stdseq< Seq, swig::GC_VALUE > {
>> typedef Seq sequence;
>> typedef swig::GC_VALUE value_type;
>>
>> static int asptr(VALUE obj, sequence **seq) {
>> if (rb_obj_is_kind_of(obj, rb_cArray) == Qtrue) {
>> if (seq) {
>> RubySequence_Cont<value_type> rubyseq(obj);
>> sequence *pseq = new sequence();
>> assign(rubyseq, pseq);
>> *seq = pseq;
>> return SWIG_NEWOBJ;
>> } else {
>> return true;
>> }
>> } else {
>> sequence *p;
>> if (SWIG_ConvertPtr(obj,(void**)&p,
>> swig::type_info<sequence>(),0) == SWIG_OK) {
>> if (seq) *seq = p;
>> return SWIG_OLDOBJ;
>> }
>> }
>> return SWIG_ERROR;
>> }
>> };
>
>
> You just did the same thing that I did. From what I see, you replaced:
>
> return pyseq.check() ? SWIG_OK : SWIG_ERROR;
>
> with:
>
> return true
>
> and I changed PyContainer::check to always return true. This method
> is used nowhere else.
>
> This check is redundant. A check is already made in PySequence_Ref
> when converting to/from the target type. These PySequence_Ref objects
> are used by PySequence_InputIterator which is used by assign to copy
> the PySequence to a C++ container. There is no reason to do a check
> of the entire PySequence at once. It should be done naturally while
> iterating over the container. In C++ STL container transfers happen
> through iterators.
>
No, there's a need to check the sequence for when a %typecheck is used.
That way an overloaded function can fail appropiately and print out
the usage message.
For example, on overloaded copy constructors. Last time I checked, that
was a feature that was broken on the python side due to the generic
traits_ptr(I think?) always returning true, thou.
--
Gonzalo Garramuño
ggarra@...
AMD4400 - ASUS48N-E
GeForce7300GT
Kubuntu Edgy

Hello,
I'm just starting with swig and I have a problem with running the swig
example from the documentation. I have 2 c++ Files, "example.cpp" and
"example.h", in the header file there is only one declaration "void
HelloWord();", the .cpp implements that by cout'ing "Hello World", just a
very simple C++ program.
My exmaple.i file:
---------
%module example
%{
extern void HelloWorld();
%}
extern void HelloWorld();
---------
After a "swig -java example.i" I do:
g++ -c example.cpp example_wrap.c
and the following error occours:
-----------
example_wrap.c: In function `void SWIG_JavaThrowException(JNIEnv*,
SWIG_JavaExceptionCodes, const char*)':
example_wrap.c:163: Error: Basic operand of >>-><< has non-pointer-type
>>_Jv_JNIEnv<<
example_wrap.c:164: Error: Basic operand of >>-><< has non-pointer-type
>>_Jv_JNIEnv<<
example_wrap.c:166: Error: Basic operand of >>-><< has non-pointer-type
>>_Jv_JNIEnv<<
-----------
Has anyone an idea how to solve this?
Thanks a lot for your help!
--
View this message in context: http://www.nabble.com/Problem-with-swig-simple-example-tf3787255.html#a10710307
Sent from the swig-user mailing list archive at Nabble.com.

hi,
i would like to add pickle support to my classes wrapped in python
using swig. i think i managed that with an stl vector, though i do not
know if this could not be done better. the code is as follows:
%extend vector<double> {
%insert("python") %{
def __getstate__(self):
return ( self.capacity(), self.size(), [ i for i in self ] )
def __setstate__( self, tup ):
self.this = _tools.new_DoubleVector()
self.thisown = 1
self.reserve( tup[0] )
self.resize( tup[1] )
for i in range(tup[0]):
self[i] = tup[2][i]
%}
}
i would like to add (similar) pickling support to structure similar to
the following:
class A {
B* b;
};
class B {
std::vector<double> data;
};
i suspect that setting the thisown flag should be done in a different
way because in this case the python proxy is not responsible for
freeing the memory. any help greatly appreciated.
ondrej marsalek

On 5/20/07, gga <ggarra@...> wrote:
> Eric Mahurin wrote:
> >
> > Also, I just ran these benchmarks again with the fix I originally
> > posted in this thread (make PySequence_Cont::check a nop and always
> > return true) and all of these benchmarks work fine. In the test, I
> > also added a big-O calculations (based on the largest 2 n's) and
> > disabled garbage collection. Here are the results (python):
> >
>
> That's probably not correct. The check is needed for verifying the
> container does not have incompatible stuff in it. If you have a
> std::vector<int>, for example, the test is needed.
> For a proper fix, you should add a partial specialization to
> swig::traits_asptr_stdseq where value_type = PyObject_ptr (as I
> suggested earlier).
> This is what I have now added for ruby (replace swig::GC_VALUE with
> PyObject_ptr). This improves the copy constructors now:
>
> // Partial specialization for GC_VALUE's. No need to typecheck each
> // element.
> template< class Seq >
> struct traits_asptr_stdseq< Seq, swig::GC_VALUE > {
> typedef Seq sequence;
> typedef swig::GC_VALUE value_type;
>
> static int asptr(VALUE obj, sequence **seq) {
> if (rb_obj_is_kind_of(obj, rb_cArray) == Qtrue) {
> if (seq) {
> RubySequence_Cont<value_type> rubyseq(obj);
> sequence *pseq = new sequence();
> assign(rubyseq, pseq);
> *seq = pseq;
> return SWIG_NEWOBJ;
> } else {
> return true;
> }
> } else {
> sequence *p;
> if (SWIG_ConvertPtr(obj,(void**)&p,
> swig::type_info<sequence>(),0) == SWIG_OK) {
> if (seq) *seq = p;
> return SWIG_OLDOBJ;
> }
> }
> return SWIG_ERROR;
> }
> };
You just did the same thing that I did. From what I see, you replaced:
return pyseq.check() ? SWIG_OK : SWIG_ERROR;
with:
return true
and I changed PyContainer::check to always return true. This method
is used nowhere else.
This check is redundant. A check is already made in PySequence_Ref
when converting to/from the target type. These PySequence_Ref objects
are used by PySequence_InputIterator which is used by assign to copy
the PySequence to a C++ container. There is no reason to do a check
of the entire PySequence at once. It should be done naturally while
iterating over the container. In C++ STL container transfers happen
through iterators.

William S Fulton wrote:
>
> Feature targeting a symbol name (non-global feature):
>
> %feature(featurename) name "val";
> %feature(featurename, val) name;
>
Hmm... this is a problem then. I may need to add a hook in Lib/std/*.i
files to allow the user to add support for their own special garbage
collecting properly in STL classes.
>
> It is rather late here, so I'm not sure if that
> helps!
>
Well, it helps in confirming I cannot currently quite do what I would
like to do.
I am still looking for answers to 1) and 2), thou. I'm still puzzled
how the normal SWIG_From_* and SWIG_AsVal_* conversion functions for
primitive types are created. There's some special undocumented macro
expansion capability happening somewhere, but I could not locate it.
Currently, I've worked around GC marking objects in the STL wrappings
for ruby by keeping an internal ruby hash for all special ruby objects
inside STL containers (which automatically does the marking for you).
While very efficient from a speed pov, this is somewhat inefficient from
a memory pov as you are storing two references to the object, so it
would be cool to allow the user to switch to a more traditional GC
marking algorithm where only a single reference is kept.
--
Gonzalo Garramuño
ggarra@...
AMD4400 - ASUS48N-E
GeForce7300GT
Kubuntu Edgy

William S Fulton wrote:
> %exception is not actually a typemap, it is a macro for a feature. If
> the macro was changed to accept an optional argument, it would mean a
> non-backwards compatible change would be made as the macro would change
> from a parameterless macro to one that takes parameters.
Ah. Of course, you are right. This makes sense.
>> Currently, I've got a whole bunch of
>> code that looks like this:
>>
>> #if defined(SWIGJAVA)
>> %javaexception("NdbApiException") {
>> #else
>> %exception {
>> #endif
>> $action
>> if (result==-1) {
>> NdbError err = arg1->getNdbError();
>> NDB_exception(NdbApiException,err.message);
>> }
>> }
>>
> Write your own macro, something like:
>
> %define %myexception(EXCEPTION)
> #if defined(SWIGJAVA)
> %javaexception(EXCEPTION)
> #else
> %exception
> #endif
> %enddef
Why yes! This works like a charm!
> We'll gladly look at any proposed patches for this if you put them on
> the SF patches.
If I come up with something sensible, I'll certainly send it in.
Monty