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

Use PyCapsule name to check capsule pointer types #124

Closed
sloretz opened this issue Oct 10, 2017 · 2 comments
Closed

Use PyCapsule name to check capsule pointer types #124

sloretz opened this issue Oct 10, 2017 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@sloretz
Copy link
Contributor

sloretz commented Oct 10, 2017

Feature request

Feature description

Currently rclpy creates PyCapsule with NULL names. This means if a function is given a PyCapsule containing rcl_client_t * but it expects rcl_node_t * the C code will still attempt to use it as such. If the capsules were given a name at creation then the methods could use the name as a type check when getting the pointer.

Implementation considerations

https://docs.python.org/3/c-api/capsule.html
PyCapsule_New accepts a const char *name argument. It could be given a string like "rcl_node_t *".

// rcl_node_t * node;
PyObject * pynode = PyCapsule_New(node, "rcl_node_t *");

PyCapsule_GetPointer accepts a const char *name argument, and it sets a python exception and returns NULL if the name does not match (it uses strcmp() ).

rcl_node_t * node = (rcl_node_t *)PyCapsule_GetPointer(pynode, "rcl_node_t *");
if (!node)
{
  // GetPointer already set an exception
  return NULL;
}

If the default exception isn't appropriate, PyCapsule_GetPointer is guaranteed to succeed if PyCapsule_IsValid returns true. This can be checked first with custom handling code to run if it returns False.

// PyObject * pynode;
if (!PyCapsule_IsValid(pynode, "rcl_node_t *"))
{
  // Do something about it
}
@sloretz sloretz added the enhancement New feature or request label Oct 10, 2017
@mikaelarguedas
Copy link
Member

👍 we discussed it but never got around renaming all the capsules

@mikaelarguedas mikaelarguedas added the ready Work is about to start (Kanban column) label Oct 11, 2017
@sloretz sloretz added in progress Actively being worked on (Kanban column) and removed ready Work is about to start (Kanban column) labels Oct 13, 2017
@mikaelarguedas
Copy link
Member

that's totally done and working now

@mikaelarguedas mikaelarguedas removed the in progress Actively being worked on (Kanban column) label Oct 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants