diff --git a/rclpy/src/rclpy/_rclpy.c b/rclpy/src/rclpy/_rclpy.c index b92426755..ee79612e3 100644 --- a/rclpy/src/rclpy/_rclpy.c +++ b/rclpy/src/rclpy/_rclpy.c @@ -501,6 +501,10 @@ rclpy_init(PyObject * Py_UNUSED(self), PyObject * args) } // Borrows a pointer, do not free arg_values[i] arg_values[i] = PyUnicode_AsUTF8(pyarg); + if (NULL == arg_values[i]) { + have_args = false; + break; + } } } diff --git a/rclpy/test/test_init_shutdown.py b/rclpy/test/test_init_shutdown.py index ee14c6797..e8360ccec 100644 --- a/rclpy/test/test_init_shutdown.py +++ b/rclpy/test/test_init_shutdown.py @@ -33,11 +33,25 @@ def func_init(): rclpy.init(context=context) except RuntimeError: return False - rclpy.shutdown(context=context) return True +def func_init_with_non_utf8_arguments(): + import rclpy + context = rclpy.context.Context() + # Embed non decodable characters e.g. due to + # wrong locale settings. + # See PEP-383 for further reference. + args = ['my-node.py', 'Ragnar\udcc3\udcb6k'] + try: + rclpy.init(context=context, args=args) + except UnicodeEncodeError: + return True + rclpy.shutdown(context=context) + return False + + def func_init_shutdown(): import rclpy context = rclpy.context.Context() @@ -124,6 +138,10 @@ def test_init(): func_launch(func_init, 'failed to initialize rclpy') +def test_init_with_non_utf8_arguments(): + func_launch(func_init_with_non_utf8_arguments, 'handled non UTF-8 arguments') + + def test_init_shutdown(): func_launch(func_init_shutdown, 'failed to shutdown rclpy')