diff CodeAnalysis/FunctionBody.mustache @ 184:f34f3a149c22 java-code-model

added class PythonThreadsAllower
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jul 2024 15:49:05 +0200
parents f49864df6f1f
children
line wrap: on
line diff
--- a/CodeAnalysis/FunctionBody.mustache	Tue Jul 02 14:52:20 2024 +0200
+++ b/CodeAnalysis/FunctionBody.mustache	Tue Jul 02 15:49:05 2024 +0200
@@ -17,16 +17,20 @@
   }
 {{/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}}
+  long value;
+  {
+    PythonThreadsAllower allower;
+    value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  }
   {{#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}}
+  const char* s;
+  {
+    PythonThreadsAllower allower;
+    s = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  }
   {{#args}}{{release}}{{/args}}
   if (s == NULL)
   {
@@ -39,10 +43,11 @@
   }
 {{/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}}
+  {
+    PythonThreadsAllower allower;
+    s.Assign({{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}));
+  }
   {{#args}}{{release}}{{/args}}
   if (s.GetContent() == NULL)
   {
@@ -55,18 +60,21 @@
   }
 {{/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}}
+  {
+    PythonThreadsAllower allower;
+    {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  }
   {{#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}}
+  OrthancPluginErrorCode code;
+  {
+    PythonThreadsAllower allower;
+    code = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  }
   {{#args}}{{release}}{{/args}}
 
   if (code == OrthancPluginErrorCode_Success)
@@ -82,9 +90,11 @@
 {{/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}}
+  {{return_object}}* obj;
+  {
+    PythonThreadsAllower allower;
+    obj = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  }
   {{#args}}{{release}}{{/args}}
   if (obj == NULL)
   {
@@ -100,10 +110,12 @@
   }
 {{/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}}
+  OrthancPluginErrorCode code;
+  {
+    PythonThreadsAllower allower;
+    code = {{c_function}}(OrthancPlugins::GetGlobalContext(), *buffer{{self}}{{call_args}});
+  }
   {{#args}}{{release}}{{/args}}
   if (code == OrthancPluginErrorCode_Success)
   {
@@ -116,9 +128,11 @@
   }
 {{/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}}
+  {{return_enumeration}} value;
+  {
+    PythonThreadsAllower allower;
+    value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  }
   {{#args}}{{release}}{{/args}}
   return PyLong_FromLong(value);
 {{/return_enumeration}}