Skip to content

Commit 7d771cb

Browse files
committed
keep parse_uri as is and have it call two different functions instead
1 parent 3afd732 commit 7d771cb

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def __init__(
778778
# it must be a URI,
779779
# https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
780780
if "/" in entity:
781-
res = uri_parser.parse_uri(
781+
res = uri_parser._validate_uri(
782782
entity,
783783
port,
784784
validate=True,
@@ -933,7 +933,7 @@ def _resolve_uri(self):
933933
timeout = common.validate_timeout_or_none_or_zero(
934934
keyword_opts.cased_key("connecttimeoutms"), timeout
935935
)
936-
res = uri_parser.parse_uri_lookups(
936+
res = uri_parser._lookup_uri(
937937
entity,
938938
self._port,
939939
validate=True,

pymongo/synchronous/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ def __init__(
776776
# it must be a URI,
777777
# https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
778778
if "/" in entity:
779-
res = uri_parser.parse_uri(
779+
res = uri_parser._validate_uri(
780780
entity,
781781
port,
782782
validate=True,
@@ -931,7 +931,7 @@ def _resolve_uri(self):
931931
timeout = common.validate_timeout_or_none_or_zero(
932932
keyword_opts.cased_key("connecttimeoutms"), timeout
933933
)
934-
res = uri_parser.parse_uri_lookups(
934+
res = uri_parser._lookup_uri(
935935
entity,
936936
self._port,
937937
validate=True,

pymongo/uri_parser.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ def parse_uri(
427427
validate: bool = True,
428428
warn: bool = False,
429429
normalize: bool = True,
430+
connect_timeout: Optional[float] = None,
431+
srv_service_name: Optional[str] = None,
430432
srv_max_hosts: Optional[int] = None,
431433
) -> dict[str, Any]:
432434
"""Parse and validate a MongoDB URI.
@@ -482,6 +484,30 @@ def parse_uri(
482484
.. versionchanged:: 3.1
483485
``warn`` added so invalid options can be ignored.
484486
"""
487+
result = _validate_uri(uri, default_port, validate, warn, normalize, srv_max_hosts)
488+
result.update(
489+
_lookup_uri(
490+
uri,
491+
default_port,
492+
validate,
493+
warn,
494+
normalize,
495+
connect_timeout,
496+
srv_service_name,
497+
srv_max_hosts,
498+
)
499+
)
500+
return result
501+
502+
503+
def _validate_uri(
504+
uri: str,
505+
default_port: Optional[int] = DEFAULT_PORT,
506+
validate: bool = True,
507+
warn: bool = False,
508+
normalize: bool = True,
509+
srv_max_hosts: Optional[int] = None,
510+
):
485511
if uri.startswith(SCHEME):
486512
is_srv = False
487513
scheme_free = uri[SCHEME_LEN:]
@@ -570,7 +596,7 @@ def parse_uri(
570596
}
571597

572598

573-
def parse_uri_lookups(
599+
def _lookup_uri(
574600
uri: str,
575601
default_port: Optional[int] = DEFAULT_PORT,
576602
validate: bool = True,

test/asynchronous/test_dns.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from pymongo.common import validate_read_preference_tags
3535
from pymongo.errors import ConfigurationError
36-
from pymongo.uri_parser import parse_uri, parse_uri_lookups, split_hosts
36+
from pymongo.uri_parser import parse_uri, split_hosts
3737

3838
_IS_SYNC = False
3939

@@ -110,7 +110,6 @@ async def run_test(self):
110110

111111
if seeds or num_seeds:
112112
result = parse_uri(uri, validate=True)
113-
result.update(parse_uri_lookups(uri, validate=True))
114113
if seeds is not None:
115114
self.assertEqual(sorted(result["nodelist"]), sorted(seeds))
116115
if num_seeds is not None:
@@ -142,16 +141,12 @@ async def run_test(self):
142141
# Our test certs don't support the SRV hosts used in these
143142
# tests.
144143
copts["tlsAllowInvalidHostnames"] = True
145-
print(uri)
146-
print(copts)
147144
client = self.simple_client(uri, **copts)
148-
await client.aconnect()
149145
if client._options.connect:
150146
await client.aconnect()
151147
if num_seeds is not None:
152148
self.assertEqual(len(client._topology_settings.seeds), num_seeds)
153149
if hosts is not None:
154-
print(client.nodes)
155150
await async_wait_until(
156151
lambda: hosts == client.nodes, "match test hosts to client nodes"
157152
)
@@ -166,7 +161,6 @@ async def run_test(self):
166161
else:
167162
try:
168163
parse_uri(uri)
169-
parse_uri_lookups(uri)
170164
except (ConfigurationError, ValueError):
171165
pass
172166
else:

test/test_dns.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from pymongo.common import validate_read_preference_tags
3535
from pymongo.errors import ConfigurationError
36-
from pymongo.uri_parser import parse_uri, parse_uri_lookups, split_hosts
36+
from pymongo.uri_parser import parse_uri, split_hosts
3737

3838
_IS_SYNC = True
3939

@@ -110,7 +110,6 @@ def run_test(self):
110110

111111
if seeds or num_seeds:
112112
result = parse_uri(uri, validate=True)
113-
result.update(parse_uri_lookups(uri, validate=True))
114113
if seeds is not None:
115114
self.assertEqual(sorted(result["nodelist"]), sorted(seeds))
116115
if num_seeds is not None:
@@ -142,16 +141,12 @@ def run_test(self):
142141
# Our test certs don't support the SRV hosts used in these
143142
# tests.
144143
copts["tlsAllowInvalidHostnames"] = True
145-
print(uri)
146-
print(copts)
147144
client = self.simple_client(uri, **copts)
148-
client._connect()
149145
if client._options.connect:
150146
client._connect()
151147
if num_seeds is not None:
152148
self.assertEqual(len(client._topology_settings.seeds), num_seeds)
153149
if hosts is not None:
154-
print(client.nodes)
155150
wait_until(lambda: hosts == client.nodes, "match test hosts to client nodes")
156151
if num_hosts is not None:
157152
wait_until(
@@ -164,7 +159,6 @@ def run_test(self):
164159
else:
165160
try:
166161
parse_uri(uri)
167-
parse_uri_lookups(uri)
168162
except (ConfigurationError, ValueError):
169163
pass
170164
else:

0 commit comments

Comments
 (0)