view CodeAnalysis/FunctionBody.mustache @ 179:f49864df6f1f java-code-model

trying Py_BEGIN_ALLOW_THREADS
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 27 Jun 2024 21:53:03 +0200
parents 8382c7dea471
children f34f3a149c22
line wrap: on
line source

{{#args}}
  {{python_type}} {{name}}{{initialization}};
{{/args}}

{{#has_args}}
  if (!PyArg_ParseTuple(args, {{tuple_format}}))
  {
    PyErr_SetString(PyExc_TypeError, "Bad types for the arguments ({{count_args}} arguments expected)");
    return NULL;
  }
{{/has_args}}
{{#args}}{{#check_object_type}}
  if ({{name}} != Py_None && Py_TYPE({{name}}) != Get{{check_object_type}}Type())
  {
    PyErr_SetString(PyExc_TypeError, "Invalid orthanc.{{check_object_type}} object");
    return NULL;
  }
{{/check_object_type}}{{/args}}
{{#return_long}}
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  long value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}
  return PyLong_FromLong(value);
{{/return_long}}
{{#return_static_string}}
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  const char* s = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}
  if (s == NULL)
  {
    Py_INCREF(Py_None);
    return Py_None;
  }
  else
  {
    return PyUnicode_FromString(s);
  }
{{/return_static_string}}
{{#return_dynamic_string}}
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  OrthancPlugins::OrthancString s;
  s.Assign({{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}));
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}
  if (s.GetContent() == NULL)
  {
    PythonLock::RaiseException(OrthancPluginErrorCode_InternalError);
    return NULL;
  }
  else
  {
    return PyUnicode_FromString(s.GetContent());
  }
{{/return_dynamic_string}}
{{#return_void}}
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}

  Py_INCREF(Py_None);
  return Py_None;
{{/return_void}}
{{#return_error}}
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  OrthancPluginErrorCode code = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}

  if (code == OrthancPluginErrorCode_Success)
  {
    Py_INCREF(Py_None);
    return Py_None;
  }
  else
  {
    PythonLock::RaiseException(code);
    return NULL;
  }
{{/return_error}}
{{#return_object}}
  // This is the case of a constructor
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  {{return_object}}* obj = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}
  if (obj == NULL)
  {
    PythonLock::RaiseException(OrthancPluginErrorCode_InternalError);
    return NULL;  
  }
  else
  {
    PyObject *argList = Py_BuildValue("Lb", obj, false /* not borrowed */);
    PyObject *python = PyObject_CallObject((PyObject *) &sdk_{{return_object}}_Type, argList);
    Py_DECREF(argList);
    return python;
  }
{{/return_object}}
{{#return_bytes}}
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  OrthancPlugins::MemoryBuffer buffer;
  OrthancPluginErrorCode code = {{c_function}}(OrthancPlugins::GetGlobalContext(), *buffer{{self}}{{call_args}});
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}
  if (code == OrthancPluginErrorCode_Success)
  {
    return PyBytes_FromStringAndSize(buffer.GetData(), buffer.GetSize());
  }
  else
  {
    PythonLock::RaiseException(code);
    return NULL;  
  }
{{/return_bytes}}
{{#return_enumeration}}
  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
  {{return_enumeration}} value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
  {{#args}}{{release}}{{/args}}
  return PyLong_FromLong(value);
{{/return_enumeration}}