On Sat, Mar 20, 2010 at 11:40 PM, Robert Bradshaw
<[email protected]> wrote:
> On Mar 20, 2010, at 11:27 PM, Ondrej Certik wrote:
[...]
>> What would you suggest me?
>
>
> I see two options. The first, is to make a "pull_borrowed" which does
> a decref (or skips an incref) before returning the value. This will
> work if what you're pulling from is actually a dictionary, as the
> object will be pointed to elsewhere so won't go away (until the

That's right, didn't occur to me that I can decref it in the pull()
method, thanks! Instead of introducing pull_borrowed(), I'll just
decref it in the pull() method, since I am decrefing it in the push()
method too. E.g. push() method steals and pull() method borrows. That
way it is reasonably simple for the user.

> dictionary is modified). The other option is to have your py2c_int
> function "steal" a reference. (How often do you need the old one
> around once you've converted it to an int?)

(I don't need it around.) The problem though is that I would need to
add the decref in the Cython code of py2c_int, and thus I would not be
able to use it in Cython. I use it in Cython though too, mainly
numpy2c_int functions, that convert numpy array to a C array. So I
would have to introduce two kinds of functions etc.

>
> Unfortunately, both require the user to be reference-count aware. I
> think the nicest interface is pull_T for several common T.

Actually, both

int i = py2c_int(p->pull("i"));
int i = p->pull_int("i"));

look simple enough for me. I like the general way of p->pull(), since
when the user gets used to it, he can use it for any other type that
he interfaces from Cython, so I like that.

Thanks again, the key idea was that pull() can returned a borrowed
reference, I didn't realize that.

I'll keep you posted how it goes.

Ondrej
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to