Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #263 from tango-controls/issue-251
Browse files Browse the repository at this point in the history
Fix #251: Python 2 vs Python 3: DevString with bytes
  • Loading branch information
tiagocoutinho authored Mar 13, 2019
2 parents a3e6a70 + 83292d1 commit d61a363
Show file tree
Hide file tree
Showing 21 changed files with 384 additions and 264 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ endif

TANGO_CFLAGS=`pkg-config --cflags-only-other tango`
TANGO_LIBS=`pkg-config --libs-only-l tango`

ifdef CONDA_PREFIX
BOOST_LIB = boost_python
else
BOOST_LIB = boost_python-py$(PY_VER_S)
endif

PRE_C_H := precompiled_header.hpp
PRE_C_H_O := $(OBJS_DIR)/$(PRE_C_H).gch
Expand All @@ -98,7 +103,7 @@ LN_STATIC := g++ -pthread -static -Wl,$(OPTIMIZE_LN) -Wl,-Bsymbolic-functions

LN_VER := -Wl,-h -Wl,--strip-all

LN_LIBS := -l$(BOOST_LIB) -lpython$(PY_VER) -ltango
LN_LIBS := -l$(BOOST_LIB) -ltango

INCLUDE_DIRS =

Expand Down
19 changes: 10 additions & 9 deletions doc/data_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ will be the used for the input arguments. Also, it is recomended to use numpy
arrays of the appropiate type for output arguments as well, as they tend to be
much more efficient.


**For scalar types (SCALAR)**

+-------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand Down Expand Up @@ -70,7 +71,7 @@ much more efficient.
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> | :py:class:`list` <:py:obj:`int`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -82,7 +83,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_SHORT + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> | :py:class:`list` <:py:obj:`int`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -94,7 +95,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_LONG + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> | :py:class:`list` <:py:obj:`int`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -106,7 +107,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_LONG64 + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <int (64 bits) / long (32 bits)> | :py:class:`list` <:py:obj:`int`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -118,7 +119,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_FLOAT + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`float`> | :py:class:`list` <:py:obj:`float`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -130,7 +131,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_DOUBLE + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`float`> | :py:class:`list` <:py:obj:`float`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -142,7 +143,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_USHORT + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> | :py:class:`list` <:py:obj:`int`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -154,7 +155,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_ULONG + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> | :py:class:`list` <:py:obj:`int`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand All @@ -166,7 +167,7 @@ much more efficient.
| or +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| (DEV_ULONG64 + IMAGE) | ByteArray | :py:obj:`bytearray` | :py:obj:`bytearray` |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | String | :py:obj:`str` | String :py:obj:`str` (decoded with default python encoding *utf-8*!!!) |
| | String | :py:obj:`str` | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
| | List | :py:class:`list` <int (64 bits) / long (32 bits)> | :py:class:`list` <:py:obj:`int`> |
| +-----------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+
Expand Down
20 changes: 17 additions & 3 deletions ext/base_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ struct StdString_from_python_str_unicode
if (PyUnicode_Check(obj))
{
decref = true;
obj = PyUnicode_AsUTF8String(obj);
obj = PyUnicode_AsLatin1String(obj);
}

const char* value = PyBytes_AsString(obj);
Py_ssize_t size = PyBytes_Size(obj);

// Grab pointer to memory into which to construct the new std::string
void* storage = (
Expand All @@ -216,7 +217,7 @@ struct StdString_from_python_str_unicode

// in-place construct the new std::string using the character data
// extraced from the python object
new (storage) std::string(value);
new (storage) std::string(value, size);

// Stash the memory chunk pointer for later use by boost.python
data->convertible = storage;
Expand All @@ -226,6 +227,18 @@ struct StdString_from_python_str_unicode
}
};

PyObject* vector_string_get_item(const StdStringVector &vec, int index)
{
size_t pos = index < 0 ? index + vec.size() : (size_t)index;
if (pos >= vec.size()) {
PyErr_SetString(PyExc_IndexError, "Index out of range");
boost::python::throw_error_already_set();
return NULL;
}
return from_char_to_python_str(vec[pos]);
}


void* convert_to_cstring(PyObject* obj)
{
return PyBytes_Check(obj) ? PyBytes_AsString(obj) : 0;
Expand Down Expand Up @@ -292,7 +305,8 @@ void export_base_types()
// >> exception (unexisting is stored in the other obj)

class_<StdStringVector>("StdStringVector")
.def(vector_indexing_suite<StdStringVector, true>());
.def(vector_indexing_suite<StdStringVector, true>())
.def("__getitem__", &vector_string_get_item);

class_<StdLongVector>("StdLongVector")
.def(vector_indexing_suite<StdLongVector, true>());
Expand Down
6 changes: 3 additions & 3 deletions ext/dev_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ namespace PyDevError
}

static inline PyObject* get_reason(Tango::DevError &self)
{ return from_char_to_str(self.reason); }
{ return from_char_to_python_str(self.reason); }

static inline void set_reason(Tango::DevError &self, PyObject *str)
{ PyDevError::from_str_to_char(str, self.reason); }

static inline PyObject* get_desc(Tango::DevError &self)
{ return from_char_to_str(self.desc); }
{ return from_char_to_python_str(self.desc); }

static inline void set_desc(Tango::DevError &self, PyObject *str)
{ PyDevError::from_str_to_char(str, self.desc); }

static inline PyObject* get_origin(Tango::DevError &self)
{ return from_char_to_str(self.origin); }
{ return from_char_to_python_str(self.origin); }

static inline void set_origin(Tango::DevError &self, PyObject *str)
{ PyDevError::from_str_to_char(str, self.origin); }
Expand Down
10 changes: 5 additions & 5 deletions ext/device_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ struct python_tangocpp<Tango::DEV_STRING>

static inline void to_cpp(const bopy::object & py_value, TangoScalarType & result)
{
result = CORBA::string_dup(bopy::extract<TangoScalarType>(py_value));
result = from_str_to_char(py_value.ptr());
}

static inline bopy::object to_python(const TangoScalarType & value)
{
return bopy::object(std::string(value));
return from_char_to_boost_str(value);
}
};

Expand Down Expand Up @@ -334,15 +334,15 @@ namespace PyDeviceAttribute
{
std::vector<std::string> r_val, w_val;
self.extract_read(r_val);
py_value.attr(value_attr_name) = object(r_val[0]);
py_value.attr(value_attr_name) = from_char_to_boost_str(r_val[0]);
self.extract_set(w_val);
py_value.attr(w_value_attr_name) = object(w_val[0]);
py_value.attr(w_value_attr_name) = from_char_to_boost_str(w_val[0]);
}
else
{
std::string rvalue;
EXTRACT_VALUE(self, rvalue)
py_value.attr(value_attr_name) = object(rvalue);
py_value.attr(value_attr_name) = from_char_to_boost_str(rvalue);
py_value.attr(w_value_attr_name) = object();
}
}
Expand Down
3 changes: 2 additions & 1 deletion ext/device_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ namespace PyDeviceAttribute {
template<>
inline void _fill_scalar_attribute<Tango::DEV_STRING>(Tango::DeviceAttribute & dev_attr, const boost::python::object & py_value)
{
std::string value = boost::python::extract<std::string>(py_value);
std::string value;
from_str_to_char(py_value.ptr(), value);
dev_attr << value;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/device_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace PyDeviceData {
typedef std::string TangoScalarType;
TangoScalarType val;
self >> val;
return boost::python::object(val);
return from_char_to_boost_str(val);
}

template <>
Expand Down
Loading

0 comments on commit d61a363

Please sign in to comment.