Robert Bradshaw | 7 Apr 22:51 2010

Re: Re: C++ Wrapper Confusion

On Apr 7, 2010, at 1:19 PM, SevenThunders wrote:

> OK well that makes sense then.  I erroneously got the impression that
> there was stealth support
> for the new syntax.
>
> I downloaded a development version and attempted to use it to start
> wrapping a somewhat complex C++ class (that is fortunately no longer
> templated).  I pointed my PYTHONPATH to the install directory and the
> linux PATH variable to the underlying bin directory.  I ran into some
> issues,  some of which I could overcome, but the final issue is
> baffling to me.
>
> First I found that cython doesn't seem to handle type declarations of
> the form std::string.  Or at least whenever I used it as a type
> signature in a function call it complained of a syntax error.

You can't use C++ :: to denote namespaces, as :: has a completely  
meaning in Python. Instead, use

cdef extern from "string" namespace std:
     cdef cppclass string

(On that note, one thing that we don't yet provide is any nice Python  
<-> C++ string conversion.)

> I then found that it doesn't like the ~ character in the destructor
> declaration.

Use del (which is they Python syntax). Again, the ~ operator already  
has a meaning in Python.

FYI, Some good examples can be found at
http://hg.cython.org/cython-devel/file/tip/tests/run/cpp_templates.pyx#l1 
  (and elsewhere in that directory). I think it's easier to take the  
perspective that you're writing Python that can call C++ than to try  
and think about writing C++ with Python syntax.

> Next,  when wrapping my extension class,  it's single data attribute
> is the pointer to the underlying C++ class.  But how do you
> dereference that pointer in cython code?  After all a lot of my
> methods take  (const gpumat &) as the type signature.  Well it seems
> you have to drop the const declaration

const support is a long-standing defect of Cython (though lower  
priority than some)

> and then if you erroneously try
> to pass *gptr as an argument, it treats it as a star argument within
> python syntax,  which I suppose is a kind of unbracket or list
> extraction operator  (reasonable I suppose).

Yep.

> So I think you have to do something like self.gptr[0]  as an argument.

That's exactly right. You can also import the actual dereference  
operator (as a function) from cython.operator, see the above link.  
This is required in case the  * and [] have been overridden in  
incompatible ways.

> I then found that cuda's dim3 struct type had to be declared
> explicitly for some reason.

Were you using it (or its members?)

> Finally, after doing all this I ran cython on this code:

Looks like you found a bug, I've made a ticket. http://trac.cython.org/cython_trac/ticket/523 
  Can you boil this down to a smaller example?

The new C++ support is a huge step forward, but there are still a lot  
of bugs to shake out, as it needs a lot of testing. Thanks so much for  
the feedback and hopefully we can get it working for you. There's  
always the "old way" of doing things, but that's both deprecated and  
clumsy.

- Robert

--

-- 
To unsubscribe, reply using "remove me" as the subject.


Gmane