summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2007-08-28 21:37:11 (GMT)
committerThomas Wouters <thomas@python.org>2007-08-28 21:37:11 (GMT)
commited03b4121ead78df76f6882de5eca01b7a3a628c (patch)
tree76c914634171f813013d7407376b7579036907ee /Objects
parent9e7c8da61c01279797197438111c33b3b8f2e3b2 (diff)
downloadcpython-ed03b4121ead78df76f6882de5eca01b7a3a628c.zip
cpython-ed03b4121ead78df76f6882de5eca01b7a3a628c.tar.gz
cpython-ed03b4121ead78df76f6882de5eca01b7a3a628c.tar.bz2
Merge the trunk changes in. Breaks socket.ssl for now.
Merged revisions 57392-57619 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r57395 | georg.brandl | 2007-08-24 19:23:23 +0200 (Fri, 24 Aug 2007) | 2 lines Bug #1011: fix rfc822.Message.getheader docs. ........ r57397 | georg.brandl | 2007-08-24 19:38:49 +0200 (Fri, 24 Aug 2007) | 2 lines Patch #1006: port test_winreg to unittest. ........ r57398 | georg.brandl | 2007-08-24 19:46:54 +0200 (Fri, 24 Aug 2007) | 2 lines Fix #1012: wrong URL to :mod:`site` in install/index.rst. ........ r57399 | georg.brandl | 2007-08-24 20:07:52 +0200 (Fri, 24 Aug 2007) | 2 lines Patch #1008: port test_signal to unittest. ........ r57400 | georg.brandl | 2007-08-24 20:22:54 +0200 (Fri, 24 Aug 2007) | 2 lines Port test_frozen to unittest. ........ r57401 | georg.brandl | 2007-08-24 20:27:43 +0200 (Fri, 24 Aug 2007) | 2 lines Document new utility functions in test_support. ........ r57402 | georg.brandl | 2007-08-24 20:30:06 +0200 (Fri, 24 Aug 2007) | 2 lines Remove test_rgbimg output file, there is no test_rgbimg.py. ........ r57403 | georg.brandl | 2007-08-24 20:35:27 +0200 (Fri, 24 Aug 2007) | 2 lines Remove output file for test_ossaudiodev, also properly close the dsp object. ........ r57404 | georg.brandl | 2007-08-24 20:46:27 +0200 (Fri, 24 Aug 2007) | 2 lines Convert test_linuxaudiodev to unittest. Fix a wrong finally clause in test_ossaudiodev. ........ r57406 | collin.winter | 2007-08-24 21:13:58 +0200 (Fri, 24 Aug 2007) | 1 line Convert test_pkg to use unittest. ........ r57408 | georg.brandl | 2007-08-24 21:22:34 +0200 (Fri, 24 Aug 2007) | 2 lines Catch the correct errors. ........ r57409 | georg.brandl | 2007-08-24 21:33:53 +0200 (Fri, 24 Aug 2007) | 2 lines Port test_class to unittest. Patch #1671298. ........ r57415 | collin.winter | 2007-08-24 23:09:42 +0200 (Fri, 24 Aug 2007) | 1 line Make test_structmembers pass when run with regrtests's -R flag. ........ r57455 | nick.coghlan | 2007-08-25 06:32:07 +0200 (Sat, 25 Aug 2007) | 1 line Revert misguided attempt at fixing incompatibility between -m and -i switches (better fix coming soon) ........ r57456 | nick.coghlan | 2007-08-25 06:35:54 +0200 (Sat, 25 Aug 2007) | 1 line Revert compile.c changes that shouldn't have been included in previous checkin ........ r57461 | nick.coghlan | 2007-08-25 12:50:41 +0200 (Sat, 25 Aug 2007) | 1 line Fix bug 1764407 - the -i switch now does the right thing when using the -m switch ........ r57464 | guido.van.rossum | 2007-08-25 17:08:43 +0200 (Sat, 25 Aug 2007) | 4 lines Server-side SSL and certificate validation, by Bill Janssen. While cleaning up Bill's C style, I may have cleaned up some code he didn't touch as well (in _ssl.c). ........ r57465 | neal.norwitz | 2007-08-25 18:41:36 +0200 (Sat, 25 Aug 2007) | 3 lines Try to get this to build with Visual Studio by moving all the variable declarations to the beginning of a scope. ........ r57466 | neal.norwitz | 2007-08-25 18:54:38 +0200 (Sat, 25 Aug 2007) | 1 line Fix test so it is skipped properly if there is no SSL support. ........ r57467 | neal.norwitz | 2007-08-25 18:58:09 +0200 (Sat, 25 Aug 2007) | 2 lines Fix a few more variables to try to get this to compile with Visual Studio. ........ r57473 | neal.norwitz | 2007-08-25 19:25:17 +0200 (Sat, 25 Aug 2007) | 1 line Try to get this test to pass for systems that do not have SO_REUSEPORT ........ r57482 | gregory.p.smith | 2007-08-26 02:26:00 +0200 (Sun, 26 Aug 2007) | 7 lines keep setup.py from listing unneeded hash modules (_md5, _sha*) as missing when they were not built because _hashlib with openssl provided their functionality instead. don't build bsddb185 if bsddb was built. ........ r57483 | neal.norwitz | 2007-08-26 03:08:16 +0200 (Sun, 26 Aug 2007) | 1 line Fix typo in docstring (missing c in reacquire) ........ r57484 | neal.norwitz | 2007-08-26 03:42:03 +0200 (Sun, 26 Aug 2007) | 2 lines Spell check (also americanify behaviour, it's almost 3 times as common) ........ r57503 | neal.norwitz | 2007-08-26 08:29:57 +0200 (Sun, 26 Aug 2007) | 4 lines Reap children before the test starts so hopefully SocketServer won't find any old children left around which causes an exception in collect_children() and the test to fail. ........ r57510 | neal.norwitz | 2007-08-26 20:50:39 +0200 (Sun, 26 Aug 2007) | 1 line Fail gracefully if the cert files cannot be created ........ r57513 | guido.van.rossum | 2007-08-26 21:35:09 +0200 (Sun, 26 Aug 2007) | 4 lines Bill Janssen wrote: Here's a patch which makes test_ssl a better player in the buildbots environment. I deep-ended on "try-except-else" clauses. ........ r57518 | neal.norwitz | 2007-08-26 23:40:16 +0200 (Sun, 26 Aug 2007) | 1 line Get the test passing by commenting out some writes (should they be removed?) ........ r57522 | neal.norwitz | 2007-08-27 00:16:23 +0200 (Mon, 27 Aug 2007) | 3 lines Catch IOError for when the device file doesn't exist or the user doesn't have permission to write to the device. ........ r57524 | neal.norwitz | 2007-08-27 00:20:03 +0200 (Mon, 27 Aug 2007) | 5 lines Another patch from Bill Janssen that: 1) Fixes the bug that two class names are initial-lower-case. 2) Replaces the poll waiting for the server to become ready with a threading.Event signal. ........ r57536 | neal.norwitz | 2007-08-27 02:58:33 +0200 (Mon, 27 Aug 2007) | 1 line Stop using string.join (from the module) to ease upgrade to py3k ........ r57537 | neal.norwitz | 2007-08-27 03:03:18 +0200 (Mon, 27 Aug 2007) | 1 line Make a utility function for handling (printing) an error ........ r57538 | neal.norwitz | 2007-08-27 03:15:33 +0200 (Mon, 27 Aug 2007) | 4 lines If we can't create a certificate, print a warning, but don't fail the test. Modified patch from what Bill Janssen sent on python-3000. ........ r57539 | facundo.batista | 2007-08-27 03:15:34 +0200 (Mon, 27 Aug 2007) | 7 lines Ignore test failures caused by 'resource temporarily unavailable' exceptions raised in the test server thread, since SimpleXMLRPCServer does not gracefully handle them. Changed number of requests handled by tests server thread to one (was 2) because no tests require more than one request. [GSoC - Alan McIntyre] ........ r57561 | guido.van.rossum | 2007-08-27 19:19:42 +0200 (Mon, 27 Aug 2007) | 8 lines > Regardless, building a fixed test certificate and checking it in sounds like > the better option. Then the openssl command in the test code can be turned > into a comment describing how the test data was pregenerated. Here's a patch that does that. Bill ........ r57568 | guido.van.rossum | 2007-08-27 20:42:23 +0200 (Mon, 27 Aug 2007) | 26 lines > Some of the code sets the error string in this directly before > returning NULL, and other pieces of the code call PySSL_SetError, > which creates the error string. I think some of the places which set > the string directly probably shouldn't; instead, they should call > PySSL_SetError to cons up the error name directly from the err code. > However, PySSL_SetError only works after the construction of an ssl > object, which means it can't be used there... I'll take a longer look > at it and see if there's a reasonable fix. Here's a patch which addresses this. It also fixes the indentation in PySSL_SetError, bringing it into line with PEP 7, fixes a compile warning about one of the OpenSSL macros, and makes the namespace a bit more consistent. I've tested it on FC 7 and OS X 10.4. % ./python ./Lib/test/regrtest.py -R :1: -u all test_ssl test_ssl beginning 6 repetitions 123456 ...... 1 test OK. [29244 refs] % [GvR: slightly edited to enforce 79-char line length, even if it required violating the style guide.] ........ r57570 | guido.van.rossum | 2007-08-27 21:11:11 +0200 (Mon, 27 Aug 2007) | 2 lines Patch 10124 by Bill Janssen, docs for the new ssl code. ........ r57574 | guido.van.rossum | 2007-08-27 22:51:00 +0200 (Mon, 27 Aug 2007) | 3 lines Patch # 1739906 by Christian Heimes -- add reduce to functools (importing it from __builtin__). ........ r57575 | guido.van.rossum | 2007-08-27 22:52:10 +0200 (Mon, 27 Aug 2007) | 2 lines News about functools.reduce. ........ r57611 | georg.brandl | 2007-08-28 10:29:08 +0200 (Tue, 28 Aug 2007) | 2 lines Document rev. 57574. ........ r57612 | sean.reifschneider | 2007-08-28 11:07:54 +0200 (Tue, 28 Aug 2007) | 2 lines Adding basic imputil documentation. ........ r57614 | georg.brandl | 2007-08-28 12:48:18 +0200 (Tue, 28 Aug 2007) | 2 lines Fix some glitches. ........ r57616 | lars.gustaebel | 2007-08-28 14:31:09 +0200 (Tue, 28 Aug 2007) | 5 lines TarFile.__init__() no longer fails if no name argument is passed and the fileobj argument has no usable name attribute (e.g. StringIO). (will backport to 2.5) ........ r57619 | thomas.wouters | 2007-08-28 17:28:19 +0200 (Tue, 28 Aug 2007) | 22 lines 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. ........
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bufferobject.c153
-rw-r--r--Objects/listobject.c43
-rw-r--r--Objects/stringobject.c11
-rw-r--r--Objects/structseq.c55
-rw-r--r--Objects/tupleobject.c6
-rw-r--r--Objects/unicodeobject.c6
6 files changed, 259 insertions, 15 deletions
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
index c415013..e307aab 100644
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -482,6 +482,75 @@ buffer_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right)
return ob;
}
+static PyObject *
+buffer_subscript(PyBufferObject *self, PyObject *item)
+{
+ PyBuffer view;
+ PyObject *ob;
+
+ if (!get_buf(self, &view, PyBUF_SIMPLE))
+ return NULL;
+ if (PyIndex_Check(item)) {
+ Py_ssize_t idx = PyNumber_AsSsize_t(item, PyExc_IndexError);
+
+ if (idx == -1 && PyErr_Occurred())
+ return NULL;
+ if (idx < 0)
+ idx += view.len;
+ if ( idx < 0 || idx >= view.len ) {
+ PyErr_SetString(PyExc_IndexError,
+ "buffer index out of range");
+ return NULL;
+ }
+ ob = PyBytes_FromStringAndSize((char *)view.buf + idx, 1);
+ PyObject_ReleaseBuffer((PyObject *)self, &view);
+ return ob;
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelength, cur, i;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item, view.len,
+ &start, &stop, &step, &slicelength) < 0) {
+ PyObject_ReleaseBuffer((PyObject *)self, &view);
+ return NULL;
+ }
+
+ if (slicelength <= 0) {
+ PyObject_ReleaseBuffer((PyObject *)self, &view);
+ return PyBytes_FromStringAndSize("", 0);
+ }
+ else if (step == 1) {
+ ob = PyBytes_FromStringAndSize((char *)view.buf +
+ start, stop - start);
+ PyObject_ReleaseBuffer((PyObject *)self, &view);
+ return ob;
+ }
+ else {
+ char *source_buf = (char *)view.buf;
+ 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];
+ }
+
+ ob = PyBytes_FromStringAndSize(result_buf,
+ slicelength);
+ PyMem_Free(result_buf);
+ PyObject_ReleaseBuffer((PyObject *)self, &view);
+ return ob;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "sequence index must be integer");
+ return NULL;
+ }
+}
+
static int
buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other)
{
@@ -587,6 +656,82 @@ buffer_ass_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right, PyObje
return 0;
}
+static int
+buffer_ass_subscript(PyBufferObject *self, PyObject *item, PyObject *value)
+{
+ PyBuffer v1;
+
+ if (!get_buf(self, &v1, PyBUF_SIMPLE))
+ return -1;
+ if (self->b_readonly || v1.readonly) {
+ PyErr_SetString(PyExc_TypeError,
+ "buffer is read-only");
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ return -1;
+ }
+ if (PyIndex_Check(item)) {
+ Py_ssize_t idx = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (idx == -1 && PyErr_Occurred())
+ return -1;
+ if (idx < 0)
+ idx += v1.len;
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ return buffer_ass_item(self, idx, value);
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelength;
+ PyBuffer v2;
+ PyBufferProcs *pb;
+
+ if (PySlice_GetIndicesEx((PySliceObject *)item, v1.len,
+ &start, &stop, &step, &slicelength) < 0) {
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ return -1;
+ }
+
+ pb = value ? value->ob_type->tp_as_buffer : NULL;
+ if (pb == NULL ||
+ pb->bf_getbuffer == NULL) {
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ PyErr_BadArgument();
+ return -1;
+ }
+ if ((*pb->bf_getbuffer)(value, &v2, PyBUF_SIMPLE) < 0) {
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ return -1;
+ }
+
+ if (v2.len != slicelength) {
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ PyObject_ReleaseBuffer(value, &v2);
+ PyErr_SetString(PyExc_TypeError, "right operand"
+ " length must match slice length");
+ return -1;
+ }
+
+ if (slicelength == 0)
+ /* nothing to do */;
+ else if (step == 1)
+ memcpy((char *)v1.buf + start, v2.buf, slicelength);
+ else {
+ Py_ssize_t cur, i;
+
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ ((char *)v1.buf)[cur] = ((char *)v2.buf)[i];
+ }
+ }
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ PyObject_ReleaseBuffer(value, &v2);
+ return 0;
+ } else {
+ PyErr_SetString(PyExc_TypeError,
+ "buffer indices must be integers");
+ PyObject_ReleaseBuffer((PyObject *)self, &v1);
+ return -1;
+ }
+}
+
/* Buffer methods */
static PySequenceMethods buffer_as_sequence = {
@@ -599,6 +744,12 @@ static PySequenceMethods buffer_as_sequence = {
(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 = {
(getbufferproc)buffer_getbuf,
(releasebufferproc)buffer_releasebuf,
@@ -617,7 +768,7 @@ PyTypeObject PyBuffer_Type = {
(reprfunc)buffer_repr, /* tp_repr */
0, /* tp_as_number */
&buffer_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
+ &buffer_as_mapping, /* tp_as_mapping */
(hashfunc)buffer_hash, /* tp_hash */
0, /* tp_call */
(reprfunc)buffer_str, /* tp_str */
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 3de07c9..c5a4287 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2461,6 +2461,9 @@ list_subscript(PyListObject* self, PyObject* item)
if (slicelength <= 0) {
return PyList_New(0);
}
+ else if (step == 1) {
+ return list_slice(self, start, stop);
+ }
else {
result = PyList_New(slicelength);
if (!result) return NULL;
@@ -2504,10 +2507,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
return -1;
}
- /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
- if (step == 1 && ((PySliceObject*)item)->step == Py_None)
+ if (step == 1)
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) {
/* delete slice */
PyObject **garbage;
@@ -2529,12 +2537,16 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
return -1;
}
- /* drawing pictures might help
- understand these for loops */
+ /* drawing pictures might help understand these for
+ 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;
cur < stop;
cur += step, i++) {
- Py_ssize_t lim = step;
+ Py_ssize_t lim = step - 1;
garbage[i] = PyList_GET_ITEM(self, cur);
@@ -2546,11 +2558,12 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
self->ob_item + cur + 1,
lim * sizeof(PyObject *));
}
-
- for (cur = start + slicelength*step + 1;
- cur < Py_Size(self); cur++) {
- PyList_SET_ITEM(self, cur - slicelength,
- PyList_GET_ITEM(self, cur));
+ cur = start + slicelength*step;
+ if (cur < Py_Size(self)) {
+ memmove(self->ob_item + cur - slicelength,
+ self->ob_item + cur,
+ (Py_Size(self) - cur) *
+ sizeof(PyObject *));
}
Py_Size(self) -= slicelength;
@@ -2565,7 +2578,8 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
}
else {
/* assign slice */
- PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
+ PyObject *ins, *seq;
+ PyObject **garbage, **seqitems, **selfitems;
Py_ssize_t cur, i;
/* protect against a[::-1] = a */
@@ -2575,14 +2589,17 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
}
else {
seq = PySequence_Fast(value,
- "must assign iterable to extended slice");
+ "must assign iterable "
+ "to extended slice");
}
if (!seq)
return -1;
if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
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),
slicelength);
Py_DECREF(seq);
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index fff4b45..8d0f4b8 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -1146,6 +1146,17 @@ string_subscript(PyStringObject* self, PyObject* item)
if (slicelength <= 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 {
source_buf = PyString_AsString((PyObject*)self);
result_buf = (char *)PyMem_Malloc(slicelength);
diff --git a/Objects/structseq.c b/Objects/structseq.c
index 5e57f1f..a3ff045 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -90,6 +90,54 @@ structseq_slice(PyStructSequence *obj, Py_ssize_t low, Py_ssize_t high)
}
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 *
structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *arg = NULL;
@@ -298,6 +346,11 @@ static PySequenceMethods structseq_as_sequence = {
(objobjproc)structseq_contains, /* sq_contains */
};
+static PyMappingMethods structseq_as_mapping = {
+ (lenfunc)structseq_length,
+ (binaryfunc)structseq_subscript,
+};
+
static PyMethodDef structseq_methods[] = {
{"__reduce__", (PyCFunction)structseq_reduce,
METH_NOARGS, NULL},
@@ -317,7 +370,7 @@ static PyTypeObject _struct_sequence_template = {
(reprfunc)structseq_repr, /* tp_repr */
0, /* tp_as_number */
&structseq_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
+ &structseq_as_mapping, /* tp_as_mapping */
structseq_hash, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 3c5dfb5..37b5a1f 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -586,6 +586,12 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
if (slicelength <= 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 {
result = PyTuple_New(slicelength);
if (!result) return NULL;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index ebf8c42..1e8f63f 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -8096,6 +8096,12 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
if (slicelength <= 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 {
source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength*