Skip to content

Commit 28abab5

Browse files
Update ll_builder.py
1 parent 1986403 commit 28abab5

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

mypyc/irbuild/ll_builder.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,39 +2441,38 @@ def builtin_len(self, val: Value, line: int, use_pyssize_t: bool = False) -> Val
24412441
offset = Integer(1, c_pyssize_t_rprimitive, line)
24422442
return self.int_op(short_int_rprimitive, size_value, offset, IntOp.LEFT_SHIFT, line)
24432443

2444-
# --- Optimized dispatch for RInstance (native/user-defined classes) ---
24452444
if isinstance(typ, RInstance):
24462445
# TODO: Support use_pyssize_t
24472446
assert not use_pyssize_t
24482447
class_ir = typ.class_ir
24492448

2450-
# Only optimize for native extension classes (not built-in base, not Python subclass)
2449+
# Optimize for native extension classes (not built-in base, not Python subclass)
2450+
# Direct C call for final native methods and exact type
24512451
if (
24522452
class_ir.is_ext_class
24532453
and not class_ir.inherits_python
24542454
and class_ir.has_method("__len__")
2455+
and class_ir.is_method_final("__len__")
24552456
):
2456-
# 1. Direct C call for final native methods and exact type
2457-
if class_ir.is_method_final("__len__"):
2458-
decl = class_ir.method_decl("__len__")
2459-
length = self.call(decl, [val], [ARG_POS], [None], line)
2460-
2461-
# Coerce/check result and error handling as before
2462-
length = self.coerce(length, int_rprimitive, line)
2463-
ok, fail = BasicBlock(), BasicBlock()
2464-
cond = self.binary_op(length, Integer(0), ">=", line)
2465-
self.add_bool_branch(cond, ok, fail)
2466-
self.activate_block(fail)
2467-
self.add(
2468-
RaiseStandardError(
2469-
RaiseStandardError.VALUE_ERROR, "__len__() should return >= 0", line
2470-
)
2457+
decl = class_ir.method_decl("__len__")
2458+
length = self.call(decl, [val], [ARG_POS], [None], line)
2459+
2460+
# Coerce/check result and error handling as before
2461+
length = self.coerce(length, int_rprimitive, line)
2462+
ok, fail = BasicBlock(), BasicBlock()
2463+
cond = self.binary_op(length, Integer(0), ">=", line)
2464+
self.add_bool_branch(cond, ok, fail)
2465+
self.activate_block(fail)
2466+
self.add(
2467+
RaiseStandardError(
2468+
RaiseStandardError.VALUE_ERROR, "__len__() should return >= 0", line
24712469
)
2472-
self.add(Unreachable())
2473-
self.activate_block(ok)
2474-
return length
2470+
)
2471+
self.add(Unreachable())
2472+
self.activate_block(ok)
2473+
return length
24752474

2476-
# 4. Fallback: generic method call for non-native or ambiguous cases
2475+
# Fallback: generic method call for non-native or ambiguous cases
24772476
length = self.gen_method_call(val, "__len__", [], int_rprimitive, line)
24782477
length = self.coerce(length, int_rprimitive, line)
24792478
ok, fail = BasicBlock(), BasicBlock()

0 commit comments

Comments
 (0)