From f6459c7f6b9cb8dbcbbf845f79f8cade65f378b7 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Sat, 14 Nov 2026 15:13:28 +0000 Subject: [PATCH] Fix deepcopy for wrapped classes and setting a zero time. --- doc/source/changelog.rst | 7 ++++++ src/sire/_pythonize.py | 28 ++++++++++++++++++++++++ src/sire/system/_system.py | 3 ++- tests/base/test_deepcopy.py | 43 +++++++++++++++++++++++++++++++++++++ tests/system/test_time.py | 12 +++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/base/test_deepcopy.py create mode 100644 tests/system/test_time.py diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 1289c9bdc..fd3cfef2f 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -204,6 +204,13 @@ 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. + +* Fixed ``System.set_time`` rejecting a zero time returned by ``System.time``, since a zero + ``GeneralUnit`` has no units. + `2026.2.0 `__ - September 2026 ---------------------------------------------------------------------------------------------- diff --git a/src/sire/_pythonize.py b/src/sire/_pythonize.py index 44a98b191..a0668acd2 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=None): + """ + 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/src/sire/system/_system.py b/src/sire/system/_system.py index 232e285bd..395e7021a 100644 --- a/src/sire/system/_system.py +++ b/src/sire/system/_system.py @@ -836,7 +836,8 @@ def set_time(self, time, map=None): from ..units import picosecond from ..base import wrap - if time == 0: + # A zero time loses its units, e.g. when round-tripping System.time(). + if time == 0 or (hasattr(time, "value") and time.value() == 0): time = wrap(0 * picosecond) else: if not hasattr(time, "has_same_units"): diff --git a/tests/base/test_deepcopy.py b/tests/base/test_deepcopy.py new file mode 100644 index 000000000..8c137c12e --- /dev/null +++ b/tests/base/test_deepcopy.py @@ -0,0 +1,43 @@ +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_no_memo(): + import sire as sr + + obj = sr.u("1 ps") + other = obj.__deepcopy__() + 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 diff --git a/tests/system/test_time.py b/tests/system/test_time.py new file mode 100644 index 000000000..f59a1a05d --- /dev/null +++ b/tests/system/test_time.py @@ -0,0 +1,12 @@ +def test_set_zero_time(ala_mols): + import sire as sr + + mols = ala_mols.clone() + + mols.set_time(0 * sr.units.picosecond) + mols.set_time(mols.time()) + assert mols.time().value() == 0 + + mols.set_time(sr.u("5 ps")) + mols.set_time(mols.time()) + assert mols.time().to(sr.units.picosecond) == 5