
This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.
Created on2014-02-04 13:09 bygdr@garethrees.org, last changed2022-04-11 14:57 byadmin. This issue is nowclosed.
| 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)*![]() | Date: 2014-02-04 13:09 | |
If you try to look up an out-of-range address from an object returnedby 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 IndexErrorNormally an IndexError is associated with a message explaining thecause of the error. For example: >>> [].pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop from empty listIt would be nice if the IndexError fromipaddress._BaseNetwork.__getitem__ included a message like this.With the attached patch, the error message looks like this in thepositive 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/125and 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 shouldread, I could submit a revised patch. I suppose it could just say"address index out of range" for consistency with list.__getitem__ andstr.__getitem__. But I think the extra information is likely to behelpful for the programmer who is trying to track down the cause of anerror.) | |||
| 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)*![]() | 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)*![]() | 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)*![]() | Date: 2016-06-11 17:05 | |
Thank you Gareth. I will commit ipaddress.patch this weekend. | |||
| msg268256 -(view) | Author: Roundup Robot (python-dev)![]() | Date: 2016-06-11 19:29 | |
New changesetbc758c62bc4f 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)*![]() | Date: 2016-06-11 19:58 | |
Thank you for applying this patch. | |||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:57:58 | admin | set | github: 64707 |
| 2016-06-11 19:58:16 | gdr@garethrees.org | set | messages: +msg268261 |
| 2016-06-11 19:30:19 | berker.peksag | set | status: open -> closed stage: patch review -> resolved resolution: fixed versions: - Python 3.5 |
| 2016-06-11 19:29:57 | python-dev | set | nosy: +python-dev messages: +msg268256 |
| 2016-06-11 17:05:53 | berker.peksag | set | messages: +msg268233 |
| 2016-06-11 15:59:47 | gdr@garethrees.org | set | files: +ipaddress.patch messages: +msg268219 |
| 2016-06-10 04:56:15 | BreamoreBoy | set | nosy: -BreamoreBoy |
| 2016-06-10 04:15:47 | berker.peksag | set | nosy: +berker.peksag messages: +msg268080 versions: + Python 3.6 |
| 2015-02-14 14:18:26 | serhiy.storchaka | set | keywords: +easy,needs review stage: patch review versions: + Python 3.5, - Python 3.4 |
| 2015-02-14 09:06:07 | BreamoreBoy | set | nosy: +BreamoreBoy messages: +msg235955 |
| 2014-02-04 21:41:37 | ned.deily | set | nosy: +ncoghlan,pmoody |
| 2014-02-04 13:19:44 | gdr@garethrees.org | set | type: behavior -> enhancement |
| 2014-02-04 13:09:31 | gdr@garethrees.org | create | |