On Feb 22, 2010, at 8:04 PM, Nick Joyce wrote:
> On 22 Feb 2010, at 17:49, Robert Bradshaw wrote:
>
>> On Feb 22, 2010, at 12:11 PM, Nick Joyce wrote:
>>
>>> I have been using Cython for some time to develop the PyAMF c-
>>> extensions and have found it to be an awesome project!
>>
>> Thanks!
>>
>>> Recently, I discovered Python type declarations e.g.:
>>>
>>> def object foo():
>>> cdef dict x = {}
>>>
>>> x['foo'] = 'bar'
>>>
>>> return x['foo']
>>>
>>> The current PyAMF code is littered with PyDict_* calls so I am in
>>> the process of refactoring it to make use of this syntactic sugar.
>>>
>>> When I generate the C code (using Cython 0.12.1) for the above
>>> function, the resulting C code will use PyDict_SetItem for the
>>> assignment statement but will use PyObject_GetItem on line 6. I have
>>> attached a snippet of the generated C [pyobject_getitem.c].
>>>
>>> Would it make more sense to use PyDict_SetItem in this case? I have
>>> included a patch for this case [getitem.diff] if its useful. I
>>> haven't developed a test for this change, some pointers as to the
>>> correct location would be useful if that is desirable.
>>
>> Yes, we would certainly like to do this. The fix isn't quite this
>> simple though, as we need to make sure x is not None before caling
>> PyDict_GetItem on it, NULL is returned if the key is not found
>> (without raising an error), and the returned reference is borrowed
>> (which changes reference counting semantics).
>>
>> - Robert
>
> I have amended the patch to include all of your comments, as well as
> included a simple test pyx.
Thanks, I'll incorporate this into the main branch. Just as a style
issue, we usually abstract away the extra logic into a inline utility
function.
> Let me know if I've missed anything.
There is the case where hash() or cmp() raised an exception, so NULL
is returned but an index error should not be set (rather the original
exception should be propagated).
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev