Stefan Behnel | 14 Dec 11:51 2010
Picon

Re: " __builtin__.str has the wrong size, try recompiling" with Python 2.7

Saúl Ibarra Corretgé, 14.12.2010 11:16:
>> Depends on what you do with it. In Py2, 'str' (as 'bytes' in Py3) is a
>> dynamically sized type (PyVarObject). As such, it cannot currently be
>> subtyped in Cython, and other things may or may not work with it.
>
> It's a Py2 application and str is not subtyped.
>
>> Could you provide some details about your use case?
>
> We wrap a C library and have several cdef classes around it. Generally
> it's used to hold attributes (cdef str something) and also for
> readonly attributes (cdef readonly str something).

Ah, ok, then just drop that declaration completely. Cython knows about the 
builtin Python types. However, 'bytes' may still be what you want to use 
here, unless you want to store text, in which case you may(!) want to 
consider using 'unicode' instead. 'str' is a somewhat underoptimised type 
in Cython due to its bytes/unicode duality when compiling for Python 3. For 
bytes and (especially) unicode, Cython can safely do various smart C-level 
optimisations internally.

https://sage.math.washington.edu:8091/hudson/job/cython-docs/doclinks/1/src/tutorial/strings.html

I should note that Cython 0.13 brings a another bunch of improvements here, 
as does 0.14 (which is close to release). Since you're migrating anyway, 
you should consider switching to 0.14 directly.

>>> Is this a bug on Cython 0.12.1?

Thinking about this some more, it's not even impossible that this *is* a 
(Continue reading)

Saúl Ibarra Corretgé | 15 Dec 00:04 2010
Picon

Re: " __builtin__.str has the wrong size, try recompiling" with Python 2.7

Hi,

> Ah, ok, then just drop that declaration completely. Cython knows about the
> builtin Python types. However, 'bytes' may still be what you want to use
> here, unless you want to store text, in which case you may(!) want to
> consider using 'unicode' instead. 'str' is a somewhat underoptimised type in
> Cython due to its bytes/unicode duality when compiling for Python 3. For
> bytes and (especially) unicode, Cython can safely do various smart C-level
> optimisations internally.
>
> https://sage.math.washington.edu:8091/hudson/job/cython-docs/doclinks/1/src/tutorial/strings.html
>
> I should note that Cython 0.13 brings a another bunch of improvements here,
> as does 0.14 (which is close to release). Since you're migrating anyway, you
> should consider switching to 0.14 directly.
>

There are 2 usecases I forgot to mention on my previous mail: we pass
str objects to C functions and we also use the 'not None'.

This two things work well with the builtin declaration in Python 2.5 /
2.6 but I'm getting that error from my first mail on 2.7. Now, if I
remove the builtin declaration I'm getting other errors like 'str
can't be coerced to a C type' and 'only extension type arguments can
have not None'.

If I understood correctly, switching to 'bytes' could help, but AFAIK
Python 2.5 doesn't have this type and I do want to support it for now,
so my only exit here would be to use only 'unicode', am I right?

(Continue reading)

Robert Bradshaw | 15 Dec 00:12 2010

Re: " __builtin__.str has the wrong size, try recompiling" with Python 2.7

On Tue, Dec 14, 2010 at 3:04 PM, Saúl Ibarra Corretgé <saghul <at> gmail.com> wrote:
> Hi,
>
>> Ah, ok, then just drop that declaration completely. Cython knows about the
>> builtin Python types. However, 'bytes' may still be what you want to use
>> here, unless you want to store text, in which case you may(!) want to
>> consider using 'unicode' instead. 'str' is a somewhat underoptimised type in
>> Cython due to its bytes/unicode duality when compiling for Python 3. For
>> bytes and (especially) unicode, Cython can safely do various smart C-level
>> optimisations internally.
>>
>> https://sage.math.washington.edu:8091/hudson/job/cython-docs/doclinks/1/src/tutorial/strings.html
>>
>> I should note that Cython 0.13 brings a another bunch of improvements here,
>> as does 0.14 (which is close to release). Since you're migrating anyway, you
>> should consider switching to 0.14 directly.
>>
>
> There are 2 usecases I forgot to mention on my previous mail: we pass
> str objects to C functions and we also use the 'not None'.
>
> This two things work well with the builtin declaration in Python 2.5 /
> 2.6 but I'm getting that error from my first mail on 2.7. Now, if I
> remove the builtin declaration I'm getting other errors like 'str
> can't be coerced to a C type' and 'only extension type arguments can
> have not None'.
>
> If I understood correctly, switching to 'bytes' could help, but AFAIK
> Python 2.5 doesn't have this type and I do want to support it for now,
> so my only exit here would be to use only 'unicode', am I right?
(Continue reading)

Saúl Ibarra Corretgé | 16 Dec 01:09 2010
Picon

Re: " __builtin__.str has the wrong size, try recompiling" with Python 2.7

> You might consider looking at
> http://wiki.cython.org/enhancements/stringliterals for some
> background. Simply put, when you write "bytes" in your Cython code it
> will become str in Python 2 and bytes in Python 3. Likewise "unicode"
> will be unicode in Python 2 and str in Python 3. We do this to
> facilitate writing a single codebase that works with both 2.x and 3.x.
>

Thanks for the pointer, starting to make sense now :-)

--

-- 
/Saúl
http://saghul.net | http://sipdoc.net

Stefan Behnel | 15 Dec 09:45 2010
Picon

Re: " __builtin__.str has the wrong size, try recompiling" with Python 2.7

Saúl Ibarra Corretgé, 15.12.2010 00:04:
>> Ah, ok, then just drop that declaration completely. Cython knows about the
>> builtin Python types. However, 'bytes' may still be what you want to use
>> here, unless you want to store text, in which case you may(!) want to
>> consider using 'unicode' instead. 'str' is a somewhat underoptimised type in
>> Cython due to its bytes/unicode duality when compiling for Python 3. For
>> bytes and (especially) unicode, Cython can safely do various smart C-level
>> optimisations internally.
>>
>> https://sage.math.washington.edu:8091/hudson/job/cython-docs/doclinks/1/src/tutorial/strings.html
>>
>> I should note that Cython 0.13 brings a another bunch of improvements here,
>> as does 0.14 (which is close to release). Since you're migrating anyway, you
>> should consider switching to 0.14 directly.
>
> There are 2 usecases I forgot to mention on my previous mail: we pass
> str objects to C functions and we also use the 'not None'.

That's just fine. I didn't mean to drop the declaration of variables as 
builtin types, just the declaration of 'str' as an external extension type.

I.e., do *not* write

     cdef extern from "stringobject.h":
         ctypedef class __builtin__.str [object PyStringObject]:
             pass

because

     cdef str somestr
(Continue reading)

Saúl Ibarra Corretgé | 16 Dec 01:10 2010
Picon

Re: " __builtin__.str has the wrong size, try recompiling" with Python 2.7

> That's just fine. I didn't mean to drop the declaration of variables as
> builtin types, just the declaration of 'str' as an external extension type.
>
> I.e., do *not* write
>
>    cdef extern from "stringobject.h":
>        ctypedef class __builtin__.str [object PyStringObject]:
>            pass
>
> because
>
>    cdef str somestr
>
> will work just fine out of the box, and will work *much* better without the
> extern type declaration.
>

I misunderstood your previous mail then, it's clear now :-)

Thanks a lot for the help!

--

-- 
/Saúl
http://saghul.net | http://sipdoc.net


Gmane