cpdef bytes get_bytes(object _key): cdef bytes key try: key = _key.encode('utf8') except AttributeError: key = _key
return key
--
--- You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stefan
Yes, in detail, many times. I find the panoply of string types and encodings and encoding and decoding to be completely confusing, especially because the string types are similar but different between python 2, python 3 and cython. It is maybe the one thing that I still struggle with the most in python, especially because I am always dealing with c++ code that works with std::string, and python has all these string-like things.
As an American, I know I have been able to avoid Unicode mostly because almost everything we do does not use Unicode. But the rest of the world is not so lucky.What I settled on was to internally do everything with bytes and convert inputs to bytes in every function. Then the string type is clear between python 2 and python 3. What if didn't do that though and went for the default string type in each of the python version?
Is there a notion of the default string type in the given python version in cython? Something like cython.default_string? That would be very useful.
Or maybe I'm thinking about the problem all wrong.
These directives should be enough to automatically convert your Python
strings (Py2 and Py3) into C++ strings, no extra helpers required.
(The only think I'm not sure of is handling subclasses of unicode, is
that important?) What kinds of errors are you seeing when you just do
self.field = py_object # where field is of type std::string
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/oqk3GQ2pJ8M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
On Fri, Feb 1, 2019, 12:32 AM Robert Bradshaw <robe...@gmail.com> wrote:These directives should be enough to automatically convert your Python
strings (Py2 and Py3) into C++ strings, no extra helpers required.
(The only think I'm not sure of is handling subclasses of unicode, is
that important?) What kinds of errors are you seeing when you just do
self.field = py_object # where field is of type std::stringIt was enough to only have the directives when I was only running under python2. When I switched to py3 I would get:Expected bytes, got stringIt seems Cython does not want to encode Unicode strings to bytes automatically when assigning to a std::string anymore. This is under Cython 0.28.5.I have no need to support string subclasses. I can pass a string literal into a function and it will raise the exception when it tried to directly assign it.
On Thu, Jan 31, 2019 at 7:35 PM Justin Israel <justin...@gmail.com> wrote:On Fri, Feb 1, 2019, 12:32 AM Robert Bradshaw <robe...@gmail.com> wrote:These directives should be enough to automatically convert your Python
strings (Py2 and Py3) into C++ strings, no extra helpers required.
(The only think I'm not sure of is handling subclasses of unicode, is
that important?) What kinds of errors are you seeing when you just do
self.field = py_object # where field is of type std::stringIt was enough to only have the directives when I was only running under python2. When I switched to py3 I would get:Expected bytes, got stringIt seems Cython does not want to encode Unicode strings to bytes automatically when assigning to a std::string anymore. This is under Cython 0.28.5.I have no need to support string subclasses. I can pass a string literal into a function and it will raise the exception when it tried to directly assign it.Hmm... that does look like a bug then.
I'm going to try and fix Cython.