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 2013年12月23日 23:51 by chad.birch, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| port-ValueError.patch | martin.panter, 2015年02月10日 03:10 | review | ||
| port-ValueError.v2.patch | martin.panter, 2015年03月23日 04:57 | review | ||
| port-ValueError.v3.patch | martin.panter, 2015年07月30日 00:47 | For 3.6 | review | |
| Messages (15) | |||
|---|---|---|---|
| msg206881 - (view) | Author: Chad Birch (chad.birch) | Date: 2013年12月23日 23:51 | |
I'm not sure if this is something that needs adjustment, but it seems somewhat inconsistent to me. After using urlparse() on various urls with invalid port values, trying to access .port on the result will raise a ValueError. This case includes urls such as: "http://www.example.com:asdf" "http://www.example.com:1.5" "http://www.example.com:" However, as of May 24 2012 (http://hg.python.org/cpython/diff/d769e64aed79/Lib/urllib/parse.py), if the invalid port value is an integer, accessing .port will result in None. So this includes urls such as: "http://www.example.com:66000" "http://www.example.com:-1" Should these two cases be made consistent, so that either .port is always None or always results in a ValueError if the port section of the url is invalid? I'd be happy to write a patch for it if it's wanted, but I thought I'd check first (and see which of the two options would be correct, if so). |
|||
| msg232797 - (view) | Author: Martin Panter (martin.panter) * (Python committer) | Date: 2014年12月17日 04:03 | |
I would go for raising ValueError for port numbers out of range. The value of None was already defined to mean that no port is included in the URL.
Also, the ValueError exception should be documented. It is surprising that urlsplit() does not raise any exception, but then simply getting the "port" attribute does raise the exception.
BTW your third example is wrong:
>>> urlparse("http://www.example.com:").port is None
True
It’s probably safest to keep this one as it is, but maybe it also needs documenting :)
|
|||
| msg235660 - (view) | Author: Martin Panter (martin.panter) * (Python committer) | Date: 2015年02月10日 03:10 | |
Mapping out-of-range ports to None was added in Issue 14036, though I don’t understand why that approach was taken instead of raising ValueError. Here is a patch to raise ValueError for out-of-range integer values instead. |
|||
| msg237002 - (view) | Author: Martin Panter (martin.panter) * (Python committer) | Date: 2015年03月02日 01:27 | |
See also Issue 20271, which has a proposed patch with more strict urlsplit() etc behaviour before even returning a SplitResult object. |
|||
| msg237038 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2015年03月02日 10:19 | |
LGTM and I think it is worth to be applied to maintained releases. |
|||
| msg237173 - (view) | Author: Berker Peksag (berker.peksag) * (Python committer) | Date: 2015年03月04日 04:30 | |
> I think it is worth to be applied to maintained releases. I'd commit this only to the default branch. Changing the return value from None to an exception after three 3.4 bugfix releases(3.4.1, 3.4.2 and 3.4.3 -- also since 3.4.3 was released in Feb 2015, 3.4.4 will probably be the last bugfix release of 3.4) would not be the best action. I understand that the current situation is inconsistent, but I'm not sure it's worth it to commit to the 2.7 and 3.4 branches. |
|||
| msg237399 - (view) | Author: Demian Brecht (demian.brecht) * (Python triager) | Date: 2015年03月06日 23:35 | |
> It is surprising that urlsplit() does not raise any exception I have a bit of a TL;DR in #20271, trying to capture what the responsibilities of split and parse methods in urllib are and what they should be if consistency is something that we're after. Around the patch though: It seems quite odd to me to be raising exceptions on accessors rather than on instantiation or when the parsing occurs (in the body of urlparse). Wouldn't it better to raise the exception there? |
|||
| msg238979 - (view) | Author: Martin Panter (martin.panter) * (Python committer) | Date: 2015年03月23日 04:57 | |
Patch v2 just changes a test to use "with self.assertRaises()".
The behaviour of urlparse() succeeding and then result.port failing is indeed odd and surprising. Hopefully documenting this behaviour will help with the "surprising" aspect. But changing it would surely break compatibility. For example, the following is still potentially useful:
>>> urlsplit("http://localhost:ipp/").netloc
'localhost:ipp'
On Unix, some programs look up port names in /etc/services, even though this is not valid for URLs according to the RFCs, and urlsplit().port does not support it. In this case "ipp" would be whatever port the Internet Printing Protocol server is configured to run on.
|
|||
| msg247621 - (view) | Author: Robert Collins (rbcollins) * (Python committer) | Date: 2015年07月29日 20:40 | |
So, I think this is worth applying. The discussion around :ipp etc is irrelevant here: this patch changes large or negative ints to be a valueerror, as non-ints are. The only question is where. I think this is in the category of 'will only break buggy applications' - applications that already handle ValueError to deal with bad inputs, will not be broken. Applications that depend on ports outside the valid range for ports will be broken, but thats the definition of the bug. So I propose to apply to 2.7/3.4/3.5/3.6, but I'm going to seek a second opinion. |
|||
| msg247625 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2015年07月29日 20:52 | |
Because it raises an error where none was raised before, I'd only apply this to 3.6. This is especially true since this issue is not a *bug* report, but a "shouldn't this be more consistent" report. That is, there's no great weight (the OP wasn't even sure it should be changed) in favor of the backport, so even a small possibility of breaking working code argues against the backport. (To be clear: by working code I'm envisioning code that is getting that None value and using it as an error signal or treating it the same as a missing port. Such code is technically "broken" but could be working if the default port happens to work, or the only out of range values it encounters are integers, or it is happy with the exception bubbling up in the non-integer cases.) |
|||
| msg247628 - (view) | Author: Robert Collins (rbcollins) * (Python committer) | Date: 2015年07月29日 20:55 | |
ok, 3.6 only. |
|||
| msg247638 - (view) | Author: Martin Panter (martin.panter) * (Python committer) | Date: 2015年07月29日 22:01 | |
If we take the 3.6-only path, does that warrant adding "Version changed" notices, and/or a What’s New entry? |
|||
| msg247642 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2015年07月29日 23:18 | |
Hmm. Good question. I think it probably does, because it means getting an exception where one did not previously happen. |
|||
| msg247643 - (view) | Author: Martin Panter (martin.panter) * (Python committer) | Date: 2015年07月30日 00:47 | |
Added versioning notices in port-ValueError.v3.patch. |
|||
| msg248340 - (view) | Author: Roundup Robot (python-dev) (Python triager) | Date: 2015年08月09日 21:53 | |
New changeset 7c78279afc30 by Robert Collins in branch 'default': Issue #20059: urllib.parse raises ValueError on all invalid ports. https://hg.python.org/cpython/rev/7c78279afc30 |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:55 | admin | set | github: 64258 |
| 2015年08月09日 21:54:02 | rbcollins | set | status: open -> closed resolution: fixed stage: commit review -> resolved |
| 2015年08月09日 21:53:48 | python-dev | set | nosy:
+ python-dev messages: + msg248340 |
| 2015年07月30日 00:47:10 | martin.panter | set | files:
+ port-ValueError.v3.patch messages: + msg247643 |
| 2015年07月29日 23:18:20 | r.david.murray | set | messages: + msg247642 |
| 2015年07月29日 22:01:33 | martin.panter | set | messages: + msg247638 |
| 2015年07月29日 20:55:09 | rbcollins | set | messages:
+ msg247628 versions: + Python 3.6, - Python 2.7, Python 3.4, Python 3.5 |
| 2015年07月29日 20:52:06 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg247625 |
| 2015年07月29日 20:40:02 | rbcollins | set | nosy:
+ rbcollins messages: + msg247621 |
| 2015年03月23日 04:57:00 | martin.panter | set | files:
+ port-ValueError.v2.patch messages: + msg238979 |
| 2015年03月06日 23:35:28 | demian.brecht | set | nosy:
+ demian.brecht messages: + msg237399 |
| 2015年03月04日 04:30:41 | berker.peksag | set | messages: + msg237173 |
| 2015年03月02日 10:19:12 | serhiy.storchaka | set | nosy:
+ orsenthil messages: + msg237038 stage: commit review |
| 2015年03月02日 01:27:07 | martin.panter | set | messages: + msg237002 |
| 2015年02月14日 23:01:09 | berker.peksag | set | nosy:
+ berker.peksag |
| 2015年02月10日 03:10:16 | martin.panter | set | files:
+ port-ValueError.patch keywords: + patch messages: + msg235660 |
| 2014年12月17日 04:03:48 | martin.panter | set | messages: + msg232797 |
| 2014年11月19日 09:04:28 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka versions: + Python 2.7, Python 3.4, Python 3.5 |
| 2013年12月28日 03:27:09 | martin.panter | set | nosy:
+ martin.panter |
| 2013年12月27日 18:15:33 | cvrebert | set | nosy:
+ cvrebert |
| 2013年12月23日 23:51:50 | chad.birch | create | |