mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Improve extended slicing support in builtin types and classes. Specifically:
- Specialcase extended slices that amount to a shallow copy the same way as is done for simple slices, in the tuple, string and unicode case. - Specialcase step-1 extended slices to optimize the common case for all involved types. - For lists, allow extended slice assignment of differing lengths as long as the step is 1. (Previously, 'l[:2:1] = []' failed even though 'l[:2] = []' and 'l[:2:None] = []' do not.) - Implement extended slicing for buffer, array, structseq, mmap and UserString.UserString. - Implement slice-object support (but not non-step-1 slice assignment) for UserString.MutableString. - Add tests for all new functionality.
This commit is contained in:
parent
0f4a14b56f
commit
3ccec68a05
16 changed files with 730 additions and 120 deletions
|
|
@ -149,15 +149,41 @@ class MutableString(UserString):
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
raise TypeError, "unhashable type (it is mutable)"
|
raise TypeError, "unhashable type (it is mutable)"
|
||||||
def __setitem__(self, index, sub):
|
def __setitem__(self, index, sub):
|
||||||
if index < 0:
|
if isinstance(index, slice):
|
||||||
index += len(self.data)
|
if isinstance(sub, UserString):
|
||||||
if index < 0 or index >= len(self.data): raise IndexError
|
sub = sub.data
|
||||||
self.data = self.data[:index] + sub + self.data[index+1:]
|
elif not isinstance(sub, basestring):
|
||||||
|
sub = str(sub)
|
||||||
|
start, stop, step = index.indices(len(self.data))
|
||||||
|
if step == -1:
|
||||||
|
start, stop = stop+1, start+1
|
||||||
|
sub = sub[::-1]
|
||||||
|
elif step != 1:
|
||||||
|
# XXX(twouters): I guess we should be reimplementing
|
||||||
|
# the extended slice assignment/deletion algorithm here...
|
||||||
|
raise TypeError, "invalid step in slicing assignment"
|
||||||
|
start = min(start, stop)
|
||||||
|
self.data = self.data[:start] + sub + self.data[stop:]
|
||||||
|
else:
|
||||||
|
if index < 0:
|
||||||
|
index += len(self.data)
|
||||||
|
if index < 0 or index >= len(self.data): raise IndexError
|
||||||
|
self.data = self.data[:index] + sub + self.data[index+1:]
|
||||||
def __delitem__(self, index):
|
def __delitem__(self, index):
|
||||||
if index < 0:
|
if isinstance(index, slice):
|
||||||
index += len(self.data)
|
start, stop, step = index.indices(len(self.data))
|
||||||
if index < 0 or index >= len(self.data): raise IndexError
|
if step == -1:
|
||||||
self.data = self.data[:index] + self.data[index+1:]
|
start, stop = stop+1, start+1
|
||||||
|
elif step != 1:
|
||||||
|
# XXX(twouters): see same block in __setitem__
|
||||||
|
raise TypeError, "invalid step in slicing deletion"
|
||||||
|
start = min(start, stop)
|
||||||
|
self.data = self.data[:start] + self.data[stop:]
|
||||||
|
else:
|
||||||
|
if index < 0:
|
||||||
|
index += len(self.data)
|
||||||
|
if index < 0 or index >= len(self.data): raise IndexError
|
||||||
|
self.data = self.data[:index] + self.data[index+1:]
|
||||||
def __setslice__(self, start, end, sub):
|
def __setslice__(self, start, end, sub):
|
||||||
start = max(start, 0); end = max(end, 0)
|
start = max(start, 0); end = max(end, 0)
|
||||||
if isinstance(sub, UserString):
|
if isinstance(sub, UserString):
|
||||||
|
|
|
||||||
|
|
@ -179,8 +179,10 @@ class CommonTest(seq_tests.CommonTest):
|
||||||
self.assertEqual(a, self.type2test(range(10)))
|
self.assertEqual(a, self.type2test(range(10)))
|
||||||
|
|
||||||
self.assertRaises(TypeError, a.__setslice__, 0, 1, 5)
|
self.assertRaises(TypeError, a.__setslice__, 0, 1, 5)
|
||||||
|
self.assertRaises(TypeError, a.__setitem__, slice(0, 1, 5))
|
||||||
|
|
||||||
self.assertRaises(TypeError, a.__setslice__)
|
self.assertRaises(TypeError, a.__setslice__)
|
||||||
|
self.assertRaises(TypeError, a.__setitem__)
|
||||||
|
|
||||||
def test_delslice(self):
|
def test_delslice(self):
|
||||||
a = self.type2test([0, 1])
|
a = self.type2test([0, 1])
|
||||||
|
|
|
||||||
|
|
@ -912,7 +912,6 @@ class MixinStrUnicodeUserStringTest:
|
||||||
self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
|
self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
|
||||||
self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
|
self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
|
||||||
self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
|
self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
|
||||||
# FIXME What about negative indices? This is handled differently by [] and __getitem__(slice)
|
|
||||||
|
|
||||||
self.checkraises(TypeError, 'abc', '__getitem__', 'def')
|
self.checkraises(TypeError, 'abc', '__getitem__', 'def')
|
||||||
|
|
||||||
|
|
@ -926,10 +925,21 @@ class MixinStrUnicodeUserStringTest:
|
||||||
self.checkequal('', 'abc', '__getslice__', 1000, 1000)
|
self.checkequal('', 'abc', '__getslice__', 1000, 1000)
|
||||||
self.checkequal('', 'abc', '__getslice__', 2000, 1000)
|
self.checkequal('', 'abc', '__getslice__', 2000, 1000)
|
||||||
self.checkequal('', 'abc', '__getslice__', 2, 1)
|
self.checkequal('', 'abc', '__getslice__', 2, 1)
|
||||||
# FIXME What about negative indizes? This is handled differently by [] and __getslice__
|
|
||||||
|
|
||||||
self.checkraises(TypeError, 'abc', '__getslice__', 'def')
|
self.checkraises(TypeError, 'abc', '__getslice__', 'def')
|
||||||
|
|
||||||
|
def test_extended_getslice(self):
|
||||||
|
# Test extended slicing by comparing with list slicing.
|
||||||
|
s = string.ascii_letters + string.digits
|
||||||
|
indices = (0, None, 1, 3, 41, -1, -2, -37)
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Skip step 0 (invalid)
|
||||||
|
for step in indices[1:]:
|
||||||
|
L = list(s)[start:stop:step]
|
||||||
|
self.checkequal(u"".join(L), s, '__getitem__',
|
||||||
|
slice(start, stop, step))
|
||||||
|
|
||||||
def test_mul(self):
|
def test_mul(self):
|
||||||
self.checkequal('', 'abc', '__mul__', -1)
|
self.checkequal('', 'abc', '__mul__', -1)
|
||||||
self.checkequal('', 'abc', '__mul__', 0)
|
self.checkequal('', 'abc', '__mul__', 0)
|
||||||
|
|
|
||||||
|
|
@ -474,6 +474,18 @@ class BaseTest(unittest.TestCase):
|
||||||
array.array(self.typecode)
|
array.array(self.typecode)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_extended_getslice(self):
|
||||||
|
# Test extended slicing by comparing with list slicing
|
||||||
|
# (Assumes list conversion works correctly, too)
|
||||||
|
a = array.array(self.typecode, self.example)
|
||||||
|
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Everything except the initial 0 (invalid step)
|
||||||
|
for step in indices[1:]:
|
||||||
|
self.assertEqual(list(a[start:stop:step]),
|
||||||
|
list(a)[start:stop:step])
|
||||||
|
|
||||||
def test_setslice(self):
|
def test_setslice(self):
|
||||||
a = array.array(self.typecode, self.example)
|
a = array.array(self.typecode, self.example)
|
||||||
a[:1] = a
|
a[:1] = a
|
||||||
|
|
@ -557,12 +569,34 @@ class BaseTest(unittest.TestCase):
|
||||||
|
|
||||||
a = array.array(self.typecode, self.example)
|
a = array.array(self.typecode, self.example)
|
||||||
self.assertRaises(TypeError, a.__setslice__, 0, 0, None)
|
self.assertRaises(TypeError, a.__setslice__, 0, 0, None)
|
||||||
|
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None)
|
||||||
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None)
|
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None)
|
||||||
|
|
||||||
b = array.array(self.badtypecode())
|
b = array.array(self.badtypecode())
|
||||||
self.assertRaises(TypeError, a.__setslice__, 0, 0, b)
|
self.assertRaises(TypeError, a.__setslice__, 0, 0, b)
|
||||||
|
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b)
|
||||||
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b)
|
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b)
|
||||||
|
|
||||||
|
def test_extended_set_del_slice(self):
|
||||||
|
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Everything except the initial 0 (invalid step)
|
||||||
|
for step in indices[1:]:
|
||||||
|
a = array.array(self.typecode, self.example)
|
||||||
|
L = list(a)
|
||||||
|
# Make sure we have a slice of exactly the right length,
|
||||||
|
# but with (hopefully) different data.
|
||||||
|
data = L[start:stop:step]
|
||||||
|
data.reverse()
|
||||||
|
L[start:stop:step] = data
|
||||||
|
a[start:stop:step] = array.array(self.typecode, data)
|
||||||
|
self.assertEquals(a, array.array(self.typecode, L))
|
||||||
|
|
||||||
|
del L[start:stop:step]
|
||||||
|
del a[start:stop:step]
|
||||||
|
self.assertEquals(a, array.array(self.typecode, L))
|
||||||
|
|
||||||
def test_index(self):
|
def test_index(self):
|
||||||
example = 2*self.example
|
example = 2*self.example
|
||||||
a = array.array(self.typecode, example)
|
a = array.array(self.typecode, example)
|
||||||
|
|
|
||||||
29
Lib/test/test_buffer.py
Normal file
29
Lib/test/test_buffer.py
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
"""Unit tests for buffer objects.
|
||||||
|
|
||||||
|
For now, tests just new or changed functionality.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from test import test_support
|
||||||
|
|
||||||
|
class BufferTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_extended_getslice(self):
|
||||||
|
# Test extended slicing by comparing with list slicing.
|
||||||
|
s = "".join(chr(c) for c in list(range(255, -1, -1)))
|
||||||
|
b = buffer(s)
|
||||||
|
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Skip step 0 (invalid)
|
||||||
|
for step in indices[1:]:
|
||||||
|
self.assertEqual(b[start:stop:step],
|
||||||
|
s[start:stop:step])
|
||||||
|
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
test_support.run_unittest(BufferTests)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_main()
|
||||||
|
|
@ -306,6 +306,40 @@ class MmapTests(unittest.TestCase):
|
||||||
m[x] = ch = chr(x & 255)
|
m[x] = ch = chr(x & 255)
|
||||||
self.assertEqual(m[x], ch)
|
self.assertEqual(m[x], ch)
|
||||||
|
|
||||||
|
def test_extended_getslice(self):
|
||||||
|
# Test extended slicing by comparing with list slicing.
|
||||||
|
s = "".join(chr(c) for c in reversed(range(256)))
|
||||||
|
m = mmap.mmap(-1, len(s))
|
||||||
|
m[:] = s
|
||||||
|
self.assertEqual(m[:], s)
|
||||||
|
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Skip step 0 (invalid)
|
||||||
|
for step in indices[1:]:
|
||||||
|
self.assertEqual(m[start:stop:step],
|
||||||
|
s[start:stop:step])
|
||||||
|
|
||||||
|
def test_extended_set_del_slice(self):
|
||||||
|
# Test extended slicing by comparing with list slicing.
|
||||||
|
s = "".join(chr(c) for c in reversed(range(256)))
|
||||||
|
m = mmap.mmap(-1, len(s))
|
||||||
|
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Skip invalid step 0
|
||||||
|
for step in indices[1:]:
|
||||||
|
m[:] = s
|
||||||
|
self.assertEqual(m[:], s)
|
||||||
|
L = list(s)
|
||||||
|
# Make sure we have a slice of exactly the right length,
|
||||||
|
# but with different data.
|
||||||
|
data = L[start:stop:step]
|
||||||
|
data = "".join(reversed(data))
|
||||||
|
L[start:stop:step] = data
|
||||||
|
m[start:stop:step] = data
|
||||||
|
self.assertEquals(m[:], "".join(L))
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
run_unittest(MmapTests)
|
run_unittest(MmapTests)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,18 @@ class StructSeqTest(unittest.TestCase):
|
||||||
t = time.gmtime()
|
t = time.gmtime()
|
||||||
x = t.__reduce__()
|
x = t.__reduce__()
|
||||||
|
|
||||||
|
def test_extended_getslice(self):
|
||||||
|
# Test extended slicing by comparing with list slicing.
|
||||||
|
t = time.gmtime()
|
||||||
|
L = list(t)
|
||||||
|
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Skip step 0 (invalid)
|
||||||
|
for step in indices[1:]:
|
||||||
|
self.assertEqual(list(t[start:stop:step]),
|
||||||
|
L[start:stop:step])
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(StructSeqTest)
|
test_support.run_unittest(StructSeqTest)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
# UserString instances should behave similar to builtin string objects.
|
# UserString instances should behave similar to builtin string objects.
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
import string
|
||||||
from test import test_support, string_tests
|
from test import test_support, string_tests
|
||||||
|
|
||||||
from UserString import UserString, MutableString
|
from UserString import UserString, MutableString
|
||||||
|
|
@ -88,6 +89,28 @@ class MutableStringTest(UserStringTest):
|
||||||
del s[-1:10]
|
del s[-1:10]
|
||||||
self.assertEqual(s, "fo")
|
self.assertEqual(s, "fo")
|
||||||
|
|
||||||
|
def test_extended_set_del_slice(self):
|
||||||
|
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
|
||||||
|
orig = string.ascii_letters + string.digits
|
||||||
|
for start in indices:
|
||||||
|
for stop in indices:
|
||||||
|
# Use indices[1:] when MutableString can handle real
|
||||||
|
# extended slices
|
||||||
|
for step in (None, 1, -1):
|
||||||
|
s = self.type2test(orig)
|
||||||
|
L = list(orig)
|
||||||
|
# Make sure we have a slice of exactly the right length,
|
||||||
|
# but with (hopefully) different data.
|
||||||
|
data = L[start:stop:step]
|
||||||
|
data.reverse()
|
||||||
|
L[start:stop:step] = data
|
||||||
|
s[start:stop:step] = "".join(data)
|
||||||
|
self.assertEquals(s, "".join(L))
|
||||||
|
|
||||||
|
del L[start:stop:step]
|
||||||
|
del s[start:stop:step]
|
||||||
|
self.assertEquals(s, "".join(L))
|
||||||
|
|
||||||
def test_immutable(self):
|
def test_immutable(self):
|
||||||
s = self.type2test("foobar")
|
s = self.type2test("foobar")
|
||||||
s2 = s.immutable()
|
s2 = s.immutable()
|
||||||
|
|
|
||||||
|
|
@ -1605,6 +1605,16 @@ array_subscr(arrayobject* self, PyObject* item)
|
||||||
if (slicelength <= 0) {
|
if (slicelength <= 0) {
|
||||||
return newarrayobject(&Arraytype, 0, self->ob_descr);
|
return newarrayobject(&Arraytype, 0, self->ob_descr);
|
||||||
}
|
}
|
||||||
|
else if (step == 1) {
|
||||||
|
PyObject *result = newarrayobject(&Arraytype,
|
||||||
|
slicelength, self->ob_descr);
|
||||||
|
if (result == NULL)
|
||||||
|
return NULL;
|
||||||
|
memcpy(((arrayobject *)result)->ob_item,
|
||||||
|
self->ob_item + start * itemsize,
|
||||||
|
slicelength * itemsize);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
|
result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
|
||||||
if (!result) return NULL;
|
if (!result) return NULL;
|
||||||
|
|
@ -1623,7 +1633,7 @@ array_subscr(arrayobject* self, PyObject* item)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"list indices must be integers");
|
"array indices must be integers");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1631,112 +1641,146 @@ array_subscr(arrayobject* self, PyObject* item)
|
||||||
static int
|
static int
|
||||||
array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
|
array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
|
||||||
{
|
{
|
||||||
|
Py_ssize_t start, stop, step, slicelength, needed;
|
||||||
|
arrayobject* other;
|
||||||
|
int itemsize;
|
||||||
|
|
||||||
if (PyIndex_Check(item)) {
|
if (PyIndex_Check(item)) {
|
||||||
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
||||||
if (i==-1 && PyErr_Occurred())
|
|
||||||
|
if (i == -1 && PyErr_Occurred())
|
||||||
return -1;
|
return -1;
|
||||||
if (i < 0)
|
if (i < 0)
|
||||||
i += Py_Size(self);
|
i += Py_Size(self);
|
||||||
return array_ass_item(self, i, value);
|
if (i < 0 || i >= Py_Size(self)) {
|
||||||
}
|
PyErr_SetString(PyExc_IndexError,
|
||||||
else if (PySlice_Check(item)) {
|
"array assignment index out of range");
|
||||||
Py_ssize_t start, stop, step, slicelength;
|
|
||||||
int itemsize = self->ob_descr->itemsize;
|
|
||||||
|
|
||||||
if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self),
|
|
||||||
&start, &stop, &step, &slicelength) < 0) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
|
|
||||||
if (step == 1 && ((PySliceObject*)item)->step == Py_None)
|
|
||||||
return array_ass_slice(self, start, stop, value);
|
|
||||||
|
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
/* delete slice */
|
/* Fall through to slice assignment */
|
||||||
Py_ssize_t cur, i, extra;
|
start = i;
|
||||||
|
stop = i + 1;
|
||||||
if (slicelength <= 0)
|
step = 1;
|
||||||
return 0;
|
slicelength = 1;
|
||||||
|
|
||||||
if (step < 0) {
|
|
||||||
stop = start + 1;
|
|
||||||
start = stop + step*(slicelength - 1) - 1;
|
|
||||||
step = -step;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (cur = start, i = 0; i < slicelength - 1;
|
|
||||||
cur += step, i++) {
|
|
||||||
memmove(self->ob_item + (cur - i)*itemsize,
|
|
||||||
self->ob_item + (cur + 1)*itemsize,
|
|
||||||
(step - 1) * itemsize);
|
|
||||||
}
|
|
||||||
extra = Py_Size(self) - (cur + 1);
|
|
||||||
if (extra > 0) {
|
|
||||||
memmove(self->ob_item + (cur - i)*itemsize,
|
|
||||||
self->ob_item + (cur + 1)*itemsize,
|
|
||||||
extra*itemsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
Py_Size(self) -= slicelength;
|
|
||||||
self->ob_item = (char *)PyMem_REALLOC(self->ob_item,
|
|
||||||
itemsize*Py_Size(self));
|
|
||||||
self->allocated = Py_Size(self);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
/* assign slice */
|
return (*self->ob_descr->setitem)(self, i, value);
|
||||||
Py_ssize_t cur, i;
|
}
|
||||||
arrayobject* av;
|
else if (PySlice_Check(item)) {
|
||||||
|
if (PySlice_GetIndicesEx((PySliceObject *)item,
|
||||||
if (!array_Check(value)) {
|
Py_Size(self), &start, &stop,
|
||||||
PyErr_Format(PyExc_TypeError,
|
&step, &slicelength) < 0) {
|
||||||
"must assign array (not \"%.200s\") to slice",
|
return -1;
|
||||||
Py_Type(value)->tp_name);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
av = (arrayobject*)value;
|
|
||||||
|
|
||||||
if (Py_Size(av) != slicelength) {
|
|
||||||
PyErr_Format(PyExc_ValueError,
|
|
||||||
"attempt to assign array of size %ld to extended slice of size %ld",
|
|
||||||
/*XXX*/(long)Py_Size(av), /*XXX*/(long)slicelength);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!slicelength)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
/* protect against a[::-1] = a */
|
|
||||||
if (self == av) {
|
|
||||||
value = array_slice(av, 0, Py_Size(av));
|
|
||||||
av = (arrayobject*)value;
|
|
||||||
if (!av)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Py_INCREF(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (cur = start, i = 0; i < slicelength;
|
|
||||||
cur += step, i++) {
|
|
||||||
memcpy(self->ob_item + cur*itemsize,
|
|
||||||
av->ob_item + i*itemsize,
|
|
||||||
itemsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
Py_DECREF(value);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"list indices must be integers");
|
"array indices must be integer");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (value == NULL) {
|
||||||
|
other = NULL;
|
||||||
|
needed = 0;
|
||||||
|
}
|
||||||
|
else if (array_Check(value)) {
|
||||||
|
other = (arrayobject *)value;
|
||||||
|
needed = Py_Size(other);
|
||||||
|
if (self == other) {
|
||||||
|
/* Special case "self[i:j] = self" -- copy self first */
|
||||||
|
int ret;
|
||||||
|
value = array_slice(other, 0, needed);
|
||||||
|
if (value == NULL)
|
||||||
|
return -1;
|
||||||
|
ret = array_ass_subscr(self, item, value);
|
||||||
|
Py_DECREF(value);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
if (other->ob_descr != self->ob_descr) {
|
||||||
|
PyErr_BadArgument();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"can only assign array (not \"%.200s\") to array slice",
|
||||||
|
Py_Type(value)->tp_name);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
itemsize = self->ob_descr->itemsize;
|
||||||
|
/* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
|
||||||
|
if ((step > 0 && stop < start) ||
|
||||||
|
(step < 0 && stop > start))
|
||||||
|
stop = start;
|
||||||
|
if (step == 1) {
|
||||||
|
if (slicelength > needed) {
|
||||||
|
memmove(self->ob_item + (start + needed) * itemsize,
|
||||||
|
self->ob_item + stop * itemsize,
|
||||||
|
(Py_Size(self) - stop) * itemsize);
|
||||||
|
if (array_resize(self, Py_Size(self) +
|
||||||
|
needed - slicelength) < 0)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else if (slicelength < needed) {
|
||||||
|
if (array_resize(self, Py_Size(self) +
|
||||||
|
needed - slicelength) < 0)
|
||||||
|
return -1;
|
||||||
|
memmove(self->ob_item + (start + needed) * itemsize,
|
||||||
|
self->ob_item + stop * itemsize,
|
||||||
|
(Py_Size(self) - start - needed) * itemsize);
|
||||||
|
}
|
||||||
|
if (needed > 0)
|
||||||
|
memcpy(self->ob_item + start * itemsize,
|
||||||
|
other->ob_item, needed * itemsize);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if (needed == 0) {
|
||||||
|
/* Delete slice */
|
||||||
|
Py_ssize_t cur, i;
|
||||||
|
|
||||||
|
if (step < 0) {
|
||||||
|
stop = start + 1;
|
||||||
|
start = stop + step * (slicelength - 1) - 1;
|
||||||
|
step = -step;
|
||||||
|
}
|
||||||
|
for (cur = start, i = 0; i < slicelength;
|
||||||
|
cur += step, i++) {
|
||||||
|
Py_ssize_t lim = step - 1;
|
||||||
|
|
||||||
|
if (cur + step >= Py_Size(self))
|
||||||
|
lim = Py_Size(self) - cur - 1;
|
||||||
|
memmove(self->ob_item + (cur - i) * itemsize,
|
||||||
|
self->ob_item + (cur + 1) * itemsize,
|
||||||
|
lim * itemsize);
|
||||||
|
}
|
||||||
|
cur = start + slicelength * step;
|
||||||
|
if (cur < Py_Size(self)) {
|
||||||
|
memmove(self->ob_item + (cur-slicelength) * itemsize,
|
||||||
|
self->ob_item + cur * itemsize,
|
||||||
|
(Py_Size(self) - cur) * itemsize);
|
||||||
|
}
|
||||||
|
if (array_resize(self, Py_Size(self) - slicelength) < 0)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Py_ssize_t cur, i;
|
||||||
|
|
||||||
|
if (needed != slicelength) {
|
||||||
|
PyErr_Format(PyExc_ValueError,
|
||||||
|
"attempt to assign array of size %zd "
|
||||||
|
"to extended slice of size %zd",
|
||||||
|
needed, slicelength);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
for (cur = start, i = 0; i < slicelength;
|
||||||
|
cur += step, i++) {
|
||||||
|
memcpy(self->ob_item + cur * itemsize,
|
||||||
|
other->ob_item + i * itemsize,
|
||||||
|
itemsize);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMappingMethods array_as_mapping = {
|
static PyMappingMethods array_as_mapping = {
|
||||||
|
|
|
||||||
|
|
@ -680,6 +680,60 @@ mmap_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh)
|
||||||
return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow);
|
return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
mmap_subscript(mmap_object *self, PyObject *item)
|
||||||
|
{
|
||||||
|
CHECK_VALID(NULL);
|
||||||
|
if (PyIndex_Check(item)) {
|
||||||
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
||||||
|
if (i == -1 && PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
if (i < 0)
|
||||||
|
i += self->size;
|
||||||
|
if (i < 0 || i > self->size) {
|
||||||
|
PyErr_SetString(PyExc_IndexError,
|
||||||
|
"mmap index out of range");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return PyString_FromStringAndSize(self->data + i, 1);
|
||||||
|
}
|
||||||
|
else if (PySlice_Check(item)) {
|
||||||
|
Py_ssize_t start, stop, step, slicelen;
|
||||||
|
|
||||||
|
if (PySlice_GetIndicesEx((PySliceObject *)item, self->size,
|
||||||
|
&start, &stop, &step, &slicelen) < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (slicelen <= 0)
|
||||||
|
return PyString_FromStringAndSize("", 0);
|
||||||
|
else if (step == 1)
|
||||||
|
return PyString_FromStringAndSize(self->data + start,
|
||||||
|
slicelen);
|
||||||
|
else {
|
||||||
|
char *result_buf = (char *)PyMem_Malloc(slicelen);
|
||||||
|
Py_ssize_t cur, i;
|
||||||
|
PyObject *result;
|
||||||
|
|
||||||
|
if (result_buf == NULL)
|
||||||
|
return PyErr_NoMemory();
|
||||||
|
for (cur = start, i = 0; i < slicelen;
|
||||||
|
cur += step, i++) {
|
||||||
|
result_buf[i] = self->data[cur];
|
||||||
|
}
|
||||||
|
result = PyString_FromStringAndSize(result_buf,
|
||||||
|
slicelen);
|
||||||
|
PyMem_Free(result_buf);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"mmap indices must be integers");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
mmap_concat(mmap_object *self, PyObject *bb)
|
mmap_concat(mmap_object *self, PyObject *bb)
|
||||||
{
|
{
|
||||||
|
|
@ -764,6 +818,96 @@ mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
|
||||||
|
{
|
||||||
|
CHECK_VALID(-1);
|
||||||
|
|
||||||
|
if (PyIndex_Check(item)) {
|
||||||
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
||||||
|
const char *buf;
|
||||||
|
|
||||||
|
if (i == -1 && PyErr_Occurred())
|
||||||
|
return -1;
|
||||||
|
if (i < 0)
|
||||||
|
i += self->size;
|
||||||
|
if (i < 0 || i > self->size) {
|
||||||
|
PyErr_SetString(PyExc_IndexError,
|
||||||
|
"mmap index out of range");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (value == NULL) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"mmap object doesn't support item deletion");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!PyString_Check(value) || PyString_Size(value) != 1) {
|
||||||
|
PyErr_SetString(PyExc_IndexError,
|
||||||
|
"mmap assignment must be single-character string");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!is_writeable(self))
|
||||||
|
return -1;
|
||||||
|
buf = PyString_AsString(value);
|
||||||
|
self->data[i] = buf[0];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if (PySlice_Check(item)) {
|
||||||
|
Py_ssize_t start, stop, step, slicelen;
|
||||||
|
|
||||||
|
if (PySlice_GetIndicesEx((PySliceObject *)item,
|
||||||
|
self->size, &start, &stop,
|
||||||
|
&step, &slicelen) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (value == NULL) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"mmap object doesn't support slice deletion");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!PyString_Check(value)) {
|
||||||
|
PyErr_SetString(PyExc_IndexError,
|
||||||
|
"mmap slice assignment must be a string");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (PyString_Size(value) != slicelen) {
|
||||||
|
PyErr_SetString(PyExc_IndexError,
|
||||||
|
"mmap slice assignment is wrong size");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!is_writeable(self))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (slicelen == 0)
|
||||||
|
return 0;
|
||||||
|
else if (step == 1) {
|
||||||
|
const char *buf = PyString_AsString(value);
|
||||||
|
|
||||||
|
if (buf == NULL)
|
||||||
|
return -1;
|
||||||
|
memcpy(self->data + start, buf, slicelen);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Py_ssize_t cur, i;
|
||||||
|
const char *buf = PyString_AsString(value);
|
||||||
|
|
||||||
|
if (buf == NULL)
|
||||||
|
return -1;
|
||||||
|
for (cur = start, i = 0; i < slicelen;
|
||||||
|
cur += step, i++) {
|
||||||
|
self->data[cur] = buf[i];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"mmap indices must be integer");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static PySequenceMethods mmap_as_sequence = {
|
static PySequenceMethods mmap_as_sequence = {
|
||||||
(lenfunc)mmap_length, /*sq_length*/
|
(lenfunc)mmap_length, /*sq_length*/
|
||||||
(binaryfunc)mmap_concat, /*sq_concat*/
|
(binaryfunc)mmap_concat, /*sq_concat*/
|
||||||
|
|
@ -774,6 +918,12 @@ static PySequenceMethods mmap_as_sequence = {
|
||||||
(ssizessizeobjargproc)mmap_ass_slice, /*sq_ass_slice*/
|
(ssizessizeobjargproc)mmap_ass_slice, /*sq_ass_slice*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static PyMappingMethods mmap_as_mapping = {
|
||||||
|
(lenfunc)mmap_length,
|
||||||
|
(binaryfunc)mmap_subscript,
|
||||||
|
(objobjargproc)mmap_ass_subscript,
|
||||||
|
};
|
||||||
|
|
||||||
static PyBufferProcs mmap_as_buffer = {
|
static PyBufferProcs mmap_as_buffer = {
|
||||||
(readbufferproc)mmap_buffer_getreadbuf,
|
(readbufferproc)mmap_buffer_getreadbuf,
|
||||||
(writebufferproc)mmap_buffer_getwritebuf,
|
(writebufferproc)mmap_buffer_getwritebuf,
|
||||||
|
|
@ -795,7 +945,7 @@ static PyTypeObject mmap_object_type = {
|
||||||
0, /* tp_repr */
|
0, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
&mmap_as_sequence, /*tp_as_sequence*/
|
&mmap_as_sequence, /*tp_as_sequence*/
|
||||||
0, /*tp_as_mapping*/
|
&mmap_as_mapping, /*tp_as_mapping*/
|
||||||
0, /*tp_hash*/
|
0, /*tp_hash*/
|
||||||
0, /*tp_call*/
|
0, /*tp_call*/
|
||||||
0, /*tp_str*/
|
0, /*tp_str*/
|
||||||
|
|
|
||||||
|
|
@ -472,6 +472,61 @@ buffer_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right)
|
||||||
right - left);
|
right - left);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
buffer_subscript(PyBufferObject *self, PyObject *item)
|
||||||
|
{
|
||||||
|
void *p;
|
||||||
|
Py_ssize_t size;
|
||||||
|
|
||||||
|
if (!get_buf(self, &p, &size, ANY_BUFFER))
|
||||||
|
return NULL;
|
||||||
|
if (PyIndex_Check(item)) {
|
||||||
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
||||||
|
if (i == -1 && PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
if (i < 0)
|
||||||
|
i += size;
|
||||||
|
return buffer_item(self, i);
|
||||||
|
}
|
||||||
|
else if (PySlice_Check(item)) {
|
||||||
|
Py_ssize_t start, stop, step, slicelength, cur, i;
|
||||||
|
|
||||||
|
if (PySlice_GetIndicesEx((PySliceObject*)item, size,
|
||||||
|
&start, &stop, &step, &slicelength) < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (slicelength <= 0)
|
||||||
|
return PyString_FromStringAndSize("", 0);
|
||||||
|
else if (step == 1)
|
||||||
|
return PyString_FromStringAndSize((char *)p + start,
|
||||||
|
stop - start);
|
||||||
|
else {
|
||||||
|
PyObject *result;
|
||||||
|
char *source_buf = (char *)p;
|
||||||
|
char *result_buf = (char *)PyMem_Malloc(slicelength);
|
||||||
|
|
||||||
|
if (result_buf == NULL)
|
||||||
|
return PyErr_NoMemory();
|
||||||
|
|
||||||
|
for (cur = start, i = 0; i < slicelength;
|
||||||
|
cur += step, i++) {
|
||||||
|
result_buf[i] = source_buf[cur];
|
||||||
|
}
|
||||||
|
|
||||||
|
result = PyString_FromStringAndSize(result_buf,
|
||||||
|
slicelength);
|
||||||
|
PyMem_Free(result_buf);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"sequence index must be integer");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other)
|
buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other)
|
||||||
{
|
{
|
||||||
|
|
@ -581,6 +636,98 @@ buffer_ass_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right, PyObje
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
buffer_ass_subscript(PyBufferObject *self, PyObject *item, PyObject *value)
|
||||||
|
{
|
||||||
|
PyBufferProcs *pb;
|
||||||
|
void *ptr1, *ptr2;
|
||||||
|
Py_ssize_t selfsize;
|
||||||
|
Py_ssize_t othersize;
|
||||||
|
|
||||||
|
if ( self->b_readonly ) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"buffer is read-only");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pb = value ? value->ob_type->tp_as_buffer : NULL;
|
||||||
|
if ( pb == NULL ||
|
||||||
|
pb->bf_getreadbuffer == NULL ||
|
||||||
|
pb->bf_getsegcount == NULL )
|
||||||
|
{
|
||||||
|
PyErr_BadArgument();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if ( (*pb->bf_getsegcount)(value, NULL) != 1 )
|
||||||
|
{
|
||||||
|
/* ### use a different exception type/message? */
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"single-segment buffer object expected");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!get_buf(self, &ptr1, &selfsize, ANY_BUFFER))
|
||||||
|
return -1;
|
||||||
|
if (PyIndex_Check(item)) {
|
||||||
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
||||||
|
if (i == -1 && PyErr_Occurred())
|
||||||
|
return -1;
|
||||||
|
if (i < 0)
|
||||||
|
i += selfsize;
|
||||||
|
return buffer_ass_item(self, i, value);
|
||||||
|
}
|
||||||
|
else if (PySlice_Check(item)) {
|
||||||
|
Py_ssize_t start, stop, step, slicelength;
|
||||||
|
|
||||||
|
if (PySlice_GetIndicesEx((PySliceObject *)item, selfsize,
|
||||||
|
&start, &stop, &step, &slicelength) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
pb = value ? value->ob_type->tp_as_buffer : NULL;
|
||||||
|
if (pb == NULL ||
|
||||||
|
pb->bf_getreadbuffer == NULL ||
|
||||||
|
pb->bf_getsegcount == NULL) {
|
||||||
|
PyErr_BadArgument();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if ((*pb->bf_getsegcount)(value, NULL) != 1) {
|
||||||
|
/* ### use a different exception type/message? */
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"single-segment buffer object expected");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if ((othersize = (*pb->bf_getreadbuffer)(value, 0, &ptr2)) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (othersize != slicelength) {
|
||||||
|
PyErr_SetString(
|
||||||
|
PyExc_TypeError,
|
||||||
|
"right operand length must match slice length");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (slicelength == 0)
|
||||||
|
return 0;
|
||||||
|
else if (step == 1) {
|
||||||
|
memcpy((char *)ptr1 + start, ptr2, slicelength);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Py_ssize_t cur, i;
|
||||||
|
|
||||||
|
for (cur = start, i = 0; i < slicelength;
|
||||||
|
cur += step, i++) {
|
||||||
|
((char *)ptr1)[cur] = ((char *)ptr2)[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"buffer indices must be integers");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Buffer methods */
|
/* Buffer methods */
|
||||||
|
|
||||||
static Py_ssize_t
|
static Py_ssize_t
|
||||||
|
|
@ -656,6 +803,12 @@ static PySequenceMethods buffer_as_sequence = {
|
||||||
(ssizessizeobjargproc)buffer_ass_slice, /*sq_ass_slice*/
|
(ssizessizeobjargproc)buffer_ass_slice, /*sq_ass_slice*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static PyMappingMethods buffer_as_mapping = {
|
||||||
|
(lenfunc)buffer_length,
|
||||||
|
(binaryfunc)buffer_subscript,
|
||||||
|
(objobjargproc)buffer_ass_subscript,
|
||||||
|
};
|
||||||
|
|
||||||
static PyBufferProcs buffer_as_buffer = {
|
static PyBufferProcs buffer_as_buffer = {
|
||||||
(readbufferproc)buffer_getreadbuf,
|
(readbufferproc)buffer_getreadbuf,
|
||||||
(writebufferproc)buffer_getwritebuf,
|
(writebufferproc)buffer_getwritebuf,
|
||||||
|
|
@ -676,7 +829,7 @@ PyTypeObject PyBuffer_Type = {
|
||||||
(reprfunc)buffer_repr, /* tp_repr */
|
(reprfunc)buffer_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
&buffer_as_sequence, /* tp_as_sequence */
|
&buffer_as_sequence, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
&buffer_as_mapping, /* tp_as_mapping */
|
||||||
(hashfunc)buffer_hash, /* tp_hash */
|
(hashfunc)buffer_hash, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
(reprfunc)buffer_str, /* tp_str */
|
(reprfunc)buffer_str, /* tp_str */
|
||||||
|
|
|
||||||
|
|
@ -2473,6 +2473,9 @@ list_subscript(PyListObject* self, PyObject* item)
|
||||||
if (slicelength <= 0) {
|
if (slicelength <= 0) {
|
||||||
return PyList_New(0);
|
return PyList_New(0);
|
||||||
}
|
}
|
||||||
|
else if (step == 1) {
|
||||||
|
return list_slice(self, start, stop);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
result = PyList_New(slicelength);
|
result = PyList_New(slicelength);
|
||||||
if (!result) return NULL;
|
if (!result) return NULL;
|
||||||
|
|
@ -2516,10 +2519,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
|
if (step == 1)
|
||||||
if (step == 1 && ((PySliceObject*)item)->step == Py_None)
|
|
||||||
return list_ass_slice(self, start, stop, value);
|
return list_ass_slice(self, start, stop, value);
|
||||||
|
|
||||||
|
/* Make sure s[5:2] = [..] inserts at the right place:
|
||||||
|
before 5, not before 2. */
|
||||||
|
if ((step < 0 && start < stop) ||
|
||||||
|
(step > 0 && start > stop))
|
||||||
|
stop = start;
|
||||||
|
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
/* delete slice */
|
/* delete slice */
|
||||||
PyObject **garbage;
|
PyObject **garbage;
|
||||||
|
|
@ -2541,12 +2549,16 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* drawing pictures might help
|
/* drawing pictures might help understand these for
|
||||||
understand these for loops */
|
loops. Basically, we memmove the parts of the
|
||||||
|
list that are *not* part of the slice: step-1
|
||||||
|
items for each item that is part of the slice,
|
||||||
|
and then tail end of the list that was not
|
||||||
|
covered by the slice */
|
||||||
for (cur = start, i = 0;
|
for (cur = start, i = 0;
|
||||||
cur < stop;
|
cur < stop;
|
||||||
cur += step, i++) {
|
cur += step, i++) {
|
||||||
Py_ssize_t lim = step;
|
Py_ssize_t lim = step - 1;
|
||||||
|
|
||||||
garbage[i] = PyList_GET_ITEM(self, cur);
|
garbage[i] = PyList_GET_ITEM(self, cur);
|
||||||
|
|
||||||
|
|
@ -2558,11 +2570,12 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
||||||
self->ob_item + cur + 1,
|
self->ob_item + cur + 1,
|
||||||
lim * sizeof(PyObject *));
|
lim * sizeof(PyObject *));
|
||||||
}
|
}
|
||||||
|
cur = start + slicelength*step;
|
||||||
for (cur = start + slicelength*step + 1;
|
if (cur < Py_Size(self)) {
|
||||||
cur < Py_Size(self); cur++) {
|
memmove(self->ob_item + cur - slicelength,
|
||||||
PyList_SET_ITEM(self, cur - slicelength,
|
self->ob_item + cur,
|
||||||
PyList_GET_ITEM(self, cur));
|
(Py_Size(self) - cur) *
|
||||||
|
sizeof(PyObject *));
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_Size(self) -= slicelength;
|
Py_Size(self) -= slicelength;
|
||||||
|
|
@ -2577,7 +2590,8 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* assign slice */
|
/* assign slice */
|
||||||
PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
|
PyObject *ins, *seq;
|
||||||
|
PyObject **garbage, **seqitems, **selfitems;
|
||||||
Py_ssize_t cur, i;
|
Py_ssize_t cur, i;
|
||||||
|
|
||||||
/* protect against a[::-1] = a */
|
/* protect against a[::-1] = a */
|
||||||
|
|
@ -2587,14 +2601,17 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
seq = PySequence_Fast(value,
|
seq = PySequence_Fast(value,
|
||||||
"must assign iterable to extended slice");
|
"must assign iterable "
|
||||||
|
"to extended slice");
|
||||||
}
|
}
|
||||||
if (!seq)
|
if (!seq)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
|
if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
|
||||||
PyErr_Format(PyExc_ValueError,
|
PyErr_Format(PyExc_ValueError,
|
||||||
"attempt to assign sequence of size %zd to extended slice of size %zd",
|
"attempt to assign sequence of "
|
||||||
|
"size %zd to extended slice of "
|
||||||
|
"size %zd",
|
||||||
PySequence_Fast_GET_SIZE(seq),
|
PySequence_Fast_GET_SIZE(seq),
|
||||||
slicelength);
|
slicelength);
|
||||||
Py_DECREF(seq);
|
Py_DECREF(seq);
|
||||||
|
|
|
||||||
|
|
@ -1222,6 +1222,17 @@ string_subscript(PyStringObject* self, PyObject* item)
|
||||||
if (slicelength <= 0) {
|
if (slicelength <= 0) {
|
||||||
return PyString_FromStringAndSize("", 0);
|
return PyString_FromStringAndSize("", 0);
|
||||||
}
|
}
|
||||||
|
else if (start == 0 && step == 1 &&
|
||||||
|
slicelength == PyString_GET_SIZE(self) &&
|
||||||
|
PyString_CheckExact(self)) {
|
||||||
|
Py_INCREF(self);
|
||||||
|
return (PyObject *)self;
|
||||||
|
}
|
||||||
|
else if (step == 1) {
|
||||||
|
return PyString_FromStringAndSize(
|
||||||
|
PyString_AS_STRING(self) + start,
|
||||||
|
slicelength);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
source_buf = PyString_AsString((PyObject*)self);
|
source_buf = PyString_AsString((PyObject*)self);
|
||||||
result_buf = (char *)PyMem_Malloc(slicelength);
|
result_buf = (char *)PyMem_Malloc(slicelength);
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,54 @@ structseq_slice(PyStructSequence *obj, Py_ssize_t low, Py_ssize_t high)
|
||||||
return (PyObject *) np;
|
return (PyObject *) np;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
structseq_subscript(PyStructSequence *self, PyObject *item)
|
||||||
|
{
|
||||||
|
if (PyIndex_Check(item)) {
|
||||||
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
||||||
|
if (i == -1 && PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (i < 0)
|
||||||
|
i += VISIBLE_SIZE(self);
|
||||||
|
|
||||||
|
if (i < 0 || i >= VISIBLE_SIZE(self)) {
|
||||||
|
PyErr_SetString(PyExc_IndexError,
|
||||||
|
"tuple index out of range");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Py_INCREF(self->ob_item[i]);
|
||||||
|
return self->ob_item[i];
|
||||||
|
}
|
||||||
|
else if (PySlice_Check(item)) {
|
||||||
|
Py_ssize_t start, stop, step, slicelen, cur, i;
|
||||||
|
PyObject *result;
|
||||||
|
|
||||||
|
if (PySlice_GetIndicesEx((PySliceObject *)item,
|
||||||
|
VISIBLE_SIZE(self), &start, &stop,
|
||||||
|
&step, &slicelen) < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (slicelen <= 0)
|
||||||
|
return PyTuple_New(0);
|
||||||
|
result = PyTuple_New(slicelen);
|
||||||
|
if (result == NULL)
|
||||||
|
return NULL;
|
||||||
|
for (cur = start, i = 0; i < slicelen;
|
||||||
|
cur += step, i++) {
|
||||||
|
PyObject *v = self->ob_item[cur];
|
||||||
|
Py_INCREF(v);
|
||||||
|
PyTuple_SET_ITEM(result, i, v);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"structseq index must be integer");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
|
|
@ -298,6 +346,11 @@ static PySequenceMethods structseq_as_sequence = {
|
||||||
(objobjproc)structseq_contains, /* sq_contains */
|
(objobjproc)structseq_contains, /* sq_contains */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static PyMappingMethods structseq_as_mapping = {
|
||||||
|
(lenfunc)structseq_length,
|
||||||
|
(binaryfunc)structseq_subscript,
|
||||||
|
};
|
||||||
|
|
||||||
static PyMethodDef structseq_methods[] = {
|
static PyMethodDef structseq_methods[] = {
|
||||||
{"__reduce__", (PyCFunction)structseq_reduce,
|
{"__reduce__", (PyCFunction)structseq_reduce,
|
||||||
METH_NOARGS, NULL},
|
METH_NOARGS, NULL},
|
||||||
|
|
@ -317,7 +370,7 @@ static PyTypeObject _struct_sequence_template = {
|
||||||
(reprfunc)structseq_repr, /* tp_repr */
|
(reprfunc)structseq_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
&structseq_as_sequence, /* tp_as_sequence */
|
&structseq_as_sequence, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
&structseq_as_mapping, /* tp_as_mapping */
|
||||||
structseq_hash, /* tp_hash */
|
structseq_hash, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
0, /* tp_str */
|
0, /* tp_str */
|
||||||
|
|
|
||||||
|
|
@ -603,6 +603,12 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
|
||||||
if (slicelength <= 0) {
|
if (slicelength <= 0) {
|
||||||
return PyTuple_New(0);
|
return PyTuple_New(0);
|
||||||
}
|
}
|
||||||
|
else if (start == 0 && step == 1 &&
|
||||||
|
slicelength == PyTuple_GET_SIZE(self) &&
|
||||||
|
PyTuple_CheckExact(self)) {
|
||||||
|
Py_INCREF(self);
|
||||||
|
return (PyObject *)self;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
result = PyTuple_New(slicelength);
|
result = PyTuple_New(slicelength);
|
||||||
if (!result) return NULL;
|
if (!result) return NULL;
|
||||||
|
|
|
||||||
|
|
@ -7385,6 +7385,12 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
|
||||||
|
|
||||||
if (slicelength <= 0) {
|
if (slicelength <= 0) {
|
||||||
return PyUnicode_FromUnicode(NULL, 0);
|
return PyUnicode_FromUnicode(NULL, 0);
|
||||||
|
} else if (start == 0 && step == 1 && slicelength == self->length &&
|
||||||
|
PyUnicode_CheckExact(self)) {
|
||||||
|
Py_INCREF(self);
|
||||||
|
return (PyObject *)self;
|
||||||
|
} else if (step == 1) {
|
||||||
|
return PyUnicode_FromUnicode(self->str + start, slicelength);
|
||||||
} else {
|
} else {
|
||||||
source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
|
source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
|
||||||
result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength*
|
result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue