Skip to content
Merged
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
1 change: 0 additions & 1 deletion rosidl_generator_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ find_package(ament_cmake REQUIRED)

ament_export_dependencies(ament_cmake)
ament_export_dependencies(rmw)
ament_export_dependencies(rosidl_cmake)

ament_index_register_resource("rosidl_generator_packages")

Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_py/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<buildtool_export_depend>ament_cmake</buildtool_export_depend>
<buildtool_export_depend>ament_index_python</buildtool_export_depend>
<buildtool_export_depend>python_cmake_module</buildtool_export_depend>
<buildtool_export_depend>rosidl_cmake</buildtool_export_depend>
<buildtool_export_depend>rosidl_generator_c</buildtool_export_depend>
<buildtool_export_depend>rosidl_pycommon</buildtool_export_depend>
<buildtool_export_depend>rosidl_typesupport_c</buildtool_export_depend>
<buildtool_export_depend>rosidl_typesupport_interface</buildtool_export_depend>

Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_py/resource/_action.py.em
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@# Included from rosidl_generator_py/resource/_idl.py.em
@{
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore

action_name = '_' + convert_camel_case_to_lower_case_underscore(action.namespaced_type.name)
module_name = '_' + convert_camel_case_to_lower_case_underscore(interface_path.stem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEMPLATE(
}@

@{
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
type_name = convert_camel_case_to_lower_case_underscore(action.namespaced_type.name)
function_name = 'type_support'
}@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@# Handle messages
@#######################################################################
@{
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
from rosidl_parser.definition import Message

include_directives = set()
Expand Down
18 changes: 12 additions & 6 deletions rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@# Included from rosidl_generator_py/resource/_idl.py.em
@{

from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
from rosidl_generator_py.generate_py_impl import constant_value_to_py
from rosidl_generator_py.generate_py_impl import get_python_type
from rosidl_generator_py.generate_py_impl import SPECIAL_NESTED_BASIC_TYPES
Expand Down Expand Up @@ -36,9 +36,15 @@ if message.structure.members:
imports.setdefault(
'import rosidl_parser.definition', []) # used for SLOT_TYPES
for member in message.structure.members:
type_ = member.type
if isinstance(type_, AbstractNestedType):
type_ = type_.value_type
if member.name != EMPTY_STRUCTURE_REQUIRED_MEMBER_NAME:
imports.setdefault(
'import builtins', []) # used for @builtins.property
if isinstance(type_, BasicType) and type_.typename in FLOATING_POINT_TYPES:
imports.setdefault(
'import math', []) # used for math.isinf
if (
isinstance(member.type, AbstractNestedType) and
isinstance(member.type.value_type, BasicType) and
Expand Down Expand Up @@ -504,16 +510,16 @@ bound = 2**nbits
@[ if type_.typename == "float"]@
@{
name = "float"
bound = 3.402823e+38
bound = 3.402823466e+38
}@
all(val >= -@(bound) and val <= @(bound) for val in value)), \
all(not (val < -@(bound) or val > @(bound)) or math.isinf(val) for val in value)), \
@{assert_msg_suffixes.append('and each float in [%f, %f]' % (-bound, bound))}@
@[ elif type_.typename == "double"]@
@{
name = "double"
bound = 1.7976931348623157e+308
}@
all(val >= -@(bound) and val <= @(bound) for val in value)), \
all(not (val < -@(bound) or val > @(bound)) or math.isinf(val) for val in value)), \
@{assert_msg_suffixes.append('and each double in [%f, %f]' % (-bound, bound))}@
@[ end if]@
@[ else]@
Expand Down Expand Up @@ -561,15 +567,15 @@ bound = 2**nbits
@[ if type_.typename == "float"]@
@{
name = "float"
bound = 3.402823e+38
bound = 3.402823466e+38
}@
@[ elif type_.typename == "double"]@
@{
name = "double"
bound = 1.7976931348623157e+308
}@
@[ end if]@
assert value >= -@(bound) and value <= @(bound), \
assert not (value < -@(bound) or value > @(bound)) or math.isinf(value), \
"The '@(member.name)' field must be a @(name) in [@(-bound), @(bound)]"
@[ end if]@
@[ else]@
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@# Included from rosidl_generator_py/resource/_idl_pkg_typesupport_entry_point.c.em
@{
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore

include_parts = idl_type.namespaces + [
'detail', convert_camel_case_to_lower_case_underscore(idl_type.name)]
Expand Down
6 changes: 3 additions & 3 deletions rosidl_generator_py/resource/_msg_support.c.em
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@# Included from rosidl_generator_py/resource/_idl_support.c.em
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
@{
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
from rosidl_generator_py.generate_py_impl import SPECIAL_NESTED_BASIC_TYPES
from rosidl_parser.definition import AbstractNestedType
from rosidl_parser.definition import AbstractSequence
Expand Down Expand Up @@ -683,7 +683,7 @@ nested_type = '__'.join(type_.namespaced_name())
(void)rc;
assert(rc == 0);
@[ elif isinstance(member.type.value_type, AbstractString)]@
PyObject * decoded_item = PyUnicode_DecodeUTF8(src[i].data, strlen(src[i].data), "strict");
PyObject * decoded_item = PyUnicode_DecodeUTF8(src[i].data, strlen(src[i].data), "replace");
if (!decoded_item) {
return NULL;
}
Expand Down Expand Up @@ -749,7 +749,7 @@ nested_type = '__'.join(type_.namespaced_name())
field = PyUnicode_DecodeUTF8(
ros_message->@(member.name).data,
strlen(ros_message->@(member.name).data),
"strict");
"replace");
if (!field) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_py/resource/_srv.py.em
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@# Included from rosidl_generator_py/resource/_idl.py.em
@{
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore

service_name = '_' + convert_camel_case_to_lower_case_underscore(service.namespaced_type.name)
module_name = '_' + convert_camel_case_to_lower_case_underscore(interface_path.stem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEMPLATE(
}@
@
@{
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
type_name = convert_camel_case_to_lower_case_underscore(service.namespaced_type.name)
function_name = 'type_support'
}@
Expand Down
10 changes: 5 additions & 5 deletions rosidl_generator_py/rosidl_generator_py/generate_py_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
import pathlib
import sys

from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_cmake import expand_template
from rosidl_cmake import generate_files
from rosidl_cmake import get_newest_modification_time
from rosidl_cmake import read_generator_arguments
from rosidl_parser.definition import AbstractGenericString
from rosidl_parser.definition import AbstractNestedType
from rosidl_parser.definition import AbstractSequence
Expand All @@ -38,6 +33,11 @@
from rosidl_parser.definition import NamespacedType
from rosidl_parser.definition import Service
from rosidl_parser.parser import parse_idl_file
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
from rosidl_pycommon import expand_template
from rosidl_pycommon import generate_files
from rosidl_pycommon import get_newest_modification_time
from rosidl_pycommon import read_generator_arguments

SPECIAL_NESTED_BASIC_TYPES = {
'float': {'dtype': 'numpy.float32', 'type_code': 'f'},
Expand Down
95 changes: 82 additions & 13 deletions rosidl_generator_py/test/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

import array
import math
import sys

import numpy
import pytest
Expand Down Expand Up @@ -112,14 +114,38 @@ def test_basic_types():
setattr(msg, 'uint%d_value' % i, -1)
with pytest.raises(AssertionError):
setattr(msg, 'int%d_value' % i, 2**i)
float32_ieee_max_next = numpy.nextafter(3.402823466e+38, math.inf)
with pytest.raises(AssertionError):
setattr(msg, 'float32_value', -3.5e+38)
setattr(msg, 'float32_value', -float32_ieee_max_next)
with pytest.raises(AssertionError):
setattr(msg, 'float32_value', 3.5e+38)
with pytest.raises(AssertionError):
setattr(msg, 'float64_value', 1.8e+308)
with pytest.raises(AssertionError):
setattr(msg, 'float64_value', -1.8e+308)
setattr(msg, 'float32_value', float32_ieee_max_next)

# Only run bounds test on system with non-compliant IEEE 754 float64.
# Otherwise the number is implicitly converted to inf.
if sys.float_info.max > 1.7976931348623157e+308:
float64_ieee_max_next = numpy.nextafter(1.7976931348623157e+308, math.inf)
with pytest.raises(AssertionError):
setattr(msg, 'float64_value', -float64_ieee_max_next)
with pytest.raises(AssertionError):
setattr(msg, 'float64_value', float64_ieee_max_next)

# NaN
setattr(msg, 'float32_value', math.nan)
assert math.isnan(msg.float32_value)
setattr(msg, 'float64_value', math.nan)
assert math.isnan(msg.float64_value)

# -Inf
setattr(msg, 'float32_value', -math.inf)
assert math.isinf(msg.float32_value)
setattr(msg, 'float64_value', -math.inf)
assert math.isinf(msg.float64_value)

# +Inf
setattr(msg, 'float32_value', math.inf)
assert math.isinf(msg.float32_value)
setattr(msg, 'float64_value', math.inf)
assert math.isinf(msg.float64_value)


def test_strings():
Expand Down Expand Up @@ -460,9 +486,37 @@ def test_arrays():
with pytest.raises(AssertionError):
setattr(msg, 'uint64_values', [-1, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
float32_ieee_max_next = numpy.nextafter(3.402823466e+38, math.inf)
setattr(msg, 'float32_values', [-float32_ieee_max_next, 0.0])
with pytest.raises(AssertionError):
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])
float32_ieee_max_next = numpy.nextafter(3.402823466e+38, math.inf)
setattr(msg, 'float32_values', [0.0, float32_ieee_max_next])

# If target system is IEEE 754 compliant, the next number is rounded to inf.
# Only perform this check on non-compliant systems.
if sys.float_info.max > 1.7976931348623157e+308:
with pytest.raises(AssertionError):
float64_ieee_max_next = numpy.nextafter(1.7976931348623157e+308, math.inf)
setattr(msg, 'float64_values', [-float64_ieee_max_next, 0.0])
with pytest.raises(AssertionError):
float64_ieee_max_next = numpy.nextafter(1.7976931348623157e+308, math.inf)
setattr(msg, 'float64_values', [-float64_ieee_max_next, 0.0])

# NaN
arr_of_float32_with_nan = numpy.array([-1.33, math.nan, 1.33], dtype=numpy.float32)
setattr(msg, 'float32_values', arr_of_float32_with_nan)
assert numpy.array_equal(arr_of_float32_with_nan, msg.float32_values, equal_nan=True)
arr_of_float64_with_nan = numpy.array([-1.66, math.nan, 1.66], dtype=numpy.float64)
setattr(msg, 'float64_values', arr_of_float64_with_nan)
assert numpy.array_equal(arr_of_float64_with_nan, msg.float64_values, equal_nan=True)

# Inf
arr_of_float32_with_inf = numpy.array([-math.inf, 5.5, math.inf], dtype=numpy.float32)
setattr(msg, 'float32_values', arr_of_float32_with_inf)
assert numpy.array_equal(arr_of_float32_with_inf, msg.float32_values)
arr_of_float64_with_inf = numpy.array([-math.inf, 5.5, math.inf], dtype=numpy.float64)
setattr(msg, 'float64_values', arr_of_float64_with_inf)
assert numpy.array_equal(arr_of_float64_with_inf, msg.float64_values)


def test_bounded_sequences():
Expand Down Expand Up @@ -675,9 +729,18 @@ def test_bounded_sequences():
with pytest.raises(AssertionError):
setattr(msg, 'uint64_values', [-1, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
float32_ieee_max_next = numpy.nextafter(3.402823466e+38, math.inf)
setattr(msg, 'float32_values', [-float32_ieee_max_next, 0.0])
with pytest.raises(AssertionError):
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])
float32_ieee_max_next = numpy.nextafter(3.402823466e+38, math.inf)
setattr(msg, 'float32_values', [0.0, float32_ieee_max_next])

# If target system is IEEE 754 compliant, the next number is rounded to inf.
# Only perform this check on non-compliant systems.
if sys.float_info.max > 1.7976931348623157e+308:
with pytest.raises(AssertionError):
float64_ieee_max_next = numpy.nextafter(1.7976931348623157e+308, math.inf)
setattr(msg, 'float64_values', [-float64_ieee_max_next, 0.0, float64_ieee_max_next])


def test_unbounded_sequences():
Expand Down Expand Up @@ -818,9 +881,15 @@ def test_unbounded_sequences():
with pytest.raises(AssertionError):
setattr(msg, 'uint64_values', [-1, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
with pytest.raises(AssertionError):
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])
float32_ieee_max_next = numpy.nextafter(3.402823466e+38, math.inf)
setattr(msg, 'float32_values', [-float32_ieee_max_next, 0.0, float32_ieee_max_next])

# If target system is IEEE 754 compliant, the next number is rounded to inf.
# Only perform this check on non-compliant systems.
if sys.float_info.max > 1.7976931348623157e+308:
with pytest.raises(AssertionError):
float64_ieee_max_next = numpy.nextafter(1.7976931348623157e+308, math.inf)
setattr(msg, 'float64_values', [-float64_ieee_max_next, 0.0, float64_ieee_max_next])


def test_slot_attributes():
Expand Down