]> granicus.if.org Git - python/commitdiff
bpo-33034: Improve exception message when cast fails for {Parse,Split}Result.port...
authorMatt Eaton <agnosticdev@gmail.com>
Tue, 20 Mar 2018 06:41:37 +0000 (01:41 -0500)
committerBerker Peksag <berker.peksag@gmail.com>
Tue, 20 Mar 2018 06:41:37 +0000 (09:41 +0300)
Lib/test/test_urlparse.py
Lib/urllib/parse.py
Misc/NEWS.d/next/Library/2018-03-11-08-44-12.bpo-33034.bpb23d.rst [new file with mode: 0644]

index ddee1c38d8b4f0c5493c6bb0d81ba87b9b350977..9d5121792833660591548ee232f3cf92e933e3a6 100644 (file)
@@ -936,6 +936,16 @@ class UrlParseTestCase(unittest.TestCase):
         self.assertEqual(p2.scheme, 'tel')
         self.assertEqual(p2.path, '+31641044153')
 
+    def test_port_casting_failure_message(self):
+        message = "Port could not be cast to integer value as 'oracle'"
+        p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle')
+        with self.assertRaisesRegex(ValueError, message):
+            p1.port
+
+        p2 = urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle')
+        with self.assertRaisesRegex(ValueError, message):
+            p2.port
+
     def test_telurl_params(self):
         p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516')
         self.assertEqual(p1.scheme, 'tel')
index 58460f9234fb96a0c8cad444b9dfd1597a91faaa..3f8cfe5300c024d085f35750a60714a4242b0600 100644 (file)
@@ -166,7 +166,11 @@ class _NetlocResultMixinBase(object):
     def port(self):
         port = self._hostinfo[1]
         if port is not None:
-            port = int(port, 10)
+            try:
+                port = int(port, 10)
+            except ValueError:
+                message = f'Port could not be cast to integer value as {port!r}'
+                raise ValueError(message) from None
             if not ( 0 <= port <= 65535):
                 raise ValueError("Port out of range 0-65535")
         return port
diff --git a/Misc/NEWS.d/next/Library/2018-03-11-08-44-12.bpo-33034.bpb23d.rst b/Misc/NEWS.d/next/Library/2018-03-11-08-44-12.bpo-33034.bpb23d.rst
new file mode 100644 (file)
index 0000000..c81683a
--- /dev/null
@@ -0,0 +1,3 @@
+Providing an explicit error message when casting the port property to anything 
+that is not an integer value using ``urlparse()`` and ``urlsplit()``.
+Patch by Matt Eaton.