Skip to content

Commit bfafc83

Browse files
committed
Some more experiments
1 parent ab6e6a6 commit bfafc83

File tree

12 files changed

+611
-5
lines changed

12 files changed

+611
-5
lines changed

mypyc/analysis/ircheck.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
)
5757
from mypyc.ir.pprint import format_func
5858
from mypyc.ir.rtypes import (
59+
KNOWN_NATIVE_TYPES,
5960
RArray,
6061
RInstance,
6162
RPrimitive,
@@ -181,7 +182,7 @@ def check_op_sources_valid(fn: FuncIR) -> list[FnError]:
181182
set_rprimitive.name,
182183
tuple_rprimitive.name,
183184
range_rprimitive.name,
184-
}
185+
} | set(KNOWN_NATIVE_TYPES.values())
185186

186187

187188
def can_coerce_to(src: RType, dest: RType) -> bool:

mypyc/build.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def mypycify(
492492
strict_dunder_typing: bool = False,
493493
group_name: str | None = None,
494494
log_trace: bool = False,
495+
include_native_lib: bool = False,
495496
) -> list[Extension]:
496497
"""Main entry point to building using mypyc.
497498
@@ -652,5 +653,11 @@ def mypycify(
652653
extensions.extend(
653654
build_single_module(group_sources, cfilenames + shared_cfilenames, cflags)
654655
)
656+
if include_native_lib:
657+
extensions.append(
658+
get_extension()(
659+
"native_buffer", sources=[os.path.join(include_dir(), "native_buffer_internal.c")]
660+
)
661+
)
655662

656663
return extensions

mypyc/codegen/emit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
is_int64_rprimitive,
4040
is_int_rprimitive,
4141
is_list_rprimitive,
42+
is_native_rprimitive,
4243
is_none_rprimitive,
4344
is_object_rprimitive,
4445
is_optional_type,
@@ -704,7 +705,7 @@ def emit_cast(
704705
self.emit_lines(f" {dest} = {src};", "else {")
705706
self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
706707
self.emit_line("}")
707-
elif is_object_rprimitive(typ):
708+
elif is_object_rprimitive(typ) or is_native_rprimitive(typ):
708709
if declare_dest:
709710
self.emit_line(f"PyObject *{dest};")
710711
self.emit_arg_check(src, dest, typ, "", optional)

mypyc/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"misc_ops.c",
8282
"generic_ops.c",
8383
"pythonsupport.c",
84+
"native_buffer_internal.c",
8485
]
8586

8687
# Python 3.12 introduced immortal objects, specified via a special reference count

mypyc/ir/rtypes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,15 @@ def __hash__(self) -> int:
512512
# Python range object.
513513
range_rprimitive: Final = RPrimitive("builtins.range", is_unboxed=False, is_refcounted=True)
514514

515+
KNOWN_NATIVE_TYPES: Final = {
516+
name: RPrimitive(name, is_unboxed=False, is_refcounted=True)
517+
for name in ["native_buffer.Buffer"]
518+
}
519+
520+
521+
def is_native_rprimitive(rtype: RType) -> bool:
522+
return isinstance(rtype, RPrimitive) and rtype.name in KNOWN_NATIVE_TYPES
523+
515524

516525
def is_tagged(rtype: RType) -> bool:
517526
return rtype is int_rprimitive or rtype is short_int_rprimitive

mypyc/irbuild/mapper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from mypyc.ir.class_ir import ClassIR
2626
from mypyc.ir.func_ir import FuncDecl, FuncSignature, RuntimeArg
2727
from mypyc.ir.rtypes import (
28+
KNOWN_NATIVE_TYPES,
2829
RInstance,
2930
RTuple,
3031
RType,
@@ -119,6 +120,8 @@ def type_to_rtype(self, typ: Type | None) -> RType:
119120
return int16_rprimitive
120121
elif typ.type.fullname == "mypy_extensions.u8":
121122
return uint8_rprimitive
123+
elif typ.type.fullname in KNOWN_NATIVE_TYPES:
124+
return KNOWN_NATIVE_TYPES[typ.type.fullname]
122125
else:
123126
return object_rprimitive
124127
elif isinstance(typ, TupleType):

0 commit comments

Comments
 (0)