From 8fdbbf8b18f1405abe677d0e04874c1c86ccdb4a Mon Sep 17 00:00:00 2001 From: Amit Lavon Date: Sat, 7 Jun 2025 14:08:44 -0700 Subject: GH-131798: Type-propagate string/list/tuple slices (GH-134671) --- Python/optimizer_bytecodes.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'Python/optimizer_bytecodes.c') diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 12efaacd8f0..fbf4dfd3db6 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1237,6 +1237,20 @@ dummy_func(void) { sym_set_const(callable, list_append); } + op(_BINARY_SLICE, (container, start, stop -- res)) { + // Slicing a string/list/tuple always returns the same type. + PyTypeObject *type = sym_get_type(container); + if (type == &PyUnicode_Type || + type == &PyList_Type || + type == &PyTuple_Type) + { + res = sym_new_type(ctx, type); + } + else { + res = sym_new_not_null(ctx); + } + } + // END BYTECODES // } -- cgit v1.2.3