| 1 |
"""Primitive bytearray ops. |
| 2 |
|
| 3 |
NOTE: Most of these should be added to bytearray_extra_ops.c, which requires the |
| 4 |
BYTEARRAY_EXTRA_OPS primitive dependency, since these are used relatively rarely and we |
| 5 |
don't want to compile them unless needed. |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
from mypyc.ir.deps import BYTEARRAY_EXTRA_OPS |
| 11 |
from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER |
| 12 |
from mypyc.ir.rtypes import bit_rprimitive, bytearray_rprimitive, object_rprimitive |
| 13 |
from mypyc.primitives.registry import custom_primitive_op, function_op, load_address_op |
| 14 |
|
| 15 |
# Get the 'bytearray' type object. |
| 16 |
load_address_op(name="builtins.bytearray", type=object_rprimitive, src="PyByteArray_Type") |
| 17 |
|
| 18 |
# bytearray(obj) |
| 19 |
function_op( |
| 20 |
name="builtins.bytearray", |
| 21 |
arg_types=[object_rprimitive], |
| 22 |
return_type=bytearray_rprimitive, |
| 23 |
c_function_name="PyByteArray_FromObject", |
| 24 |
error_kind=ERR_MAGIC, |
| 25 |
) |
| 26 |
|
| 27 |
# bytearray() -- construct empty bytearray |
| 28 |
function_op( |
| 29 |
name="builtins.bytearray", |
| 30 |
arg_types=[], |
| 31 |
return_type=bytearray_rprimitive, |
| 32 |
c_function_name="CPyByteArray_New", |
| 33 |
error_kind=ERR_MAGIC, |
| 34 |
dependencies=[BYTEARRAY_EXTRA_OPS], |
| 35 |
) |
| 36 |
|
| 37 |
# isinstance(obj, bytearray) |
| 38 |
isinstance_bytearray = custom_primitive_op( |
| 39 |
name="builtins.isinstance", |
| 40 |
arg_types=[object_rprimitive], |
| 41 |
return_type=bit_rprimitive, |
| 42 |
c_function_name="PyByteArray_Check", |
| 43 |
error_kind=ERR_NEVER, |
| 44 |
) |