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.

Let me know if I've missed anything.

Cheers,

Nick

Attachment: getitem-2.diff
Description: Binary data

Attachment: dict_getitem.pyx
Description: Binary data

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

Reply via email to