Skip to content

Check return value of Python functions that can fail #139

Closed

Description

@sloretz

This is a tracking ticket for checking return values in rclpy. These functions are used and can fail, but their return value is not being checked.

  • PyList_New
  • PyCapsule_New
  • PyLong_FromUnsignedLongLong
  • PyMem_Malloc
  • PyUnicode_FromString
  • PyLong_FromSize_t
  • PyObject_GetAtterString
  • PyLong_FromLongLong
  • PyCapsule_GetName
  • PyList_Append
  • PyObject_CallObject
  • PyTuple_New
  • PyTuple_SetItem
  • PyList_SetItem
  • PyImport_ImportModule
  • PyObject_SetAttrString

From discussion here
#138 (comment)
#138 (comment)

Labeling this as good first issue. This doesnt imply that a contribution for this should tackle all the functions listed above

Activity

  1. added
    enhancement
    good first issue
    on Mar 1, 2018
  2. darrylbalderas commented on Aug 18, 2018

    @darrylbalderas

    First time contributing to open source and interested in learning more about how to fix this issue.

  3. sloretz commented on Sep 6, 2018

    @sloretz
    ContributorAuthor

    Hi @db118 ,

    This issue is about checking error codes of python functions. Here is an example of error checking done correctly: PyArg_ParseTuple(...) can fail, and if it does the function should return NULL right away.

    rclpy/rclpy/src/rclpy/_rclpy.c

    Lines 1550 to 1552 in 62012d3

    if (!PyArg_ParseTuple(args, "OOOO", &pynode, &pymsg_type, &pytopic, &pyqos_profile)) {
    return NULL;
    }

    Here is an example of what should be fixed: PyList_New() can set an exception and return NULL, so pylist needs to be checked before calling PyList_SET_ITEM() with it.

    rclpy/rclpy/src/rclpy/_rclpy.c

    Lines 1693 to 1697 in 62012d3

    PyObject * pylist = PyList_New(2);
    PyList_SET_ITEM(pylist, 0, PyCapsule_New(client, "rcl_client_t", NULL));
    PyList_SET_ITEM(pylist, 1, PyLong_FromUnsignedLongLong((uint64_t)&client->impl));
    return pylist;

    A good first PR would fix one instance of a function that can fail not being checked.

  4. darrylbalderas commented on Sep 7, 2018

    @darrylbalderas

    @sloretz Ahhh that make sense. Also I don't see a requirements.txt. Is there certain python libraries I need to get this up and running.

  5. darrylbalderas commented on Sep 7, 2018

    @darrylbalderas

    @sloretz
    Do you want me to switch this null error checking

    rclpy/rclpy/src/rclpy/_rclpy.c

    Lines 3642 to 3645 in 62012d3

    value = PyList_New(variant->string_array_value->size);
    if (NULL == value) {
    return NULL;
    }

    to this implementation instead

    rclpy/rclpy/src/rclpy/_rclpy.c

    Lines 1550 to 1552 in 62012d3

    if (!PyArg_ParseTuple(args, "OOOO", &pynode, &pymsg_type, &pytopic, &pyqos_profile)) {
    return NULL;
    }

  6. sloretz commented on Sep 10, 2018

    @sloretz
    ContributorAuthor

    @db118 See the section "Building from source" for instructions to build this library. It lists the dependencies, as well as shows how to build the other ros 2 packages this one depends on. Once you've built everything once, you can build and test just this backage with colcon build --packages-select rclpy and colcon test --event-handlers console_direct+ --packages-select rclpy.

    I don't think I understand the question about swtiching NULL error checking; both of those look fine to me.

  7. suab321321 commented on Jun 23, 2019

    @suab321321
    Contributor

    I would like to help with this one @sloretz should I proceed?

  8. pr-db commented on Aug 12, 2021

    @pr-db

    @sloretz Hello, I'm new to open source and love working with ros. How can I approach this issue?

  9. clalancette commented on Aug 12, 2021

    @clalancette
    Contributor

    Given our recent switch to pybind11 (which gets completed with #814), I actually think this issue is mostly obsolete. @sloretz what do you think? Should we just close this one out?

  10. sloretz commented on Aug 12, 2021

    @sloretz
    ContributorAuthor

    Yup, this one can be closed 🎉

  11. added a commit that references this issue on Nov 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions