summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-07-10 17:37:30 (GMT)
committerGuido van Rossum <guido@python.org>1998-07-10 17:37:30 (GMT)
commitfa4ac71dd6602347d80e4cdb71f3867fb63d3578 (patch)
tree3e2d2bd1f36093765fa1f81576c13b64e368211d /Python
parentbfc725bf64c1da301fe29b9722efc56e34799cf4 (diff)
downloadcpython-fa4ac71dd6602347d80e4cdb71f3867fb63d3578.zip
cpython-fa4ac71dd6602347d80e4cdb71f3867fb63d3578.tar.gz
cpython-fa4ac71dd6602347d80e4cdb71f3867fb63d3578.tar.bz2
Small changes to map() and filter():
(1) If a sequence S is shorter than len(S) indicated, don't fail -- just use the shorter size. (I.e, len(S) is just a hint.) (2) Implement the special case map(None, S) as list(S) -- it's faster.
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 775c318..63531db 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -190,8 +190,6 @@ builtin_filter(self, args)
int ok;
if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
- if (i < len)
- goto Fail_1;
if (PyErr_ExceptionMatches(PyExc_IndexError)) {
PyErr_Clear();
break;
@@ -784,6 +782,11 @@ builtin_map(self, args)
func = PyTuple_GetItem(args, 0);
n--;
+ if (func == Py_None && n == 1) {
+ /* map(None, S) is the same as list(S). */
+ return PySequence_List(PyTuple_GetItem(args, 1));
+ }
+
if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
PyErr_NoMemory();
goto Fail_2;
@@ -820,7 +823,6 @@ builtin_map(self, args)
if ((result = (PyObject *) PyList_New(len)) == NULL)
goto Fail_2;
- /* XXX Special case map(None, single_list) could be more efficient */
for (i = 0; ; ++i) {
PyObject *alist, *item=NULL, *value;
int any = 0;
@@ -840,8 +842,6 @@ builtin_map(self, args)
else {
item = (*sqp->sqf->sq_item)(sqp->seq, i);
if (item == NULL) {
- if (i < sqp->len)
- goto Fail_0;
if (PyErr_ExceptionMatches(
PyExc_IndexError))
{
@@ -897,6 +897,9 @@ builtin_map(self, args)
}
}
+ if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
+ goto Fail_1;
+
PyMem_DEL(seqs);
return result;