Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[mypyc] Add primitive type for bytearray
  • Loading branch information
JukkaL committed Jan 9, 2026
commit 27fe2f67b7e6d65d59440558fd52c606e9b52faf
11 changes: 11 additions & 0 deletions mypyc/codegen/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
RUnion,
int_rprimitive,
is_bool_or_bit_rprimitive,
is_bytearray_rprimitive,
is_bytes_rprimitive,
is_dict_rprimitive,
is_fixed_width_rtype,
Expand Down Expand Up @@ -664,6 +665,16 @@ def emit_cast(
self.emit_lines(f" {dest} = {src};", "else {")
self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
self.emit_line("}")
elif is_bytearray_rprimitive(typ):
if declare_dest:
self.emit_line(f"PyObject *{dest};")
check = "(PyByteArray_Check({}))"
if likely:
check = f"(likely{check})"
self.emit_arg_check(src, dest, typ, check.format(src, src), optional)
self.emit_lines(f" {dest} = {src};", "else {")
self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
self.emit_line("}")
elif is_tuple_rprimitive(typ):
if declare_dest:
self.emit_line(f"{self.ctype(typ)} {dest};")
Expand Down
8 changes: 8 additions & 0 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ def __hash__(self) -> int:
# Python bytes object.
bytes_rprimitive: Final = RPrimitive("builtins.bytes", is_unboxed=False, is_refcounted=True)

# Python bytearray object.
bytearray_rprimitive: Final = RPrimitive("builtins.bytearray", is_unboxed=False, is_refcounted=True)

# Tuple of an arbitrary length (corresponds to Tuple[t, ...], with
# explicit '...').
tuple_rprimitive: Final = RPrimitive("builtins.tuple", is_unboxed=False, is_refcounted=True)
Expand Down Expand Up @@ -632,6 +635,10 @@ def is_bytes_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.bytes"


def is_bytearray_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.bytearray"


def is_tuple_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.tuple"

Expand All @@ -646,6 +653,7 @@ def is_sequence_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
or is_tuple_rprimitive(rtype)
or is_str_rprimitive(rtype)
or is_bytes_rprimitive(rtype)
or is_bytearray_rprimitive(rtype)
)


Expand Down
3 changes: 3 additions & 0 deletions mypyc/irbuild/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
RType,
RUnion,
bool_rprimitive,
bytearray_rprimitive,
bytes_rprimitive,
dict_rprimitive,
float_rprimitive,
Expand Down Expand Up @@ -88,6 +89,8 @@ def type_to_rtype(self, typ: Type | None) -> RType:
return str_rprimitive
elif typ.type.fullname == "builtins.bytes":
return bytes_rprimitive
elif typ.type.fullname == "builtins.bytearray":
return bytearray_rprimitive
elif typ.type.fullname == "builtins.list":
return list_rprimitive
# Dict subclasses are at least somewhat common and we
Expand Down
3 changes: 2 additions & 1 deletion mypyc/primitives/bytes_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
RUnion,
bit_rprimitive,
bool_rprimitive,
bytearray_rprimitive,
bytes_rprimitive,
c_int_rprimitive,
c_pyssize_t_rprimitive,
Expand Down Expand Up @@ -51,7 +52,7 @@
function_op(
name="builtins.bytearray",
arg_types=[object_rprimitive],
return_type=object_rprimitive,
return_type=bytearray_rprimitive,
c_function_name="PyByteArray_FromObject",
error_kind=ERR_MAGIC,
)
Expand Down
18 changes: 11 additions & 7 deletions mypyc/test-data/irbuild-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ def f(s, num):
num :: int
r0 :: object
r1 :: str
r2, r3, a, r4, b, r5, r6, c :: object
r2, r3 :: object
r4, a, r5, b :: bytearray
r6 :: object
r7, c :: bytearray
L0:
r0 = builtins :: module
r1 = 'bytearray'
r2 = CPyObject_GetAttr(r0, r1)
r3 = PyObject_Vectorcall(r2, 0, 0, 0)
a = r3
r4 = PyByteArray_FromObject(s)
b = r4
r5 = box(int, num)
r6 = PyByteArray_FromObject(r5)
c = r6
r4 = cast(bytearray, r3)
a = r4
r5 = PyByteArray_FromObject(s)
b = r5
r6 = box(int, num)
r7 = PyByteArray_FromObject(r6)
c = r7
return 1

[case testBytesEquality]
Expand Down