Skip to content

Commit 86a2077

Browse files
1 parent a1863c9 commit 86a2077

File tree

10 files changed

+26
-39
lines changed

10 files changed

+26
-39
lines changed

mypyc/codegen/emit.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
is_list_rprimitive, is_dict_rprimitive, is_set_rprimitive, is_tuple_rprimitive,
1616
is_none_rprimitive, is_object_rprimitive, object_rprimitive, is_str_rprimitive,
1717
int_rprimitive, is_optional_type, optional_value_type, is_int32_rprimitive,
18-
is_int64_rprimitive, is_bit_rprimitive, is_range_rprimitive
18+
is_int64_rprimitive, is_bit_rprimitive, is_range_rprimitive, is_bytes_rprimitive
1919
)
2020
from mypyc.ir.func_ir import FuncDecl
2121
from mypyc.ir.class_ir import ClassIR, all_concrete_classes
@@ -451,8 +451,8 @@ def emit_cast(self,
451451

452452
# TODO: Verify refcount handling.
453453
if (is_list_rprimitive(typ) or is_dict_rprimitive(typ) or is_set_rprimitive(typ)
454-
or is_str_rprimitive(typ) or is_range_rprimitive(typ) or is_float_rprimitive(typ)
455-
or is_int_rprimitive(typ) or is_bool_rprimitive(typ)):
454+
or is_str_rprimitive(typ) or is_bytes_rprimitive(typ) or is_range_rprimitive(typ)
455+
or is_float_rprimitive(typ) or is_int_rprimitive(typ) or is_bool_rprimitive(typ)):
456456
if declare_dest:
457457
self.emit_line('PyObject *{};'.format(dest))
458458
if is_list_rprimitive(typ):
@@ -463,6 +463,8 @@ def emit_cast(self,
463463
prefix = 'PySet'
464464
elif is_str_rprimitive(typ):
465465
prefix = 'PyUnicode'
466+
elif is_bytes_rprimitive(typ):
467+
prefix = 'PyBytes'
466468
elif is_range_rprimitive(typ):
467469
prefix = 'PyRange'
468470
elif is_float_rprimitive(typ):

mypyc/ir/rtypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ def __hash__(self) -> int:
328328
# (PyUnicode).
329329
str_rprimitive: Final = RPrimitive("builtins.str", is_unboxed=False, is_refcounted=True)
330330

331+
# Python bytes object.
332+
bytes_rprimitive: Final = RPrimitive('builtins.bytes', is_unboxed=False, is_refcounted=True)
333+
331334
# Tuple of an arbitrary length (corresponds to Tuple[t, ...], with
332335
# explicit '...').
333336
tuple_rprimitive: Final = RPrimitive("builtins.tuple", is_unboxed=False, is_refcounted=True)
@@ -410,6 +413,10 @@ def is_str_rprimitive(rtype: RType) -> bool:
410413
return isinstance(rtype, RPrimitive) and rtype.name == 'builtins.str'
411414

412415

416+
def is_bytes_rprimitive(rtype: RType) -> bool:
417+
return isinstance(rtype, RPrimitive) and rtype.name == 'builtins.bytes'
418+
419+
413420
def is_tuple_rprimitive(rtype: RType) -> bool:
414421
return isinstance(rtype, RPrimitive) and rtype.name == 'builtins.tuple'
415422

mypyc/irbuild/ll_builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
is_list_rprimitive, is_tuple_rprimitive, is_dict_rprimitive, is_set_rprimitive, PySetObject,
3434
none_rprimitive, RTuple, is_bool_rprimitive, is_str_rprimitive, c_int_rprimitive,
3535
pointer_rprimitive, PyObject, PyListObject, bit_rprimitive, is_bit_rprimitive,
36-
object_pointer_rprimitive, c_size_t_rprimitive, dict_rprimitive
36+
object_pointer_rprimitive, c_size_t_rprimitive, dict_rprimitive, bytes_rprimitive
3737
)
3838
from mypyc.ir.func_ir import FuncDecl, FuncSignature
3939
from mypyc.ir.class_ir import ClassIR, all_concrete_classes
@@ -797,7 +797,7 @@ def load_str(self, value: str) -> Value:
797797

798798
def load_bytes(self, value: bytes) -> Value:
799799
"""Load a bytes literal value."""
800-
return self.add(LoadLiteral(value, object_rprimitive))
800+
return self.add(LoadLiteral(value, bytes_rprimitive))
801801

802802
def load_complex(self, value: complex) -> Value:
803803
"""Load a complex literal value."""

mypyc/irbuild/mapper.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from mypyc.ir.rtypes import (
1313
RType, RUnion, RTuple, RInstance, object_rprimitive, dict_rprimitive, tuple_rprimitive,
1414
none_rprimitive, int_rprimitive, float_rprimitive, str_rprimitive, bool_rprimitive,
15-
list_rprimitive, set_rprimitive, range_rprimitive
15+
list_rprimitive, set_rprimitive, range_rprimitive, bytes_rprimitive
1616
)
1717
from mypyc.ir.func_ir import FuncSignature, FuncDecl, RuntimeArg
1818
from mypyc.ir.class_ir import ClassIR
@@ -43,10 +43,12 @@ def type_to_rtype(self, typ: Optional[Type]) -> RType:
4343
return int_rprimitive
4444
elif typ.type.fullname == 'builtins.float':
4545
return float_rprimitive
46-
elif typ.type.fullname == 'builtins.str':
47-
return str_rprimitive
4846
elif typ.type.fullname == 'builtins.bool':
4947
return bool_rprimitive
48+
elif typ.type.fullname == 'builtins.str':
49+
return str_rprimitive
50+
elif typ.type.fullname == 'builtins.bytes':
51+
return bytes_rprimitive
5052
elif typ.type.fullname == 'builtins.list':
5153
return list_rprimitive
5254
# Dict subclasses are at least somewhat common and we

mypyc/primitives/registry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def load_address_op(name: str,
239239
# Import various modules that set up global state.
240240
import mypyc.primitives.int_ops # noqa
241241
import mypyc.primitives.str_ops # noqa
242+
import mypyc.primitives.bytes_ops # noqa
242243
import mypyc.primitives.list_ops # noqa
243244
import mypyc.primitives.dict_ops # noqa
244245
import mypyc.primitives.tuple_ops # noqa

mypyc/test-data/driver/driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
def extract_line(tb):
2828
formatted = '\n'.join(format_tb(tb))
29-
m = re.search('File "native.py", line ([0-9]+), in test_', formatted)
29+
m = re.search('File "(native|driver).py", line ([0-9]+), in (test_|<module>)', formatted)
3030
if m is None:
3131
return "0"
3232
return m.group(1)

mypyc/test-data/fixtures/ir.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ def __truediv__(self, n: complex) -> complex: pass
9696
def __neg__(self) -> complex: pass
9797

9898
class bytes:
99-
def __init__(self, x: object) -> None: pass
100-
def __add__(self, x: object) -> bytes: pass
101-
def __eq__(self, x:object) -> bool:pass
99+
def __add__(self, x: bytes) -> bytes: pass
100+
def __eq__(self, x: object) -> bool: pass
102101
def __ne__(self, x: object) -> bool: pass
102+
def __getitem__(self, i: int) -> int: pass
103103
def join(self, x: Iterable[object]) -> bytes: pass
104104

105105
class bool(int):

mypyc/test-data/irbuild-basic.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def f() -> bytes:
761761
return b'1234'
762762
[out]
763763
def f():
764-
r0, x, r1 :: object
764+
r0, x, r1 :: bytes
765765
L0:
766766
r0 = b'\xf0'
767767
x = r0

mypyc/test-data/run-primitives.test

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -255,32 +255,6 @@ assert str(to_int(3.14)) == '3'
255255
assert str(to_int(3)) == '3'
256256
assert get_complex() == 3.5 + 6.2j
257257

258-
[case testBytes]
259-
def f(x: bytes) -> bytes:
260-
return x
261-
262-
def concat(a: bytes, b: bytes) -> bytes:
263-
return a + b
264-
265-
def eq(a: bytes, b: bytes) -> bool:
266-
return a == b
267-
268-
def neq(a: bytes, b: bytes) -> bool:
269-
return a != b
270-
271-
def join() -> bytes:
272-
seq = (b'1', b'"', b'\xf0')
273-
return b'\x07'.join(seq)
274-
[file driver.py]
275-
from native import f, concat, eq, neq, join
276-
assert f(b'123') == b'123'
277-
assert f(b'\x07 \x0b " \t \x7f \xf0') == b'\x07 \x0b " \t \x7f \xf0'
278-
assert concat(b'123', b'456') == b'123456'
279-
assert eq(b'123', b'123')
280-
assert not eq(b'123', b'1234')
281-
assert neq(b'123', b'1234')
282-
assert join() == b'1\x07"\x07\xf0'
283-
284258
[case testDel]
285259
from typing import List
286260
from testutil import assertRaises

mypyc/test/test_run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'run-floats.test',
3737
'run-bools.test',
3838
'run-strings.test',
39+
'run-bytes.test',
3940
'run-tuples.test',
4041
'run-lists.test',
4142
'run-dicts.test',

0 commit comments

Comments
 (0)