Skip to content

Commit 0b375a2

Browse files
PYTHON-2295 MongoClient with multiple hosts directConnection=True should raise an error (#446)
1 parent bfd297f commit 0b375a2

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

pymongo/mongo_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,11 @@ def __init__(
670670
# Normalize combined options.
671671
opts = _normalize_options(opts)
672672

673+
# Ensure directConnection was not True if there are multiple seeds.
674+
if len(seeds) > 1 and opts.get('directconnection'):
675+
raise ConfigurationError(
676+
"Cannot specify multiple hosts with directConnection=true")
677+
673678
# Username and password passed as kwargs override user info in URI.
674679
username = opts.get("username", username)
675680
password = opts.get("password", password)

test/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,10 @@ def test_direct_connection(self):
15721572
TOPOLOGY_TYPE.ReplicaSetWithPrimary])
15731573
client.close()
15741574

1575+
# directConnection=True, should error with multiple hosts as a list.
1576+
with self.assertRaises(ConfigurationError):
1577+
MongoClient(['host1', 'host2'], directConnection=True)
1578+
15751579

15761580
class TestExhaustCursor(IntegrationTest):
15771581
"""Test that clients properly handle errors from exhaust cursors."""

0 commit comments

Comments
 (0)