Skip to content
Snippets Groups Projects
Commit a8e8bbd3 authored by Alex Buts's avatar Alex Buts
Browse files

Re #14022 export weak_ptr to python and move self-assignment test

parent 986b1d78
No related merge requests found
...@@ -1135,6 +1135,11 @@ MatrixWorkspace::maskedBins(const size_t &workspaceIndex) const { ...@@ -1135,6 +1135,11 @@ MatrixWorkspace::maskedBins(const size_t &workspaceIndex) const {
*/ */
void MatrixWorkspace::setMonitorWorkspace( void MatrixWorkspace::setMonitorWorkspace(
const boost::shared_ptr<MatrixWorkspace> &monitorWS) { 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; m_monitorWorkspace = monitorWS;
} }
......
...@@ -89,15 +89,16 @@ void setMonitorWorkspace(MatrixWorkspace &self, ...@@ -89,15 +89,16 @@ void setMonitorWorkspace(MatrixWorkspace &self,
MatrixWorkspace_sptr monWS = boost::dynamic_pointer_cast<MatrixWorkspace>( MatrixWorkspace_sptr monWS = boost::dynamic_pointer_cast<MatrixWorkspace>(
Mantid::PythonInterface::ExtractWorkspace(value)()); 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); 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. * Clear monitor workspace attached to for current workspace.
* *
...@@ -350,7 +351,7 @@ void export_MatrixWorkspace() { ...@@ -350,7 +351,7 @@ void export_MatrixWorkspace() {
"Performs a comparison operation on two workspaces, using the " "Performs a comparison operation on two workspaces, using the "
"CheckWorkspacesMatch algorithm") "CheckWorkspacesMatch algorithm")
//--------- monitor workspace -------------------------------------- //--------- monitor workspace --------------------------------------
.def("getMonitorWorkspace", &MatrixWorkspace::monitorWorkspace, .def("getMonitorWorkspace", &getMonitorWorkspace,
args("self"), args("self"),
"Return internal monitor workspace bound to current workspace.") "Return internal monitor workspace bound to current workspace.")
.def("setMonitorWorkspace", &setMonitorWorkspace, .def("setMonitorWorkspace", &setMonitorWorkspace,
......
import unittest import unittest
import sys import sys
import math import math
from testhelpers import create_algorithm, run_algorithm, can_be_instantiated, WorkspaceCreationHelper from testhelpers import create_algorithm, run_algorithm, can_be_instantiated, WorkspaceCreationHelper
...@@ -349,11 +349,11 @@ class MatrixWorkspaceTest(unittest.TestCase): ...@@ -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='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') 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() monWs = ws1.getMonitorWorkspace()
self.assertTrue(monWs is None) self.assertTrue(monWs is None)
monWs = mtd['ws_mon'] monWs = AnalysisDataService.retrieve('ws_mon')
ws1.setMonitorWorkspace(monWs) ws1.setMonitorWorkspace(monWs)
monWs.setTitle("My Fake Monitor workspace") monWs.setTitle("My Fake Monitor workspace")
...@@ -362,13 +362,21 @@ class MatrixWorkspaceTest(unittest.TestCase): ...@@ -362,13 +362,21 @@ class MatrixWorkspaceTest(unittest.TestCase):
ws1.clearMonitorWorkspace() ws1.clearMonitorWorkspace()
monWs1 = ws1.getMonitorWorkspace() 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__': if __name__ == '__main__':
unittest.main() #unittest.main()
#Testing particular test from Mantid #Testing particular test from Mantid
#class theTester(MatrixWorkspaceTest): tester=MatrixWorkspaceTest('test_setGetMonitorWS')
# def runTest(): tester.test_setGetMonitorWS()
# pass
#tester = theTester()
#tester.test_setGetMonitorWS()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment