Skip to content

Commit 92968aa

Browse files
committed
PYTHON-965 Backport MongoClient.address.
This change also deprecates MongoClient.host and MongoClient.port, which are removed in PyMongo 3.
1 parent c11aae4 commit 92968aa

File tree

9 files changed

+75
-31
lines changed

9 files changed

+75
-31
lines changed

doc/migrate-to-pymongo3.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,21 @@ can be replaced by this with PyMongo 2.9 or later:
469469

470470
>>> client.close()
471471

472+
The host and port attributes are removed
473+
........................................
474+
475+
Code like this::
476+
477+
>>> host = client.host
478+
>>> port = client.port
479+
480+
can be replaced by this with PyMongo 2.9 or later:
481+
482+
.. doctest::
483+
484+
>>> address = client.address
485+
>>> host, port = address or (None, None)
486+
472487
BSON
473488
----
474489

pymongo/mongo_client.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,19 @@ def __member_property(self, attr_name, default=None):
558558

559559
@property
560560
def host(self):
561-
"""Current connected host.
561+
"""**DEPRECATED** Current connected host.
562562
563+
.. warning:: :attr:`host` is deprecated in this version of PyMongo and
564+
removed in PyMongo 3.0. Use :attr:`address` instead.
565+
566+
.. versionchanged:: 2.9
567+
Deprecated :attr:`host`.
563568
.. versionchanged:: 1.3
564569
``host`` is now a property rather than a method.
565570
"""
571+
warnings.warn("host is deprecated in this version of PyMongo and "
572+
"removed in PyMongo 3. Use address instead.",
573+
DeprecationWarning, stacklevel=2)
566574
member = self.__member
567575
if member:
568576
return member.host[0]
@@ -571,16 +579,34 @@ def host(self):
571579

572580
@property
573581
def port(self):
574-
"""Current connected port.
582+
"""**DEPRECATED** Current connected port.
583+
584+
.. warning:: :attr:`port` is deprecated in this version of PyMongo and
585+
removed in PyMongo 3.0. Use :attr:`address` instead.
575586
587+
.. versionchanged:: 2.9
588+
Deprecated :attr:`port`.
576589
.. versionchanged:: 1.3
577590
``port`` is now a property rather than a method.
578591
"""
592+
warnings.warn("port is deprecated in this version of PyMongo and "
593+
"removed in PyMongo 3. Use address instead.",
594+
DeprecationWarning, stacklevel=2)
579595
member = self.__member
580596
if member:
581597
return member.host[1]
582598

599+
@property
600+
def address(self):
601+
"""(host, port) of the current standalone, primary, or mongos, or None.
602+
603+
.. versionadded:: 2.9
604+
"""
605+
member = self.__member
606+
if member:
607+
return member.host
583608
return None
609+
584610
@property
585611
def is_primary(self):
586612
"""If this instance is connected to a standalone, a replica set
@@ -1415,15 +1441,15 @@ def end_request(self):
14151441

14161442
def __eq__(self, other):
14171443
if isinstance(other, self.__class__):
1418-
return self.host == other.host and self.port == other.port
1444+
return self.address == other.address
14191445
return NotImplemented
14201446

14211447
def __ne__(self, other):
14221448
return not self == other
14231449

14241450
def __repr__(self):
14251451
if len(self.__nodes) == 1:
1426-
return "MongoClient(%r, %r)" % (self.host, self.port)
1452+
return "MongoClient(%r, %r)" % self.address
14271453
else:
14281454
return "MongoClient(%r)" % ["%s:%d" % n for n in self.__nodes]
14291455

pymongo/mongo_replica_set_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,16 @@ def hosts(self):
901901
"""
902902
return self.__rs_state.hosts
903903

904+
@property
905+
def address(self):
906+
"""The (host, port) of the current primary of the replica set.
907+
908+
Returns None if there is no primary.
909+
910+
.. versionadded:: 2.9
911+
"""
912+
return self.__rs_state.writer
913+
904914
@property
905915
def primary(self):
906916
"""The (host, port) of the current primary of the replica set.

test/test_client.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ def test_init_disconnected(self):
162162
ctx.exit()
163163

164164
c.pymongo_test.test.find_one() # Auto-connect.
165-
self.assertEqual(host, c.host)
166-
self.assertEqual(port, c.port)
165+
self.assertEqual((host, port), c.address)
167166

168167
if version.at_least(c, (2, 5, 4, -1)):
169168
self.assertTrue(c.max_wire_version > 0)
@@ -219,8 +218,7 @@ def test_repr(self):
219218
"MongoClient('%s', %d)" % (host, port))
220219

221220
def test_getters(self):
222-
self.assertEqual(MongoClient(host, port).host, host)
223-
self.assertEqual(MongoClient(host, port).port, port)
221+
self.assertEqual(MongoClient(host, port).address, (host, port))
224222
self.assertEqual(set([(host, port)]),
225223
MongoClient(host, port).nodes)
226224

@@ -1056,7 +1054,7 @@ def test_wire_version_mongos_ha(self):
10561054
c.db.collection.find_one() # Connect.
10571055

10581056
# Which member did we use?
1059-
used_host = '%s:%s' % (c.host, c.port)
1057+
used_host = '%s:%s' % c.address
10601058
expected_min, expected_max = c.mock_wire_versions[used_host]
10611059
self.assertEqual(expected_min, c.min_wire_version)
10621060
self.assertEqual(expected_max, c.max_wire_version)
@@ -1066,7 +1064,7 @@ def test_wire_version_mongos_ha(self):
10661064
c.set_wire_version_range('c:3', 0, 0)
10671065
c.close()
10681066
c.db.collection.find_one()
1069-
used_host = '%s:%s' % (c.host, c.port)
1067+
used_host = '%s:%s' % c.address
10701068
expected_min, expected_max = c.mock_wire_versions[used_host]
10711069
self.assertEqual(expected_min, c.min_wire_version)
10721070
self.assertEqual(expected_max, c.max_wire_version)
@@ -1184,8 +1182,7 @@ def test_discover_primary(self):
11841182
host='b:2', # Pass a secondary.
11851183
replicaSet='rs')
11861184

1187-
self.assertEqual('a', c.host)
1188-
self.assertEqual(1, c.port)
1185+
self.assertEqual(('a', 1), c.address)
11891186
self.assertEqual(3, len(c.nodes))
11901187

11911188
# Fail over.
@@ -1195,8 +1192,7 @@ def test_discover_primary(self):
11951192
# Force reconnect.
11961193
c.close()
11971194
c.db.collection.find_one()
1198-
self.assertEqual('b', c.host)
1199-
self.assertEqual(2, c.port)
1195+
self.assertEqual(('b', 2), c.address)
12001196

12011197
# a:1 is still in nodes.
12021198
self.assertEqual(3, len(c.nodes))

test/test_collection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,8 +1440,8 @@ def test_aggregation_cursor(self):
14401440
db = self.db
14411441
if self.setname:
14421442
# Test that getMore messages are sent to the right server.
1443-
db = MongoReplicaSetClient(host=self.client.host,
1444-
port=self.client.port,
1443+
db = MongoReplicaSetClient(host=self.client.address[0],
1444+
port=self.client.address[1],
14451445
replicaSet=self.setname,
14461446
readPreference='secondary')[db.name]
14471447

@@ -1482,8 +1482,8 @@ def test_parallel_scan(self):
14821482
db.drop_collection("test")
14831483
if self.setname:
14841484
# Test that getMore messages are sent to the right server.
1485-
db = MongoReplicaSetClient(host=self.client.host,
1486-
port=self.client.port,
1485+
db = MongoReplicaSetClient(host=self.client.address[0],
1486+
port=self.client.address[1],
14871487
replicaSet=self.setname,
14881488
readPreference='secondary')[db.name]
14891489
coll = db.test

test/test_mongos_ha.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_failover(self):
9393
self.assertEqual(len(mock_hosts), len(client.nodes))
9494

9595
# Our chosen mongos goes down.
96-
client.kill_host('%s:%s' % (client.host, client.port))
96+
client.kill_host('%s:%s' % client.address)
9797

9898
# Trigger failover. AutoReconnect should be raised exactly once.
9999
errors = []
@@ -146,7 +146,7 @@ def test_acceptable_latency(self):
146146
# No error
147147
client.db.collection.find_one()
148148
# Our chosen mongos goes down.
149-
client.kill_host('%s:%s' % (client.host, client.port))
149+
client.kill_host('%s:%s' % client.address)
150150
try:
151151
client.db.collection.find_one()
152152
except:
@@ -185,7 +185,7 @@ def test_backport_localthresholdms_kwarg(self):
185185
# secondaryAcceptableLatencyMS.
186186
client.db.collection.find_one()
187187
# Our chosen mongos goes down.
188-
client.kill_host('%s:%s' % (client.host, client.port))
188+
client.kill_host('%s:%s' % client.address)
189189
try:
190190
client.db.collection.find_one()
191191
except:

test/test_pymongo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class TestPyMongo(unittest.TestCase):
2525
def test_mongo_client_alias(self):
2626
# Testing that pymongo module imports mongo_client.MongoClient
2727
c = pymongo.MongoClient(host, port)
28-
self.assertEqual(c.host, host)
29-
self.assertEqual(c.port, port)
28+
self.assertEqual(c.address, (host, port))
3029

3130

3231
if __name__ == "__main__":

test/test_replica_set_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def test_properties(self):
264264
try:
265265
warnings.simplefilter("ignore", DeprecationWarning)
266266
self.assertEqual(c.primary, self.primary)
267+
self.assertEqual(c.address, self.primary)
267268
self.assertEqual(c.hosts, self.hosts)
268269
self.assertEqual(c.arbiters, self.arbiters)
269270
self.assertEqual(c.max_pool_size, 100)
@@ -296,6 +297,7 @@ def test_properties(self):
296297
tag_sets=copy.deepcopy(tag_sets),
297298
secondary_acceptable_latency_ms=77)
298299
self.assertEqual(c.primary, self.primary)
300+
self.assertEqual(c.address, self.primary)
299301
self.assertEqual(c.hosts, self.hosts)
300302
self.assertEqual(c.arbiters, self.arbiters)
301303
self.assertEqual(c.max_pool_size, 25)

test/test_replica_set_reconfig.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ def test_client(self):
4141
replicaSet='rs')
4242

4343
# MongoClient connects to primary by default.
44-
self.assertEqual('a', c.host)
45-
self.assertEqual(1, c.port)
44+
self.assertEqual(('a', 1), c.address)
4645

4746
# C is brought up as a standalone.
4847
c.mock_members.remove('c:3')
@@ -62,8 +61,7 @@ def test_client(self):
6261
else:
6362
self.fail("MongoClient didn't raise AutoReconnect")
6463

65-
self.assertEqual(None, c.host)
66-
self.assertEqual(None, c.port)
64+
self.assertEqual(None, c.address)
6765

6866
def test_replica_set_client(self):
6967
c = MockReplicaSetClient(
@@ -137,8 +135,7 @@ def test_client(self):
137135
replicaSet='rs')
138136

139137
# MongoClient connects to primary by default.
140-
self.assertEqual('a', c.host)
141-
self.assertEqual(1, c.port)
138+
self.assertEqual(('a', 1), c.address)
142139
self.assertEqual(set([('a', 1), ('b', 2)]), c.nodes)
143140

144141
# C is added.
@@ -148,8 +145,7 @@ def test_client(self):
148145
c.close()
149146
c.db.collection.find_one()
150147

151-
self.assertEqual('a', c.host)
152-
self.assertEqual(1, c.port)
148+
self.assertEqual(('a', 1), c.address)
153149
self.assertEqual(set([('a', 1), ('b', 2), ('c', 3)]), c.nodes)
154150

155151
def test_replica_set_client(self):

0 commit comments

Comments
 (0)