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.

Author gdr@garethrees.org
Recipients gdr@garethrees.org
Date 2014-02-04.13:09:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1391519371.56.0.280883005316.issue20508@psf.upfronthosting.co.za>
In-reply-to
Content
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.)
History
Date User Action Args
2014-02-04 13:09:31gdr@garethrees.orgsetrecipients: + gdr@garethrees.org
2014-02-04 13:09:31gdr@garethrees.orgsetmessageid: <1391519371.56.0.280883005316.issue20508@psf.upfronthosting.co.za>
2014-02-04 13:09:31gdr@garethrees.orglinkissue20508 messages
2014-02-04 13:09:31gdr@garethrees.orgcreate