[Cython] [cython-users] Re: Cython 0.20 beta 1

Stefan Behnel stefan_ml at behnel.de
Tue Jan 7 13:29:15 CET 2014


Hi,

let's keep this on the cython-devel list.


Andreas van Cranenburgh, 07.01.2014 12:54:
> On Tuesday, January 7, 2014 12:25:20 PM UTC+1, Stefan Behnel wrote:
>> I added an entry. Could you provide a code snippet that shows what you 
>> were 
>> doing? Just to be sure it's really a problem in your code and not a wrong 
>> assumption in Cython. Optimisations shouldn't break code. 
> 
> Here goes:
> 
> $ cat t.pyx
> cdef str foo
> bar = u'bar'
> foo = 'foo%s' % (bar, )
> print(foo)
> $ python -c 'import pyximport; pyximport.install(); import t'  # Cython 
> 0.19.2
> foobar

Interesting. I assume "python" is Py2.x.


> $ python -c 'import pyximport; pyximport.install(); import t'  # Cython 
> 0.20b1
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
>   File 
> "/home/acranenb/.local/lib/python2.7/site-packages/Cython-0.20b1-py2.7-linux-x86_64.egg/pyximport/pyximport.py", 
> line 431, in load_module
>     language_level=self.language_level)
>   File 
> "/home/acranenb/.local/lib/python2.7/site-packages/Cython-0.20b1-py2.7-linux-x86_64.egg/pyximport/pyximport.py", 
> line 210, in load_module
>     mod = imp.load_dynamic(name, so_path)
>   File "t.pyx", line 3, in init t 
> (/home/acranenb/.pyxbld/temp.linux-x86_64-2.7/pyrex/t.c:824)
>     foo = 'foo%s' % (bar, )
> ImportError: Building module t failed: ['TypeError: Expected str, got 
> unicode\n']

Hmm, the problem here is not the string formatting, it's the explicitly
typed "foo". The string formatting expands the plain str formatting string
into a Unicode string in Py2, but that's not a "str" any more, so the
subsequent assignment fails due to Cython's type check. You could simply
your code to this:

    bar = u'bar'
    cdef str foo = bar

(if you do this inside of a function, Cython's type inference should be
able to deduct a compile time error, but it doesn't do that at the global
module level)

I wonder why it doesn't produce an error in Cython 0.19 for you...

Stefan



More information about the cython-devel mailing list