8000 bpo-33034: Improve exception message when cast fails for {Parse,Split… · python/cpython@2cb4661 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2cb4661

Browse files
agnosticdevberkerpeksag
authored andcommitted
bpo-33034: Improve exception message when cast fails for {Parse,Split}Result.port (GH-6078)
1 parent 7389fd9 commit 2cb4661

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/test/test_urlparse.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,16 @@ def test_issue14072(self):
936936
self.assertEqual(p2.scheme, 'tel')
937937
self.assertEqual(p2.path, '+31641044153')
938938

939+
def test_port_casting_failure_message(self):
940+
message = "Port could not be cast to integer value as 'oracle'"
941+
p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle')
942+
with self.assertRaisesRegex(ValueError, message):
943+
p1.port
944+
945+
p2 = urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle')
946+
with self.assertRaisesRegex(ValueError, message):
947+
p2.port
948+
939949
def test_telurl_params(self):
940950
p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516')
941951
self.assertEqual(p1.scheme, 'tel')

Lib/urllib/parse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ def hostname(self):
166166
def port(self):
167167
port = self._hostinfo[1]
168168
if port is not None:
169-
port = int(port, 10)
169+
try:
170+
port = int(port, 10)
171+
except ValueError:
172+
message = f'Port could not be cast to integer value as {port!r}'
173+
raise ValueError(message) from None
170174
if not ( 0 <= port <= 65535):
171175
raise ValueError("Port out of range 0-65535")
172176
return port
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Providing an explicit error message when casting the port property to anything
2+
that is not an integer value using ``urlparse()`` and ``urlsplit()``.
3+
Patch by Matt Eaton.

0 commit comments

Comments
 (0)
0