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

Stefan Behnel stefan_ml at behnel.de
Tue Jan 7 15:09:37 CET 2014

CC-ing cython-users again, since others might run into this, too.

Stefan Behnel, 07.01.2014 13:29:
> 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...

Tried it, and it seems like it simply doesn't generate a type check at all.
That's clearly wrong and apparently fixed in 0.20, although I don't
remember a change specifically in that corner.

What you can do in Cython 0.20 to make it work with a statically typed
variable, is to use "cdef basestring foo" as a type instead of "str", but
that doesn't work in older Cython versions.

Or, in fact, you could not type it at all. Cython 0.20 should be able to
figure it out all by itself now (as it understands what "some_string %
something_else" actually does) and automatically type the result as basestring.


More information about the cython-devel mailing list