Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ organisation on `GitHub <https://github.com/openbiosim/sire>`__.
* 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 <https://github.com/openbiosim/sire/compare/2026.1.0...2026.2.0>`__ - September 2026
----------------------------------------------------------------------------------------------

Expand Down
28 changes: 28 additions & 0 deletions src/sire/_pythonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/sire/system/_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
43 changes: 43 additions & 0 deletions tests/base/test_deepcopy.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tests/system/test_time.py
Original file line number Diff line number Diff line change
@@ -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