Skip to content

Commit 956ce3d

Browse files
committed
Incorporate review changes
1 parent 9fc7ed1 commit 956ce3d

File tree

9 files changed

+29
-24
lines changed

9 files changed

+29
-24
lines changed

pymongo/change_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ def try_next(self):
302302
self._resume()
303303
change = self._cursor._try_next(False)
304304
except OperationFailure as exc:
305-
is_resumable = ((exc.max_wire_version >= 9 and
305+
is_resumable = ((exc._max_wire_version >= 9 and
306306
exc.has_error_label("ResumableChangeStreamError")) or
307-
(exc.max_wire_version < 9 and
307+
(exc._max_wire_version < 9 and
308308
exc.code in _RESUMABLE_GETMORE_ERRORS))
309309
if not is_resumable:
310310
raise

pymongo/errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ def __init__(self, error, code=None, details=None, max_wire_version=None):
156156
self.__details = details
157157
self.__max_wire_version = max_wire_version
158158

159+
@property
160+
def _max_wire_version(self):
161+
return self.__max_wire_version
162+
159163
@property
160164
def code(self):
161165
"""The error code returned by the server, if any.
@@ -189,6 +193,7 @@ def __str__(self):
189193
return output_str.encode('utf-8', errors='replace')
190194
return output_str
191195

196+
192197
class CursorNotFound(OperationFailure):
193198
"""Raised while iterating query results if the cursor is
194199
invalidated on the server.

pymongo/helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ def _index_document(index_list):
102102
return index
103103

104104

105-
def _check_command_response(response, msg=None, allowable_errors=None,
106-
parse_write_concern_error=False,
107-
max_wire_version=None):
105+
def _check_command_response(response, max_wire_version, msg=None,
106+
allowable_errors=None,
107+
parse_write_concern_error=False):
108108
"""Check the response to a command for errors.
109109
"""
110110
if "ok" not in response:
@@ -168,10 +168,10 @@ def _check_command_response(response, msg=None, allowable_errors=None,
168168
max_wire_version)
169169

170170

171-
def _check_gle_response(result):
171+
def _check_gle_response(result, max_wire_version):
172172
"""Return getlasterror response as a dict, or raise OperationFailure."""
173173
# Did getlasterror itself fail?
174-
_check_command_response(result)
174+
_check_command_response(result, max_wire_version)
175175

176176
if result.get("wtimeout", False):
177177
# MongoDB versions before 1.8.0 return the error message in an "errmsg"

pymongo/network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
_UNPACK_HEADER = struct.Struct("<iiii").unpack
4141

4242

43-
def command(sock_info, dbname, spec, slave_ok, is_mongos,
43+
def command(sock_info, max_wire_version, dbname, spec, slave_ok, is_mongos,
4444
read_preference, codec_options, session, client, check=True,
4545
allowable_errors=None, address=None,
4646
check_keys=False, listeners=None, max_bson_size=None,
@@ -157,7 +157,7 @@ def command(sock_info, dbname, spec, slave_ok, is_mongos,
157157
client._process_response(response_doc, session)
158158
if check:
159159
helpers._check_command_response(
160-
response_doc, None, allowable_errors,
160+
response_doc, max_wire_version, None, allowable_errors,
161161
parse_write_concern_error=parse_write_concern_error)
162162
except Exception as exc:
163163
if publish:

pymongo/pool.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,11 @@ def command(self, dbname, spec, slave_ok=False,
680680
if self.op_msg_enabled:
681681
self._raise_if_not_writable(unacknowledged)
682682
try:
683-
return command(self, dbname, spec, slave_ok,
684-
self.is_mongos, read_preference, codec_options,
685-
session, client, check, allowable_errors,
686-
self.address, check_keys, listeners,
687-
self.max_bson_size, read_concern,
683+
return command(self, self.max_wire_version, dbname, spec,
684+
slave_ok, self.is_mongos, read_preference,
685+
codec_options, session, client, check,
686+
allowable_errors, self.address, check_keys,
687+
listeners, self.max_bson_size, read_concern,
688688
parse_write_concern_error=parse_write_concern_error,
689689
collation=collation,
690690
compression_ctx=self.compression_context,
@@ -751,7 +751,8 @@ def legacy_write(self, request_id, msg, max_doc_size, with_last_error):
751751
self.send_message(msg, max_doc_size)
752752
if with_last_error:
753753
reply = self.receive_message(request_id)
754-
return helpers._check_gle_response(reply.command_response())
754+
return helpers._check_gle_response(reply.command_response(),
755+
self.max_wire_version)
755756

756757
def write_command(self, request_id, msg):
757758
"""Send "insert" etc. command, returning response as a dict.
@@ -767,7 +768,7 @@ def write_command(self, request_id, msg):
767768
result = reply.command_response()
768769

769770
# Raises NotMasterError or OperationFailure.
770-
helpers._check_command_response(result)
771+
helpers._check_command_response(result, self.max_wire_version)
771772
return result
772773

773774
def check_auth(self, all_credentials):

pymongo/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def run_operation_with_response(
134134
operation.client._process_response(
135135
first, operation.session)
136136
_check_command_response(
137-
first, max_wire_version=sock_info.max_wire_version)
137+
first, sock_info.max_wire_version)
138138
except Exception as exc:
139139
if publish:
140140
duration = datetime.now() - start

test/test_change_stream.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from bson.raw_bson import DEFAULT_RAW_BSON_OPTIONS, RawBSONDocument
3838

3939
from pymongo import MongoClient
40-
# from pymongo.change_stream import _NON_RESUMABLE_GETMORE_ERRORS
4140
from pymongo.command_cursor import CommandCursor
4241
from pymongo.errors import (InvalidOperation, OperationFailure,
4342
ServerSelectionTimeoutError)

test/test_database.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,10 @@ def test_command_response_without_ok(self):
954954
# command document will have no 'ok' field. We should raise
955955
# OperationFailure instead of KeyError.
956956
self.assertRaises(OperationFailure,
957-
helpers._check_command_response, {})
957+
helpers._check_command_response, {}, None)
958958

959959
try:
960-
helpers._check_command_response({'$err': 'foo'})
960+
helpers._check_command_response({'$err': 'foo'}, None)
961961
except OperationFailure as e:
962962
self.assertEqual(e.args[0], 'foo')
963963
else:
@@ -970,7 +970,7 @@ def test_mongos_response(self):
970970
'raw': {'shard0/host0,host1': {'ok': 0, 'errmsg': 'inner'}}}
971971

972972
with self.assertRaises(OperationFailure) as context:
973-
helpers._check_command_response(error_document)
973+
helpers._check_command_response(error_document, None)
974974

975975
self.assertIn('inner', str(context.exception))
976976

@@ -983,7 +983,7 @@ def test_mongos_response(self):
983983
'raw': {'shard0/host0,host1': {}}}
984984

985985
with self.assertRaises(OperationFailure) as context:
986-
helpers._check_command_response(error_document)
986+
helpers._check_command_response(error_document, None)
987987

988988
self.assertIn('outer', str(context.exception))
989989

@@ -994,7 +994,7 @@ def test_mongos_response(self):
994994
'raw': {'shard0/host0,host1': {'ok': 0}}}
995995

996996
with self.assertRaises(OperationFailure) as context:
997-
helpers._check_command_response(error_document)
997+
helpers._check_command_response(error_document, None)
998998

999999
self.assertIn('outer', str(context.exception))
10001000

test/test_discovery_and_monitoring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def got_app_error(topology, app_error):
110110
# Pool/SocketInfo.
111111
try:
112112
if error_type == 'command':
113-
_check_command_response(app_error['response'])
113+
_check_command_response(app_error['response'], max_wire_version)
114114
elif error_type == 'network':
115115
raise AutoReconnect('mock non-timeout network error')
116116
elif error_type == 'timeout':

0 commit comments

Comments
 (0)