Skip to content

Commit 30523d2

Browse files
authored
PYTHON-2415 Fix pickle support for BulkWriteError exceptions (#514)
1 parent f1f8cad commit 30523d2

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pymongo/errors.py

+2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ class BulkWriteError(OperationFailure):
240240
def __init__(self, results):
241241
super(BulkWriteError, self).__init__(
242242
"batch op errors occurred", 65, results)
243+
# For pickle support
244+
self.args = (results,)
243245

244246

245247
class InvalidOperation(PyMongoError):

test/test_errors.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pickle
1516
import sys
1617
import traceback
1718

1819
sys.path[0:0] = [""]
1920

20-
from pymongo.errors import (NotMasterError,
21+
from pymongo.errors import (BulkWriteError,
22+
EncryptionError,
23+
NotMasterError,
2124
OperationFailure)
2225
from test import (PyMongoTestCase,
2326
unittest)
@@ -67,6 +70,37 @@ def test_unicode_strs_not_master_error(self):
6770
{"errmsg": u'unicode \U0001f40d'})
6871
self._test_unicode_strs(exc)
6972

73+
def assertPyMongoErrorEqual(self, exc1, exc2):
74+
self.assertEqual(exc1._message, exc2._message)
75+
self.assertEqual(exc1._error_labels, exc2._error_labels)
76+
self.assertEqual(exc1.args, exc2.args)
77+
self.assertEqual(str(exc1), str(exc2))
78+
79+
def assertOperationFailureEqual(self, exc1, exc2):
80+
self.assertPyMongoErrorEqual(exc1, exc2)
81+
self.assertEqual(exc1.code, exc2.code)
82+
self.assertEqual(exc1.details, exc2.details)
83+
self.assertEqual(exc1._max_wire_version, exc2._max_wire_version)
84+
85+
def test_pickle_NotMasterError(self):
86+
exc = NotMasterError("not master test", {"errmsg": "error"})
87+
self.assertPyMongoErrorEqual(exc, pickle.loads(pickle.dumps(exc)))
88+
89+
def test_pickle_OperationFailure(self):
90+
exc = OperationFailure('error', code=5, details={}, max_wire_version=7)
91+
self.assertOperationFailureEqual(exc, pickle.loads(pickle.dumps(exc)))
92+
93+
def test_pickle_BulkWriteError(self):
94+
exc = BulkWriteError({})
95+
self.assertOperationFailureEqual(exc, pickle.loads(pickle.dumps(exc)))
96+
97+
def test_pickle_EncryptionError(self):
98+
cause = OperationFailure('error', code=5, details={},
99+
max_wire_version=7)
100+
exc = EncryptionError(cause)
101+
exc2 = pickle.loads(pickle.dumps(exc))
102+
self.assertPyMongoErrorEqual(exc, exc2)
103+
self.assertOperationFailureEqual(cause, exc2.cause)
70104

71105

72106
if __name__ == "__main__":

0 commit comments

Comments
 (0)