MCPcopy Index your code
hub / github.com/python/mypy / try_gen_slice_op

Function try_gen_slice_op

mypyc/irbuild/expression.py:803–840  ·  view source on GitHub ↗

Generate specialized slice op for some index expressions. Return None if a specialized op isn't available. This supports obj[x:y], obj[:x], and obj[x:] for a few types.

(builder: IRBuilder, base: Value, index: SliceExpr)

Source from the content-addressed store, hash-verified

801
802
803def try_gen_slice_op(builder: IRBuilder, base: Value, index: SliceExpr) -> Value | None:
804 """Generate specialized slice op for some index expressions.
805
806 Return None if a specialized op isn't available.
807
808 This supports obj[x:y], obj[:x], and obj[x:] for a few types.
809 """
810 if index.stride:
811 # We can only handle the default stride of 1.
812 return None
813
814 if index.begin_index:
815 begin_type = builder.node_type(index.begin_index)
816 else:
817 begin_type = int_rprimitive
818 if index.end_index:
819 end_type = builder.node_type(index.end_index)
820 else:
821 end_type = int_rprimitive
822
823 # Both begin and end index must be int (or missing).
824 if is_any_int(begin_type) and is_any_int(end_type):
825 if index.begin_index:
826 begin = builder.accept(index.begin_index)
827 else:
828 begin = builder.load_int(0)
829 if index.end_index:
830 end = builder.accept(index.end_index)
831 else:
832 # Replace missing end index with the largest short integer
833 # (a sequence can't be longer).
834 end = builder.load_int(MAX_SHORT_INT)
835 if isinstance(base.type, RVec):
836 return vec_slice(builder.builder, base, begin, end, index.line)
837 candidates = [list_slice_op, tuple_slice_op, str_slice_op, bytes_slice_op]
838 return builder.builder.matching_call_c(candidates, [base, begin, end], index.line)
839
840 return None
841
842
843def transform_conditional_expr(builder: IRBuilder, expr: ConditionalExpr) -> Value:

Callers 1

transform_index_exprFunction · 0.85

Calls 7

is_any_intFunction · 0.90
vec_sliceFunction · 0.90
isinstanceFunction · 0.85
node_typeMethod · 0.80
matching_call_cMethod · 0.80
acceptMethod · 0.45
load_intMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…