-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mypyc] Add bytes and bytearray initialization ops (#10900)
Use `PyBytes_FromObject` for `bytes(o)`. Use `PyByteArray_FromObject` for `bytearray(o)`. Current approach doesn't support empty initialization. Related to mypyc/mypyc#880.
- Loading branch information
1 parent
d7fabc9
commit d991d19
Showing
8 changed files
with
123 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Bytes primitive operations | ||
// | ||
// These are registered in mypyc.primitives.bytes_ops. | ||
|
||
#include <Python.h> | ||
#include "CPy.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
"""Primitive bytes ops.""" | ||
|
||
from mypyc.ir.rtypes import object_rprimitive | ||
from mypyc.primitives.registry import load_address_op | ||
from mypyc.ir.ops import ERR_MAGIC | ||
from mypyc.ir.rtypes import ( | ||
object_rprimitive, bytes_rprimitive, list_rprimitive, dict_rprimitive, | ||
str_rprimitive, RUnion | ||
) | ||
from mypyc.primitives.registry import load_address_op, function_op | ||
|
||
|
||
# Get the 'bytes' type object. | ||
load_address_op( | ||
name='builtins.bytes', | ||
type=object_rprimitive, | ||
src='PyBytes_Type') | ||
|
||
# bytes(obj) | ||
function_op( | ||
name='builtins.bytes', | ||
arg_types=[RUnion([list_rprimitive, dict_rprimitive, str_rprimitive])], | ||
return_type=bytes_rprimitive, | ||
c_function_name='PyBytes_FromObject', | ||
error_kind=ERR_MAGIC) | ||
|
||
# bytearray(obj) | ||
function_op( | ||
name='builtins.bytearray', | ||
arg_types=[object_rprimitive], | ||
return_type=bytes_rprimitive, | ||
c_function_name='PyByteArray_FromObject', | ||
error_kind=ERR_MAGIC) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
[case testBytesBasics] | ||
def f(num: int, l: list, d: dict, s: str) -> None: | ||
b1 = bytes() | ||
b2 = bytes(num) | ||
b3 = bytes(l) | ||
b4 = bytes(d) | ||
b5 = bytes(s) | ||
[out] | ||
def f(num, l, d, s): | ||
num :: int | ||
l :: list | ||
d :: dict | ||
s :: str | ||
r0, r1 :: object | ||
r2, b1 :: bytes | ||
r3, r4, r5 :: object | ||
r6, b2, r7, b3, r8, b4, r9, b5 :: bytes | ||
L0: | ||
r0 = load_address PyBytes_Type | ||
r1 = PyObject_CallFunctionObjArgs(r0, 0) | ||
r2 = cast(bytes, r1) | ||
b1 = r2 | ||
r3 = load_address PyBytes_Type | ||
r4 = box(int, num) | ||
r5 = PyObject_CallFunctionObjArgs(r3, r4, 0) | ||
r6 = cast(bytes, r5) | ||
b2 = r6 | ||
r7 = PyBytes_FromObject(l) | ||
b3 = r7 | ||
r8 = PyBytes_FromObject(d) | ||
b4 = r8 | ||
r9 = PyBytes_FromObject(s) | ||
b5 = r9 | ||
return 1 | ||
|
||
[case testBytearrayBasics] | ||
def f(s: str, num: int) -> None: | ||
a = bytearray() | ||
b = bytearray(s) | ||
c = bytearray(num) | ||
[out] | ||
def f(s, num): | ||
s :: str | ||
num :: int | ||
r0 :: object | ||
r1 :: str | ||
r2, r3, a :: object | ||
r4 :: bytes | ||
b, r5 :: object | ||
r6 :: bytes | ||
c :: object | ||
L0: | ||
r0 = builtins :: module | ||
r1 = 'bytearray' | ||
r2 = CPyObject_GetAttr(r0, r1) | ||
r3 = PyObject_CallFunctionObjArgs(r2, 0) | ||
a = r3 | ||
r4 = PyByteArray_FromObject(s) | ||
b = r4 | ||
r5 = box(int, num) | ||
r6 = PyByteArray_FromObject(r5) | ||
c = r6 | ||
return 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters