Commit 95a3efd8 authored by Stefan Behnel's avatar Stefan Behnel

fix bytearray.append(ch) for non-ASCII 1-char strings in Py2 and extend test

parent 6405dd9e
......@@ -695,7 +695,7 @@ static CYTHON_INLINE int __Pyx_PyByteArray_AppendObject(PyObject* bytearray, PyO
PyErr_SetString(PyExc_ValueError, "string must be of size 1");
return -1;
}
ival = PyString_AS_STRING(value)[0];
ival = (unsigned char) (PyString_AS_STRING(value)[0]);
} else
#endif
{
......
......@@ -211,6 +211,21 @@ def bytearray_append(bytearray b, char c, int i, object o):
>>> print(b.decode('ascii'))
abcXxyz
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('\\xc3') if IS_PY3 else b'\\xc3')
>>> print(b[:-1].decode('ascii'))
abcXxy
>>> print('%x' % b[-1])
c3
>>> b = bytearray(b'abc')
>>> try:
... b = bytearray_append(b, ord('x'), ord('y'), b'zz')
... except (TypeError, ValueError): pass # (Py3, Py2)
... else: print("FAIL")
>>> print(b.decode('ascii'))
abcXxy
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, -1, ord('y'), ord('z')) # doctest: +ELLIPSIS
Traceback (most recent call last):
......
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