From 7f42430978310e8bd9c4ed2995abd7da24c0f52f Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Tue, 24 Feb 2015 23:48:17 -0500 Subject: [PATCH] PYTHON-847 Remove disconnect(), synonym of MongoClient.close(). close() seems more standard among MongoDB drivers. --- doc/api/pymongo/mongo_client.rst | 1 - doc/api/pymongo/mongo_replica_set_client.rst | 1 - doc/faq.rst | 2 +- pymongo/mongo_client.py | 22 +++++++------------- pymongo/monitor.py | 2 +- test/mod_wsgi_test/mod_wsgi_test.wsgi | 2 +- test/test_client.py | 14 ++++++------- test/test_database.py | 2 +- test/test_mongos_ha.py | 2 +- test/test_pooling.py | 2 +- test/test_replica_set_reconfig.py | 4 ++-- test/test_threads.py | 4 ++-- 12 files changed, 25 insertions(+), 33 deletions(-) diff --git a/doc/api/pymongo/mongo_client.rst b/doc/api/pymongo/mongo_client.rst index 699d541f1..68d43fc9d 100644 --- a/doc/api/pymongo/mongo_client.rst +++ b/doc/api/pymongo/mongo_client.rst @@ -6,7 +6,6 @@ .. autoclass:: pymongo.mongo_client.MongoClient(host='localhost', port=27017, max_pool_size=100, document_class=dict, tz_aware=False, **kwargs) - .. automethod:: disconnect .. automethod:: close .. describe:: c[db_name] || c.db_name diff --git a/doc/api/pymongo/mongo_replica_set_client.rst b/doc/api/pymongo/mongo_replica_set_client.rst index bffa256ee..22c7887f0 100644 --- a/doc/api/pymongo/mongo_replica_set_client.rst +++ b/doc/api/pymongo/mongo_replica_set_client.rst @@ -6,7 +6,6 @@ .. autoclass:: pymongo.mongo_replica_set_client.MongoReplicaSetClient(hosts_or_uri, max_pool_size=100, document_class=dict, tz_aware=False, **kwargs) - .. automethod:: disconnect .. automethod:: close .. describe:: c[db_name] || c.db_name diff --git a/doc/faq.rst b/doc/faq.rst index 0dae5a39a..f3c9cd152 100644 --- a/doc/faq.rst +++ b/doc/faq.rst @@ -69,7 +69,7 @@ A thread that waits more than 100ms (in this example) for a socket raises important to bound the duration of operations during a load spike than it is to complete every operation. -When :meth:`~pymongo.mongo_client.MongoClient.disconnect` is called by any +When :meth:`~pymongo.mongo_client.MongoClient.close` is called by any thread, all sockets are closed. Does PyMongo support Python 3? diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 6c0925fa4..07259d32e 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -241,6 +241,9 @@ class MongoClient(common.BaseObject): The ``copy_database`` method is removed, see the :doc:`copy_database examples ` for alternatives. + The :meth:`MongoClient.disconnect` method is removed; it was a + synonym for :meth:`~pymongo.MongoClient.close`. + :class:`~pymongo.mongo_client.MongoClient` no longer returns an instance of :class:`~pymongo.database.Database` for attribute names with leading underscores. You must use dict-style lookups instead:: @@ -643,24 +646,15 @@ class MongoClient(common.BaseObject): except ConnectionFailure: return False - def disconnect(self): + def close(self): """Disconnect from MongoDB. - Disconnecting will close all underlying sockets in the connection - pools. If this instance is used again it will be automatically - re-opened. + Close all sockets in the connection pools and stop the monitor threads. + If this instance is used again it will be automatically re-opened and + the threads restarted. """ self._topology.close() - def close(self): - """Alias for :meth:`disconnect` - - Disconnecting will close all underlying sockets in the connection - pools. If this instance is used again it will be automatically - re-opened. - """ - self.disconnect() - def set_cursor_manager(self, manager_class): """Set this client's cursor manager. @@ -1079,7 +1073,7 @@ class MongoClient(common.BaseObject): return self def __exit__(self, exc_type, exc_val, exc_tb): - self.disconnect() + self.close() def __iter__(self): return self diff --git a/pymongo/monitor.py b/pymongo/monitor.py index e0b3ff1ca..2a174208e 100644 --- a/pymongo/monitor.py +++ b/pymongo/monitor.py @@ -76,7 +76,7 @@ class Monitor(object): self._executor.open() def close(self): - """Disconnect and stop monitoring. + """Close and stop monitoring. open() restarts the monitor after closing. """ diff --git a/test/mod_wsgi_test/mod_wsgi_test.wsgi b/test/mod_wsgi_test/mod_wsgi_test.wsgi index 1ea7d7ed1..bf45db350 100644 --- a/test/mod_wsgi_test/mod_wsgi_test.wsgi +++ b/test/mod_wsgi_test/mod_wsgi_test.wsgi @@ -40,7 +40,7 @@ ndocs = 20 collection.drop() collection.insert_many([{'i': i} for i in range(ndocs)]) -client.disconnect() # Discard main thread's request socket. +client.close() # Discard main thread's request socket. try: from mod_wsgi import version as mod_wsgi_version diff --git a/test/test_client.py b/test/test_client.py index 1c505ad09..4d319f0ea 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -302,16 +302,16 @@ class TestClient(IntegrationTest): self.assertNotIn("pymongo_test", dbs) self.assertNotIn("pymongo_test2", dbs) - def test_disconnect(self): + def test_close(self): coll = self.client.pymongo_test.bar - self.client.disconnect() - self.client.disconnect() + self.client.close() + self.client.close() coll.count() - self.client.disconnect() - self.client.disconnect() + self.client.close() + self.client.close() coll.count() @@ -975,7 +975,7 @@ class TestClientProperties(MockClientTest): c.set_wire_version_range('a:1', 0, 0) c.set_wire_version_range('b:2', 0, 0) c.set_wire_version_range('c:3', 0, 0) - c.disconnect() + c.close() c.db.command('ismaster') used_host = '%s:%s' % (c.host, c.port) expected_min, expected_max = c.mock_wire_versions[used_host] @@ -1081,7 +1081,7 @@ class TestMongoClientFailover(MockClientTest): c.kill_host('a:1') c.mock_primary = 'b:2' - c.disconnect() + c.close() self.assertEqual(0, len(c.nodes)) t = c._get_topology() diff --git a/test/test_database.py b/test/test_database.py index 6008b9b82..50a912901 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -539,7 +539,7 @@ class TestDatabase(IntegrationTest): other_db.test.insert_one, {}) # Close all sockets. - client.disconnect() + client.close() # We should still be able to write to the regular user's db. self.assertTrue(users_db.test.delete_many({})) diff --git a/test/test_mongos_ha.py b/test/test_mongos_ha.py index 3862b4e35..71d8955fd 100644 --- a/test/test_mongos_ha.py +++ b/test/test_mongos_ha.py @@ -85,7 +85,7 @@ class TestMongosHA(MockClientTest): wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses') # Trigger reconnect. - client.disconnect() + client.close() do_simple_op(client, nthreads) wait_until(lambda: len(client.nodes) == 3, diff --git a/test/test_pooling.py b/test/test_pooling.py index a78a5d43c..8e4ec8c31 100644 --- a/test/test_pooling.py +++ b/test/test_pooling.py @@ -104,7 +104,7 @@ class NonUnique(MongoThread): class Disconnect(MongoThread): def run_mongo_thread(self): for _ in range(N): - self.client.disconnect() + self.client.close() class SocketGetter(MongoThread): diff --git a/test/test_replica_set_reconfig.py b/test/test_replica_set_reconfig.py index 8ad5bf804..e1b5e9e3d 100644 --- a/test/test_replica_set_reconfig.py +++ b/test/test_replica_set_reconfig.py @@ -55,7 +55,7 @@ class TestSecondaryBecomesStandalone(MockClientTest): c.kill_host('b:2') # Force reconnect. - c.disconnect() + c.close() with self.assertRaises(AutoReconnect): c.db.command('ismaster') @@ -152,7 +152,7 @@ class TestSecondaryAdded(MockClientTest): c.mock_members.append('c:3') c.mock_ismaster_hosts.append('c:3') - c.disconnect() + c.close() c.db.command('ismaster') self.assertEqual('a', c.host) diff --git a/test/test_threads.py b/test/test_threads.py index 0a3ae0ae4..3160a5c47 100644 --- a/test/test_threads.py +++ b/test/test_threads.py @@ -125,7 +125,7 @@ class Disconnect(threading.Thread): def run(self): for _ in range(self.n): - self.client.disconnect() + self.client.close() self.passed = True @@ -189,7 +189,7 @@ class TestThreads(IntegrationTest): self.db.test.insert_many([{"x": i} for i in range(1000)]) # Start 10 threads that execute a query, and 10 threads that call - # client.disconnect() 10 times in a row. + # client.close() 10 times in a row. threads = [SaveAndFind(self.db.test) for _ in range(10)] threads.extend(Disconnect(self.db.connection, 10) for _ in range(10))