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

IndexError from ipaddress._BaseNetwork.__getitem__ has no message #64707

Closed
gareth-rees mannequin opened this issue Feb 4, 2014 · 7 comments
Closed

IndexError from ipaddress._BaseNetwork.__getitem__ has no message #64707

gareth-rees mannequin opened this issue Feb 4, 2014 · 7 comments
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@gareth-rees
Copy link
Mannequin

gareth-rees mannequin commented Feb 4, 2014

BPO 20508
Nosy @ncoghlan, @gareth-rees, @berkerpeksag
Files
  • ipaddress.patch
  • ipaddress.patch
  • 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 2016-06-11.19:30:19.147>
    created_at = <Date 2014-02-04.13:09:31.540>
    labels = ['easy', 'type-feature', 'library']
    title = 'IndexError from ipaddress._BaseNetwork.__getitem__ has no message'
    updated_at = <Date 2016-06-11.19:58:16.111>
    user = 'https://github.com/gareth-rees'

    bugs.python.org fields:

    activity = <Date 2016-06-11.19:58:16.111>
    actor = 'gdr@garethrees.org'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-06-11.19:30:19.147>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2014-02-04.13:09:31.540>
    creator = 'gdr@garethrees.org'
    dependencies = []
    files = ['33903', '43341']
    hgrepos = []
    issue_num = 20508
    keywords = ['patch', 'easy', 'needs review']
    message_count = 7.0
    messages = ['210224', '235955', '268080', '268219', '268233', '268256', '268261']
    nosy_count = 5.0
    nosy_names = ['ncoghlan', 'pmoody', 'python-dev', 'gdr@garethrees.org', 'berker.peksag']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue20508'
    versions = ['Python 3.6']

    @gareth-rees
    Copy link
    Mannequin Author

    gareth-rees mannequin commented Feb 4, 2014

    If you try to look up an out-of-range address from an object returned
    by ipaddress.ip_network, then ipaddress._BaseNetwork.__getitem__
    raises an IndexError with no message:

        Python 3.4.0b3 (default, Jan 27 2014, 02:26:41) 
        [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
        Type "help", "copyright", "credits" or "license" for more information.
        >>> import ipaddress
        >>> ipaddress.ip_network('2001:db8::8/125')[100]
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ipaddress.py", line 601, in __getitem__
            raise IndexError
        IndexError

    Normally an IndexError is associated with a message explaining the
    cause of the error. For example:

        >>> [].pop()
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        IndexError: pop from empty list

    It would be nice if the IndexError from
    ipaddress._BaseNetwork.__getitem__ included a message like this.

    With the attached patch, the error message looks like this in the
    positive case:

        >>> ipaddress.ip_network('2001:db8::8/125')[100]
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/Users/gdr/hg.python.org/cpython/Lib/ipaddress.py", line 602, in __getitem__
            % (self, self.num_addresses))
        IndexError: 100 out of range 0..7 for 2001:db8::8/125

    and like this in the negative case:

        >>> ipaddress.ip_network('2001:db8::8/125')[-100]
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/Users/gdr/hg.python.org/cpython/Lib/ipaddress.py", line 608, in __getitem__
            % (n - 1, self.num_addresses, self))
        IndexError: -100 out of range -8..-1 for 2001:db8::8/125

    (If you have a better suggestion for how the error message should
    read, I could submit a revised patch. I suppose it could just say
    "address index out of range" for consistency with list.__getitem__ and
    str.__getitem__. But I think the extra information is likely to be
    helpful for the programmer who is trying to track down the cause of an
    error.)

    @gareth-rees gareth-rees mannequin added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Feb 4, 2014
    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Feb 14, 2015

    Could someone review the attached patch please. I've looked at the test code and there is one assertRaises for IndexError which I'm assuming covers this case.

    @berkerpeksag
    Copy link
    Member

    +1 for "address index out of range". The current test only covers the first IndexError. We also need to add another one for the else branch.

    @gareth-rees
    Copy link
    Mannequin Author

    gareth-rees mannequin commented Jun 11, 2016

    I've attached a revised patch that addresses Berker Peksag's concerns:

    1. The message associated with the IndexError is now "address out of range" with no information about which address failed or why.

    2. There's a new test case for an IndexError from an IPv6 address lookup.

    @berkerpeksag
    Copy link
    Member

    Thank you Gareth. I will commit ipaddress.patch this weekend.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 11, 2016

    New changeset bc758c62bc4f by Berker Peksag in branch 'default':
    Issue bpo-20508: Improve exception message of IPv{4,6}Network.__getitem__
    https://hg.python.org/cpython/rev/bc758c62bc4f

    @gareth-rees
    Copy link
    Mannequin Author

    gareth-rees mannequin commented Jun 11, 2016

    Thank you for applying this patch.

    @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
    easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants