diff options
author | Michael Droettboom <mdboom@gmail.com> | 2024-11-07 16:39:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-07 16:39:23 (GMT) |
commit | a38e82bd8c249c126ab033c078170b6dea27a619 (patch) | |
tree | 30d94d6b3ac4f1c8ada50988a33bc3ae6656a4f3 /Objects/codeobject.c | |
parent | 9357fdcaf0b08dac9396c17e8695b420fad887f8 (diff) | |
download | cpython-a38e82bd8c249c126ab033c078170b6dea27a619.zip cpython-a38e82bd8c249c126ab033c078170b6dea27a619.tar.gz cpython-a38e82bd8c249c126ab033c078170b6dea27a619.tar.bz2 |
gh-126298: Don't deduplicate slice constants based on equality (#126398)
* gh-126298: Don't deduplicated slice constants based on equality
* NULL check for PySlice_New
* Fix refcounting
* Fix refcounting some more
* Fix refcounting
* Make tests more complete
* Fix tests
Diffstat (limited to 'Objects/codeobject.c')
-rw-r--r-- | Objects/codeobject.c | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 1cf9740..dba43d5 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -2388,7 +2388,6 @@ _PyCode_ConstantKey(PyObject *op) if (op == Py_None || op == Py_Ellipsis || PyLong_CheckExact(op) || PyUnicode_CheckExact(op) - || PySlice_Check(op) /* code_richcompare() uses _PyCode_ConstantKey() internally */ || PyCode_Check(op)) { @@ -2496,6 +2495,40 @@ _PyCode_ConstantKey(PyObject *op) Py_DECREF(set); return key; } + else if (PySlice_Check(op)) { + PySliceObject *slice = (PySliceObject *)op; + PyObject *start_key = NULL; + PyObject *stop_key = NULL; + PyObject *step_key = NULL; + key = NULL; + + start_key = _PyCode_ConstantKey(slice->start); + if (start_key == NULL) { + goto slice_exit; + } + + stop_key = _PyCode_ConstantKey(slice->stop); + if (stop_key == NULL) { + goto slice_exit; + } + + step_key = _PyCode_ConstantKey(slice->step); + if (step_key == NULL) { + goto slice_exit; + } + + PyObject *slice_key = PySlice_New(start_key, stop_key, step_key); + if (slice_key == NULL) { + goto slice_exit; + } + + key = PyTuple_Pack(2, slice_key, op); + Py_DECREF(slice_key); + slice_exit: + Py_XDECREF(start_key); + Py_XDECREF(stop_key); + Py_XDECREF(step_key); + } else { /* for other types, use the object identifier as a unique identifier * to ensure that they are seen as unequal. */ |