|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Ludovic Henry <git@ludovic.dev> |
| 3 | +Date: Thu, 01 Oct 2026 00:00:00 +0000 |
| 4 | +Subject: [PATCH] molecules, io/lammpsdata: replace ndarray.resize for Python 3.14 |
| 5 | + |
| 6 | +From NumPy 2.4 on, ndarray.resize(refcheck=True) on Python >= 3.14 uses |
| 7 | +PyUnstable_Object_IsUniquelyReferenced, which an attribute-held array |
| 8 | +never satisfies, so Molecules.__init__ and LAMMPSData's setter/reader |
| 9 | +raise "ValueError: cannot resize an array that may be referenced by |
| 10 | +another object" on cp314 only (test_newmb.py, test_neighbours.py, |
| 11 | +test_io.py collection errors, test_manybody_molecules.py setup errors). |
| 12 | +Upstream replaced every such resize with a fresh np.empty of the same |
| 13 | +dtype in libAtoms/matscipy#312 (commits 1b006eb4 and 3a1ec1dc, released |
| 14 | +in 1.3.0); this is those two hunks unchanged. |
| 15 | + |
| 16 | +Upstream-Status: Backport [https://github.com/libAtoms/matscipy/pull/312] |
| 17 | + |
| 18 | +Signed-off-by: Ludovic Henry <git@ludovic.dev> |
| 19 | +--- |
| 20 | +diff --git a/matscipy/io/lammpsdata.py b/matscipy/io/lammpsdata.py |
| 21 | +index 4525a786..e7200f0a 100644 |
| 22 | +--- a/matscipy/io/lammpsdata.py |
| 23 | ++++ b/matscipy/io/lammpsdata.py |
| 24 | +@@ -193,10 +193,15 @@ class LAMMPSData: |
| 25 | + """Set data component.""" |
| 26 | + if name in self._type_names: |
| 27 | + name = self._type_names[name] |
| 28 | +- self.__data[name].resize(len(value)) |
| 29 | ++ old = self.__data[name] |
| 30 | ++ new_arr = np.empty(len(value), dtype=self._dtypes[name]) |
| 31 | ++ n = min(len(old), len(value)) |
| 32 | ++ if n > 0: |
| 33 | ++ new_arr[:n] = old[:n] |
| 34 | ++ self.__data[name] = new_arr |
| 35 | + self.__data[name]['type'] = value |
| 36 | + elif name in self._data_names: |
| 37 | +- self.__data[name].resize(len(value)) |
| 38 | ++ self.__data[name] = np.empty(len(value), dtype=self._dtypes[name]) |
| 39 | + data = self.__data[name] |
| 40 | + try: |
| 41 | + data[data.dtype.names[-1]] = np.array(value) |
| 42 | +@@ -309,16 +314,15 @@ class LAMMPSData: |
| 43 | + for linum, line in enumerate(fd): |
| 44 | + if 'Masses' in line: |
| 45 | + ntypes = type_counts['atom types'] |
| 46 | +- self['masses'].resize(ntypes) |
| 47 | +- self['masses'][:] = \ |
| 48 | ++ self.__data['masses'] = \ |
| 49 | + np.genfromtxt(fd, skip_header=1, |
| 50 | +- max_rows=ntypes, usecols=(1,)) |
| 51 | ++ max_rows=ntypes, usecols=(1,), dtype=self._dtypes['masses']) |
| 52 | + |
| 53 | + else: |
| 54 | + for label in self._data_names: |
| 55 | + if self.__headers[label] in line: |
| 56 | + nlines = data_counts[label] |
| 57 | +- self[label].resize(nlines) |
| 58 | ++ self.__data[label] = np.empty(nlines, dtype=self._dtypes[label]) |
| 59 | + dtype = self[label].dtype |
| 60 | + |
| 61 | + raw_dtype = np.dtype([('num', np.int32)] + [ |
| 62 | +diff --git a/matscipy/molecules.py b/matscipy/molecules.py |
| 63 | +index 4657da62..d9a88996 100644 |
| 64 | +--- a/matscipy/molecules.py |
| 65 | ++++ b/matscipy/molecules.py |
| 66 | +@@ -72,19 +72,19 @@ class Molecules: |
| 67 | + self.__dict__[data] = np.array([], dtype=dtype) |
| 68 | + |
| 69 | + if bonds_connectivity is not None: |
| 70 | +- self.bonds.resize(len(bonds_connectivity)) |
| 71 | ++ self.bonds = np.empty(len(bonds_connectivity), dtype=self._dtypes["bonds"]) |
| 72 | + self.bonds["atoms"][:] = bonds_connectivity |
| 73 | + self.bonds["type"][:] = bonds_types \ |
| 74 | + if bonds_types is not None else default_type |
| 75 | + |
| 76 | + if angles_connectivity is not None: |
| 77 | +- self.angles.resize(len(angles_connectivity)) |
| 78 | ++ self.angles = np.empty(len(angles_connectivity), dtype=self._dtypes["angles"]) |
| 79 | + self.angles["atoms"][:] = angles_connectivity |
| 80 | + self.angles["type"][:] = angles_types \ |
| 81 | + if angles_types is not None else default_type |
| 82 | + |
| 83 | + if dihedrals_connectivity is not None: |
| 84 | +- self.dihedrals.resize(len(dihedrals_connectivity)) |
| 85 | ++ self.dihedrals = np.empty(len(dihedrals_connectivity), dtype=self._dtypes["dihedrals"]) |
| 86 | + self.dihedrals["atoms"][:] = dihedrals_connectivity |
| 87 | + self.dihedrals["type"][:] = dihedrals_types \ |
| 88 | + if dihedrals_types is not None else default_type |
0 commit comments