classification
Title: Tricky behavior of builtin-function map
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: DarrenDanielDay, Jim.Jewett, rhettinger, steven.daprano
Priority: normal Keywords:

Created on 2020-07-27 10:01 by DarrenDanielDay, last changed 2020-07-28 01:47 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
issue.py DarrenDanielDay, 2020-07-27 10:01 The file's content is just the comment.
Messages (4)
msg374371 - (view) Author: DarrenDanielDay (DarrenDanielDay) Date: 2020-07-27 10:01
# The following is a tricky example:

# This is an example function that possibly raises `StopIteration`.
# In some cases, this function may raise `StopIteration` in the first iteration.
def raise_stop_iteration(param):
    if param == 10:
        raise StopIteration
    # Suppose this function will do some simple calculation
    return -param

# Then when we use builtin-function `map` and for-loop:

print('example 1'.center(30, '='))
for item in map(raise_stop_iteration, range(5)):
    print(item)
print('end of example 1'.center(30, '='))

# It works well. The output of example 1 is 0 to -4.
# But the following can be triky:

print('example 2'.center(30, '='))
for item in map(raise_stop_iteration, range(10, 20)):
    print(item)
print('end of example 2'.center(30, '='))

# The output of exapmle 2 is just nothing,
# and no errors are reported,
# because `map` simply let the exception spread upward when executing `raise_stop_iteration(10)`.
# However, the exception type is StopIteration, so the for-loop catches it and breaks the loop,
# assuming this is the end of the loop.
# When the exception raised from buggy mapping function is exactly `StopIteration`, 
# for example, functions implemented with builtin-function `next`, 
# it can be confusing to find the real issue.

# I think it might be better improved in this way:

class my_map:
    def __init__(self, mapping_function, *iterators):
        self.mapping = mapping_function
        self.iterators = iterators
    
    def __iter__(self):
        for parameter_tuple in zip(*self.iterators):
            try:
                yield self.mapping(*parameter_tuple)
            except BaseException as e:
                raise RuntimeError(*e.args) from e
        
# It works like the map in most cases:

print('example 3'.center(30, '='))
for item in my_map(raise_stop_iteration, range(5)):
    print(item)
print('end of example 3'.center(30, '='))

# And then, the crash of the buggy mapping function will be reported:
print('example 4'.center(30, '='))
for item in my_map(raise_stop_iteration, range(10, 20)):
    print(item)
print('end of example 4'.center(30, '='))
msg374372 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-07-27 10:54
Converting *all* exceptions into RuntimeError is certainly not a good idea, especially since you include KeyboardInterrupt and other non-errors.

I'm probably going to be on the losing side of this one (I lost the argument back when a similar issue was raised about comprehensions), but I argue that this is not a bug it is a useful feature.

Having the map function raise StopIteration is a good way for the map to halt the loop early, when it detects a special sentinel or condition. A few years ago the change to comprehensions broke my code so I changed to map, and now you want to break it again :-(
msg374444 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2020-07-27 23:44
Why would you raise StopIteration if you didn't want to stop the nearest iteration loop?  I agree that the result of your sample code seems strange, but that is because it is strange code.

I agree with Steven D'Aprano that changing it would cause more pain than it would remove.

Unless it gets a lot more support by the first week of August, I recommend closing this request as rejected.
msg374449 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-28 01:47
I concur with Jim and Steven, so we'll mark this a closed.  

If you want to go forward, consider bringing this up on python-ideas.  If it gets a favorable reception, this can be re-opened.
History
Date User Action Args
2020-07-28 01:47:16rhettingersetstatus: pending -> closed

nosy: + rhettinger
messages: + msg374449

resolution: rejected
stage: resolved
2020-07-27 23:44:35Jim.Jewettsetstatus: open -> pending
nosy: + Jim.Jewett
messages: + msg374444

2020-07-27 10:54:35steven.dapranosetnosy: + steven.daprano
messages: + msg374372
2020-07-27 10:01:38DarrenDanielDaycreate