aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/specialize.c
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@microsoft.com>2023-08-08 13:42:43 -0700
committerGitHub <noreply@github.com>2023-08-08 13:42:43 -0700
commitea72c6fe3b6db5f4e8ce3d3405c0ea65dc002faf (patch)
tree97e185191d3f852d6533f5db5af5f32d7cc3d5f6 /Python/specialize.c
parentaab6f7173a3b825599629dd6fa5cb7e477421595 (diff)
downloadcpython-ea72c6fe3b6db5f4e8ce3d3405c0ea65dc002faf.tar.gz
cpython-ea72c6fe3b6db5f4e8ce3d3405c0ea65dc002faf.zip
GH-107596: Specialize str[int] (GH-107597)
Diffstat (limited to 'Python/specialize.c')
-rw-r--r--Python/specialize.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/Python/specialize.c b/Python/specialize.c
index de329ef1195..855252e066d 100644
--- a/Python/specialize.c
+++ b/Python/specialize.c
@@ -363,7 +363,6 @@ _PyCode_Quicken(PyCodeObject *code)
#define SPEC_FAIL_SUBSCR_ARRAY_SLICE 10
#define SPEC_FAIL_SUBSCR_LIST_SLICE 11
#define SPEC_FAIL_SUBSCR_TUPLE_SLICE 12
-#define SPEC_FAIL_SUBSCR_STRING_INT 13
#define SPEC_FAIL_SUBSCR_STRING_SLICE 14
#define SPEC_FAIL_SUBSCR_BUFFER_INT 15
#define SPEC_FAIL_SUBSCR_BUFFER_SLICE 16
@@ -1260,16 +1259,7 @@ success:
static int
binary_subscr_fail_kind(PyTypeObject *container_type, PyObject *sub)
{
- if (container_type == &PyUnicode_Type) {
- if (PyLong_CheckExact(sub)) {
- return SPEC_FAIL_SUBSCR_STRING_INT;
- }
- if (PySlice_Check(sub)) {
- return SPEC_FAIL_SUBSCR_STRING_SLICE;
- }
- return SPEC_FAIL_OTHER;
- }
- else if (strcmp(container_type->tp_name, "array.array") == 0) {
+ if (strcmp(container_type->tp_name, "array.array") == 0) {
if (PyLong_CheckExact(sub)) {
return SPEC_FAIL_SUBSCR_ARRAY_INT;
}
@@ -1376,6 +1366,19 @@ _Py_Specialize_BinarySubscr(
PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_TUPLE_SLICE : SPEC_FAIL_OTHER);
goto fail;
}
+ if (container_type == &PyUnicode_Type) {
+ if (PyLong_CheckExact(sub)) {
+ if (_PyLong_IsNonNegativeCompact((PyLongObject *)sub)) {
+ instr->op.code = BINARY_SUBSCR_STR_INT;
+ goto success;
+ }
+ SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
+ goto fail;
+ }
+ SPECIALIZATION_FAIL(BINARY_SUBSCR,
+ PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_STRING_SLICE : SPEC_FAIL_OTHER);
+ goto fail;
+ }
if (container_type == &PyDict_Type) {
instr->op.code = BINARY_SUBSCR_DICT;
goto success;