| 93 | } |
| 94 | |
| 95 | void |
| 96 | PyCommand::Execute() |
| 97 | { |
| 98 | // if null do nothing |
| 99 | if (!this->m_Object) |
| 100 | { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // make sure that the CommandCallable is in fact callable |
| 105 | if (!PyCallable_Check(this->m_Object)) |
| 106 | { |
| 107 | // we throw a standard ITK exception: this makes it possible for |
| 108 | // our standard CableSwig exception handling logic to take this |
| 109 | // through to the invoking Python process |
| 110 | sitkExceptionMacro(<< "Python Callable is not a callable Python object, " |
| 111 | << "or it has not been set."); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | PyGILStateEnsure gil; |
| 116 | |
| 117 | PyObject * result; |
| 118 | |
| 119 | result = PyObject_CallObject(this->m_Object, (PyObject *)NULL); |
| 120 | |
| 121 | if (result) |
| 122 | { |
| 123 | Py_DECREF(result); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | // there was a Python error. Clear the error by printing to stdout |
| 128 | PyErr_Print(); |
| 129 | // make sure the invoking Python code knows there was a problem |
| 130 | // by raising an exception |
| 131 | sitkExceptionMacro(<< "There was an error executing the " |
| 132 | << "Python Callable."); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | } // namespace simple |
no outgoing calls