From 953cef01205b20989bb98776dbb3fe16e45179ef Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Wed, 11 Nov 2026 11:48:34 +0000 Subject: [PATCH 1/2] Fix copy.deepcopy for wrapped classes. --- doc/source/changelog.rst | 4 ++++ src/sire/_pythonize.py | 28 ++++++++++++++++++++++++++++ tests/base/test_deepcopy.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 tests/base/test_deepcopy.py 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..6f375cb6d --- /dev/null +++ b/tests/base/test_deepcopy.py @@ -0,0 +1,30 @@ +import copy + +import pytest +import sire as sr + + +@pytest.mark.parametrize( + "obj", + [ + sr.u("1 ps"), + sr.legacy.Mol.AtomIdx(3), + sr.cas.LambdaSchedule.standard_morph(), + ], +) +def test_deepcopy(obj): + 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(): + d = {"time": sr.u("2 ps"), "atoms": [sr.legacy.Mol.AtomIdx(1)]} + assert copy.deepcopy(d) == d From f53d2c812d5d163d1f8433f71b08584c88107ac1 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Wed, 11 Nov 2026 13:08:33 +0000 Subject: [PATCH 2/2] Create objects inside the deepcopy tests to avoid activating the API at collection. --- tests/base/test_deepcopy.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/base/test_deepcopy.py b/tests/base/test_deepcopy.py index 6f375cb6d..fc9be8d07 100644 --- a/tests/base/test_deepcopy.py +++ b/tests/base/test_deepcopy.py @@ -1,18 +1,20 @@ import copy import pytest -import sire as sr @pytest.mark.parametrize( - "obj", + "create", [ - sr.u("1 ps"), - sr.legacy.Mol.AtomIdx(3), - sr.cas.LambdaSchedule.standard_morph(), + lambda sr: sr.u("1 ps"), + lambda sr: sr.legacy.Mol.AtomIdx(3), + lambda sr: sr.cas.LambdaSchedule.standard_morph(), ], ) -def test_deepcopy(obj): +def test_deepcopy(create): + import sire as sr + + obj = create(sr) other = copy.deepcopy(obj) assert other == obj assert other is not obj @@ -26,5 +28,7 @@ def test_deepcopy_openmm(): 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