This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ebfe
Recipients ebfe
Date 2011-03-23.22:28:21
SpamBayes Score 4.5544513e-11
Marked as misclassified No
Message-id <1300919303.37.0.228968832685.issue11655@psf.upfronthosting.co.za>
In-reply-to
Content
The built-in function map() currently swallows any exception that might have occured while trying to get an iterator from any parameter. This produces unexpected behaviour for applications that require a certain type of exception to be raised when __iter__() is called on their objects.

From 24179f82b7de, inside map_new():

973 /* Get iterator. */
974 curseq = PyTuple_GetItem(args, i+1);
975 sqp->it = PyObject_GetIter(curseq);
976 if (sqp->it == NULL) {
977 static char errmsg[] =
978 "argument %d to map() must support iteration";
979 char errbuf[sizeof(errmsg) + 25];
980 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
981 PyErr_SetString(PyExc_TypeError, errbuf);
982 goto Fail_2;
983 }

We *must* check if there has been any other kind of exception already being set when returning from PyObject_GetIter before setting PyExc_TypeError in line 981. If there is none, it is ok to raise a TypeError; any other exception must be passed on.

For example: raising TooManyCacheMissesException in __iter__() causes map(foobar, myobject) to raise TypeError instead of TooManyCacheMissesException.

Workaround: use map(foobar, iter(myobject)). The explicit call to iter will either produce an iterator object (which returns self to map()) or raises the correct exception.

Python 3 is not affected as map_new() does not throw it's own TypeError in case PyObject_GetIter() fails.
History
Date User Action Args
2011-03-23 22:28:23ebfesetrecipients: + ebfe
2011-03-23 22:28:23ebfesetmessageid: <1300919303.37.0.228968832685.issue11655@psf.upfronthosting.co.za>
2011-03-23 22:28:21ebfelinkissue11655 messages
2011-03-23 22:28:21ebfecreate