Skip to content

Commit

Permalink
fix a lifetime bug Andrew Dalke identified (#7408)
Browse files Browse the repository at this point in the history
  • Loading branch information
greglandrum committed May 9, 2024
1 parent 18d1ff5 commit a64ab4e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ void wrapGenerator(const std::string &nm) {
"generator\n\n"
" RETURNS: an information string\n\n")
.def("GetOptions", getOptions<T>,
python::return_value_policy<python::reference_existing_object>(),
python::return_internal_reference<
1, python::with_custodian_and_ward_postcall<0, 1>>(),
python::args("self"), "return the fingerprint options object");
}

Expand Down
65 changes: 37 additions & 28 deletions Code/GraphMol/Fingerprints/Wrap/testGenerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,44 +343,53 @@ def testTopologicalTorsionShortestPaths(self):
self.assertEqual(len(nz), 1)

def testMorganGeneratorMultiMol(self):
smis = ['CC1CCC1','CCC1CCC1','CCCC1CCC1','CC1CC(O)C1','CC1CC(OC)C1',]
smis = [
'CC1CCC1',
'CCC1CCC1',
'CCCC1CCC1',
'CC1CC(O)C1',
'CC1CC(OC)C1',
]
for i in range(4):
smis = smis + smis
ms = [Chem.MolFromSmiles(smi) for smi in smis]
g = rdFingerprintGenerator.GetMorganGenerator()
ofps = tuple([g.GetFingerprint(m) for m in ms])
tfps = g.GetFingerprints(ms,numThreads=1)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)
tfps = g.GetFingerprints(ms,numThreads=4)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)
tfps = g.GetFingerprints(ms, numThreads=1)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)
tfps = g.GetFingerprints(ms, numThreads=4)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)

ofps = tuple([g.GetCountFingerprint(m) for m in ms])
tfps = g.GetCountFingerprints(ms,numThreads=1)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)
tfps = g.GetCountFingerprints(ms,numThreads=4)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)
tfps = g.GetCountFingerprints(ms, numThreads=1)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)
tfps = g.GetCountFingerprints(ms, numThreads=4)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)

ofps = tuple([g.GetSparseFingerprint(m) for m in ms])
tfps = g.GetSparseFingerprints(ms,numThreads=1)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)
tfps = g.GetSparseFingerprints(ms,numThreads=4)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)
tfps = g.GetSparseFingerprints(ms, numThreads=1)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)
tfps = g.GetSparseFingerprints(ms, numThreads=4)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)

ofps = tuple([g.GetSparseCountFingerprint(m) for m in ms])
tfps = g.GetSparseCountFingerprints(ms,numThreads=1)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)
tfps = g.GetSparseCountFingerprints(ms,numThreads=4)
for ofp,tfp in zip(ofps,tfps):
self.assertEqual(ofp,tfp)


tfps = g.GetSparseCountFingerprints(ms, numThreads=1)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)
tfps = g.GetSparseCountFingerprints(ms, numThreads=4)
for ofp, tfp in zip(ofps, tfps):
self.assertEqual(ofp, tfp)

def testFingerprintGeneratorOptionsLifetime(self):
# this should not result in a seg fault
import inspect
inspect.getmembers(rdFingerprintGenerator.GetRDKitFPGenerator().GetOptions())


if __name__ == '__main__':
Expand Down

0 comments on commit a64ab4e

Please sign in to comment.