On 12 March 2010 21:57, Ondrej Certik <[email protected]> wrote:
> On Fri, Mar 12, 2010 at 6:28 AM, Lisandro Dalcin <[email protected]> wrote:
>> On 11 March 2010 23:14, Ondrej Certik <[email protected]> wrote:
> [...]
>>> but the rest will be a bit more complicated. Let me know if you think
>>> this is the right approach. I guess I could add some cython cmdline
>>> option to control the global/local api.
>>>
>>
>> How about controling global/local api at C compile time, using
>> preprocessor definitions?
>
> But how about the generation of the .cpp file? I need cython to
> generate it. But most users imho just need the .h file.
>

I'm thinking about and additional C macro.. then you just write a
"hermes_api.cpp" with

#define EMIT_CAPI_SYMBOLS_HERE
#include "hermes_api.h"


After compiling this file, you are done...


>>>
>>>    p->insert_object("i", c2py_int(5));
>>>
>>
>> I would prefer p->put("i", c2py_int(5))
>
> As I wrote, I would actually prefer push. That's what ipython uses
> (Brian suggested this to me) and I like it. E.g. push/pull.
>

Looks good, too..

> Yours idea would be put/get. I guess that's cool too. I am not a
> native speaker, so my intuition might be wrong, but I like to think of
> pushing the variables in there, rather than just putting them there.
> :)
>

I´m not a native speaker, too.. So let's follow Brian...

>>
>>>
>>>    int i = py2c_int(p->get_object("i"));
>>>
>>
>> Can you explain me how are you managing reference counting? I think
>> that you will need a proxy C++ class to manage incref()/decref()
>
> Here are the definitions of py2c_int and c2py_int:
>
> cdef api object c2py_int(int i):
>    return i
>

This function will return a brand new object (or a new reference of a
caches Python intinstance, this is a CPython implementation detail)...
Anyway, if you call this from C/C++, you have to somehow take
ownership of that object, of use it and throw it away (i.e. decref)

>
> cdef api int py2c_int(object i):
>    return i
>

This functions just take a (borrowed) object as its argument...

So if you do:

py2c_int( c2py_int(7) )

you have just leaked a reference :-) ...


>
> So what exactly do you mean by reference counting? Isn't this safe?
>

When you code in Cython, all is safe, and nice, and all gory details
are taken into account... But when you move to C and start using C
functions (it does not actually matter if they are auto-generated by
Cython) taking and returning Cython objects, then YOU are in charge of
following the rules and managing refcounting.

In short, you have to be VERY careful about what your
methods/functions do regarding ownership of Python references... BTW,
Boost.Python solves this more or less nicely using proxy,
smart-pointer-like classes for holding PyObject* references (e.g. you
incref() in copy constructors and operator=(), decref() in
destructors).


Moral: Once you get a decent knowledge of Python's C-API, you start to
REALLY appreciate the Cython's (and Pyrex's) magic... This is the
reason I've switched all my projects to Cython, starting from scratch
and thorowing away all the previous hand-written C crap!

Of course, working in C++ is other story... because the compiler can
help you to manage these references (using those proxy classes).

-- 
Lisandro Dalcin
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to