From 45da03afada015e94f0570db5943e4e2450f484d Mon Sep 17 00:00:00 2001 From: Prashant Mital Date: Thu, 8 Aug 2019 16:03:53 -0700 Subject: [PATCH] PYTHON-1914 MongoClient.is_locked should not raise DeprecationWarning --- pymongo/database.py | 30 +++++++++++++++++------------- pymongo/mongo_client.py | 2 +- test/test_client.py | 8 ++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/pymongo/database.py b/pymongo/database.py index 52fbda81e..c6900fed1 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -979,6 +979,21 @@ class Database(common.BaseObject): return result + def _current_op(self, include_all=False, session=None): + """Helper for running $currentOp.""" + cmd = SON([("currentOp", 1), ("$all", include_all)]) + with self.__client._socket_for_writes(session) as sock_info: + if sock_info.max_wire_version >= 4: + return self.__client.admin._command( + sock_info, cmd, codec_options=self.codec_options, + session=session) + else: + spec = {"$all": True} if include_all else {} + return _first_batch(sock_info, "admin", "$cmd.sys.inprog", + spec, -1, True, self.codec_options, + ReadPreference.PRIMARY, cmd, + self.client._event_listeners) + def current_op(self, include_all=False, session=None): """**DEPRECATED**: Get information on operations currently running. @@ -1015,21 +1030,10 @@ class Database(common.BaseObject): .. _$currentOp aggregation pipeline stage: https://docs.mongodb.com/manual/reference/operator/aggregation/currentOp/ .. _currentOp command: https://docs.mongodb.com/manual/reference/command/currentOp/ """ - warnings.warn("current_op() is deprecated. See the documentation for" + warnings.warn("current_op() is deprecated. See the documentation for " "more information", DeprecationWarning, stacklevel=2) - cmd = SON([("currentOp", 1), ("$all", include_all)]) - with self.__client._socket_for_writes(session) as sock_info: - if sock_info.max_wire_version >= 4: - return self.__client.admin._command( - sock_info, cmd, codec_options=self.codec_options, - session=session) - else: - spec = {"$all": True} if include_all else {} - return _first_batch(sock_info, "admin", "$cmd.sys.inprog", - spec, -1, True, self.codec_options, - ReadPreference.PRIMARY, cmd, - self.client._event_listeners) + return self._current_op(include_all, session) def profiling_level(self, session=None): """Get the database's current profiling level. diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 019033133..80a5db454 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -2065,7 +2065,7 @@ class MongoClient(common.BaseObject): are blocked, although read operations may still be allowed. Use :meth:`unlock` to unlock. """ - ops = self._database_default_options('admin').current_op() + ops = self._database_default_options('admin')._current_op() return bool(ops.get('fsyncLock', 0)) def fsync(self, **kwargs): diff --git a/test/test_client.py b/test/test_client.py index c67f07f60..1b255045a 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -1064,6 +1064,14 @@ class TestClient(IntegrationTest): time.sleep(1) self.assertFalse(locked) + def test_is_locked_does_not_raise_warning(self): + client = rs_or_single_client() + with warnings.catch_warnings(record=True) as ctx: + warnings.simplefilter("always") + _ = client.is_locked + self.assertFalse( + any(issubclass(w.category, DeprecationWarning) for w in ctx)) + def test_contextlib(self): client = rs_or_single_client() client.pymongo_test.drop_collection("test")