Skip to content

[mypyc] Add bytearray support #10891

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 3 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions mypyc/codegen/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ def emit_cast(self,

# TODO: Verify refcount handling.
if (is_list_rprimitive(typ) or is_dict_rprimitive(typ) or is_set_rprimitive(typ)
or is_str_rprimitive(typ) or is_bytes_rprimitive(typ) or is_range_rprimitive(typ)
or is_float_rprimitive(typ) or is_int_rprimitive(typ) or is_bool_rprimitive(typ)):
or is_str_rprimitive(typ) or is_range_rprimitive(typ) or is_float_rprimitive(typ)
or is_int_rprimitive(typ) or is_bool_rprimitive(typ) or is_bit_rprimitive(typ)):
if declare_dest:
self.emit_line('PyObject *{};'.format(dest))
if is_list_rprimitive(typ):
Expand All @@ -463,8 +463,6 @@ def emit_cast(self,
prefix = 'PySet'
elif is_str_rprimitive(typ):
prefix = 'PyUnicode'
elif is_bytes_rprimitive(typ):
prefix = 'PyBytes'
elif is_range_rprimitive(typ):
prefix = 'PyRange'
elif is_float_rprimitive(typ):
Expand All @@ -484,6 +482,18 @@ def emit_cast(self,
'else {',
err,
'}')
elif is_bytes_rprimitive(typ):
if declare_dest:
self.emit_line('PyObject *{};'.format(dest))
check = '(PyBytes_Check({}) || PyByteArray_Check({}))'
if likely:
check = '(likely{})'.format(check)
self.emit_arg_check(src, dest, typ, check.format(src, src), optional)
self.emit_lines(
' {} = {};'.format(dest, src),
'else {',
err,
'}')
elif is_tuple_rprimitive(typ):
if declare_dest:
self.emit_line('{} {};'.format(self.ctype(typ), dest))
Expand Down
8 changes: 8 additions & 0 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ def __ne__(self, x: object) -> bool: pass
def __getitem__(self, i: int) -> int: pass
def join(self, x: Iterable[object]) -> bytes: pass

class bytearray:
@overload
def __init__(self) -> None: pass
@overload
def __init__(self, x: object) -> None: pass
@overload
def __init__(self, string: str, encoding: str, err: str = ...) -> None: pass

class bool(int):
def __init__(self, o: object = ...) -> None: ...
@overload
Expand Down
28 changes: 27 additions & 1 deletion mypyc/test-data/run-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,30 @@ def test_len() -> None:
# Use bytes() to avoid constant folding
b = b'foo' + bytes()
assert len(b) == 3
assert len(bytes()) == 0
assert len(bytes()) == 0

[case testBytearrayBasics]
from typing import Any

def test_init() -> None:
brr1: bytes = bytearray(3)
assert brr1 == bytearray(b'\x00\x00\x00')
assert brr1 == b'\x00\x00\x00'
l = [10, 20, 30, 40]
brr2: bytes = bytearray(l)
assert brr2 == bytearray(b'\n\x14\x1e(')
assert brr2 == b'\n\x14\x1e('
brr3: bytes = bytearray(range(5))
assert brr3 == bytearray(b'\x00\x01\x02\x03\x04')
assert brr3 == b'\x00\x01\x02\x03\x04'
brr4: bytes = bytearray('string', 'utf-8')
assert brr4 == bytearray(b'string')
assert brr4 == b'string'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly annotate the variables as bytes, since right now the inferred type will be bytearray, which doesn't have a mypyc primitive type, so it reverts back to object.

Also test calling a function that accepts bytes with an actual argument that is a bytearray object annotated with type Any, to check the runtime type checking from Any to bytes works if the runtime value is a bytearray .


def f(b: bytes) -> bool:
return True

def test_bytearray_passed_into_bytes() -> None:
assert f(bytearray(3))
brr1: Any = bytearray()
assert f(brr1)