Fix deepcopy on EmbeddedDocument

This commit is contained in:
Timothé Perez 2021-03-23 22:11:41 +01:00 committed by Bastien Gerard
parent 3b10236b5e
commit 88642eb021
4 changed files with 20 additions and 0 deletions

View File

@ -260,3 +260,4 @@ that much better:
* Stankiewicz Mateusz (https://github.com/mas15)
* Felix Schultheiß (https://github.com/felix-smashdocs)
* Jan Stein (https://github.com/janste63)
* Timothé Perez (https://github.com/AchilleAsh)

View File

@ -13,6 +13,7 @@ Changes in 0.23.1
===========
- Bug fix: ignore LazyReferenceFields when clearing _changed_fields #2484
- Improve connection doc #2481
- Fix deepcopy on EmbeddedDocument
Changes in 0.23.0
=================

View File

@ -99,6 +99,15 @@ class EmbeddedDocument(BaseDocument, metaclass=DocumentMetaclass):
def __ne__(self, other):
return not self.__eq__(other)
def __getstate__(self):
data = super().__getstate__()
data["_instance"] = self._instance
return data
def __setstate__(self, state):
super().__setstate__(state)
self._instance = state["_instance"]
def to_mongo(self, *args, **kwargs):
data = super().to_mongo(*args, **kwargs)

View File

@ -3,6 +3,7 @@ import pickle
import unittest
import uuid
import weakref
from copy import deepcopy
from datetime import datetime
import bson
@ -78,6 +79,14 @@ class TestDocumentInstance(MongoDBTestCase):
else:
assert field._instance == instance
def test_deepcopy(self):
"""Ensure that the _instance attribute on EmbeddedDocument exists after a deepcopy"""
doc = self.Job()
assert doc._instance is None
copied = deepcopy(doc)
assert copied._instance is None
def test_capped_collection(self):
"""Ensure that capped collections work properly."""