Skip to content
Snippets Groups Projects
Unverified Commit d63d4acd authored by WHITFIELDRE email's avatar WHITFIELDRE email Committed by GitHub
Browse files

Merge pull request #21639 from mantidproject/unicode_workspace_name

Unicode workspace name
parents c0ac50d9 f31c372a
No related merge requests found
#ifndef MANTID_PYTHONINERFACE_CONVERTERS_PYOBJECTTOSTRING_H_
#define MANTID_PYTHONINERFACE_CONVERTERS_PYOBJECTTOSTRING_H_
/**
Copyright © 2018 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/System.h"
#include <boost/python/object.hpp>
namespace Mantid {
namespace PythonInterface {
namespace Converters {
/**
* Convert a python object to a string or throw an exception. This will convert
* unicode strings in python2 via utf8.
*/
DLLExport std::string pyObjToStr(const boost::python::object &value);
} // namespace Converters
} // namespace PythonInterface
} // namespace Mantid
#endif /* MANTID_PYTHONINERFACE_CONVERTERS_PYOBJECTTOSTRING_H_ */
......@@ -24,11 +24,13 @@
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/Exception.h"
#include "MantidPythonInterface/kernel/Converters/PyObjectToString.h"
#include "MantidPythonInterface/kernel/WeakPtr.h"
#include <boost/python/class.hpp>
#include <boost/python/list.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/list.hpp>
#include <boost/python/str.hpp>
#include <set>
......@@ -108,9 +110,14 @@ template <typename SvcType, typename SvcPtrType> struct DataServiceExporter {
* @param name The name to assign to this in the service
* @param item A boost.python wrapped SvcHeldType object
*/
static void addItem(SvcType &self, const std::string &name,
static void addItem(SvcType &self, const boost::python::object &name,
const boost::python::object &item) {
self.add(name, extractCppValue(item));
try {
std::string namestr = PythonInterface::Converters::pyObjToStr(name);
self.add(namestr, extractCppValue(item));
} catch (std::invalid_argument &) {
throw std::invalid_argument("Failed to convert name to a string");
}
}
/**
......@@ -120,9 +127,14 @@ template <typename SvcType, typename SvcPtrType> struct DataServiceExporter {
* @param name The name to assign to this in the service
* @param item A boost.python wrapped SvcHeldType object
*/
static void addOrReplaceItem(SvcType &self, const std::string &name,
static void addOrReplaceItem(SvcType &self, const boost::python::object &name,
const boost::python::object &item) {
self.addOrReplace(name, extractCppValue(item));
try {
std::string namestr = PythonInterface::Converters::pyObjToStr(name);
self.addOrReplace(namestr, extractCppValue(item));
} catch (std::invalid_argument &) {
throw std::invalid_argument("Failed to convert name to a string");
}
}
/**
......@@ -155,14 +167,23 @@ template <typename SvcType, typename SvcPtrType> struct DataServiceExporter {
* @return A shared_ptr to the named object. If the name does not exist it
* sets a KeyError error indicator.
*/
static WeakPtr retrieveOrKeyError(SvcType &self, const std::string &name) {
static WeakPtr retrieveOrKeyError(SvcType &self,
const boost::python::object &name) {
using namespace Mantid::Kernel;
std::string namestr;
try {
namestr = PythonInterface::Converters::pyObjToStr(name);
} catch (std::invalid_argument &) {
throw std::invalid_argument("Failed to convert name to a string");
}
SvcPtrType item;
try {
item = self.retrieve(name);
item = self.retrieve(namestr);
} catch (Exception::NotFoundError &) {
// Translate into a Python KeyError
std::string err = "'" + name + "' does not exist.";
std::string err = "'" + namestr + "' does not exist.";
PyErr_SetString(PyExc_KeyError, err.c_str());
throw boost::python::error_already_set();
}
......
......@@ -76,6 +76,7 @@ set ( SRC_FILES
src/Converters/NumpyFunctions.cpp
src/Converters/PyArrayType.cpp
src/Converters/PyObjectToMatrix.cpp
src/Converters/PyObjectToString.cpp
src/Converters/PyObjectToV3D.cpp
src/Converters/PyObjectToVMD.cpp
src/Converters/WrapWithNumpy.cpp
......@@ -103,6 +104,7 @@ set ( INC_FILES
${HEADER_DIR}/kernel/Converters/VectorToNDArray.h
${HEADER_DIR}/kernel/Converters/PyArrayType.h
${HEADER_DIR}/kernel/Converters/PyObjectToMatrix.h
${HEADER_DIR}/kernel/Converters/PyObjectToString.h
${HEADER_DIR}/kernel/Converters/PyObjectToV3D.h
${HEADER_DIR}/kernel/Converters/PyObjectToVMD.h
${HEADER_DIR}/kernel/Converters/PySequenceToVector.h
......
#include "MantidPythonInterface/kernel/Converters/PyObjectToString.h"
#include <boost/python/extract.hpp>
#include <boost/python/str.hpp>
using namespace boost;
namespace Mantid {
namespace PythonInterface {
namespace Converters {
std::string pyObjToStr(const python::object &value) {
python::extract<std::string> extractor(value);
std::string valuestr;
if (extractor.check()) {
valuestr = extractor();
#if PY_VERSION_HEX < 0x03000000
} else if (PyUnicode_Check(value.ptr())) {
valuestr =
python::extract<std::string>(python::str(value).encode("utf-8"))();
#endif
} else {
throw std::invalid_argument("Failed to convert python object a string");
}
return valuestr;
}
} // namespace Converters
} // namespace PythonInterface
} // namespace Mantid
#include "MantidKernel/IPropertyManager.h"
#include "MantidKernel/IPropertySettings.h"
#include "MantidPythonInterface/kernel/Converters/PyObjectToString.h"
#include "MantidPythonInterface/kernel/GetPointer.h"
#include "MantidPythonInterface/kernel/Registry/TypeRegistry.h"
#include "MantidPythonInterface/kernel/Registry/PropertyValueHandler.h"
#include "MantidPythonInterface/kernel/Registry/PropertyWithValueFactory.h"
#include "MantidPythonInterface/kernel/Registry/TypeRegistry.h"
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
......@@ -17,32 +18,12 @@
using namespace Mantid::Kernel;
namespace Registry = Mantid::PythonInterface::Registry;
namespace Converters = Mantid::PythonInterface::Converters;
using namespace boost::python;
GET_POINTER_SPECIALIZATION(IPropertyManager)
namespace {
/**
* Convert a python object to a string or throw an exception. This will convert
* unicode strings in python2 via utf8.
*/
std::string pyObjToStr(const boost::python::object &value) {
extract<std::string> extractor(value);
std::string valuestr;
if (extractor.check()) {
valuestr = extractor();
#if PY_VERSION_HEX < 0x03000000
} else if (PyUnicode_Check(value.ptr())) {
valuestr = extract<std::string>(str(value).encode("utf-8"))();
#endif
} else {
throw std::invalid_argument("Failed to convert python object a string");
}
return valuestr;
}
/**
* Set the value of a property from the value within the
* boost::python object
......@@ -55,7 +36,7 @@ void setProperty(IPropertyManager &self, const boost::python::object &name,
const boost::python::object &value) {
std::string namestr;
try {
namestr = pyObjToStr(name);
namestr = Converters::pyObjToStr(name);
} catch (std::invalid_argument &) {
throw std::invalid_argument("Failed to convert property name to a string");
}
......@@ -103,7 +84,7 @@ void setProperties(IPropertyManager &self, const boost::python::dict &kwargs) {
*/
void declareProperty(IPropertyManager &self, const boost::python::object &name,
boost::python::object value) {
std::string nameStr = pyObjToStr(name);
std::string nameStr = Converters::pyObjToStr(name);
auto p = std::unique_ptr<Property>(
Registry::PropertyWithValueFactory::create(nameStr, value, 0));
self.declareProperty(std::move(p));
......@@ -120,7 +101,7 @@ void declareProperty(IPropertyManager &self, const boost::python::object &name,
void declareOrSetProperty(IPropertyManager &self,
const boost::python::object &name,
boost::python::object value) {
std::string nameStr = pyObjToStr(name);
std::string nameStr = Converters::pyObjToStr(name);
bool propExists = self.existsProperty(nameStr);
if (propExists) {
setProperty(self, name, value);
......
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