Skip to content

Commit b341226

Browse files
Merge branch 'master' into fix_list_comprehension
2 parents 9c27701 + 766c43c commit b341226

22 files changed

+1210
-146
lines changed

mypyc/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
MODULE_PREFIX: Final = "CPyModule_" # Cached modules
1616
TYPE_VAR_PREFIX: Final = "CPyTypeVar_" # Type variables when using new-style Python 3.12 syntax
1717
ATTR_PREFIX: Final = "_" # Attributes
18+
FAST_PREFIX: Final = "__mypyc_fast_" # Optimized methods in non-extension classes
1819

1920
ENV_ATTR_NAME: Final = "__mypyc_env__"
2021
NEXT_LABEL_ATTR_NAME: Final = "__mypyc_next_label__"

mypyc/ir/class_ir.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ def __init__(
210210
# per-type free "list" of up to length 1.
211211
self.reuse_freed_instance = False
212212

213+
# Is this a class inheriting from enum.Enum? Such classes can be special-cased.
214+
self.is_enum = False
215+
213216
def __repr__(self) -> str:
214217
return (
215218
"ClassIR("
@@ -410,6 +413,7 @@ def serialize(self) -> JsonDict:
410413
"init_self_leak": self.init_self_leak,
411414
"env_user_function": self.env_user_function.id if self.env_user_function else None,
412415
"reuse_freed_instance": self.reuse_freed_instance,
416+
"is_enum": self.is_enum,
413417
}
414418

415419
@classmethod
@@ -466,6 +470,7 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> ClassIR:
466470
ctx.functions[data["env_user_function"]] if data["env_user_function"] else None
467471
)
468472
ir.reuse_freed_instance = data["reuse_freed_instance"]
473+
ir.is_enum = data["is_enum"]
469474

470475
return ir
471476

mypyc/ir/rtypes.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def may_be_immortal(self) -> bool:
192192
def serialize(self) -> str:
193193
return "void"
194194

195-
def __eq__(self, other: object) -> bool:
195+
def __eq__(self, other: object) -> TypeGuard[RVoid]:
196196
return isinstance(other, RVoid)
197197

198198
def __hash__(self) -> int:
@@ -279,7 +279,7 @@ def serialize(self) -> str:
279279
def __repr__(self) -> str:
280280
return "<RPrimitive %s>" % self.name
281281

282-
def __eq__(self, other: object) -> bool:
282+
def __eq__(self, other: object) -> TypeGuard[RPrimitive]:
283283
return isinstance(other, RPrimitive) and other.name == self.name
284284

285285
def __hash__(self) -> int:
@@ -513,15 +513,15 @@ def __hash__(self) -> int:
513513
range_rprimitive: Final = RPrimitive("builtins.range", is_unboxed=False, is_refcounted=True)
514514

515515

516-
def is_tagged(rtype: RType) -> bool:
516+
def is_tagged(rtype: RType) -> TypeGuard[RPrimitive]:
517517
return rtype is int_rprimitive or rtype is short_int_rprimitive
518518

519519

520-
def is_int_rprimitive(rtype: RType) -> bool:
520+
def is_int_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
521521
return rtype is int_rprimitive
522522

523523

524-
def is_short_int_rprimitive(rtype: RType) -> bool:
524+
def is_short_int_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
525525
return rtype is short_int_rprimitive
526526

527527

@@ -535,7 +535,7 @@ def is_int32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
535535
)
536536

537537

538-
def is_int64_rprimitive(rtype: RType) -> bool:
538+
def is_int64_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
539539
return rtype is int64_rprimitive or (
540540
rtype is c_pyssize_t_rprimitive and rtype._ctype == "int64_t"
541541
)
@@ -554,81 +554,93 @@ def is_uint8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
554554
return rtype is uint8_rprimitive
555555

556556

557-
def is_uint32_rprimitive(rtype: RType) -> bool:
557+
def is_uint32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
558558
return rtype is uint32_rprimitive
559559

560560

561-
def is_uint64_rprimitive(rtype: RType) -> bool:
561+
def is_uint64_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
562562
return rtype is uint64_rprimitive
563563

564564

565-
def is_c_py_ssize_t_rprimitive(rtype: RType) -> bool:
565+
def is_c_py_ssize_t_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
566566
return rtype is c_pyssize_t_rprimitive
567567

568568

569-
def is_pointer_rprimitive(rtype: RType) -> bool:
569+
def is_pointer_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
570570
return rtype is pointer_rprimitive
571571

572572

573-
def is_float_rprimitive(rtype: RType) -> bool:
573+
def is_float_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
574574
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.float"
575575

576576

577-
def is_bool_rprimitive(rtype: RType) -> bool:
577+
def is_bool_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
578578
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.bool"
579579

580580

581-
def is_bit_rprimitive(rtype: RType) -> bool:
581+
def is_bit_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
582582
return isinstance(rtype, RPrimitive) and rtype.name == "bit"
583583

584584

585-
def is_bool_or_bit_rprimitive(rtype: RType) -> bool:
585+
def is_bool_or_bit_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
586586
return is_bool_rprimitive(rtype) or is_bit_rprimitive(rtype)
587587

588588

589-
def is_object_rprimitive(rtype: RType) -> bool:
589+
def is_object_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
590590
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.object"
591591

592592

593-
def is_none_rprimitive(rtype: RType) -> bool:
593+
def is_none_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
594594
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.None"
595595

596596

597-
def is_list_rprimitive(rtype: RType) -> bool:
597+
def is_list_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
598598
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.list"
599599

600600

601-
def is_dict_rprimitive(rtype: RType) -> bool:
601+
def is_dict_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
602602
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.dict"
603603

604604

605-
def is_set_rprimitive(rtype: RType) -> bool:
605+
def is_set_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
606606
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.set"
607607

608608

609-
def is_frozenset_rprimitive(rtype: RType) -> bool:
609+
def is_frozenset_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
610610
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.frozenset"
611611

612612

613-
def is_str_rprimitive(rtype: RType) -> bool:
613+
def is_str_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
614614
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.str"
615615

616616

617-
def is_bytes_rprimitive(rtype: RType) -> bool:
617+
def is_bytes_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
618618
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.bytes"
619619

620620

621-
def is_tuple_rprimitive(rtype: RType) -> bool:
621+
def is_tuple_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
622622
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.tuple"
623623

624624

625-
def is_range_rprimitive(rtype: RType) -> bool:
625+
def is_range_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
626626
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.range"
627627

628628

629-
def is_sequence_rprimitive(rtype: RType) -> bool:
629+
def is_sequence_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
630630
return isinstance(rtype, RPrimitive) and (
631-
is_list_rprimitive(rtype) or is_tuple_rprimitive(rtype) or is_str_rprimitive(rtype)
631+
is_list_rprimitive(rtype)
632+
or is_tuple_rprimitive(rtype)
633+
or is_str_rprimitive(rtype)
634+
or is_bytes_rprimitive(rtype)
635+
)
636+
637+
638+
def is_immutable_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
639+
return (
640+
is_str_rprimitive(rtype)
641+
or is_bytes_rprimitive(rtype)
642+
or is_tuple_rprimitive(rtype)
643+
or is_frozenset_rprimitive(rtype)
632644
)
633645

634646

@@ -717,7 +729,7 @@ def __str__(self) -> str:
717729
def __repr__(self) -> str:
718730
return "<RTuple %s>" % ", ".join(repr(typ) for typ in self.types)
719731

720-
def __eq__(self, other: object) -> bool:
732+
def __eq__(self, other: object) -> TypeGuard[RTuple]:
721733
return isinstance(other, RTuple) and self.types == other.types
722734

723735
def __hash__(self) -> int:
@@ -850,7 +862,7 @@ def __repr__(self) -> str:
850862
", ".join(name + ":" + repr(typ) for name, typ in zip(self.names, self.types)),
851863
)
852864

853-
def __eq__(self, other: object) -> bool:
865+
def __eq__(self, other: object) -> TypeGuard[RStruct]:
854866
return (
855867
isinstance(other, RStruct)
856868
and self.name == other.name
@@ -920,7 +932,7 @@ def attr_type(self, name: str) -> RType:
920932
def __repr__(self) -> str:
921933
return "<RInstance %s>" % self.name
922934

923-
def __eq__(self, other: object) -> bool:
935+
def __eq__(self, other: object) -> TypeGuard[RInstance]:
924936
return isinstance(other, RInstance) and other.name == self.name
925937

926938
def __hash__(self) -> int:
@@ -974,7 +986,7 @@ def __str__(self) -> str:
974986
return "union[%s]" % ", ".join(str(item) for item in self.items)
975987

976988
# We compare based on the set because order in a union doesn't matter
977-
def __eq__(self, other: object) -> bool:
989+
def __eq__(self, other: object) -> TypeGuard[RUnion]:
978990
return isinstance(other, RUnion) and self.items_set == other.items_set
979991

980992
def __hash__(self) -> int:
@@ -1016,7 +1028,7 @@ def optional_value_type(rtype: RType) -> RType | None:
10161028
return None
10171029

10181030

1019-
def is_optional_type(rtype: RType) -> bool:
1031+
def is_optional_type(rtype: RType) -> TypeGuard[RUnion]:
10201032
"""Is rtype an optional type with exactly two union items?"""
10211033
return optional_value_type(rtype) is not None
10221034

@@ -1048,7 +1060,7 @@ def __str__(self) -> str:
10481060
def __repr__(self) -> str:
10491061
return f"<RArray {self.item_type!r}[{self.length}]>"
10501062

1051-
def __eq__(self, other: object) -> bool:
1063+
def __eq__(self, other: object) -> TypeGuard[RArray]:
10521064
return (
10531065
isinstance(other, RArray)
10541066
and self.item_type == other.item_type

mypyc/irbuild/builder.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
RType,
9292
RUnion,
9393
bitmap_rprimitive,
94+
bytes_rprimitive,
9495
c_pyssize_t_rprimitive,
9596
dict_rprimitive,
9697
int_rprimitive,
@@ -962,8 +963,12 @@ def get_sequence_type_from_type(self, target_type: Type) -> RType:
962963
elif isinstance(target_type, Instance):
963964
if target_type.type.fullname == "builtins.str":
964965
return str_rprimitive
965-
else:
966+
elif target_type.type.fullname == "builtins.bytes":
967+
return bytes_rprimitive
968+
try:
966969
return self.type_to_rtype(target_type.args[0])
970+
except IndexError:
971+
raise ValueError(f"{target_type!r} is not a valid sequence.") from None
967972
# This elif-blocks are needed for iterating over classes derived from NamedTuple.
968973
elif isinstance(target_type, TypeVarLikeType):
969974
return self.get_sequence_type_from_type(target_type.upper_bound)

mypyc/irbuild/classdef.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
)
6767
from mypyc.irbuild.prepare import GENERATOR_HELPER_NAME
6868
from mypyc.irbuild.util import dataclass_type, get_func_def, is_constant, is_dataclass_decorator
69-
from mypyc.primitives.dict_ops import dict_new_op, dict_set_item_op
69+
from mypyc.primitives.dict_ops import dict_new_op, exact_dict_set_item_op
7070
from mypyc.primitives.generic_ops import (
7171
iter_op,
7272
next_op,
@@ -271,8 +271,8 @@ def finalize(self, ir: ClassIR) -> None:
271271
)
272272

273273
# Add the non-extension class to the dict
274-
self.builder.primitive_op(
275-
dict_set_item_op,
274+
self.builder.call_c(
275+
exact_dict_set_item_op,
276276
[
277277
self.builder.load_globals_dict(),
278278
self.builder.load_str(self.cdef.name),
@@ -487,8 +487,10 @@ def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value:
487487
builder.add(InitStatic(tp, cdef.name, builder.module_name, NAMESPACE_TYPE))
488488

489489
# Add it to the dict
490-
builder.primitive_op(
491-
dict_set_item_op, [builder.load_globals_dict(), builder.load_str(cdef.name), tp], cdef.line
490+
builder.call_c(
491+
exact_dict_set_item_op,
492+
[builder.load_globals_dict(), builder.load_str(cdef.name), tp],
493+
cdef.line,
492494
)
493495

494496
return tp
@@ -672,7 +674,7 @@ def add_non_ext_class_attr_ann(
672674
typ = builder.add(LoadAddress(type_object_op.type, type_object_op.src, stmt.line))
673675

674676
key = builder.load_str(lvalue.name)
675-
builder.primitive_op(dict_set_item_op, [non_ext.anns, key, typ], stmt.line)
677+
builder.call_c(exact_dict_set_item_op, [non_ext.anns, key, typ], stmt.line)
676678

677679

678680
def add_non_ext_class_attr(

mypyc/irbuild/expression.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
)
9898
from mypyc.irbuild.specialize import apply_function_specialization, apply_method_specialization
9999
from mypyc.primitives.bytes_ops import bytes_slice_op
100-
from mypyc.primitives.dict_ops import dict_get_item_op, dict_new_op, dict_set_item_op
100+
from mypyc.primitives.dict_ops import dict_get_item_op, dict_new_op, exact_dict_set_item_op
101101
from mypyc.primitives.generic_ops import iter_op
102102
from mypyc.primitives.list_ops import list_append_op, list_extend_op, list_slice_op
103103
from mypyc.primitives.misc_ops import ellipsis_op, get_module_dict_op, new_slice_op, type_op
@@ -1030,7 +1030,7 @@ def transform_dictionary_comprehension(builder: IRBuilder, o: DictionaryComprehe
10301030
def gen_inner_stmts() -> None:
10311031
k = builder.accept(o.key)
10321032
v = builder.accept(o.value)
1033-
builder.primitive_op(dict_set_item_op, [builder.read(d), k, v], o.line)
1033+
builder.call_c(exact_dict_set_item_op, [builder.read(d), k, v], o.line)
10341034

10351035
comprehension_helper(builder, loop_params, gen_inner_stmts, o.line)
10361036
return builder.read(d)

0 commit comments

Comments
 (0)