diff options
author | Robert Collins <rbtcollins@hp.com> | 2015-08-09 21:53:30 (GMT) |
---|---|---|
committer | Robert Collins <rbtcollins@hp.com> | 2015-08-09 21:53:30 (GMT) |
commit | dfa95c9a8f2772d1e8e54aa5aa14c91d4971964f (patch) | |
tree | c86f63dde8e78423f0384abeb55ec02cfd3e64c1 /Lib | |
parent | 846a1487cb2dea74dcb858b35fa406ac38641fe6 (diff) | |
download | cpython-dfa95c9a8f2772d1e8e54aa5aa14c91d4971964f.zip cpython-dfa95c9a8f2772d1e8e54aa5aa14c91d4971964f.tar.gz cpython-dfa95c9a8f2772d1e8e54aa5aa14c91d4971964f.tar.bz2 |
Issue #20059: urllib.parse raises ValueError on all invalid ports.
Patch by Martin Panter.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_urlparse.py | 36 | ||||
-rw-r--r-- | Lib/urllib/parse.py | 3 |
2 files changed, 18 insertions, 21 deletions
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index 0552f90..fcf5082 100644 --- 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 + # Verify an illegal port raises ValueError url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag" p = urllib.parse.urlsplit(url) - self.assertEqual(p.port, None) + with self.assertRaisesRegex(ValueError, "out of range"): + p.port def test_attributes_bad_port(self): - """Check handling of non-integer ports.""" - p = urllib.parse.urlsplit("http://www.example.net:foo") - self.assertEqual(p.netloc, "www.example.net:foo") - self.assertRaises(ValueError, lambda: p.port) - - p = urllib.parse.urlparse("http://www.example.net:foo") - self.assertEqual(p.netloc, "www.example.net:foo") - self.assertRaises(ValueError, lambda: p.port) - - # Once again, repeat ourselves to test bytes - p = urllib.parse.urlsplit(b"http://www.example.net:foo") - self.assertEqual(p.netloc, b"www.example.net:foo") - self.assertRaises(ValueError, lambda: p.port) - - p = urllib.parse.urlparse(b"http://www.example.net:foo") - self.assertEqual(p.netloc, b"www.example.net:foo") - self.assertRaises(ValueError, lambda: p.port) + """Check handling of invalid ports.""" + for bytes in (False, True): + for parse in (urllib.parse.urlsplit, urllib.parse.urlparse): + for port in ("foo", "1.5", "-1", "0x10"): + with self.subTest(bytes=bytes, parse=parse, port=port): + netloc = "www.example.net:" + port + url = "http://" + netloc + if bytes: + netloc = netloc.encode("ascii") + url = url.encode("ascii") + p = parse(url) + self.assertEqual(p.netloc, netloc) + with self.assertRaises(ValueError): + p.port def test_attributes_without_netloc(self): # This example is straight from RFC 3261. It looks like it diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 01c9e58..5e2155c 100644 --- 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 if not ( 0 <= port <= 65535): - return None + raise ValueError("Port out of range 0-65535") return port |