Skip to content

Commit 45da03a

Browse files
committed
PYTHON-1914 MongoClient.is_locked should not raise DeprecationWarning
1 parent a9953d0 commit 45da03a

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

pymongo/database.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,21 @@ def validate_collection(self, name_or_collection,
979979

980980
return result
981981

982+
def _current_op(self, include_all=False, session=None):
983+
"""Helper for running $currentOp."""
984+
cmd = SON([("currentOp", 1), ("$all", include_all)])
985+
with self.__client._socket_for_writes(session) as sock_info:
986+
if sock_info.max_wire_version >= 4:
987+
return self.__client.admin._command(
988+
sock_info, cmd, codec_options=self.codec_options,
989+
session=session)
990+
else:
991+
spec = {"$all": True} if include_all else {}
992+
return _first_batch(sock_info, "admin", "$cmd.sys.inprog",
993+
spec, -1, True, self.codec_options,
994+
ReadPreference.PRIMARY, cmd,
995+
self.client._event_listeners)
996+
982997
def current_op(self, include_all=False, session=None):
983998
"""**DEPRECATED**: Get information on operations currently running.
984999
@@ -1015,21 +1030,10 @@ def current_op(self, include_all=False, session=None):
10151030
.. _$currentOp aggregation pipeline stage: https://docs.mongodb.com/manual/reference/operator/aggregation/currentOp/
10161031
.. _currentOp command: https://docs.mongodb.com/manual/reference/command/currentOp/
10171032
"""
1018-
warnings.warn("current_op() is deprecated. See the documentation for"
1033+
warnings.warn("current_op() is deprecated. See the documentation for "
10191034
"more information",
10201035
DeprecationWarning, stacklevel=2)
1021-
cmd = SON([("currentOp", 1), ("$all", include_all)])
1022-
with self.__client._socket_for_writes(session) as sock_info:
1023-
if sock_info.max_wire_version >= 4:
1024-
return self.__client.admin._command(
1025-
sock_info, cmd, codec_options=self.codec_options,
1026-
session=session)
1027-
else:
1028-
spec = {"$all": True} if include_all else {}
1029-
return _first_batch(sock_info, "admin", "$cmd.sys.inprog",
1030-
spec, -1, True, self.codec_options,
1031-
ReadPreference.PRIMARY, cmd,
1032-
self.client._event_listeners)
1036+
return self._current_op(include_all, session)
10331037

10341038
def profiling_level(self, session=None):
10351039
"""Get the database's current profiling level.

pymongo/mongo_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ def is_locked(self):
20652065
are blocked, although read operations may still be allowed.
20662066
Use :meth:`unlock` to unlock.
20672067
"""
2068-
ops = self._database_default_options('admin').current_op()
2068+
ops = self._database_default_options('admin')._current_op()
20692069
return bool(ops.get('fsyncLock', 0))
20702070

20712071
def fsync(self, **kwargs):

test/test_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,14 @@ def test_fsync_lock_unlock(self):
10641064
time.sleep(1)
10651065
self.assertFalse(locked)
10661066

1067+
def test_is_locked_does_not_raise_warning(self):
1068+
client = rs_or_single_client()
1069+
with warnings.catch_warnings(record=True) as ctx:
1070+
warnings.simplefilter("always")
1071+
_ = client.is_locked
1072+
self.assertFalse(
1073+
any(issubclass(w.category, DeprecationWarning) for w in ctx))
1074+
10671075
def test_contextlib(self):
10681076
client = rs_or_single_client()
10691077
client.pymongo_test.drop_collection("test")

0 commit comments

Comments
 (0)