fix bytearray.append(ch) for non-ASCII 1-char strings in Py2 and extend test
authorStefan Behnel <stefan_ml@behnel.de>
Sat, 4 Jan 2014 21:39:08 +0000 (22:39 +0100)
committerStefan Behnel <stefan_ml@behnel.de>
Sat, 4 Jan 2014 21:39:08 +0000 (22:39 +0100)
Cython/Utility/StringTools.c
tests/run/bytearraymethods.pyx

index 0869f8184eafe8b961cdd7af436723bff9aeafc7..7b4b15c22832e16924779005baee0bb8ccb6b373 100644 (file)
@@ -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
     {
index 16c9d5fe3f90bb24a4525082b4f959b5bbfddbc3..b2b27e2fd0532f899b03c12bac7ff479b3de85a1 100644 (file)
@@ -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):