Skip to content

Commit 55de310

Browse files
committed
Be careful with shallow copies of dicts of dicts! (eg. bonds matrices)
When you do a shallow copy of a dictionary of dictionaries, the inner dictionaries have not been copied - they are the original ones. This is important when you have: bonds1[atom1][atom2] = bond then bonds2 = bonds.copy() is NOT a safe copy to manipulate without messing up bonds1. It is NOT a 2-D array, but a dictionary of mutable dictionaries. You need to copy it at least one level deep (see the diff)
1 parent 872d9c2 commit 55de310

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

rmgpy/molecule_draw.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,12 @@ def drawMolecule(molecule, path=None, surface=''):
11391139
molecule.makeHydrogensImplicit()
11401140

11411141
atoms = molecule.atoms[:]
1142-
bonds = molecule.bonds.copy()
1142+
# bonds = molecule.bonds.copy() is too shallow for a dict-of-dicts,
1143+
# so we loop one level deep and copy the inner dicts.
1144+
bonds = dict()
1145+
for atom1,atom2dict in molecule.bonds.iteritems():
1146+
bonds[atom1] = atom2dict.copy()
1147+
11431148
cycles = molecule.getSmallestSetOfSmallestRings()
11441149

11451150
# Special cases: H, H2, anything with one heavy atom

0 commit comments

Comments
 (0)