Skip to content

Commit 16f4b2c

Browse files
committed
Add primitive for bytearray()
1 parent f99065e commit 16f4b2c

6 files changed

Lines changed: 50 additions & 17 deletions

File tree

mypyc/ir/deps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ def get_header(self) -> str:
5151

5252
BYTES_EXTRA_OPS: Final = SourceDep("bytes_extra_ops.c")
5353
BYTES_WRITER_EXTRA_OPS: Final = SourceDep("byteswriter_extra_ops.c")
54+
BYTEARRAY_EXTRA_OPS: Final = SourceDep("bytearray_extra_ops.c")

mypyc/lib-rt/bytearray_extra_ops.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "bytearray_extra_ops.h"
2+
3+
PyObject *CPyByteArray_New(void) {
4+
return PyByteArray_FromStringAndSize(NULL, 0);
5+
}

mypyc/lib-rt/bytearray_extra_ops.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef MYPYC_BYTEARRAY_EXTRA_OPS_H
2+
#define MYPYC_BYTEARRAY_EXTRA_OPS_H
3+
4+
#include <Python.h>
5+
#include "CPy.h"
6+
7+
// Construct empty bytearray
8+
PyObject *CPyByteArray_New(void);
9+
10+
#endif

mypyc/primitives/bytearray_ops.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
"""Primitive bytearray ops."""
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+
"""
27

38
from __future__ import annotations
49

10+
from mypyc.ir.deps import BYTEARRAY_EXTRA_OPS
511
from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER
612
from mypyc.ir.rtypes import bit_rprimitive, bytearray_rprimitive, object_rprimitive
7-
from mypyc.primitives.registry import function_op, load_address_op
13+
from mypyc.primitives.registry import function_op, load_address_op, custom_primitive_op
814

915
# Get the 'bytearray' type object.
1016
load_address_op(name="builtins.bytearray", type=object_rprimitive, src="PyByteArray_Type")
@@ -16,10 +22,20 @@
1622
return_type=bytearray_rprimitive,
1723
c_function_name="PyByteArray_FromObject",
1824
error_kind=ERR_MAGIC,
25+
dependencies=[BYTEARRAY_EXTRA_OPS],
26+
)
27+
28+
# bytearray() -- construct empty bytearray
29+
function_op(
30+
name="builtins.bytearray",
31+
arg_types=[],
32+
return_type=bytearray_rprimitive,
33+
c_function_name="CPyByteArray_New",
34+
error_kind=ERR_MAGIC,
1935
)
2036

21-
# translate isinstance(obj, bytearray)
22-
isinstance_bytearray = function_op(
37+
# isinstance(obj, bytearray)
38+
isinstance_bytearray = custom_primitive_op(
2339
name="builtins.isinstance",
2440
arg_types=[object_rprimitive],
2541
return_type=bit_rprimitive,

mypyc/test-data/irbuild-bytes.test

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,17 @@ def f(s: str, num: int) -> None:
4848
def f(s, num):
4949
s :: str
5050
num :: int
51-
r0, r1 :: object
52-
r2, a, r3, b :: bytearray
53-
r4 :: object
54-
r5, c :: bytearray
51+
r0, a, r1, b :: bytearray
52+
r2 :: object
53+
r3, c :: bytearray
5554
L0:
56-
r0 = load_address PyByteArray_Type
57-
r1 = PyObject_Vectorcall(r0, 0, 0, 0)
58-
r2 = cast(bytearray, r1)
59-
a = r2
60-
r3 = PyByteArray_FromObject(s)
61-
b = r3
62-
r4 = box(int, num)
63-
r5 = PyByteArray_FromObject(r4)
64-
c = r5
55+
r0 = CPyByteArray_New()
56+
a = r0
57+
r1 = PyByteArray_FromObject(s)
58+
b = r1
59+
r2 = box(int, num)
60+
r3 = PyByteArray_FromObject(r2)
61+
c = r3
6562
return 1
6663

6764
[case testBytesEquality]

mypyc/test-data/run-bytes.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ from typing import Any
260260
from testutil import assertRaises
261261

262262
def test_basics() -> None:
263+
brr0 = bytearray()
264+
assert brr0 == bytearray(b'')
265+
assert brr0 == b''
266+
assert len(brr0) == 0
263267
brr1 = bytearray(3)
264268
assert brr1 == bytearray(b'\x00\x00\x00')
265269
assert brr1 == b'\x00\x00\x00'

0 commit comments

Comments
 (0)