Commit fc634304 authored by Stefan Behnel's avatar Stefan Behnel

speed up appending integer values to bytearray objects in Py3

parent f6439a49
...@@ -728,6 +728,20 @@ static CYTHON_INLINE int __Pyx_PyByteArray_AppendObject(PyObject* bytearray, PyO ...@@ -728,6 +728,20 @@ static CYTHON_INLINE int __Pyx_PyByteArray_AppendObject(PyObject* bytearray, PyO
} }
ival = (unsigned char) (PyString_AS_STRING(value)[0]); ival = (unsigned char) (PyString_AS_STRING(value)[0]);
} else } else
#else
#if CYTHON_USE_PYLONG_INTERNALS
if (likely(PyLong_CheckExact(value)) && likely(Py_SIZE(value) == 1 || Py_SIZE(value) == 0)) {
if (Py_SIZE(value) == 0) {
ival = 0;
} else {
ival = ((PyLongObject*)value)->ob_digit[0];
if (unlikely(ival > 255)) {
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
return -1;
}
}
} else
#endif
#endif #endif
{ {
// CPython calls PyNumber_Index() internally // CPython calls PyNumber_Index() internally
......
...@@ -207,6 +207,13 @@ def bytearray_append(bytearray b, signed char c, int i, object o): ...@@ -207,6 +207,13 @@ def bytearray_append(bytearray b, signed char c, int i, object o):
>>> print(b.decode('ascii')) >>> print(b.decode('ascii'))
abcX@xyz abcX@xyz
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), ord('y'), 0)
>>> print(b.decode('ascii')[:-1])
abcX@xy
>>> b[-1]
0
>>> b = bytearray(b'abc') >>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('z') if IS_PY3 else b'z') >>> b = bytearray_append(b, ord('x'), ord('y'), ord('z') if IS_PY3 else b'z')
>>> print(b.decode('ascii')) >>> print(b.decode('ascii'))
......
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