Projet

Général

Profil

0001-misc-add-dedicated-methods-for-copy-and-deepcopy-240.patch

Frédéric Péters, 23 mai 2018 17:42

Télécharger (1,65 ko)

Voir les différences:

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(+)
wcs/formdef.py
1328 1328
            if changed:
1329 1329
                formdef.store()
1330 1330

  
1331
    class _EmptyClass(object):  # helper for instance creation without calling __init__
1332
        pass
1333

  
1334
    def __copy__(self, memo=None, deepcopy=False):
1335
        formdef_copy = self._EmptyClass()
1336
        formdef_copy.__class__ = self.__class__
1337
        if deepcopy:
1338
            formdef_copy.__dict__ = copy.deepcopy(self.__dict__, memo=memo)
1339
        else:
1340
            formdef_copy.__dict__ = copy.copy(self.__dict__)
1341
        return formdef_copy
1342

  
1343
    def __deepcopy__(self, memo=None):
1344
        return self.__copy__(memo=memo, deepcopy=True)
1345

  
1331 1346
    # don't pickle computed attributes
1332 1347
    def __getstate__(self):
1333 1348
        odict = copy.copy(self.__dict__)
1334
-