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.

classification
Title: map() must not swallow exceptions from PyObject_GetIter
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: ebfe, terry.reedy, ysj.ray
Priority: normal Keywords:

Created on 2011-03-23 22:28 by ebfe, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg131932 - (view) Author: Lukas Lueg (ebfe) Date: 2011-03-23 22:28
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.
msg131973 - (view) Author: ysj.ray (ysj.ray) Date: 2011-03-24 12:43
There maybe compatibility issues which prevent such behavior change.
msg132371 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-03-27 22:11
I agree with Ray. This is essentially a feature request which you say has already been implemented in Py 3 but which cannot go into Py2.7. Only fixes for bugs (discrepancies between doc and behavior) can go into 2.7. I suspect 2.6 and before acted the same way.
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55864
2011-03-27 22:11:29terry.reedysetstatus: open -> closed

type: enhancement
components: + Interpreter Core, - None

nosy: + terry.reedy
messages: + msg132371
resolution: out of date
2011-03-24 12:43:30ysj.raysetnosy: + ysj.ray
messages: + msg131973
2011-03-23 22:28:21ebfecreate