-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python 3 compatibility #22
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,7 +201,12 @@ def check_type(field_name, field_type, field_val): | |
elif field_type == 'string': | ||
if sys.hexversion > 0x03000000: | ||
if type(field_val) == str: | ||
raise SerializationError('field %s is a unicode string instead of an ascii string'%field_name) | ||
try: | ||
field_val.encode('ascii') | ||
except UnicodeEncodeError: | ||
raise SerializationError('field %s is a non-ascii string'%field_name) | ||
elif not type(field_val) == bytes: | ||
raise SerializationError('field %s must be of type bytes or an ascii string'%field_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mimics the same behavior as the Python 2 code path: bail out on non-ascii strings. |
||
else: | ||
if type(field_val) == unicode: | ||
raise SerializationError('field %s is a unicode string instead of an ascii string'%field_name) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,20 +30,38 @@ | |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import cStringIO | ||
try: | ||
from cStringIO import StringIO | ||
except ImportError: | ||
from io import BytesIO as StringIO | ||
import sys | ||
import time | ||
|
||
def test_generate_dynamic(): | ||
import genpy | ||
from genpy.dynamic import generate_dynamic | ||
msgs = generate_dynamic("gd_msgs/EasyString", "string data\n") | ||
assert ['gd_msgs/EasyString'] == msgs.keys() | ||
assert ['gd_msgs/EasyString'] == list(msgs.keys()) | ||
m_cls = msgs['gd_msgs/EasyString'] | ||
m_instance = m_cls() | ||
m_instance.data = 'foo' | ||
buff = cStringIO.StringIO() | ||
buff = StringIO() | ||
m_instance.serialize(buff) | ||
m_cls().deserialize(buff.getvalue()) | ||
m_instance2 = m_cls().deserialize(buff.getvalue()) | ||
assert m_instance == m_instance2 | ||
|
||
try: | ||
char = unichr | ||
except NameError: | ||
char = chr | ||
m_instance.data = 'foo' + char(1234) | ||
buff = StringIO() | ||
m_instance.serialize(buff) | ||
m_instance2 = m_cls().deserialize(buff.getvalue()) | ||
if sys.hexversion < 0x03000000: | ||
# python 2 requires manual decode into unicode | ||
m_instance2.data = m_instance2.data.decode('utf-8') | ||
assert m_instance == m_instance2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a serialize / deserialize test with unicode characters. |
||
|
||
# 'probot_msgs' is a test for #1183, failure if the package no longer exists | ||
msgs = generate_dynamic("gd_msgs/MoveArmState", """Header header | ||
|
@@ -102,7 +120,7 @@ def test_generate_dynamic(): | |
m_instance1 = msgs['probot_msgs/ControllerStatus']() | ||
m_instance2 = msgs['probot_msgs/ControllerStatus'](value=4, comment=str(time.time())) | ||
d = {'UNDEFINED':0,'SUCCESS':1,'ABORTED':2,'PREEMPTED':3,'ACTIVE':4} | ||
for k, v in d.iteritems(): | ||
for k, v in d.items(): | ||
assert v == getattr(m_instance1, k) | ||
_test_ser_deser(m_instance2, m_instance1) | ||
|
||
|
@@ -124,7 +142,7 @@ def test_generate_dynamic(): | |
_test_ser_deser(m_instance2, m_instance1) | ||
|
||
def _test_ser_deser(m_instance1, m_instance2): | ||
buff = cStringIO.StringIO() | ||
buff = StringIO() | ||
m_instance1.serialize(buff) | ||
m_instance2.deserialize(buff.getvalue()) | ||
assert m_instance1 == m_instance2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since in Python 3 the
str
is encoded it must be written asbytes
.