diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index ae75f6a59..7d74ff812 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -1474,10 +1474,15 @@ class MongoClient(common.BaseObject): return not self == other def __repr__(self): - if len(self.__nodes) == 1: - return "MongoClient(%r, %r)" % self.address + nodes = self.__nodes.copy() + if not nodes: + # We haven't connected yet. + nodes = self.__seeds.copy() + if len(nodes) == 1: + address = iter(nodes).next() + return "MongoClient(%r, %r)" % address else: - return "MongoClient(%r)" % ["%s:%d" % n for n in self.__nodes] + return "MongoClient(%r)" % ["%s:%d" % n for n in nodes] def __getattr__(self, name): """Get a database by name. diff --git a/test/test_client.py b/test/test_client.py index b84a637f8..739ecf672 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -214,7 +214,14 @@ class TestClient(unittest.TestCase, TestRequestMixin): def test_repr(self): # Making host a str avoids the 'u' prefix in Python 2, so the repr is # the same in Python 2 and 3. - self.assertEqual(repr(MongoClient(str(host), port)), + c = MongoClient(str(host), port) + self.assertEqual(repr(c), + "MongoClient('%s', %d)" % (host, port)) + c.close() + self.assertEqual(repr(c), + "MongoClient('%s', %d)" % (host, port)) + c = MongoClient(str(host), port, connect=False) + self.assertEqual(repr(c), "MongoClient('%s', %d)" % (host, port)) def test_getters(self):