On May 10, 2010, at 9:54 PM, Paul Harrison wrote: > On Tue, May 11, 2010 at 2:16 PM, Robert Bradshaw > <[email protected]> wrote: >> On May 10, 2010, at 5:27 PM, Paul Harrison wrote: >> >>> Hello, >>> >>> I found a problem in Cython 12.1 (with Python 2.6.1). If you try to >>> compile the following: >>> >>> str1 = 'a' >>> str2 = 'a\n' >>> >>> It produces the following code, which will not compile: >>> >>> static char __pyx_k__a[] = "a"; >>> static char __pyx_k__a >>> [] = "a\n"; >> >> I thought C was whitespace agnostic. What compiler are you using? In >> any case, it's probably a trivial fix. >> > > The problem is the duplicated name __pyx_k__a. The C compiler (gcc) > ignores the newline, which is probably why this hasn't caused a > problem in the past. You need the two similar string constants in > order for there to be a problem.
Ah, that's pretty subtle. > > >>> Also, I find I can no longer cast str to char* (again with Python >>> 2.6.1). The documentation seems to indicate this should only be a >>> problem in Python 3, or have I missed something? >> >> >> Cython is super strict about this right now even if you're only using >> Py2 (too strict in my opinion, but that's a whole can of worms there >> that we're not going to re-visit at this time...) Cast to an object >> first, i.e. if x is a str, do >> >> char* s = <object>x >> > > Ah, thanks. This fixes the error, which only occurs when the variable > is declared to be a str. I take it the correct idiom is to do > everything with bytes. > >> (Short of type checking, is there a good way to encode in 3.x and >> still allow str in 2.x?) Also, you might consider whether it is worth >> declaring your variable to be of type str--are you using this fact >> anywhere? >> > > I was hoping for a speed increase from declaring it as str so that I > could get its length and contents quickly. This worked in Cython 0.11, > but stopped working when I upgraded to 0.12.1. Yes, see http://wiki.cython.org/enhancements/stringliterals . You could use bytes instead of str everywhere, because that's essentially what you have. > > > Thanks for the quick answer. No problem--please let us know if you find any other issues. - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
