-
Notifications
You must be signed in to change notification settings - Fork 44
Fix #251: Python 2 vs Python 3: DevString with bytes #263
Conversation
also make sure c++ strings are properly ended
1e0e04d
to
3851afc
Compare
3851afc
to
cdfa525
Compare
0a9105e
to
81320ae
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for improvements.
I'm also wondering if the mentions of UTF-8 in https://github.com/tango-controls/pytango/blob/develop/doc/data_types.rst are still correct after this change?
ext/device_attribute.cpp
Outdated
} | ||
|
||
static inline bopy::object to_python(const TangoScalarType & value) | ||
{ | ||
return bopy::object(std::string(value)); | ||
return from_char_to_str2(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it didn't come in with this PR, but this is a pretty bad name for a function. The difference between the behaviour of from_char_to_str
and from_char_to_str2
isn't clear at all.
Looking at the implementation, maybe from_char_to_python_str
and from_char_to_boost_str
?
Similarly, from_str_to_char
isn't quite saying what it does. Seems to create a C++ string from a Python string. Maybe from_python_str_to_char
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree with your naming.
ext/from_py.cpp
Outdated
} | ||
return ret; | ||
// DRY! | ||
return from_str_to_char(obj_ptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great improvement, but now we have a function that just calls another function. Might as well just use from_str_to_char
directly. I only see usages of obj_to_new_char
in this file, so should be OK to drop it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
ext/from_py.cpp
Outdated
} | ||
return ret; | ||
// DRY! | ||
return from_str_to_char(obj_ptr); | ||
} | ||
|
||
char* obj_to_new_char(bopy::object obj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the usages are of the the form: obj_to_new_char(py_obj.attr("min_alarm"))
, so I'm guessing we can probably just remove this boost object variant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure I understand what you mean.
by the previous comments I was thinking to obj_to_new_char
would be replaced by from_str_to_char
. Is that is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's it.
ext/from_py.cpp
Outdated
{ | ||
result = PyBytes_AsString(obj_ptr); | ||
} | ||
from_str_to_char(obj_ptr, result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, rather drop the obj_to_string
function, as use this one elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
ext/from_py.cpp
Outdated
{ | ||
result = PyBytes_AsString(obj_ptr); | ||
} | ||
from_str_to_char(obj_ptr, result); | ||
} | ||
|
||
void obj_to_string(bopy::object obj, std::string& result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, this variant seems to be unused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
tests/test_server.py
Outdated
@@ -221,7 +221,7 @@ def bad_attr(self): | |||
# Test properties | |||
|
|||
def test_device_property_no_default(typed_values, server_green_mode): | |||
dtype, values = typed_values | |||
dtype, values, expected_values = typed_values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected_values
isn't used in this test, but it should be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this specific test, the expected value is always the default values[0]
, so here I can remove it.
tests/test_server.py
Outdated
@@ -245,7 +245,7 @@ def get_prop(self): | |||
|
|||
|
|||
def test_device_property_with_default_value(typed_values, server_green_mode): | |||
dtype, values = typed_values | |||
dtype, values, expected_values = typed_values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, expected_values
should probably be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
tests/test_server.py
Outdated
@@ -359,7 +359,7 @@ def PolledAttribute3(self): | |||
|
|||
|
|||
def test_mandatory_device_property(typed_values, server_green_mode): | |||
dtype, values = typed_values | |||
dtype, values, expected_values = typed_values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, expected_values
should probably be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
@@ -87,7 +87,7 @@ endif | |||
|
|||
TANGO_CFLAGS=`pkg-config --cflags-only-other tango` | |||
TANGO_LIBS=`pkg-config --libs-only-l tango` | |||
BOOST_LIB = boost_python-py$(PY_VER_S) | |||
BOOST_LIB = boost_python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change? (I don't know anything about the boost library naming.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, the Makefile commit is a spurious commit.
This was because in my dev env I was using conda and the conda boost package does not suffix the library name with the python version.
I will undo this
Yes, you are right. Every mention to UTF-8 should be changed to latin-1 |
Thanks for the revision @ajoubertza. It is much appreciated! |
This reverts commit bca0f67.
@ajoubertza, I think I have made all the modifications according to your review. I have spotted some problems with the database as well. I believe I managed to fix them all. If you have time, could you check that everything is ok now? Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, @tiagocoutinho - thanks. This was a difficult one!
(Sorry for delay in reviewing - I've been on vacation)
eid = proxy.command_inout_asynch('identity', value) | ||
result = proxy.command_inout_reply(eid, timeout=500) | ||
assert_array_equal(result, expected_value) | ||
assert_array_equal(result, expected(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice improvement. More readable now, and don't need to do the zip.
tests/test_client.py
Outdated
# char \x00 cannot be sent in a DevString. All other 1-255 chars can | ||
ints = tuple(range(1, 256)) | ||
bytes_devstring = bytes(ints) if PY3 else ''.join(map(chr, ints)) | ||
str_devstring = bytes_devstring.decode('latin-1') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still some duplication here - these things are in test_utils.py
.
Checked: