Re: [cython-users] How to create Cython cdef public class from C

112 views
Skip to first unread message

Stefan Behnel

unread,
Jun 18, 2012, 10:29:58 AM6/18/12
to cython...@googlegroups.com
Dmitry Trofimov, 18.06.2012 14:42:
> I have following test.pyx
>
> cdef public class Foo[object Foo, type FooType]:
> cdef public char* foo(self):
> r = "Foo"
> return r
>
> Cython compiles that code to test.h and test.c and everything looks fine,
> but I can't figure out how to create *Foo* object from C-code.
>
> Even if I create it using Cython function:
>
> cdef public Foo create_Foo():
> return Foo()
>
> I can't figure out how to invoke *foo* method.
>
> While Cython documentation says that it is possible, example is missing.

It's obviously possible - Cython does it in C. :) It simply calls into the
CPython C-API, so you should read up on that.

Still, could you tell us a bit about your use case? I mean, the easiest way
to do this is through Cython, so it would be helpful to know something
about the requirements that make you want to do these things in plain C
instead.

Stefan

Dmitry Trofimov

unread,
Jun 18, 2012, 11:36:10 AM6/18/12
to cython...@googlegroups.com
Hi,
yeah I've seen C sources generated by Cython and managed to use parts of generated code in my plain C though that code was not good looking and
I'd like to know the *recommended* way to do that. 

The problem I'm trying to solve sounds like following:
I have python debugger written in python and I want to port it to C.
I've implemented core tracing in plain C. But I don't wont to rewrite all the python code in C, so I'd like to translate it to C automatically.
I've decided to use Cython for that.
Maybe all that approach is wrong, but I'm just experimenting know, so if you have any ideas how that could be made effectively - welcome.

P.S. The global problem I'm solving - making PyCharm python debugger work with gevent and in other cases where threading and socket modules got totally monkey-patched breaking current debugger implementation. That requires to trace python from C-extension.

--
Dmitry   

Stefan Behnel

unread,
Jun 18, 2012, 12:54:54 PM6/18/12
to cython...@googlegroups.com
Hi,

please don't top-post.

Dmitry Trofimov, 18.06.2012 17:36:
> On Mon, Jun 18, 2012 at 4:29 PM, Stefan Behnel wrote:
>> Dmitry Trofimov, 18.06.2012 14:42:
>>> I have following test.pyx
>>>
>>> cdef public class Foo[object Foo, type FooType]:
>>> cdef public char* foo(self):
>>> r = "Foo"
>>> return r
>>>
>>> Cython compiles that code to test.h and test.c and everything looks fine,
>>> but I can't figure out how to create *Foo* object from C-code.
>>>
>>> Even if I create it using Cython function:
>>>
>>> cdef public Foo create_Foo():
>>> return Foo()
>>>
>>> I can't figure out how to invoke *foo* method.
>>>
>>> While Cython documentation says that it is possible, example is missing.
>>
>> It's obviously possible - Cython does it in C. :) It simply calls into the
>> CPython C-API, so you should read up on that.
>>
>> Still, could you tell us a bit about your use case? I mean, the easiest way
>> to do this is through Cython, so it would be helpful to know something
>> about the requirements that make you want to do these things in plain C
>> instead.
>
> yeah I've seen C sources generated by Cython and managed to use parts of
> generated code in my plain C though that code was not good looking and
> I'd like to know the *recommended* way to do that.

I recommend not to do it. Just use Cython.


> The problem I'm trying to solve sounds like following:
> I have python debugger written in python and I want to port it to C.
> I've implemented core tracing in plain C. But I don't wont to rewrite all
> the python code in C, so I'd like to translate it to C automatically.
> I've decided to use Cython for that.
> Maybe all that approach is wrong, but I'm just experimenting know, so if
> you have any ideas how that could be made effectively - welcome.

A C level tracer is a perfectly reasonable thing. However, I really don't
see why you would want to write C for it. This functionality is so deeply
linked into CPython that it will be way harder to write in C than in
Cython, especially if you start writing C prematurely without knowing
exactly where it makes sense.


> P.S. The global problem I'm solving - making PyCharm python debugger work
> with gevent and in other cases where threading and socket modules got
> totally monkey-patched breaking current debugger implementation. That
> requires to trace python from C-extension.

I'm not sure I can follow your chain of reasoning here, but I take it that
PyCharm's debugger doesn't work well with gevent and that you're trying to
fix that.

Stefan

mark florisson

unread,
Jun 20, 2012, 9:05:06 AM6/20/12
to cython...@googlegroups.com
On 18 June 2012 13:42, Dmitry Trofimov <trofimo...@gmail.com> wrote:
> I have following test.pyx
>
> cdef public class Foo[object Foo, type FooType]:
>    cdef public char* foo(self):
>        r = "Foo"
>        return r
>
> Cython compiles that code to test.h and test.c and everything looks fine,
> but I can't figure out how to create Foo object from C-code.
>
> Even if I create it using Cython function:
>
> cdef public Foo create_Foo():
>    return Foo()
>
> I can't figure out how to invoke foo method.
>
> While Cython documentation says that it is possible, example is missing.
>
> Thanks.

Public should probably be disallowed for methods, since calling the
functions directly would break subclassing. It's probably easiest to
do in Cython, as Stefan suggested. To elaborate a bit, if you need to
invoke the method from C, write a wrapper function for each method
that takes the object as extra argument, and invoke the method. E.g.

cdef char *foo_wrapper(Foo obj):
return obj.foo()

It's inconvenient if you have many methods, so if you have control
over the design of Foo to begin with, don't use methods but use
functions instead.

BTW, great work on the PyCharm debugger, it's awesome. I only
sometimes wish it were faster, but I presume it's slow due to the
Python tracing. Completely OT, but a post-mortem feature would be
great, since it allows the process to run untraced and start the
debugger on uncaught exceptions.
Reply all
Reply to author
Forward
0 new messages