On Nov 15, 2008, at 1:17 PM, Aaron DeVore wrote:

> I'm trying to use PyDict_Next to iterate over a dict in a way that is
> identical to this statement:
> for k, v in d.items():
>     # do stuff with key and value
>
> PyDict_Next has the signature:
> int PyDict_Next(PyObject *dictionary, Py_ssize_t *pos, PyObject **key,
> PyObject **value)
>
>
> The basic idea is that the dict uses pos to track which key it is on.
> The key is then assigned to the key pointer and the corresponding
> value is assigned to the value pointer. It can really help efficiency
> because it doesn't involve iterators, tuples, etc.
>
> The most obvious code is the following:
>
> cdef int pos = 0
> cdef object key, value
> while PyDict_Next(d, &pos, &key, &value):
>     # do stuff with key and value
>
> However, apparently &python_object is not legal and I'm running into
> odd type issues with &pos. Is there a way to get around those
> limitations?

Yes, declare them to be PyObject* rather than object. Then you'll  
have to do all refcounting manually (as you would have had to do  
anyways, as PyDict_Next doesn't decref its input). However, I doubt  
iterating over the dict manually like that will be a significant  
speed increase than the basic Python way of doing it.

- Robert

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

Reply via email to