diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 1289c9bdc..e5e233b6f 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -204,6 +204,10 @@ organisation on `GitHub `__. * Fixed angle, dihedral and improper properties being dropped from the connectivity when the molecule was edited without changing the order of its atoms, e.g. renaming an atom. +* Fixed ``copy.deepcopy`` failing for wrapped classes, e.g. ``GeneralUnit``, since the + generated wrappers bound ``__deepcopy__`` to the one-argument ``__copy__``, while + ``copy.deepcopy`` also passes a memo dictionary. + `2026.2.0 `__ - September 2026 ---------------------------------------------------------------------------------------------- diff --git a/src/sire/_pythonize.py b/src/sire/_pythonize.py index 44a98b191..7c134feb4 100644 --- a/src/sire/_pythonize.py +++ b/src/sire/_pythonize.py @@ -23,6 +23,27 @@ def _upper_split(data): yield buff +def _deepcopy(self, memo): + """ + Deep copy a wrapped object, which is the same as a copy since these + objects copy by value. + """ + return self.__copy__() + + +def _fix_deepcopy(C) -> None: + """ + Make copy.deepcopy work for a wrapped class. The generated wrappers bind + __deepcopy__ to the one-argument __copy__, but copy.deepcopy also passes + a memo dictionary. + """ + if hasattr(C, "__copy__"): + try: + C.__deepcopy__ = _deepcopy + except Exception: + pass + + def _pythonize(C, delete_old: bool = True) -> None: """Pythonize the passed class. This will rename the functions so that they better match the Python style @@ -53,6 +74,8 @@ def _pythonize(C, delete_old: bool = True) -> None: _pythonize(CLS) return + _fix_deepcopy(C) + import re for attr in dir(C): @@ -252,6 +275,11 @@ def _load_new_api_modules(delete_old: bool = True, is_base: bool = False): _pythonize(Convert._SireOpenMM.TorchQMEngine, delete_old=delete_old) _pythonize(Convert._SireOpenMM.TorchQMForce, delete_old=delete_old) + import inspect + + for _, cls in inspect.getmembers(Convert._SireOpenMM, inspect.isclass): + _fix_deepcopy(cls) + from ._lazy_import import force_load # Now make sure that all new modules have been loaded diff --git a/tests/base/test_deepcopy.py b/tests/base/test_deepcopy.py new file mode 100644 index 000000000..fc9be8d07 --- /dev/null +++ b/tests/base/test_deepcopy.py @@ -0,0 +1,34 @@ +import copy + +import pytest + + +@pytest.mark.parametrize( + "create", + [ + lambda sr: sr.u("1 ps"), + lambda sr: sr.legacy.Mol.AtomIdx(3), + lambda sr: sr.cas.LambdaSchedule.standard_morph(), + ], +) +def test_deepcopy(create): + import sire as sr + + obj = create(sr) + other = copy.deepcopy(obj) + assert other == obj + assert other is not obj + + +def test_deepcopy_openmm(): + from sire.legacy.Convert import _SireOpenMM + + lever = _SireOpenMM.LambdaLever() + assert copy.deepcopy(lever) is not lever + + +def test_deepcopy_container(): + import sire as sr + + d = {"time": sr.u("2 ps"), "atoms": [sr.legacy.Mol.AtomIdx(1)]} + assert copy.deepcopy(d) == d