[Cython] Fixing NumPy support for Python 3 (Stefan, please help!)

Stefan Behnel stefan_ml at behnel.de
Fri Feb 18 16:23:32 CET 2011


Lisandro Dalcin, 18.02.2011 15:38:
> On 18 February 2011 02:19, Stefan Behnel<stefan_ml at behnel.de>  wrote:
>> Lisandro Dalcin, 17.02.2011 17:24:
>>>
>>>
>>> OK, I've found an alternative workaround. What do you think?
>>>
>>> diff --git a/Cython/Compiler/Interpreter.py
>>> b/Cython/Compiler/Interpreter.py
>>> index 83cb184..9fb5fe5 100644
>>> --- a/Cython/Compiler/Interpreter.py
>>> +++ b/Cython/Compiler/Interpreter.py
>>> @@ -6,6 +6,7 @@ For now this only covers parse tree to value conversion of
>>>   compile-time values.
>>>   """
>>>
>>> +import sys
>>>   from Nodes import *
>>>   from ExprNodes import *
>>>   from Errors import CompileError
>>> @@ -44,6 +45,10 @@ def interpret_compiletime_options(optlist, optdict,
>>> type_env=None, type_args=())
>>>               else:
>>>                   raise CompileError(node.pos, "Type not allowed here.")
>>>           else:
>>> +            if (sys.version_info[0]>=3 and
>>> +                isinstance(node, StringNode) and
>>> +                node.unicode_value is not None):
>>> +                return (node.unicode_value, node.pos)
>>>               return (node.compile_time_value(empty_scope), node.pos)
>>>
>>>       if optlist:
>>> @@ -52,6 +57,7 @@ def interpret_compiletime_options(optlist, optdict,
>>> type_env=None, type_args=())
>>>           assert isinstance(optdict, DictNode)
>>>           new_optdict = {}
>>>           for item in optdict.key_value_pairs:
>>> -            new_optdict[item.key.value] = interpret(item.value,
>>> item.key.value)
>>> +            new_key, dummy = interpret(item.key, None)
>>> +            new_optdict[new_key] = interpret(item.value, item.key.value)
>>>           optdict = new_optdict
>>>       return (optlist, new_optdict)
>>
>> This still isn't something that looks right. It just does the same thing at
>> a different place. Actually, I'm not sure there is a way to "get it right".
>>
>
> IIUC, the purpose of that code is to bring stuff from .pyx code to
> Python world at Cython runtime, that code is trying to "coerce" a
> DictNode object to a regular Python dict to be used later for
> implementing compiler directives. So, if your pyx/pxd says
> {"mode":"strided"} , and that is going to be compiler directive
> options, you need to map StringNode ->  __builtin__.str (and then use
> node.value in Py3 or node.unicode_value in Py3).
>
> Stefan, look at numpy.pxd, you will see $cdef __cythonbufferdefaults__
> = {"mode": "strided"}$. W need to "convert" that declaration in a
> regular Python dict to be used in Buffer.py, in such a way that
> $"mode" in option_dict$ or $option_dict["mode"]$ work. Currently, in
> Python 3, the key,value pair endup being bytes, then things fails...
>
> I still think that what I'm doing is not so bad. After all, I'm
> hacking a method called  interpret_compiletime_options(), the purpose
> of its output is not code generation.

Hmm, right. I guess that'll do then.

Please go ahead and push this in. If it helps NumPy users in Py3, it's 
worth getting out rather sooner than later.

Stefan


More information about the cython-devel mailing list