Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
3b04d75a
Commit
3b04d75a
authored
Jun 20, 2018
by
scoder
Committed by
GitHub
Jun 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2379 from gabrieldemarmiesse/test_string_5
Adding tests for "Unicode and passing strings" part 5
parents
cbdca7c2
41dc52e6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
22 deletions
+32
-22
docs/examples/tutorial/string/api_func.pyx
docs/examples/tutorial/string/api_func.pyx
+5
-0
docs/examples/tutorial/string/to_unicode.pxd
docs/examples/tutorial/string/to_unicode.pxd
+1
-0
docs/examples/tutorial/string/to_unicode.pyx
docs/examples/tutorial/string/to_unicode.pyx
+21
-0
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+5
-22
No files found.
docs/examples/tutorial/string/api_func.pyx
0 → 100644
View file @
3b04d75a
from
to_unicode
cimport
_ustring
def
api_func
(
s
):
text
=
_ustring
(
s
)
# ...
docs/examples/tutorial/string/to_unicode.pxd
0 → 100644
View file @
3b04d75a
cdef
unicode
_ustring
(
s
)
docs/examples/tutorial/string/to_unicode.pyx
0 → 100644
View file @
3b04d75a
# to_unicode.pyx
from
cpython.version
cimport
PY_MAJOR_VERSION
cdef
unicode
_ustring
(
s
):
if
type
(
s
)
is
unicode
:
# fast path for most common case(s)
return
<
unicode
>
s
elif
PY_MAJOR_VERSION
<
3
and
isinstance
(
s
,
bytes
):
# only accept byte strings in Python 2.x, not in Py3
return
(
<
bytes
>
s
).
decode
(
'ascii'
)
elif
isinstance
(
s
,
unicode
):
# an evil cast to <unicode> might work here in some(!) cases,
# depending on what the further processing does. to be safe,
# we can always create a copy instead
return
unicode
(
s
)
else
:
raise
TypeError
(
"Could not convert to unicode."
)
docs/src/tutorial/strings.rst
View file @
3b04d75a
...
...
@@ -236,30 +236,13 @@ way to go, since it allows for easy adaptation of the input normalisation
process later.
This kind of input normalisation function will commonly look similar to
the following::
from cpython.version cimport PY_MAJOR_VERSION
cdef unicode _ustring(s):
if type(s) is unicode:
# fast path for most common case(s)
return <unicode>s
elif PY_MAJOR_VERSION < 3 and isinstance(s, bytes):
# only accept byte strings in Python 2.x, not in Py3
return (<bytes>s).decode('ascii')
elif isinstance(s, unicode):
# an evil cast to <unicode> might work here in some(!) cases,
# depending on what the further processing does. to be safe,
# we can always create a copy instead
return unicode(s)
else:
raise TypeError(...)
the following:
And should then be used like this::
.. literalinclude:: ../../examples/tutorial/string/to_unicode.pyx
def api_func(s)
:
text = _ustring(s)
...
And should then be used like this
:
.. literalinclude:: ../../examples/tutorial/string/api_func.pyx
Similarly, if the further processing happens at the byte level, but Unicode
string input should be accepted, then the following might work, if you are
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment