diff --git a/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/Unit.cpp b/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/Unit.cpp index bea7feae396e..8223b2a35e54 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/Unit.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/Unit.cpp @@ -18,6 +18,16 @@ namespace PyErr_Warn(PyExc_DeprecationWarning, "'name' is deprecated, use 'caption' instead."); return self.caption(); } + + /** + * @param self A reference to the calling object + * @return A new Python unicode string with the contents of the utf8Label + */ + PyObject * utf8LabelToUnicode(Unit & self) + { + const auto label = self.utf8Label(); + return PyUnicode_FromWideChar(label.c_str(), label.size()); + } } void export_Unit() @@ -28,6 +38,7 @@ void export_Unit() .def("name", &deprecatedName, "Return the full name of the unit (deprecated, use caption)") .def("caption", &Unit::caption, "Return the full name of the unit") .def("label", &Unit::label, "Returns a label to be printed on the axis") + .def("utf8Label", &utf8LabelToUnicode, "Returns a unicode string to be printed on the axis") .def("unitID", &Unit::unitID, "Returns the string ID of the unit. This may/may not match its name") ; diff --git a/Code/Mantid/Framework/PythonInterface/test/python/mantid/kernel/UnitsTest.py b/Code/Mantid/Framework/PythonInterface/test/python/mantid/kernel/UnitsTest.py index c2b72d8743d5..9eee96a17bd1 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/mantid/kernel/UnitsTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/kernel/UnitsTest.py @@ -1,5 +1,6 @@ import unittest from mantid.kernel import UnitFactory, Unit, Label +import types class UnitsTest(unittest.TestCase): @@ -11,5 +12,12 @@ def test_Label_is_returned_from_Factory(self): self.assertEquals("Temperature", label_unit.caption()) self.assertEquals("K", label_unit.label()) + def test_utf8Label_is_converted_to_unicode_object(self): + tof = UnitFactory.Instance().create("TOF") + unit_lbl = tof.utf8Label() + self.assertTrue(isinstance(unit_lbl, types.UnicodeType)) + self.assertEquals(u"\u03bcs", unit_lbl) + + if __name__ == '__main__': unittest.main()