diff --git a/Framework/API/src/MatrixWorkspace.cpp b/Framework/API/src/MatrixWorkspace.cpp index 9e0a2d7917dc9f0237286363bc36947d5888ff7c..053fde6811ccb6bc6ca7c958729fa6ec6aebb322 100644 --- a/Framework/API/src/MatrixWorkspace.cpp +++ b/Framework/API/src/MatrixWorkspace.cpp @@ -1135,6 +1135,11 @@ MatrixWorkspace::maskedBins(const size_t &workspaceIndex) const { */ void MatrixWorkspace::setMonitorWorkspace( const boost::shared_ptr<MatrixWorkspace> &monitorWS) { + if (monitorWS.get() == this) { + throw std::runtime_error( + "To avoid memory leak, monitor workspace" + " can not be the same workspace as the host workspace"); + } m_monitorWorkspace = monitorWS; } diff --git a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp index f7784bae01b293fab29939503d7380ff7f9eb2ac..4325fcb625287870582479e01dff59d3df1116c6 100644 --- a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp +++ b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp @@ -89,15 +89,16 @@ void setMonitorWorkspace(MatrixWorkspace &self, MatrixWorkspace_sptr monWS = boost::dynamic_pointer_cast<MatrixWorkspace>( Mantid::PythonInterface::ExtractWorkspace(value)()); - if (monWS) { - if (monWS.get() == &self) { - throw std::runtime_error( - "To avoid memory leak, monitor workspace" - " can not be the same workspace as the host workspace"); - } - } self.setMonitorWorkspace(monWS); } +/** +* @param self :: A reference to the calling object +* +*@return weak pointer to monitor workspace used by python +*/ +boost::weak_ptr<MatrixWorkspace> getMonitorWorkspace(MatrixWorkspace &self) { + return boost::weak_ptr<MatrixWorkspace>(self.monitorWorkspace()); +} /** * Clear monitor workspace attached to for current workspace. * @@ -350,7 +351,7 @@ void export_MatrixWorkspace() { "Performs a comparison operation on two workspaces, using the " "CheckWorkspacesMatch algorithm") //--------- monitor workspace -------------------------------------- - .def("getMonitorWorkspace", &MatrixWorkspace::monitorWorkspace, + .def("getMonitorWorkspace", &getMonitorWorkspace, args("self"), "Return internal monitor workspace bound to current workspace.") .def("setMonitorWorkspace", &setMonitorWorkspace, diff --git a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py index a50c062822fa22ef69c858b59ea2dcdeda9bade6..791e14f7d667e4e27e6bf6366fcba09aca63fb57 100644 --- a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py +++ b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py @@ -1,4 +1,4 @@ -import unittest +import unittest import sys import math from testhelpers import create_algorithm, run_algorithm, can_be_instantiated, WorkspaceCreationHelper @@ -349,11 +349,11 @@ class MatrixWorkspaceTest(unittest.TestCase): run_algorithm('CreateWorkspace', OutputWorkspace='ws1',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF') run_algorithm('CreateWorkspace', OutputWorkspace='ws_mon',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF') - ws1=mtd['ws1'] + ws1=AnalysisDataService.retrieve('ws1') monWs = ws1.getMonitorWorkspace() self.assertTrue(monWs is None) - monWs = mtd['ws_mon'] + monWs = AnalysisDataService.retrieve('ws_mon') ws1.setMonitorWorkspace(monWs) monWs.setTitle("My Fake Monitor workspace") @@ -362,13 +362,21 @@ class MatrixWorkspaceTest(unittest.TestCase): ws1.clearMonitorWorkspace() monWs1 = ws1.getMonitorWorkspace() - self.assertTrue(monWs1 is None) + self.assertTrue(monWs1 is None) + + # Check weak pointer issues + ws1.setMonitorWorkspace(monWs) + wms=ws1.getMonitorWorkspace() + allFine = False + try: + ws1.setMonitorWorkspace(wms) + allFine = True + except ValueError: + pass + self.assertTrue(allFine) if __name__ == '__main__': - unittest.main() + #unittest.main() #Testing particular test from Mantid - #class theTester(MatrixWorkspaceTest): - # def runTest(): - # pass - #tester = theTester() - #tester.test_setGetMonitorWS() + tester=MatrixWorkspaceTest('test_setGetMonitorWS') + tester.test_setGetMonitorWS()