Commit 3d6f0126 authored by Stefan Behnel's avatar Stefan Behnel

make sure we always optimise bytearray.append(INT_LITERAL)

parent 73e24e80
......@@ -2450,7 +2450,7 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform):
func_type = self.PyByteArray_Append_func_type
value = unwrap_coerced_node(args[1])
if value.type.is_int:
if value.type.is_int or isinstance(value, ExprNodes.IntNode):
value = value.coerce_to(PyrexTypes.c_int_type, self.current_env())
utility_code = UtilityCode.load_cached("ByteArrayAppend", "StringTools.c")
elif value.is_string_literal:
......
......@@ -198,23 +198,24 @@ def bytearray_decode_unbound_method(bytearray s, start=None, stop=None):
else:
return bytearray.decode(s[start:stop], 'utf8')
@cython.test_fail_if_path_exists('//SimpleCallNode')
@cython.test_assert_path_exists('//PythonCapiCallNode')
def bytearray_append(bytearray b, signed char c, int i, object o):
"""
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('z'))
>>> print(b.decode('ascii'))
abcXxyz
abcX@xyz
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('z') if IS_PY3 else b'z')
>>> print(b.decode('ascii'))
abcXxyz
abcX@xyz
>>> 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
abcX@xy
>>> print('%x' % b[-1])
c3
......@@ -224,44 +225,45 @@ def bytearray_append(bytearray b, signed char c, int i, object o):
... except (TypeError, ValueError): pass # (Py3, Py2)
... else: print("FAIL")
>>> print(b.decode('ascii'))
abcXxy
abcX@xy
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, -1, ord('y'), ord('z')) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcX
abcX@
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), -1, ord('z')) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXx
abcX@x
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), 256, ord('z')) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXx
abcX@x
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), ord('y'), -1) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXxy
abcX@xy
>>> b = bytearray(b'abc')
>>> b = bytearray_append(b, ord('x'), ord('y'), 256) # doctest: +ELLIPSIS
Traceback (most recent call last):
ValueError: ...
>>> print(b.decode('ascii'))
abcXxy
abcX@xy
"""
assert b.append('X') is None
b.append(64)
b.append(c)
b.append(i)
b.append(o)
......
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