Skip to content
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

Deprecated byte char #191

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern "C"
// array functions for all primitive types
ROSIDL_GENERATOR_C__DECLARE_PRIMITIVE_ARRAY_FUNCTIONS(bool, bool)
ROSIDL_GENERATOR_C__DECLARE_PRIMITIVE_ARRAY_FUNCTIONS(byte, uint8_t)
ROSIDL_GENERATOR_C__DECLARE_PRIMITIVE_ARRAY_FUNCTIONS(char, char)
ROSIDL_GENERATOR_C__DECLARE_PRIMITIVE_ARRAY_FUNCTIONS(char, int8_t)
ROSIDL_GENERATOR_C__DECLARE_PRIMITIVE_ARRAY_FUNCTIONS(float32, float)
ROSIDL_GENERATOR_C__DECLARE_PRIMITIVE_ARRAY_FUNCTIONS(float64, double)
ROSIDL_GENERATOR_C__DECLARE_PRIMITIVE_ARRAY_FUNCTIONS(int8, int8_t)
Expand Down
8 changes: 4 additions & 4 deletions rosidl_generator_c/test/test_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ int test_primitives_unbounded_arrays(void)
}

// byte_array
res = rosidl_generator_c__byte__Array__init(&arrays->byte_array, ARRAY_SIZE);
res = rosidl_generator_c__uint8__Array__init(&arrays->byte_array, ARRAY_SIZE);
EXPECT_EQ(true, res);
uint8_t test_array_byte[7] = {0, 57, 110, 177, 201, 240, 255};
for (i = 0; i < ARRAY_SIZE; i++) {
Expand All @@ -346,7 +346,7 @@ int test_primitives_unbounded_arrays(void)


// char array
res = rosidl_generator_c__char__Array__init(&arrays->char_array, ARRAY_SIZE);
res = rosidl_generator_c__int8__Array__init(&arrays->char_array, ARRAY_SIZE);
EXPECT_EQ(true, res);
char test_array_char[7] = {'a', '5', '#', 'Z', '@', '-', ' '};
for (i = 0; i < ARRAY_SIZE; i++) {
Expand Down Expand Up @@ -527,7 +527,7 @@ int test_primitives_bounded_arrays(void)
}

// byte_array
res = rosidl_generator_c__byte__Array__init(&arrays->byte_array, ARRAY_SIZE);
res = rosidl_generator_c__uint8__Array__init(&arrays->byte_array, ARRAY_SIZE);
EXPECT_EQ(true, res);
uint8_t test_array_byte[8] = {0, 57, 110, 177, 201, 240, 255, 111};
for (i = 0; i < ARRAY_SIZE; i++) {
Expand All @@ -540,7 +540,7 @@ int test_primitives_bounded_arrays(void)


// char array
res = rosidl_generator_c__char__Array__init(&arrays->char_array, ARRAY_SIZE);
res = rosidl_generator_c__int8__Array__init(&arrays->char_array, ARRAY_SIZE);
EXPECT_EQ(true, res);
char test_array_char[7] = {'a', '5', '#', 'Z', '@', '-', ' '};
for (i = 0; i < ARRAY_SIZE; i++) {
Expand Down
20 changes: 10 additions & 10 deletions rosidl_generator_cpp/test/test_interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ void test_message_primitives_static(rosidl_generator_cpp::msg::PrimitivesStatic
#ifdef __linux__
#pragma GCC diagnostic pop
#endif
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, byte_value, 0, 255)
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, char_value, CHAR_MIN, CHAR_MAX)
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, byte_value, 0, UINT8_MAX)
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, char_value, INT8_MIN, INT8_MAX)
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, float32_value, FLT_MIN, FLT_MAX)
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, float64_value, DBL_MIN, DBL_MAX)
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, int8_value, INT8_MIN, INT8_MAX)
Expand Down Expand Up @@ -198,8 +198,8 @@ void test_message_primitives_bounded(rosidl_generator_cpp::msg::PrimitivesBounde
{
TEST_BOUNDED_ARRAY_PRIMITIVE(message, bool_value, bool, PRIMITIVES_ARRAY_SIZE, \
false, true)
TEST_BOUNDED_ARRAY_PRIMITIVE(message, char_value, char, PRIMITIVES_ARRAY_SIZE, \
CHAR_MIN, CHAR_MAX)
TEST_BOUNDED_ARRAY_PRIMITIVE(message, char_value, int8_t, PRIMITIVES_ARRAY_SIZE, \
INT8_MIN, INT8_MAX)
TEST_BOUNDED_ARRAY_PRIMITIVE(message, byte_value, uint8_t, PRIMITIVES_ARRAY_SIZE, \
0, UINT8_MAX)
TEST_BOUNDED_ARRAY_PRIMITIVE(message, float32_value, float, PRIMITIVES_ARRAY_SIZE, \
Expand Down Expand Up @@ -250,8 +250,8 @@ void test_message_primitives_unbounded(rosidl_generator_cpp::msg::PrimitivesUnbo
{
TEST_UNBOUNDED_ARRAY_PRIMITIVE(message, bool_value, bool, PRIMITIVES_ARRAY_SIZE, \
false, true)
TEST_UNBOUNDED_ARRAY_PRIMITIVE(message, char_value, char, PRIMITIVES_ARRAY_SIZE, \
CHAR_MIN, CHAR_MAX)
TEST_UNBOUNDED_ARRAY_PRIMITIVE(message, char_value, int8_t, PRIMITIVES_ARRAY_SIZE, \
INT8_MIN, INT8_MAX)
TEST_UNBOUNDED_ARRAY_PRIMITIVE(message, byte_value, uint8_t, PRIMITIVES_ARRAY_SIZE, \
0, UINT8_MAX)
TEST_UNBOUNDED_ARRAY_PRIMITIVE(message, float32_value, float, PRIMITIVES_ARRAY_SIZE, \
Expand Down Expand Up @@ -290,8 +290,8 @@ void test_message_primitives_static_arrays(rosidl_generator_cpp::msg::PrimitiveS
{
TEST_STATIC_ARRAY_PRIMITIVE(message, bool_value, bool, PRIMITIVES_ARRAY_SIZE, \
false, true)
TEST_STATIC_ARRAY_PRIMITIVE(message, char_value, char, PRIMITIVES_ARRAY_SIZE, \
CHAR_MIN, CHAR_MAX)
TEST_STATIC_ARRAY_PRIMITIVE(message, char_value, int8_t, PRIMITIVES_ARRAY_SIZE, \
INT8_MIN, INT8_MAX)
TEST_STATIC_ARRAY_PRIMITIVE(message, byte_value, uint8_t, PRIMITIVES_ARRAY_SIZE, \
0, UINT8_MAX)
TEST_STATIC_ARRAY_PRIMITIVE(message, float32_value, float, PRIMITIVES_ARRAY_SIZE, \
Expand Down Expand Up @@ -450,8 +450,8 @@ TEST(Test_messages, primitives_default) {
#ifdef __linux__
#pragma GCC diagnostic pop
#endif
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, byte_value, 50, 255);
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, char_value, 100, CHAR_MAX);
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, byte_value, 50, UINT8_MAX);
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, char_value, 100, INT8_MAX);
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, float32_value, 1.125f, FLT_MAX);
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, float64_value, 1.125, DBL_MAX);
TEST_PRIMITIVE_FIELD_ASSIGNMENT(message, int8_value, -50, INT8_MAX);
Expand Down
28 changes: 0 additions & 28 deletions rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,12 @@ class @(spec.base_type.type)(metaclass=Metaclass):
from @(field.type.pkg_name).msg import @(field.type.type)
@[ end if]@
@[ if field.type.array_size and not field.type.is_upper_bound]@
@[ if field.type.type == 'byte']@
self.@(field.name) = kwargs.get(
'@(field.name)',
list([bytes([0]) for x in range(@(field.type.array_size))])
)
@[ elif field.type.type == 'char']@
self.@(field.name) = kwargs.get(
'@(field.name)',
list([chr(0) for x in range(@(field.type.array_size))])
)
@[ else]@
self.@(field.name) = kwargs.get(
'@(field.name)',
list([@(get_python_type(field.type))() for x in range(@(field.type.array_size))])
)
@[ end if]@
@[ elif field.type.is_array]@
self.@(field.name) = kwargs.get('@(field.name)', list())
@[ elif field.type.type == 'byte']@
self.@(field.name) = kwargs.get('@(field.name)', bytes([0]))
@[ elif field.type.type == 'char']@
self.@(field.name) = kwargs.get('@(field.name)', chr(0))
@[ else]@
self.@(field.name) = kwargs.get('@(field.name)', @(get_python_type(field.type))())
@[ end if]@
Expand Down Expand Up @@ -167,10 +151,6 @@ class @(spec.base_type.type)(metaclass=Metaclass):
from collections import UserString
@[ elif field.type.string_upper_bound]@
from collections import UserString
@[ elif field.type.type == 'byte']@
from collections import ByteString
@[ elif field.type.type in ['char']]@
from collections import UserString
@[ end if]@
assert \
@[ if field.type.is_array]@
Expand Down Expand Up @@ -202,8 +182,6 @@ nbits = int(field.type.type[4:])
bound = 2**nbits
}@
all([val >= 0 and val < @(bound) for val in value]))
@[ elif field.type.type == 'char']@
all([ord(val) >= -128 and ord(val) < 128 for val in value]))
@[ else]@
True)
@[ end if]@
Expand All @@ -212,12 +190,6 @@ bound = 2**nbits
len(value) <= @(field.type.string_upper_bound))
@[ elif not field.type.is_primitive_type()]@
isinstance(value, @(field.type.type))
@[ elif field.type.type == 'byte']@
((isinstance(value, bytes) or isinstance(value, ByteString)) and
len(value) == 1)
@[ elif field.type.type == 'char']@
((isinstance(value, str) or isinstance(value, UserString)) and
len(value) == 1 and ord(value) >= -128 and ord(value) < 128)
@[ elif field.type.type in [
'bool',
'float32', 'float64',
Expand Down
21 changes: 4 additions & 17 deletions rosidl_generator_py/test/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def test_constants():
assert 123 == Constants.X
assert -123 == Constants.Y
assert 'foo' == Constants.FOO
assert '\x7F' == Constants.TOTO
assert b'0' == Constants.TATA
assert ord('\x7F') == Constants.TOTO
assert ord('0') == Constants.TATA

assert_raises(AttributeError, setattr, Constants, 'FOO', 'bar')

Expand All @@ -120,9 +120,9 @@ def test_default_values():

assert [5, 23] == b.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT

assert '\x01' == b.CHAR_VALUE__DEFAULT
assert 1 == b.CHAR_VALUE__DEFAULT
assert '1' != b.CHAR_VALUE__DEFAULT
assert b'\x01' == b.BYTE_VALUE__DEFAULT
assert 1 == b.BYTE_VALUE__DEFAULT
assert b'1' != b.BYTE_VALUE__DEFAULT


Expand Down Expand Up @@ -169,19 +169,6 @@ def test_check_constraints():
assert [primitives, primitives] == b.unbounded_primitives

c = Various()
c.byte_value = b'a'
assert b'a' == c.byte_value
assert 'a' != c.byte_value
assert_raises(AssertionError, setattr, c, 'byte_value', 'a')
assert_raises(AssertionError, setattr, c, 'byte_value', b'abc')
assert_raises(AssertionError, setattr, c, 'byte_value', 'abc')

c.char_value = 'a'
assert 'a' == c.char_value
assert b'a' != c.char_value
assert_raises(AssertionError, setattr, c, 'char_value', b'a')
assert_raises(AssertionError, setattr, c, 'char_value', 'abc')
assert_raises(AssertionError, setattr, c, 'char_value', b'abc')

c.up_to_three_int32_values = []
assert [] == c.up_to_three_int32_values
Expand Down
29 changes: 23 additions & 6 deletions rosidl_parser/rosidl_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

PRIMITIVE_TYPES = [
'bool',
'byte',
'char',
# TODO reconsider wchar
'float32',
'float64',
Expand All @@ -47,6 +45,11 @@
'time', # for compatibility only
]

DEPRECATED_TYPES = {
'byte': 'uint8',
'char': 'int8'
}

VALID_PACKAGE_NAME_PATTERN = re.compile('^[a-z]([a-z0-9_]?[a-z0-9]+)*$')
VALID_FIELD_NAME_PATTERN = re.compile('^[a-z]([a-z0-9_]?[a-z0-9]+)*$')
# relaxed patterns used for compatibility with ROS 1 messages
Expand Down Expand Up @@ -132,7 +135,16 @@ class BaseType(object):

def __init__(self, type_string, context_package_name=None):
# check for primitive types
if type_string in PRIMITIVE_TYPES:
if type_string in DEPRECATED_TYPES.keys():
print("WARNING: '%s' is DEPRECATED, it will be remapped to '%s'" %
(type_string, DEPRECATED_TYPES[type_string]))
print("please use the equivalent primitive type '%s'\n" % (
DEPRECATED_TYPES[type_string]))
self.pkg_name = None
self.type = DEPRECATED_TYPES[type_string]
self.string_upper_bound = None

elif type_string in PRIMITIVE_TYPES:
self.pkg_name = None
self.type = type_string
self.string_upper_bound = None
Expand Down Expand Up @@ -277,16 +289,21 @@ class Constant(object):
__slots__ = ['type', 'name', 'value']

def __init__(self, primitive_type, name, value_string):
if primitive_type not in PRIMITIVE_TYPES:
if primitive_type not in PRIMITIVE_TYPES and primitive_type not in DEPRECATED_TYPES.keys():
raise TypeError("the constant type '%s' must be a primitive type" %
primitive_type)
self.type = primitive_type
if not is_valid_constant_name(name):
raise NameError("the constant name '%s' is not valid" % name)
self.name = name
if value_string is None:
raise ValueError("the constant value must not be 'None'")

if primitive_type in DEPRECATED_TYPES.keys():
print("WARNING '%s' is DEPRECATED, it will be remapped to '%s'" %
(primitive_type, DEPRECATED_TYPES[primitive_type]))
print("please use the equivalent primitive type '%s'\n" % (
DEPRECATED_TYPES[primitive_type]))
primitive_type = DEPRECATED_TYPES[primitive_type]
self.type = primitive_type
self.value = parse_primitive_value_string(
Type(primitive_type), value_string)

Expand Down
13 changes: 11 additions & 2 deletions rosidl_parser/test/test_base_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
def test_base_type_constructor():
primitive_types = [
'bool',
'byte',
'char',
'float32',
'float64',
'int8',
Expand All @@ -34,11 +32,22 @@ def test_base_type_constructor():
'int64',
'uint64',
'string']

deprecated_types = {
'byte': 'uint8',
'char': 'int8'
}

for primitive_type in primitive_types:
base_type = BaseType(primitive_type)
assert base_type.pkg_name is None
assert base_type.type == primitive_type
assert base_type.string_upper_bound is None
for deprecated_type in deprecated_types.keys():
base_type = BaseType(deprecated_type)
assert base_type.pkg_name is None
assert base_type.type == deprecated_types[deprecated_type]
assert base_type.string_upper_bound is None

base_type = BaseType('string<=23')
assert base_type.pkg_name is None
Expand Down
2 changes: 1 addition & 1 deletion rosidl_parser/test/test_constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_constant_methods():
assert Constant('bool', 'FOO', '1') == Constant('bool', 'FOO', '1')
assert Constant('bool', 'FOO', '1') != Constant('bool', 'FOO', '0')
assert Constant('bool', 'FOO', '1') != Constant('bool', 'BAR', '1')
assert Constant('bool', 'FOO', '1') != Constant('byte', 'FOO', '1')
assert Constant('bool', 'FOO', '1') != Constant('uint8', 'FOO', '1')

assert str(Constant('bool', 'FOO', '1')) == 'bool FOO=True'

Expand Down
2 changes: 1 addition & 1 deletion rosidl_parser/test/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_field_methods():
assert (Field(Type('bool'), 'foo', '1') !=
Field(Type('bool'), 'bar', '1'))
assert (Field(Type('bool'), 'foo', '1') !=
Field(Type('byte'), 'foo', '1'))
Field(Type('uint8'), 'foo', '1'))

assert str(Field(Type('bool'), 'foo', '1')) == 'bool foo True'

Expand Down