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.

Title: str.format_map() silences exceptions in __getitem__
Type: behavior Stage: resolved
Components: Interpreter Core, Unicode Versions: Python 3.7, Python 3.6, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Akuli, brett.cannon, eric.smith, ezio.melotti, rhettinger, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2017-07-20 19:53 by Akuli, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 2790 merged serhiy.storchaka, 2017-07-21 04:42
PR 2992 merged serhiy.storchaka, 2017-08-03 08:50
Messages (12)
msg298747 - (view) Author: Akuli (Akuli) Date: 2017-07-20 19:53

    class BrokenMapping:
        def __getitem__(self, key):
    # this silences the ZeroDivisionError and raises KeyError('world')
    'hello {world}'.format_map(BrokenMapping())

I have tried this on several different CPython versions on Ubuntu 14.04
(including the latest Python 3.7.0a0 from github) and they all do this.
PyPy passes the ZeroDivisionError through correctly.
msg298767 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-07-21 04:46
Yet one consequence: this swallows KeyboardInterrupt and converts it to KeyError.
msg298794 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-07-21 10:36
While KeyboardInterrupt and ZeroDivisionError are easy to understand, IndexError converted to KeyError doesn't look like a major bug. So I'm not sure about fixing Python 3.5 and 3.6. Maybe it's ok because this function is rarely used and exceptions are not "expected": I hardly imagine a code explicitly catching KeyError to do something on format_map() error.

In short, my vote for backport is +0 :-)
msg298799 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2017-07-21 11:02
I'm -0 on a backport. The new behavior is more correct, but I don't like changing the exception type in a micro release.
msg298809 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-07-21 15:55
What are your thoughts about backporting this Raymond?
msg298812 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-07-21 16:10
For Python 3.5 and 3.6, would it make sense to modify the code to pass though BaseException, but replace all other exceptions with KeyError? It would be a compromise between doing nothing and fixing the bug :-)
msg298816 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-07-21 18:35
> What are your thoughts about backporting this Raymond?

+0 On the one hand, this can be considered a bug fix.  On the other hand, I would suspect that no code that is currently working would benefit.
msg298818 - (view) Author: Akuli (Akuli) Date: 2017-07-21 19:37
I think all exceptions should be passed through. Things like
dict.__contains__ don't selectively turn some errors to KeyError
either; if something is not hashable it's a TypeError, not a KeyError.

    >>> [] in {}
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'
msg298819 - (view) Author: Akuli (Akuli) Date: 2017-07-21 19:40
Oops, I forgot to mention that I have no thoughts about backporting
this. It would be nice to see this fixed in 3.7 though :)
msg298821 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-07-21 19:51
I'm -0 on the backport since as Raymond and Eric pointed out it won't benefit currently working code and I personally don't like changing what exception is raised in a bugfix release.
msg299692 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-03 08:45
New changeset 5075416b8fedc5526b643dabc915f7945fa0d969 by Serhiy Storchaka in branch 'master':
bpo-30978: str.format_map() now passes key lookup exceptions through. (#2790)
msg299693 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-03 09:14
New changeset f08b2be4416163e3d18b8d09891e7cda0d8a21d4 by Serhiy Storchaka in branch '3.6':
[3.6] bpo-30978: str.format_map() now passes key lookup exceptions through. (GH-2790) (#2992)
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75161
2017-08-03 09:17:00serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-08-03 09:14:10serhiy.storchakasetmessages: + msg299693
2017-08-03 08:50:06serhiy.storchakasetpull_requests: + pull_request3031
2017-08-03 08:45:28serhiy.storchakasetmessages: + msg299692
2017-07-21 19:51:19brett.cannonsetnosy: + brett.cannon
messages: + msg298821
2017-07-21 19:40:06Akulisetmessages: + msg298819
2017-07-21 19:37:18Akulisetmessages: + msg298818
2017-07-21 18:35:30rhettingersetmessages: + msg298816
2017-07-21 16:10:39vstinnersetmessages: + msg298812
2017-07-21 15:55:43serhiy.storchakasetnosy: + rhettinger
messages: + msg298809
2017-07-21 11:02:43eric.smithsetmessages: + msg298799
2017-07-21 10:36:46vstinnersetmessages: + msg298794
2017-07-21 04:46:01serhiy.storchakasetversions: + Python 3.6, - Python 3.3, Python 3.4
nosy: + ezio.melotti, vstinner, serhiy.storchaka

messages: + msg298767

components: + Interpreter Core, Unicode
stage: patch review
2017-07-21 04:42:23serhiy.storchakasetpull_requests: + pull_request2840
2017-07-20 21:37:12eric.smithsetnosy: + eric.smith
2017-07-20 19:53:55Akulicreate