Skip to content

PYTHON-2415 Support pickle in BulkWriteError #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pymongo/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ class BulkWriteError(OperationFailure):
def __init__(self, results):
super(BulkWriteError, self).__init__(
"batch op errors occurred", 65, results)
# For pickle support
self.args = (results,)


class InvalidOperation(PyMongoError):
Expand Down
36 changes: 35 additions & 1 deletion test/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pickle
import sys
import traceback

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

from pymongo.errors import (NotMasterError,
from pymongo.errors import (BulkWriteError,
EncryptionError,
NotMasterError,
OperationFailure)
from test import (PyMongoTestCase,
unittest)
Expand Down Expand Up @@ -67,6 +70,37 @@ def test_unicode_strs_not_master_error(self):
{"errmsg": u'unicode \U0001f40d'})
self._test_unicode_strs(exc)

def assertPyMongoErrorEqual(self, exc1, exc2):
self.assertEqual(exc1._message, exc2._message)
self.assertEqual(exc1._error_labels, exc2._error_labels)
self.assertEqual(exc1.args, exc2.args)
self.assertEqual(str(exc1), str(exc2))

def assertOperationFailureEqual(self, exc1, exc2):
self.assertPyMongoErrorEqual(exc1, exc2)
self.assertEqual(exc1.code, exc2.code)
self.assertEqual(exc1.details, exc2.details)
self.assertEqual(exc1._max_wire_version, exc2._max_wire_version)

def test_pickle_NotMasterError(self):
exc = NotMasterError("not master test", {"errmsg": "error"})
self.assertPyMongoErrorEqual(exc, pickle.loads(pickle.dumps(exc)))

def test_pickle_OperationFailure(self):
exc = OperationFailure('error', code=5, details={}, max_wire_version=7)
self.assertOperationFailureEqual(exc, pickle.loads(pickle.dumps(exc)))

def test_pickle_BulkWriteError(self):
exc = BulkWriteError({})
self.assertOperationFailureEqual(exc, pickle.loads(pickle.dumps(exc)))

def test_pickle_EncryptionError(self):
cause = OperationFailure('error', code=5, details={},
max_wire_version=7)
exc = EncryptionError(cause)
exc2 = pickle.loads(pickle.dumps(exc))
self.assertPyMongoErrorEqual(exc, exc2)
self.assertOperationFailureEqual(cause, exc2.cause)


if __name__ == "__main__":
Expand Down