8000 [mypyc] Add bytes concat op by 97littleleaf11 · Pull Request #10926 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[mypyc] Add bytes concat op #10926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 6, 2021
Prev Previous commit
Next Next commit
[mypyc] Support bytes concat
  • Loading branch information
97littleleaf11 committed Aug 4, 2021
commit e871d1b69d0077640fcc41935bdf75fb4ab8ff1d
2 changes: 2 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ Py_ssize_t CPyStr_Size_size_t(PyObject *str);
// Bytes operations


PyObject *CPyBytes_Concat(PyObject *a, PyObject *b);


// Set operations

Expand Down
9 changes: 9 additions & 0 deletions mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@

#include <Python.h>
#include "CPy.h"

PyObject *CPyBytes_Concat(PyObject *a, PyObject *b) {
if (PyByteArray_Check(a) && PyByteArray_Check(b)) {
return PyByteArray_Concat(a, b);
} else {
PyBytes_Concat(&a, b);
return a;
}
}
13 changes: 11 additions & 2 deletions mypyc/primitives/bytes_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
object_rprimitive, bytes_rprimitive, list_rprimitive, dict_rprimitive,
str_rprimitive, RUnion
)
from mypyc.primitives.registry import load_address_op, function_op

from mypyc.primitives.registry import load_address_op, function_op, binary_op

# Get the 'bytes' type object.
load_address_op(
Expand All @@ -29,3 +28,13 @@
return_type=bytes_rprimitive,
c_function_name='PyByteArray_FromObject',
error_kind=ERR_MAGIC)

# bytes + bytes
# bytearray + bytearray
binary_op(
name='+',
arg_types=[bytes_rprimitive, bytes_rprimitive],
return_type=bytes_rprimitive,
c_function_name='CPyBytes_Concat',
error_kind=ERR_MAGIC,
steals=[True, False])
9 changes: 9 additions & 0 deletions mypyc/test-data/irbuild-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ L0:
c = r6
return 1

[case testBytesOps]
def f1(a: bytes, b: bytes) -> bytes:
return a + b
[out]
def f1(a, b):
a, b, r0 :: bytes
L0:
r0 = CPyBytes_Concat(a, b)
return r0
18 changes: 18 additions & 0 deletions mypyc/test-data/run-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ def test_concat() -> None:
b1 = b'123' + bytes()
b2 = b'456' + bytes()
assert b1 + b2 == b'123456'
b3 = b1 + b2
b3 = b3 + b1
assert b3 == b'123456123'
assert b1 == b'123'
assert b2 == b'456'
brr1: bytes = bytearray(3)
brr2: bytes = bytearray(range(5))
b4 = b1 + brr1
assert b4 == b'123\x00\x00\x00'
brr3 = brr1 + brr2
assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04')
assert len(brr3) == 8
brr3 = brr3 + bytearray([10])
assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04\n')
b5 = brr2 + b2
assert b5 == bytearray(b'\x00\x01\x02\x03\x04456')
b5 = b2 + brr2
assert b5 == b'456\x00\x01\x02\x03\x04'

def test_join() -> None:
seq = (b'1', b'"', b'\xf0')
Expand Down
0