[Cython] Odd behavior with std::string and .decode()

Stefan Behnel stefan_ml at behnel.de
Thu Jul 19 12:33:52 CEST 2012


Stefan Behnel, 06.07.2012 06:48:
> One thing I would generally suggest is to do this:
> 
>     descr = self._this.get_description()
>     return descr.data()[:descr.size()].decode('utf-8')
> 
> Avoids the call to strlen() by explicitly slicing the pointer. Also avoids
> needing to make sure the C string is 0-terminated.
> 
> Barry Warsaw, 06.07.2012 00:29:
>> I looked at the generated code in the first example, but didn't really see
>> anything obvious.  There are no NULs in the char* description afaict.  I
>> haven't yet tested Cython 0.16 or 0.17 to see if this behaves differently.
> 
> I wouldn't know any differences out of the top of my head, except that 0.17
> has generally better support for STL containers and std:string (but that's
> unrelated to this failure). I'm planning to enable direct support for
> cpp_string.decode(...) as well, but that's not implemented yet. It would
> basically generate the verbose code above automatically.

Done.

https://github.com/cython/cython/commit/bf702727e1bb129e9d74548625658a224ce9e30d

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

Stefan


More information about the cython-devel mailing list