Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

str.format_map() silences exceptions in __getitem__ #75161

Closed
Akuli mannequin opened this issue Jul 20, 2017 · 12 comments
Closed

str.format_map() silences exceptions in __getitem__ #75161

Akuli mannequin opened this issue Jul 20, 2017 · 12 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@Akuli
Copy link
Mannequin

Akuli mannequin commented Jul 20, 2017

BPO 30978
Nosy @brettcannon, @rhettinger, @vstinner, @ericvsmith, @ezio-melotti, @serhiy-storchaka, @Akuli
PRs
  • bpo-30978: str.format_map() now passes key lookup exceptions through. #2790
  • [3.6] bpo-30978: str.format_map() now passes key lookup exceptions through. (GH-2790) #2992
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-08-03.09:17:00.328>
    created_at = <Date 2017-07-20.19:53:55.353>
    labels = ['interpreter-core', 'type-bug', '3.7', 'expert-unicode']
    title = 'str.format_map() silences exceptions in __getitem__'
    updated_at = <Date 2017-08-03.09:17:00.328>
    user = 'https://github.com/Akuli'

    bugs.python.org fields:

    activity = <Date 2017-08-03.09:17:00.328>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-08-03.09:17:00.328>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core', 'Unicode']
    creation = <Date 2017-07-20.19:53:55.353>
    creator = 'Akuli'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30978
    keywords = []
    message_count = 12.0
    messages = ['298747', '298767', '298794', '298799', '298809', '298812', '298816', '298818', '298819', '298821', '299692', '299693']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'rhettinger', 'vstinner', 'eric.smith', 'ezio.melotti', 'serhiy.storchaka', 'Akuli']
    pr_nums = ['2790', '2992']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue30978'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    @Akuli
    Copy link
    Mannequin Author

    Akuli mannequin commented Jul 20, 2017

    Example:

        class BrokenMapping:
            def __getitem__(self, key):
                1/0
        
        # 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.

    @Akuli Akuli mannequin added 3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error labels Jul 20, 2017
    @serhiy-storchaka
    Copy link
    Member

    Yet one consequence: this swallows KeyboardInterrupt and converts it to KeyError.

    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode labels Jul 21, 2017
    @vstinner
    Copy link
    Member

    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 :-)

    @ericvsmith
    Copy link
    Member

    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.

    @serhiy-storchaka
    Copy link
    Member

    What are your thoughts about backporting this Raymond?

    @vstinner
    Copy link
    Member

    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 :-)

    @rhettinger
    Copy link
    Contributor

    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.

    @Akuli
    Copy link
    Mannequin Author

    Akuli mannequin commented Jul 21, 2017

    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'

    @Akuli
    Copy link
    Mannequin Author

    Akuli mannequin commented Jul 21, 2017

    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 :)

    @brettcannon
    Copy link
    Member

    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.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 5075416 by Serhiy Storchaka in branch 'master':
    bpo-30978: str.format_map() now passes key lookup exceptions through. (bpo-2790)
    5075416

    @serhiy-storchaka
    Copy link
    Member

    New changeset f08b2be by Serhiy Storchaka in branch '3.6':
    [3.6] bpo-30978: str.format_map() now passes key lookup exceptions through. (GH-2790) (bpo-2992)
    f08b2be

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants