[issue20508] IndexError from ipaddress._BaseNetwork.__getitem__ has no message
Gareth Rees
report at bugs.python.org
Tue Feb 4 14:09:31 CET 2014
New submission from Gareth Rees:
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.)
----------
components: Library (Lib)
files: ipaddress.patch
keywords: patch
messages: 210224
nosy: Gareth.Rees
priority: normal
severity: normal
status: open
title: IndexError from ipaddress._BaseNetwork.__getitem__ has no message
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file33903/ipaddress.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20508>
_______________________________________
More information about the Python-bugs-list
mailing list