Hoyt Koepke wrote:
>> The best way would be to let Cython do it for you, instead of calling
>> the function yourself. ;)
>
> I definitely agree, but here I couldn't find any other way to do it.
>
>> What function do you mean?
>>
>> For example, Cython uses PyDict_Next() when looping over a dictionary, so
>> there is no need to call that function yourself. Other use cases may get
>> optimised into a direct C-API call in a similar fashion.
>
> That's exactly the function I'm using.  My use case is iterating
> through a tree-like structure, where each node holds a dict of values
> and/or branches.

If the nodes are cdef classes, it should be enough to type the attribute
as "cdef dict" and use the iter*() dict methods in your code. Especially
the improvement compared to the plain (untyped) iteritems() method call
(which requires redundant tuple packing and unpacking) should be
substantial.


> When I encounter a branch, I iterate recursively
> over that branch before continuing iteration locally.  The best way of
> implementing this, at least that I found, is to use a stack of node
> dictionaries references + position variables and invoke PyDict_Next()
> while pushing/popping as needed.

That sounds like you are not using a recursive function here. If so, why not?


> I now implemented it using the intermediate variable method given
> above.  It works great.

That's why I implemented that loop optimisation. :)


> I assume you still have to have the "ctypedef
> void PyObject" in the extern block; correct?

No, why? You just call iter*() and get plain Python objects back. No
explict PyObject* involved.


> BTW, this solution:
>
>> You can do <PyObject*>a, so do &(<PyObject*>a) to get a PyObject**
>
> gives me the error message
>
> /home/hoytak/workspace/paramtree/paramtree/paramtree.pyx:648:31:
> Taking address of non-lvalue
>
> Is that a bug?

Hmm, I don't see an obvious problem with this, so it looks like a bug.
Could you file a bug report with a test case that shows how to reproduce
this?

Stefan

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

Reply via email to