From 2fd2abcc2ad0458cd063f18b604c816b254fb86d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 23 May 2018 17:14:28 +0200 Subject: [PATCH] misc: add dedicated methods for copy and deepcopy (#24033) copy and deepcopy are recycling __getstate__ originally defined for pickling objects; however we have a custom __getstate__ method for methods because we want to save fields in a different pickle chunk. But calls to copy/deepcopy won't restore fields properly and create copies with formdef as None. This could cause all sorts of malfunctions including a not so subtle removal of all fields (if the copied formdef was stored on disk). --- wcs/formdef.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/wcs/formdef.py b/wcs/formdef.py index 32aec981..0e894a9c 100644 --- a/wcs/formdef.py +++ b/wcs/formdef.py @@ -1328,6 +1328,21 @@ class FormDef(StorableObject): if changed: formdef.store() + class _EmptyClass(object): # helper for instance creation without calling __init__ + pass + + def __copy__(self, memo=None, deepcopy=False): + formdef_copy = self._EmptyClass() + formdef_copy.__class__ = self.__class__ + if deepcopy: + formdef_copy.__dict__ = copy.deepcopy(self.__dict__, memo=memo) + else: + formdef_copy.__dict__ = copy.copy(self.__dict__) + return formdef_copy + + def __deepcopy__(self, memo=None): + return self.__copy__(memo=memo, deepcopy=True) + # don't pickle computed attributes def __getstate__(self): odict = copy.copy(self.__dict__) -- 2.17.0