Stefan Behnel | 8 Feb 16:33
Picon

[Cython] C++ method overloading?

Hi,

I'm trying to implement the PyBindGen micro-benchmarks in cython-devel by
wrapping this source file:

http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/testapi.cc
http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/testapi.h

(I know, few Cython users would wrap code at that granularity, but that's
about the limit of what PyBindGen can handle.)

The API is basically this (I attached the two wrapper source files I wrote):

------------------
cdef extern from "testapi.h":
    void func1()
    double func2(double x, double y, double z)

    cdef cppclass Multiplier:
        Multiplier()
        Multiplier(double factor)
        void SetFactor()
        void SetFactor(double f)
        double GetFactor()
        double Multiply(double value)

    double call_virtual_from_cpp (Multiplier *obj, double value)
------------------

Cython parses this just fine - however, when I try to call the SetFactor
(Continue reading)

Picon
Picon

Re: [Cython] C++ method overloading?


Stefan Behnel wrote: > Hi, > > I'm trying to implement the PyBindGen micro-benchmarks in cython-devel by > wrapping this source file: > > http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/testapi.cc > http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/testapi.h
Woah, I never knew about that project. Could we borrow the .h parser strategy (whatever that is) for Cython? Dag Sverre
Sebastien Binet | 8 Feb 18:27
Picon

Re: [Cython] C++ method overloading?

On Monday 08 February 2010 18:04:18 Dag Sverre Seljebotn wrote:
> Stefan Behnel wrote:
> > Hi,
> >
> > I'm trying to implement the PyBindGen micro-benchmarks in cython-devel by
> > wrapping this source file:
> >
> > http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/
> >testapi.cc
> > http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/
> >testapi.h
> 
> Woah, I never knew about that project.
> 
> Could we borrow the .h parser strategy (whatever that is) for Cython?

IIRC, pybindgen relies on gccxml[0] for the automatic .h parsing.

gccxml, which is barely maintained, hacks the gcc compiler to get its internal 
representation of a compilation unit to spit it out as a fat xml file that one 
can post-process to do all kind of things.

this is how the reflex[1] (CERN-based) project automatically generates 
bindings and runtime reflection/introspection to/for C++ classes.

the only problem is that gccxml is actually based on the C++ parser, so if the 
header you feed it with is only C-compliant then you are screwed...

there are alternatives:
 - the mozilla guys developed a gcc plugin[2] (for gcc-4.5 or a patched 
gcc-4.4) to do the same kind of thing (but their main goal is to perform C++ 
code refactorization)
 - clang/llvm, when the C++ support will be improved

cheers,
sebastien.

[0] http://www.gccxml.org/HTML/Index.html
[1] http://root.cern.ch/drupal/content/reflex
[2] https://developer.mozilla.org/en/Dehydra

> 
> Dag Sverre
> _______________________________________________
> Cython-dev mailing list
> Cython-dev@...
> http://codespeak.net/mailman/listinfo/cython-dev
> 

--

-- 
#########################################
# Dr. Sebastien Binet
(Continue reading)

Robert Bradshaw | 8 Feb 19:57
Favicon

Re: [Cython] C++ method overloading?


On Feb 8, 2010, at 7:33 AM, Stefan Behnel wrote: > Hi, > > I'm trying to implement the PyBindGen micro-benchmarks in cython- > devel by > wrapping this source file: > > http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/testapi.cc > http://bazaar.launchpad.net/~gjc/pybindgen/trunk/annotate/735/benchmarks/testapi.h > > (I know, few Cython users would wrap code at that granularity, but > that's > about the limit of what PyBindGen can handle.) > > The API is basically this (I attached the two wrapper source files I > wrote): > > ------------------ > cdef extern from "testapi.h": > void func1() > double func2(double x, double y, double z) > > cdef cppclass Multiplier: > Multiplier() > Multiplier(double factor) > void SetFactor() > void SetFactor(double f) > double GetFactor() > double Multiply(double value) > > double call_virtual_from_cpp (Multiplier *obj, double value) > ------------------ > > Cython parses this just fine - however, when I try to call the > SetFactor > method in Cython code, I get this: > > Error converting Pyrex file to C: > ------------------------------------------------------------ > ... > def __dealloc__(self): > del self.multiplier > > def SetFactor(self, f=None): > if f is None: > self.multiplier.SetFactor() > ^ > ------------------------------------------------------------ > .../pytestapi.pyx:18:37: Call with wrong number of arguments > (expected 1, > got 0) > > The exact error depends on the order in which I declare the two > methods. It > seems that the last declaration wins. And Cython also doesn't complain > about the constructor call right two methods above, so that seems to > have > worked. > > Is this supposed to work for regular C++ methods?
Yes, this should have worked. I'm surprised there aren't any tests for this... - Robert
(Continue reading)

Robert Bradshaw | 8 Feb 20:20
Favicon

Re: [Cython] C++ method overloading?


On Feb 8, 2010, at 10:57 AM, Robert Bradshaw wrote: > On Feb 8, 2010, at 7:33 AM, Stefan Behnel wrote: > >> >> The exact error depends on the order in which I declare the two >> methods. It >> seems that the last declaration wins. And Cython also doesn't >> complain >> about the constructor call right two methods above, so that seems to >> have >> worked. >> >> Is this supposed to work for regular C++ methods? > > Yes, this should have worked. I'm surprised there aren't any tests for > this...
OK, pushed a quick fix: http://hg.cython.org/cython-devel/rev/94c13764ba4a - Robert
Stefan Behnel | 9 Feb 10:18
Picon

Re: [Cython] C++ method overloading?


Robert Bradshaw, 08.02.2010 20:20:

> On Feb 8, 2010, at 10:57 AM, Robert Bradshaw wrote: > >> On Feb 8, 2010, at 7:33 AM, Stefan Behnel wrote: >> >>> The exact error depends on the order in which I declare the two >>> methods. It >>> seems that the last declaration wins. And Cython also doesn't >>> complain >>> about the constructor call right two methods above, so that seems to >>> have >>> worked. >>> >>> Is this supposed to work for regular C++ methods? >> Yes, this should have worked. I'm surprised there aren't any tests for >> this... > > OK, pushed a quick fix: http://hg.cython.org/cython-devel/rev/94c13764ba4a
Ok, that got me past this problem. The next one was that I was using self.multiplier = testapi.Multiplier() by habit, instead of self.multiplier = new testapi.Multiplier() This lead to an "<error>" being generated in the code, but no error shown on the output. After figuring that out, I got this: Error converting Pyrex file to C: ------------------------------------------------------------ ... cdef class Multiplier: cdef testapi.Multiplier* multiplier def __cinit__(self, factor=None): if factor is None: self.multiplier = new testapi.Multiplier() ^ ------------------------------------------------------------ .../pytestapi.pyx:9:54: Expected ')'
(Continue reading)

Craig Citro | 9 Feb 10:28
Picon

Re: [Cython] C++ method overloading?


> The second error I got says: "Invalid conversion from 'Python object' to > 'double'", which is something that should *always* work. I get the same > error for other numeric types in other tests. Looks like Python object > conversion is broken in cython-devel. >
I ran into this same problem earlier, and posted a potential fix on #506. It definitely solved the problem I was running into -- I'd be curious as to whether or not it solves the issues you're hitting ... -cc
Danilo Freitas | 9 Feb 17:01
Picon

Re: [Cython] C++ method overloading?

2010/2/9 Stefan Behnel <stefan_ml@...>:

> > Robert Bradshaw, 08.02.2010 20:20: >> On Feb 8, 2010, at 10:57 AM, Robert Bradshaw wrote: >> >>> On Feb 8, 2010, at 7:33 AM, Stefan Behnel wrote: >>> >>>> The exact error depends on the order in which I declare the two >>>> methods. It >>>> seems that the last declaration wins. And Cython also doesn't >>>> complain >>>> about the constructor call right two methods above, so that seems to >>>> have >>>> worked. >>>> >>>> Is this supposed to work for regular C++ methods? >>> Yes, this should have worked. I'm surprised there aren't any tests for >>> this... >> >> OK, pushed a quick fix: http://hg.cython.org/cython-devel/rev/94c13764ba4a > > Ok, that got me past this problem. The next one was that I was using > >    self.multiplier = testapi.Multiplier() > > by habit, instead of > >    self.multiplier = new testapi.Multiplier() > > This lead to an "<error>" being generated in the code, but no error shown > on the output. > > After figuring that out, I got this: > > Error converting Pyrex file to C: > ------------------------------------------------------------ > ... > cdef class Multiplier: >    cdef testapi.Multiplier* multiplier > >    def __cinit__(self, factor=None): >        if factor is None: >            self.multiplier = new testapi.Multiplier() >                                                     ^ > ------------------------------------------------------------ > > .../pytestapi.pyx:9:54: Expected ')' > > Totally weird kind of error, given that it points to the closing > parenthesis already.
We didn't used cimported C++ classes. I'll take a look at this. The problem is with the parser. It look at the first name and thinks it is a class name, and so, expect a '('. -- -- - Danilo Freitas
(Continue reading)

Robert Bradshaw | 9 Feb 18:46
Favicon

Re: [Cython] C++ method overloading?

On Feb 9, 2010, at 1:18 AM, Stefan Behnel wrote:

> Robert Bradshaw, 08.02.2010 20:20:
>> On Feb 8, 2010, at 10:57 AM, Robert Bradshaw wrote:
>>
>>> On Feb 8, 2010, at 7:33 AM, Stefan Behnel wrote:
>>>
>>>> The exact error depends on the order in which I declare the two
>>>> methods. It
>>>> seems that the last declaration wins. And Cython also doesn't
>>>> complain
>>>> about the constructor call right two methods above, so that seems  
>>>> to
>>>> have
>>>> worked.
>>>>
>>>> Is this supposed to work for regular C++ methods?
>>> Yes, this should have worked. I'm surprised there aren't any tests  
>>> for
>>> this...
>>
>> OK, pushed a quick fix: http://hg.cython.org/cython-devel/rev/94c13764ba4a
>
> Ok, that got me past this problem. The next one was that I was using
>
>    self.multiplier = testapi.Multiplier()
>
> by habit, instead of
>
>    self.multiplier = new testapi.Multiplier()
>
> This lead to an "<error>" being generated in the code, but no error  
> shown
> on the output.
>
> After figuring that out, I got this:
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
> ...
> cdef class Multiplier:
>    cdef testapi.Multiplier* multiplier
>
>    def __cinit__(self, factor=None):
>        if factor is None:
>            self.multiplier = new testapi.Multiplier()
>                                                     ^
> ------------------------------------------------------------
>
> .../pytestapi.pyx:9:54: Expected ')'
>
> Totally weird kind of error, given that it points to the closing
> parenthesis already.
>
> The second error I got says: "Invalid conversion from 'Python  
> object' to
> 'double'", which is something that should *always* work. I get the  
> same
> error for other numeric types in other tests. Looks like Python object
> conversion is broken in cython-devel.
>
> It seems to me that the new C++ support is lacking a lot of important
> tests.

There is a *huge* deficiency in testing, and nearly every time I write  
a test I find bugs to fix. That's why it has been sitting so long. But  
there's a lot of working stuff in there too.

There's some refactoring I intend to do both in parsing and analysis,  
as the current system doesn't always work.

> I added a naming convention to the test runner that finds related
> ".c" and ".cpp" source files in the same test directory if they  
> start with
> the module name plus an underscore, i.e. a set of files like this:
>
> cppwrap_lib.cpp
> cppwrap_lib.h
> cppwrap_lib.pxd
> cppwrap.pyx
>
> will create a module based on the source files "cppwrap.cpp" and
> "cppwrap_lib.cpp". That should make it easy enough to write tests for
> wrapper code.

Nice.

- Robert

(Continue reading)

Dan Helfman | 5 Feb 19:04
Picon
Favicon

[Cython] Incorrect HTML annotations with NumPy

I've encountered a problem with Cython 0.12.1 in which incorrect code 
annotations show up when HTML is generated with the "--annotate" option.

I believe this is the same issue described here:

   http://trac.cython.org/cython_trac/ticket/292

I've come up with an even simpler test case that triggers the behavior. 
The test.pyx file is attached. What happens is that the generated HTML 
for line 187 ("cdef int y = 6"), once expanded, reads as follows:

static int __pyx_pf_5numpy_7ndarray___getbuffer__(PyObject 
*__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); 
__pyx_v_4test_y = 6;

It appears to be pulling in code from both test.pyx line 187 *and* 
numpy.pxd line 187, even though the annotation should only be for 
test.pyx. The generated code in test.c is simply "__pyx_v_4test_y = 6;" 
without the extraneous numpy.pxd __getbuffer__ code.

This problem makes Cython HTML annotation with NumPy code very difficult 
to use, because it's not immediately apparent from any given yellow line 
whether it's actually triggering the generation of NumPy code or whether 
it's merely including it in the annotation erroneously.

If this sort of thing is more appropriate on the other mailing list, 
please let me know.

Dan
(Continue reading)

Picon
Picon

Re: [Cython] Incorrect HTML annotations with NumPy

Dan Helfman wrote:
> I've encountered a problem with Cython 0.12.1 in which incorrect code 
> annotations show up when HTML is generated with the "--annotate" option.
>
> I believe this is the same issue described here:
>
>   http://trac.cython.org/cython_trac/ticket/292
>
> I've come up with an even simpler test case that triggers the 
> behavior. The test.pyx file is attached. What happens is that the 
> generated HTML for line 187 ("cdef int y = 6"), once expanded, reads 
> as follows:
>
> static int __pyx_pf_5numpy_7ndarray___getbuffer__(PyObject 
> *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); 
> __pyx_v_4test_y = 6;
>
> It appears to be pulling in code from both test.pyx line 187 *and* 
> numpy.pxd line 187, even though the annotation should only be for 
> test.pyx. The generated code in test.c is simply "__pyx_v_4test_y = 
> 6;" without the extraneous numpy.pxd __getbuffer__ code.
>
> This problem makes Cython HTML annotation with NumPy code very 
> difficult to use, because it's not immediately apparent from any given 
> yellow line whether it's actually triggering the generation of NumPy 
> code or whether it's merely including it in the annotation erroneously.
Thanks -- your description makes it much easier to understand #292 and 
how to fix it.

Dag Sverre

>
> If this sort of thing is more appropriate on the other mailing list, 
> please let me know.
>
> Dan
> ------------------------------------------------------------------------
>
> _______________________________________________
> Cython-dev mailing list
> Cython-dev@...
> http://codespeak.net/mailman/listinfo/cython-dev

(Continue reading)


Gmane