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.

Created on 2014-02-04 13:09 by gdr@garethrees.org, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ipaddress.patch gdr@garethrees.org, 2014-02-04 13:09 review
ipaddress.patch gdr@garethrees.org, 2016-06-11 15:59 review
Messages (7)
msg210224 - (view) Author: Gareth Rees (gdr@garethrees.org) * (Python triager) Date: 2014-02-04 13:09
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.)
msg235955 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2015-02-14 09:06
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.
msg268080 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-06-10 04:15
+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.
msg268219 - (view) Author: Gareth Rees (gdr@garethrees.org) * (Python triager) Date: 2016-06-11 15:59
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.
msg268233 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-06-11 17:05
Thank you Gareth. I will commit ipaddress.patch this weekend.
msg268256 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-06-11 19:29
New changeset bc758c62bc4f by Berker Peksag in branch 'default': Issue #20508: Improve exception message of IPv{4,6}Network.__getitem__ https://hg.python.org/cpython/rev/bc758c62bc4f
msg268261 - (view) Author: Gareth Rees (gdr@garethrees.org) * (Python triager) Date: 2016-06-11 19:58
Thank you for applying this patch.
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64707
2016-06-11 19:58:16gdr@garethrees.orgsetmessages: + msg268261
2016-06-11 19:30:19berker.peksagsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: - Python 3.5
2016-06-11 19:29:57python-devsetnosy: + python-dev
messages: + msg268256
2016-06-11 17:05:53berker.peksagsetmessages: + msg268233
2016-06-11 15:59:47gdr@garethrees.orgsetfiles: + ipaddress.patch

messages: + msg268219
2016-06-10 04:56:15BreamoreBoysetnosy: - BreamoreBoy
2016-06-10 04:15:47berker.peksagsetnosy: + berker.peksag

messages: + msg268080
versions: + Python 3.6
2015-02-14 14:18:26serhiy.storchakasetkeywords: + easy, needs review
stage: patch review
versions: + Python 3.5, - Python 3.4
2015-02-14 09:06:07BreamoreBoysetnosy: + BreamoreBoy
messages: + msg235955
2014-02-04 21:41:37ned.deilysetnosy: + ncoghlan, pmoody
2014-02-04 13:19:44gdr@garethrees.orgsettype: behavior -> enhancement
2014-02-04 13:09:31gdr@garethrees.orgcreate