InsightSoftwareConsortium / InsightSoftwareConsortium/ITK

PyCommand utilizes Object and EventObject parameters

Open
#3,194 0 comments 1 reaction 0 assignees View on GitHub
area:Python wrapping type:Enhancement
Dominant language
C++
Stars
1.7k
Forks
748
Avg merge
1d 1h
Merged PRs (30d)
64

Description

### Description

Currently PyCommand ignores the Command class' Object and EventObject parameters. This prevents doing things like interrogating the EventObject or the callback Object.

### Expected behavior

PyCommand passes Object and EventObject parameters to the user-specified Python callback.

### Actual behavior

PyCommand ignores Object and EventObject parameters from the Command.

### Additional Information

I started to implement allowing for both argument-less callbacks (the current case) and additionally callbacks that would take the Object and EventObject parameters. Where I'm stuck is how to convert the Object * and EventObject & into their SWIG/Python equivalents before calling PyEval_CallObject. For example,

```
void
PyCommand::PyExecute(Object * o, const EventObject & ev)
{
// make sure that the CommandCallable is in fact callable
if (!PyCallable_Check(this->m_Object))
{
// we throw a standard ITK exception: this makes it possible for
// our standard Swig exception handling logic to take this
// through to the invoking Python process
itkExceptionMacro(<< "CommandCallable is not a callable Python object, "
<< "or it has not been set.");
}
else
{
PyGILStateEnsure gil;

PyObject * result = nullptr;
if (GetNumberOfArgs(this->m_Object) == 0)
{ // the callback takes 0 arguments
result = PyEval_CallObject(this->m_Object, (PyObject *)nullptr);
}
else
{
PyObject * args = PyTuple_Pack(o,ev); <<<<<<<<------ how to get o, ev into Python objects
result = PyEval_CallObject(this->m_Object, args);
Py_DECREF(args);
if (result)
{
Py_DECREF(result);
}
else
{
// there was a Python error. Clear the error by printing to stdout
PyErr_Print();
// make sure the invoking Python code knows there was a problem
// by raising an exception
itkExceptionMacro(<< "There was an error executing the "
<< "CommandCallable.");
}
}
}
```

I checked out itkPyImageFilter.h but it seems like it skirts around this issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.