# HG changeset patch # Parent 76170e33f25183101000668be79754654f9070a6 #20059: ValueError for out-of-range ports; document it diff -r 76170e33f251 Doc/library/urllib.parse.rst --- a/Doc/library/urllib.parse.rst Mon Feb 09 21:00:00 2015 -0500 +++ b/Doc/library/urllib.parse.rst Tue Feb 10 03:01:50 2015 +0000 @@ -113,8 +113,9 @@ | | | if present | | +------------------+-------+--------------------------+----------------------+ - See section :ref:`urlparse-result-object` for more information on the result - object. + 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. @@ -226,8 +227,9 @@ | | | if present | | +------------------+-------+-------------------------+----------------------+ - See section :ref:`urlparse-result-object` for more information on the result - object. + 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. .. function:: urlunsplit(parts) diff -r 76170e33f251 Lib/test/test_urlparse.py --- a/Lib/test/test_urlparse.py Mon Feb 09 21:00:00 2015 -0500 +++ b/Lib/test/test_urlparse.py Tue Feb 10 03:01:50 2015 +0000 @@ -552,29 +552,26 @@ 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.assertRaises(ValueError): + 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) + self.assertRaises(ValueError, lambda: p.port) def test_attributes_without_netloc(self): # This example is straight from RFC 3261. It looks like it diff -r 76170e33f251 Lib/urllib/parse.py --- a/Lib/urllib/parse.py Mon Feb 09 21:00:00 2015 -0500 +++ b/Lib/urllib/parse.py Tue Feb 10 03:01:50 2015 +0000 @@ -154,9 +154,8 @@ 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