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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
41dc52e6
Commit
41dc52e6
authored
Jun 19, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved an example from string.rst to the examples directory.
parent
5c04c1a8
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 @
41dc52e6
from
to_unicode
cimport
_ustring
def
api_func
(
s
):
text
=
_ustring
(
s
)
# ...
docs/examples/tutorial/string/to_unicode.pxd
0 → 100644
View file @
41dc52e6
cdef
unicode
_ustring
(
s
)
docs/examples/tutorial/string/to_unicode.pyx
0 → 100644
View file @
41dc52e6
# 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 @
41dc52e6
...
...
@@ -248,30 +248,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