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
Gwenaël Samain
cython
Commits
bc3667ab
Commit
bc3667ab
authored
Oct 31, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Plain Diff
merge 0.21.x branch into master
parents
dba0a5f0
ea071f25
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
171 additions
and
22 deletions
+171
-22
Cython/Utility/CppConvert.pyx
Cython/Utility/CppConvert.pyx
+7
-18
tests/run/cpp_stl_string_ascii_auto_encoding.pyx
tests/run/cpp_stl_string_ascii_auto_encoding.pyx
+14
-4
tests/run/cpp_stl_string_ascii_auto_encoding_str.pyx
tests/run/cpp_stl_string_ascii_auto_encoding_str.pyx
+150
-0
No files found.
Cython/Utility/CppConvert.pyx
View file @
bc3667ab
...
...
@@ -24,26 +24,15 @@ cdef extern from *:
cdef
cppclass
string
"{{type}}"
:
char
*
data
()
size_t
size
()
cdef
object
__Pyx_PyObject_FromStringAndSize
(
char
*
,
size_t
)
cdef
object
__Pyx_PyBytes_FromStringAndSize
(
char
*
,
size_t
)
cdef
object
__Pyx_PyByteArray_FromStringAndSize
(
char
*
,
size_t
)
cdef
object
__Pyx_PyUnicode_FromStringAndSize
(
char
*
,
size_t
)
@
cname
(
"{{cname}}"
)
cdef
inline
object
{{
cname
}}(
const
string
&
s
):
return
__Pyx_PyObject_FromStringAndSize
(
s
.
data
(),
s
.
size
())
@
cname
(
"{{cname.replace("
PyObject
", "
PyUnicode
", 1)}}"
)
cdef
inline
object
{{
cname
.
replace
(
"PyObject"
,
"PyUnicode"
,
1
)}}(
const
string
&
s
):
return
__Pyx_PyUnicode_FromStringAndSize
(
s
.
data
(),
s
.
size
())
@
cname
(
"{{cname.replace("
PyObject
", "
PyBytes
", 1)}}"
)
cdef
inline
object
{{
cname
.
replace
(
"PyObject"
,
"PyBytes"
,
1
)}}(
const
string
&
s
):
return
__Pyx_PyBytes_FromStringAndSize
(
s
.
data
(),
s
.
size
())
{{
for
py_type
in
[
'PyObject'
,
'PyUnicode'
,
'PyStr'
,
'PyBytes'
,
'PyByteArray'
]}}
cdef
extern
from
*
:
cdef
object
__Pyx_
{{
py_type
}}
_FromStringAndSize
(
char
*
,
size_t
)
@
cname
(
"{{cname.replace("
PyObject
", "
PyByteArray
", 1)}}"
)
cdef
inline
object
{{
cname
.
replace
(
"PyObject"
,
"PyByteArray"
,
1
)}}(
const
string
&
s
):
return
__Pyx_PyByteArray_FromStringAndSize
(
s
.
data
(),
s
.
size
())
@
cname
(
"{{cname.replace("
PyObject
", py_type, 1)}}"
)
cdef
inline
object
{{
cname
.
replace
(
"PyObject"
,
py_type
,
1
)}}(
const
string
&
s
):
return
__Pyx_
{{
py_type
}}
_FromStringAndSize
(
s
.
data
(),
s
.
size
())
{{
endfor
}}
#################### vector.from_py ####################
...
...
tests/run/cpp_stl_string_ascii_auto_encoding.pyx
View file @
bc3667ab
...
...
@@ -7,11 +7,8 @@ cimport cython
from
libcpp.string
cimport
string
b_asdf
=
b'asdf'
b_asdg
=
b'asdg'
b_s
=
b's'
s_asdf
=
'asdf'
u_asdf
=
u'asdf'
u_asdg
=
u'asdg'
u_s
=
u's'
...
...
@@ -138,3 +135,16 @@ def test_unicode_cast(a):
cdef
string
s
=
a
assert
s
.
length
()
==
<
size_t
>
len
(
a
),
"%d != %d"
%
(
s
.
length
(),
len
(
a
))
return
<
unicode
>
s
def
test_str_cast
(
a
):
"""
>>> s = test_str_cast(b'abc')
>>> type(s) is type(s_asdf) or type(s)
True
>>> print(s)
abc
"""
cdef
string
s
=
a
assert
s
.
length
()
==
<
size_t
>
len
(
a
),
"%d != %d"
%
(
s
.
length
(),
len
(
a
))
return
<
str
>
s
tests/run/cpp_stl_string_ascii_auto_encoding_str.pyx
0 → 100644
View file @
bc3667ab
# mode: run
# tag: cpp
# cython: c_string_encoding=ascii, c_string_type=str
cimport
cython
from
libcpp.string
cimport
string
b_asdf
=
b'asdf'
u_asdf
=
u'asdf'
s_asdf
=
'asdf'
s_s
=
's'
def
test_conversion
(
py_obj
):
"""
>>> test_conversion(b_asdf) == s_asdf or test_conversion(b_asdf)
True
>>> test_conversion(u_asdf) == s_asdf or test_conversion(u_asdf)
True
>>> test_conversion(123) # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError: expected ..., int found
"""
cdef
string
s
=
py_obj
assert
<
size_t
>
len
(
py_obj
)
==
s
.
length
(),
'%d != %d'
%
(
len
(
py_obj
),
s
.
length
())
return
s
def
test_empty
(
py_obj
):
"""
>>> test_empty('')
True
>>> test_empty('abc')
False
>>> test_empty(u_asdf[:0])
True
>>> test_empty(u_asdf)
False
"""
cdef
string
a
=
py_obj
return
a
.
empty
()
def
test_push_back
(
a
):
"""
>>> test_push_back(b_asdf) == s_asdf + s_s
True
>>> test_push_back(u_asdf) == s_asdf + s_s
True
"""
cdef
string
s
=
a
s
.
push_back
(
<
char
>
ord
(
's'
))
return
s
def
test_clear
(
a
):
"""
>>> test_clear(u_asdf) == s_s[:0]
True
>>> test_clear(b_asdf) == s_s[:0]
True
"""
cdef
string
s
=
a
s
.
clear
()
return
s
def
test_assign
(
char
*
a
):
"""
>>> test_assign(b_asdf) == 'ggg'
True
"""
cdef
string
s
=
string
(
a
)
s
.
assign
(
<
char
*>
"ggg"
)
return
s
.
c_str
()
def
test_bytes_cast
(
a
):
"""
>>> b = test_bytes_cast(b'abc')
>>> isinstance(b, bytes)
True
>>> print(b.decode('ascii'))
abc
>>> b = test_bytes_cast(b'abc
\
\
xe4
\
\
xfc')
>>> isinstance(b, bytes)
True
>>> len(b)
5
>>> print(b[:3].decode('ascii'))
abc
>>> print(ord(b[3:4]))
228
>>> print(ord(b[4:5]))
252
"""
cdef
string
s
=
a
assert
s
.
length
()
==
<
size_t
>
len
(
a
),
"%d != %d"
%
(
s
.
length
(),
len
(
a
))
return
<
bytes
>
s
def
test_bytearray_cast
(
a
):
"""
>>> b = test_bytearray_cast(b'abc')
>>> isinstance(b, bytearray)
True
>>> print(b.decode('ascii'))
abc
>>> b = test_bytearray_cast(b'abc
\
\
xe4
\
\
xfc')
>>> isinstance(b, bytearray)
True
>>> len(b)
5
>>> print(b[:3].decode('ascii'))
abc
>>> print(ord(b[3:4]))
228
>>> print(ord(b[4:5]))
252
"""
cdef
string
s
=
a
assert
s
.
length
()
==
<
size_t
>
len
(
a
),
"%d != %d"
%
(
s
.
length
(),
len
(
a
))
return
<
bytearray
>
s
def
test_unicode_cast
(
a
):
"""
>>> u = test_unicode_cast(b'abc')
>>> type(u) is type(u_asdf) or type(u)
True
>>> print(u)
abc
"""
cdef
string
s
=
a
assert
s
.
length
()
==
<
size_t
>
len
(
a
),
"%d != %d"
%
(
s
.
length
(),
len
(
a
))
return
<
unicode
>
s
def
test_str_cast
(
a
):
"""
>>> s = test_str_cast(b'abc')
>>> type(s) is type(s_asdf) or type(s)
True
>>> print(s)
abc
"""
cdef
string
s
=
a
assert
s
.
length
()
==
<
size_t
>
len
(
a
),
"%d != %d"
%
(
s
.
length
(),
len
(
a
))
return
<
str
>
s
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