def sequence_set_slice(self, start, end, step, src_inst): '''Assign a sequence into a sliced sequence.''' # Case 0: no step means we can use the fast sequence SetSlice if step is None: tmp_inst = CIntegerLL(None, self.v) tmp_inst.declare_tmp(name='_rv_slice') if start is None: _start = c.Constant('integer', 0) else: _start = c.ID(start.as_ssize().name) if end is None: _end = c.Constant('integer', CIntegerLL.MAX) else: _end = c.ID(end.as_ssize().name) self.v.ctx.add(c.Assignment('=', c.ID(tmp_inst.name), c.FuncCall(c.ID('PySequence_SetSlice'), c.ExprList(c.ID(self.name), _start, _end, c.ID(src_inst.name))))) self.fail_if_negative(tmp_inst.name) tmp_inst.decref() else: slice_inst = PySliceLL(None, self.v) slice_inst.declare_tmp(name='_slice') if start is None: _start = self.v.none; _start.incref() else: _start = start.as_pyobject() if end is None: _end = self.v.none; _end.incref() else: _end = end.as_pyobject() _step = step.as_pyobject() slice_inst.new(_start, _end, _step) self.set_item(slice_inst, src_inst) slice_inst.decref()
def sequence_del_slice(self, start, end, step): if step is None: tmp_inst = CIntegerLL(None, self.v) tmp_inst.declare_tmp(name='_rv_slice') if start is None: _start = c.Constant('integer', 0) else: _start = c.ID(start.as_ssize().name) if end is None: _end = c.Constant('integer', CIntegerLL.MAX) else: _end = c.ID(end.as_ssize().name) self.v.ctx.add(c.Assignment('=', c.ID(tmp_inst.name), c.FuncCall(c.ID('PySequence_DelSlice'), c.ExprList(c.ID(self.name), _start, _end)))) self.fail_if_nonzero(tmp_inst.name) tmp_inst.decref() else: slice_inst = PySliceLL(None, self.v) slice_inst.declare_tmp(name='_slice') if start is None: _start = self.v.none; _start.incref() else: _start = start.as_pyobject() if end is None: _end = self.v.none; _end.incref() else: _end = end.as_pyobject() _step = step.as_pyobject() slice_inst.new(_start, _end, _step) self.del_item(slice_inst) slice_inst.decref()
def sequence_get_slice(self, start, end, step, out_inst=None): '''Extract a sequence from this sequence which is a slice of this sequence.''' # Case 0: no step -> just call GetSlice with ints if step is None: if not out_inst: out_inst = PyObjectLL(None, self.v) out_inst.declare_tmp(name='_sliced') if start is None: _start = c.Constant('integer', 0) else: _start = c.ID(start.as_ssize().name) if end is None: _end = c.Constant('integer', CIntegerLL.MAX) else: _end = c.ID(end.as_ssize().name) self.v.ctx.add(c.Assignment('=', c.ID(out_inst.name), c.FuncCall(c.ID('PySequence_GetSlice'), c.ExprList(c.ID(self.name), _start, _end)))) self.fail_if_null(out_inst.name) # Case 1: have a step -> create a new Slice with PyObject's, use that to get item else: slice_inst = PySliceLL(None, self.v) slice_inst.declare_tmp(name='_slice') if start is None: _start = self.v.none; _start.incref() else: _start = start.as_pyobject() if end is None: _end = self.v.none; _end.incref() else: _end = end.as_pyobject() _step = step.as_pyobject() slice_inst.new(_start, _end, _step) out_inst = self.get_item(slice_inst) slice_inst.decref() return out_inst