cpython: 7c78279afc30 (original) (raw)
Mercurial > cpython
changeset 97346:7c78279afc30
Issue #20059: urllib.parse raises ValueError on all invalid ports. Patch by Martin Panter. [#20059]
Robert Collins rbtcollins@hp.com | |
---|---|
date | Mon, 10 Aug 2015 09:53:30 +1200 |
parents | a16c6456f34b |
children | f304ba9425a3 |
files | Doc/library/urllib.parse.rst Doc/whatsnew/3.6.rst Lib/test/test_urlparse.py Lib/urllib/parse.py Misc/NEWS |
diffstat | 5 files changed, 39 insertions(+), 26 deletions(-)[+] [-] Doc/library/urllib.parse.rst 18 Doc/whatsnew/3.6.rst 5 Lib/test/test_urlparse.py 36 Lib/urllib/parse.py 3 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/urllib.parse.rst +++ b/Doc/library/urllib.parse.rst @@ -115,8 +115,9 @@ or on combining URL components into a UR | | | if present | | +------------------+-------+--------------------------+----------------------+
- Reading the :attr:
port
attribute will raise a :exc:ValueError
if - an invalid port is specified in the URL. See section
- :ref:
urlparse-result-object
for more information on the result object. .. versionchanged:: 3.2 Added IPv6 URL parsing capabilities. @@ -126,6 +127,10 @@ or on combining URL components into a UR false), in accordance with :rfc:3986
. Previously, a whitelist of schemes that support fragments existed. - .. versionchanged:: 3.6
Out-of-range port numbers now raise :exc:`ValueError`, instead of[](#l1.20)
returning :const:`None`.[](#l1.21)
+ .. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace') @@ -228,8 +233,13 @@ or on combining URL components into a UR | | | if present | | +------------------+-------+-------------------------+----------------------+
- Reading the :attr:
port
attribute will raise a :exc:ValueError
if - an invalid port is specified in the URL. See section
- :ref:
urlparse-result-object
for more information on the result object. + - .. versionchanged:: 3.6
Out-of-range port numbers now raise :exc:`ValueError`, instead of[](#l1.37)
returning :const:`None`.[](#l1.38)
.. function:: urlunsplit(parts)
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -162,7 +162,10 @@ that may require changes to your code.
Changes in the Python API
-------------------------
-* None yet.
+* Reading the :attr:~urllib.parse.SplitResult.port
attribute of
- :func:
urllib.parse.urlsplit
and :func:~urllib.parse.urlparse
results - now raises :exc:
ValueError
for out-of-range values, rather than - returning :const:
None
. See :issue:20059
. Changes in the C API
--- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -554,29 +554,27 @@ class UrlParseTestCase(unittest.TestCase self.assertEqual(p.port, 80) self.assertEqual(p.geturl(), url)
# Verify an illegal port is returned as None[](#l3.7)
# Verify an illegal port raises ValueError[](#l3.8) url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag"[](#l3.9) p = urllib.parse.urlsplit(url)[](#l3.10)
self.assertEqual(p.port, None)[](#l3.11)
with self.assertRaisesRegex(ValueError, "out of range"):[](#l3.12)
p.port[](#l3.13)
def test_attributes_bad_port(self):
"""Check handling of non-integer ports."""[](#l3.16)
p = urllib.parse.urlsplit("http://www.example.net:foo")[](#l3.17)
self.assertEqual(p.netloc, "www.example.net:foo")[](#l3.18)
self.assertRaises(ValueError, lambda: p.port)[](#l3.19)
p = urllib.parse.urlparse("http://www.example.net:foo")[](#l3.21)
self.assertEqual(p.netloc, "www.example.net:foo")[](#l3.22)
self.assertRaises(ValueError, lambda: p.port)[](#l3.23)
# Once again, repeat ourselves to test bytes[](#l3.25)
p = urllib.parse.urlsplit(b"http://www.example.net:foo")[](#l3.26)
self.assertEqual(p.netloc, b"www.example.net:foo")[](#l3.27)
self.assertRaises(ValueError, lambda: p.port)[](#l3.28)
p = urllib.parse.urlparse(b"http://www.example.net:foo")[](#l3.30)
self.assertEqual(p.netloc, b"www.example.net:foo")[](#l3.31)
self.assertRaises(ValueError, lambda: p.port)[](#l3.32)
"""Check handling of invalid ports."""[](#l3.33)
for bytes in (False, True):[](#l3.34)
for parse in (urllib.parse.urlsplit, urllib.parse.urlparse):[](#l3.35)
for port in ("foo", "1.5", "-1", "0x10"):[](#l3.36)
with self.subTest(bytes=bytes, parse=parse, port=port):[](#l3.37)
netloc = "www.example.net:" + port[](#l3.38)
url = "http://" + netloc[](#l3.39)
if bytes:[](#l3.40)
netloc = netloc.encode("ascii")[](#l3.41)
url = url.encode("ascii")[](#l3.42)
p = parse(url)[](#l3.43)
self.assertEqual(p.netloc, netloc)[](#l3.44)
with self.assertRaises(ValueError):[](#l3.45)
p.port[](#l3.46)
def test_attributes_without_netloc(self): # This example is straight from RFC 3261. It looks like it
--- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -156,9 +156,8 @@ class _NetlocResultMixinBase(object): port = self._hostinfo[1] if port is not None: port = int(port, 10)
# Return None on an illegal port[](#l4.7) if not ( 0 <= port <= 65535):[](#l4.8)
return None[](#l4.9)
raise ValueError("Port out of range 0-65535")[](#l4.10) return port[](#l4.11)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -15,6 +15,9 @@ Core and Builtins Library ------- +- Issue #20059: urllib.parse raises ValueError on all invalid ports.