diff options
Diffstat (limited to 'contrib/tools/python3/src/Objects/sliceobject.c')
| -rw-r--r-- | contrib/tools/python3/src/Objects/sliceobject.c | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/contrib/tools/python3/src/Objects/sliceobject.c b/contrib/tools/python3/src/Objects/sliceobject.c index 391711f711a..22fb7c61c35 100644 --- a/contrib/tools/python3/src/Objects/sliceobject.c +++ b/contrib/tools/python3/src/Objects/sliceobject.c @@ -15,7 +15,8 @@ this type and there is exactly one in existence. #include "Python.h" #include "pycore_abstract.h" // _PyIndex_Check() -#include "pycore_object.h" +#include "pycore_long.h" // _PyLong_GetZero() +#include "pycore_object.h" // _PyObject_GC_TRACK() #include "structmember.h" // PyMemberDef static PyObject * @@ -95,16 +96,12 @@ PyObject _Py_EllipsisObject = { /* Slice object implementation */ -/* Using a cache is very effective since typically only a single slice is - * created and then deleted again - */ -static PySliceObject *slice_cache = NULL; -void _PySlice_Fini(void) +void _PySlice_Fini(PyInterpreterState *interp) { - PySliceObject *obj = slice_cache; + PySliceObject *obj = interp->slice_cache; if (obj != NULL) { - slice_cache = NULL; + interp->slice_cache = NULL; PyObject_GC_Del(obj); } } @@ -116,26 +113,35 @@ void _PySlice_Fini(void) PyObject * PySlice_New(PyObject *start, PyObject *stop, PyObject *step) { + if (step == NULL) { + step = Py_None; + } + if (start == NULL) { + start = Py_None; + } + if (stop == NULL) { + stop = Py_None; + } + + PyInterpreterState *interp = _PyInterpreterState_GET(); PySliceObject *obj; - if (slice_cache != NULL) { - obj = slice_cache; - slice_cache = NULL; + if (interp->slice_cache != NULL) { + obj = interp->slice_cache; + interp->slice_cache = NULL; _Py_NewReference((PyObject *)obj); - } else { + } + else { obj = PyObject_GC_New(PySliceObject, &PySlice_Type); - if (obj == NULL) + if (obj == NULL) { return NULL; + } } - if (step == NULL) step = Py_None; Py_INCREF(step); - if (start == NULL) start = Py_None; - Py_INCREF(start); - if (stop == NULL) stop = Py_None; - Py_INCREF(stop); - obj->step = step; + Py_INCREF(start); obj->start = start; + Py_INCREF(stop); obj->stop = stop; _PyObject_GC_TRACK(obj); @@ -324,14 +330,17 @@ Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); static void slice_dealloc(PySliceObject *r) { + PyInterpreterState *interp = _PyInterpreterState_GET(); _PyObject_GC_UNTRACK(r); Py_DECREF(r->step); Py_DECREF(r->start); Py_DECREF(r->stop); - if (slice_cache == NULL) - slice_cache = r; - else + if (interp->slice_cache == NULL) { + interp->slice_cache = r; + } + else { PyObject_GC_Del(r); + } } static PyObject * @@ -379,7 +388,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, /* Convert step to an integer; raise for zero step. */ if (self->step == Py_None) { - step = _PyLong_One; + step = _PyLong_GetOne(); Py_INCREF(step); step_is_negative = 0; } @@ -408,7 +417,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, goto error; } else { - lower = _PyLong_Zero; + lower = _PyLong_GetZero(); Py_INCREF(lower); upper = length; Py_INCREF(upper); |
