Commit c9f7bce2 authored by Stefan Behnel's avatar Stefan Behnel

docs: Give the string input conversion function in the string tutorial a...

docs: Give the string input conversion function in the string tutorial a better name and clarify some comments.
parent 3b04d75a
from to_unicode cimport _ustring from to_unicode cimport _text
def api_func(s): def api_func(s):
text = _ustring(s) text_input = _text(s)
# ... # ...
cdef unicode _ustring(s) cdef unicode _text(s)
...@@ -2,19 +2,20 @@ ...@@ -2,19 +2,20 @@
from cpython.version cimport PY_MAJOR_VERSION from cpython.version cimport PY_MAJOR_VERSION
cdef unicode _ustring(s): cdef unicode _text(s):
if type(s) is unicode: if type(s) is unicode:
# fast path for most common case(s) # Fast path for most common case(s).
return <unicode>s return <unicode>s
elif PY_MAJOR_VERSION < 3 and isinstance(s, bytes): elif PY_MAJOR_VERSION < 3 and isinstance(s, bytes):
# only accept byte strings in Python 2.x, not in Py3 # Only accept byte strings as text input in Python 2.x, not in Py3.
return (<bytes>s).decode('ascii') return (<bytes>s).decode('ascii')
elif isinstance(s, unicode): elif isinstance(s, unicode):
# an evil cast to <unicode> might work here in some(!) cases, # We know from the fast path above that 's' can only be a subtype here.
# depending on what the further processing does. to be safe, # An evil cast to <unicode> might still work in some(!) cases,
# we can always create a copy instead # depending on what the further processing does. To be safe,
# we can always create a copy instead.
return unicode(s) return unicode(s)
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment