8000 [mypyc] Add bytes primitive type by 97littleleaf11 · Pull Request #10881 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[mypyc] Add bytes primitive type #10881

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 7 commits into from
Jul 28, 2021
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
Prev Previous commit
Next Next commit
Sync
  • Loading branch information
97littleleaf11 committed Jul 24, 2021
commit b2f9e695b5481e0ba06083e6ef324a95aafb26b5
11 changes: 11 additions & 0 deletions mypyc/primitives/bytes_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Primitive bytes ops."""

from mypyc.ir.rtypes import object_rprimitive
from mypyc.primitives.registry import load_address_op


# Get the 'bytes' type object.
load_address_op(
name='builtins.bytes',
type=object_rprimitive,
src='PyBytes_Type')
55 changes: 55 additions & 0 deletions mypyc/test-data/run-bytes.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Bytes test cases (compile and run)

[case testBytesBasics]
# Note: Add tests for additional operations to testBytesOps or in a new test case

def f(x: bytes) -> bytes:
return x

def eq(a: bytes, b: bytes) -> bool:
return a == b

def neq(a: bytes, b: bytes) -> bool:
return a != b
[file driver.py]
from native import f, eq, neq
assert f(b'123') == b'123'
assert f(b'\x07 \x0b " \t \x7f \xf0') == b'\x07 \x0b " \t \x7f \xf0'
assert eq(b'123', b'123')
assert not eq(b'123', b'1234')
assert neq(b'123', b'1234')
try:
f('x')
assert False
except TypeError:
pass

[case testBytesOps]
def test_indexing() -> None:
# Use bytes() to avoid constant folding
b = b'asdf' + bytes()
assert b[0] == 97
assert b[1] == 115
assert b[3] == 102
assert b[-1] == 102
b = b'\xfe\x15' + bytes()
assert b[0] == 254
assert b[1] == 21

def test_concat() -> None:
b1 = b'123' + bytes()
b2 = b'456' + bytes()
assert b1 + b2 == b'123456'

def test_join() -> None:
seq = (b'1', b'"', b'\xf0')
assert b'\x07'.join(seq) == b'1\x07"\x07\xf0'
assert b', '.join(()) == b''
assert b', '.join([bytes() + b'ab']) == b'ab'
assert b', '.join([bytes() + b'ab', b'cd']) == b'ab, cd'

def test_len() -> None:
# Use bytes() to avoid constant folding
b = b'foo' + bytes()
assert len(b) == 3
assert len(bytes()) == 0
0