On Tue, Dec 22, 2009 at 10:58 AM, Stefan Behnel <[email protected]> wrote:
>
> Lisandro Dalcín, 22.12.2009 14:47:
>> While testing type inference in mpi4py, I noticed that code like the
>> one below is failing in the last line. It seems that in  Py>=2.6,
>> Python makes a coercion on "kw" and builds an actual dict instance
>> before calling.
>>
>> class Foo:
>>     def view(self, **kw):
>>         print (kw)
>>         for k in kw.keys():
>>             print (k)
>>
>> Foo().view(a=0, b=1, c=2)
>>
>> class MyDict(object):
>>     def __getitem__(self, k):
>>         assert k == 'a'
>>         return 7
>>     def keys(self):
>>         return ['a']
>>
>> Foo().view(**MyDict()) # <-- Here is the issue
>>
>>
>> Running on Py>=2.6, the code above gives:
>>
>> {'a': 0, 'c': 2, 'b': 1}
>> a
>> c
>> b
>> {'a': 7}
>> a
>>
>> so that makes me think that at some point Python is building a tmp
>> dict out of MyDict instance.
>
> Interesting. So, what's your point? It didn't work before and it works now
> by passing in a real dict. Does this impact Cython in any way?
>

Sorry, I was not clear enough. My point is that the line below fails:

Foo().view(**MyDict())

see yourself:



In [1]: import pyximport; pyximport.install()

In [2]: import kargs
{'a': 0, 'c': 2, 'b': 1}
a
c
b
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)

/u/dalcinl/tmp/<ipython console> in <module>()
...
---> 18 Foo().view(**MyDict())

ImportError: Building module failed: ['TypeError: keyword list must be
a dictionary\n']


So, in short Cython does not follow Python (>=2.6, including 3.x).
Cython should generate some extra code to support this new Python
feature:

tmp =MyDict()
if not isinstace(tmp, dict):
    tmp = dict(tmp)
Foo().view(**tmp)


PS: The support for new feature in core CPython is at Python/ceval.c,
function ext_do_call(). This feature was tracked here:
http://bugs.python.org/issue1686487


-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to