Skip to content

Commit 9a829ac

Browse files
authored
PYTHON-3251 Make extra whitespace visible in invalid port exception (#937)
1 parent 252ed1c commit 9a829ac

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

pymongo/uri_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def parse_host(entity: str, default_port: Optional[int] = DEFAULT_PORT) -> _Addr
134134
host, port = host.split(":", 1)
135135
if isinstance(port, str):
136136
if not port.isdigit() or int(port) > 65535 or int(port) <= 0:
137-
raise ValueError("Port must be an integer between 0 and 65535: %s" % (port,))
137+
raise ValueError("Port must be an integer between 0 and 65535: %r" % (port,))
138138
port = int(port)
139139

140140
# Normalize hostname to lowercase, since DNS is case-insensitive:

test/test_uri_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ def test_parse_uri(self):
147147
self.assertRaises(InvalidURI, parse_uri, "http://foo@foobar.com")
148148
self.assertRaises(ValueError, parse_uri, "mongodb://::1", 27017)
149149

150+
# Extra whitespace should be visible in error message.
151+
with self.assertRaisesRegex(ValueError, "'27017 '"):
152+
parse_uri("mongodb://localhost:27017 ")
153+
150154
orig: dict = {
151155
"nodelist": [("localhost", 27017)],
152156
"username": None,

0 commit comments

Comments
 (0)