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
f3c5faea
Commit
f3c5faea
authored
Dec 24, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inline bytearray.append()
parent
1d413f0a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
1 deletion
+86
-1
Cython/Compiler/Builtin.py
Cython/Compiler/Builtin.py
+4
-1
Cython/Utility/StringTools.c
Cython/Utility/StringTools.c
+33
-0
tests/run/bytearraymethods.pyx
tests/run/bytearraymethods.pyx
+49
-0
No files found.
Cython/Compiler/Builtin.py
View file @
f3c5faea
...
...
@@ -280,7 +280,10 @@ builtin_types_table = [
BuiltinMethod
(
"join"
,
"TO"
,
"T"
,
"__Pyx_PyBaseString_Join"
,
utility_code
=
UtilityCode
.
load
(
"StringJoin"
,
"StringTools.c"
)),
]),
(
"bytearray"
,
"PyByteArray_Type"
,
[]),
(
"bytearray"
,
"PyByteArray_Type"
,
[
BuiltinMethod
(
"append"
,
"Tz"
,
"r"
,
"__Pyx_PyByteArray_Append"
,
utility_code
=
UtilityCode
.
load
(
"ByteArrayAppend"
,
"StringTools.c"
)),
]),
(
"bytes"
,
"PyBytes_Type"
,
[
BuiltinMethod
(
"__contains__"
,
"TO"
,
"b"
,
"PySequence_Contains"
),
BuiltinMethod
(
"join"
,
"TO"
,
"O"
,
"__Pyx_PyBytes_Join"
,
utility_code
=
UtilityCode
.
load
(
"StringJoin"
,
"StringTools.c"
)),
...
...
Cython/Utility/StringTools.c
View file @
f3c5faea
...
...
@@ -678,3 +678,36 @@ static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* value
return
PyObject_CallMethodObjArgs
(
sep
,
PYIDENT
(
"join"
),
values
,
NULL
)
}
#endif
//////////////////// ByteArrayAppend.proto ////////////////////
static
CYTHON_INLINE
int
__Pyx_PyByteArray_Append
(
PyObject
*
bytearray
,
Py_ssize_t
value
);
//////////////////// ByteArrayAppend ////////////////////
//@requires: ObjectHandling.c::PyObjectCallMethod
// signature uses Py_ssize_t to make coercions use PyNumber_Index(), as CPython does
static
CYTHON_INLINE
int
__Pyx_PyByteArray_Append
(
PyObject
*
bytearray
,
Py_ssize_t
value
)
{
PyObject
*
pyval
,
*
retval
;
#if CYTHON_COMPILING_IN_CPYTHON
Py_ssize_t
n
=
Py_SIZE
(
bytearray
);
if
(
likely
((
value
>=
0
)
&
(
value
<=
255
)))
{
if
(
likely
(
n
<
PY_SSIZE_T_MAX
))
{
if
(
unlikely
(
PyByteArray_Resize
(
bytearray
,
n
+
1
)
<
0
))
return
-
1
;
PyByteArray_AS_STRING
(
bytearray
)[
n
]
=
value
;
return
0
;
}
}
#endif
pyval
=
PyInt_FromLong
(
value
);
if
(
unlikely
(
!
pyval
))
return
-
1
;
retval
=
__Pyx_PyObject_CallMethod1
(
bytearray
,
PYIDENT
(
"append"
),
pyval
);
Py_DECREF
(
pyval
);
if
(
unlikely
(
!
retval
))
return
-
1
;
Py_DECREF
(
retval
);
return
0
;
}
tests/run/bytearraymethods.pyx
View file @
f3c5faea
...
...
@@ -193,3 +193,52 @@ def bytearray_decode_unbound_method(bytearray s, start=None, stop=None):
return
bytearray
.
decode
(
s
[
start
:],
'utf8'
)
else
:
return
bytearray
.
decode
(
s
[
start
:
stop
],
'utf8'
)
def
bytearray_append
(
bytearray
b
,
char
c
,
int
i
,
object
o
):
"""
>>> b = bytearray('abc'.encode('ascii'))
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('z'))
>>> print(b.decode('ascii'))
abcXxyz
>>> b = bytearray('abc'.encode('ascii'))
>>> b = bytearray_append(b, -1, ord('y'), ord('z')) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcX
>>> b = bytearray('abc'.encode('ascii'))
>>> b = bytearray_append(b, ord('x'), -1, ord('z')) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXx
>>> b = bytearray('abc'.encode('ascii'))
>>> b = bytearray_append(b, ord('x'), 256, ord('z')) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXx
>>> b = bytearray('abc'.encode('ascii'))
>>> b = bytearray_append(b, ord('x'), ord('y'), -1) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXxy
>>> b = bytearray('abc'.encode('ascii'))
>>> b = bytearray_append(b, ord('x'), ord('y'), 256) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXxy
"""
b
.
append
(
'X'
)
b
.
append
(
c
)
b
.
append
(
i
)
b
.
append
(
o
)
return
b
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