[Cython] Fixing NumPy support for Python 3 (Stefan, please help!)
Stefan Behnel
stefan_ml at behnel.de
Thu Feb 17 16:16:26 CET 2011
Lisandro Dalcin, 17.02.2011 15:32:
> Stefan, what do you think about the patch below? This hunk is part of
> a series of fixes required to get numpy-dev working under Python 3.2.
> The root of the issue is that __cythonbufferdefaults__ keys&values
> end-up being "bytes" (this coercion is triggered in Interpreter.py).
>
>
> diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
> index 5b339da..b72deef 100755
> --- a/Cython/Compiler/ExprNodes.py
> +++ b/Cython/Compiler/ExprNodes.py
> @@ -12,6 +12,7 @@ cython.declare(error=object, warning=object, warn_once=object,
> Builtin=object, Symtab=object, Utils=object, find_coercion_error
> debug_disposal_code=object, debug_temp_alloc=object, debug_coerc
>
> +import sys
> import operator
>
> from Errors import error, warning, warn_once, InternalError, CompileError
> @@ -1136,6 +1137,8 @@ class StringNode(PyConstNode):
> return self.result_code
>
> def compile_time_value(self, env):
> + if sys.version_info[0]>= 3 and self.unicode_value:
You must use "self.unicode_value is not None" here, it may be the empty string.
> + return self.unicode_value
> return self.value
Ok, that's a tricky one. Just because the compilation is running in Py3
doesn't mean that the correct compile time value is a Unicode string - we
don't know what it'll be used for. Doing the above will do the wrong thing
e.g. in this case:
DEF const_x = "abc"
cdef str x = const_x
The problem is: it is broken already, returning self.value is wrong because
it drops available type information by returning plain bytes instead of
str. And simply returning self.unicode_value in Py3 doesn't fix that.
Stefan
More information about the cython-devel
mailing list