comparison 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
comparison
equal deleted inserted replaced
183:d31f216c9462 184:f34f3a149c22
15 PyErr_SetString(PyExc_TypeError, "Invalid orthanc.{{check_object_type}} object"); 15 PyErr_SetString(PyExc_TypeError, "Invalid orthanc.{{check_object_type}} object");
16 return NULL; 16 return NULL;
17 } 17 }
18 {{/check_object_type}}{{/args}} 18 {{/check_object_type}}{{/args}}
19 {{#return_long}} 19 {{#return_long}}
20 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}} 20 long value;
21 long value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}); 21 {
22 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 22 PythonThreadsAllower allower;
23 value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
24 }
23 {{#args}}{{release}}{{/args}} 25 {{#args}}{{release}}{{/args}}
24 return PyLong_FromLong(value); 26 return PyLong_FromLong(value);
25 {{/return_long}} 27 {{/return_long}}
26 {{#return_static_string}} 28 {{#return_static_string}}
27 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}} 29 const char* s;
28 const char* s = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}); 30 {
29 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 31 PythonThreadsAllower allower;
32 s = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
33 }
30 {{#args}}{{release}}{{/args}} 34 {{#args}}{{release}}{{/args}}
31 if (s == NULL) 35 if (s == NULL)
32 { 36 {
33 Py_INCREF(Py_None); 37 Py_INCREF(Py_None);
34 return Py_None; 38 return Py_None;
37 { 41 {
38 return PyUnicode_FromString(s); 42 return PyUnicode_FromString(s);
39 } 43 }
40 {{/return_static_string}} 44 {{/return_static_string}}
41 {{#return_dynamic_string}} 45 {{#return_dynamic_string}}
42 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
43 OrthancPlugins::OrthancString s; 46 OrthancPlugins::OrthancString s;
44 s.Assign({{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}})); 47 {
45 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 48 PythonThreadsAllower allower;
49 s.Assign({{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}));
50 }
46 {{#args}}{{release}}{{/args}} 51 {{#args}}{{release}}{{/args}}
47 if (s.GetContent() == NULL) 52 if (s.GetContent() == NULL)
48 { 53 {
49 PythonLock::RaiseException(OrthancPluginErrorCode_InternalError); 54 PythonLock::RaiseException(OrthancPluginErrorCode_InternalError);
50 return NULL; 55 return NULL;
53 { 58 {
54 return PyUnicode_FromString(s.GetContent()); 59 return PyUnicode_FromString(s.GetContent());
55 } 60 }
56 {{/return_dynamic_string}} 61 {{/return_dynamic_string}}
57 {{#return_void}} 62 {{#return_void}}
58 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}} 63 {
59 {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}); 64 PythonThreadsAllower allower;
60 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 65 {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
66 }
61 {{#args}}{{release}}{{/args}} 67 {{#args}}{{release}}{{/args}}
62 68
63 Py_INCREF(Py_None); 69 Py_INCREF(Py_None);
64 return Py_None; 70 return Py_None;
65 {{/return_void}} 71 {{/return_void}}
66 {{#return_error}} 72 {{#return_error}}
67 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}} 73 OrthancPluginErrorCode code;
68 OrthancPluginErrorCode code = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}); 74 {
69 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 75 PythonThreadsAllower allower;
76 code = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
77 }
70 {{#args}}{{release}}{{/args}} 78 {{#args}}{{release}}{{/args}}
71 79
72 if (code == OrthancPluginErrorCode_Success) 80 if (code == OrthancPluginErrorCode_Success)
73 { 81 {
74 Py_INCREF(Py_None); 82 Py_INCREF(Py_None);
80 return NULL; 88 return NULL;
81 } 89 }
82 {{/return_error}} 90 {{/return_error}}
83 {{#return_object}} 91 {{#return_object}}
84 // This is the case of a constructor 92 // This is the case of a constructor
85 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}} 93 {{return_object}}* obj;
86 {{return_object}}* obj = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}); 94 {
87 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 95 PythonThreadsAllower allower;
96 obj = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
97 }
88 {{#args}}{{release}}{{/args}} 98 {{#args}}{{release}}{{/args}}
89 if (obj == NULL) 99 if (obj == NULL)
90 { 100 {
91 PythonLock::RaiseException(OrthancPluginErrorCode_InternalError); 101 PythonLock::RaiseException(OrthancPluginErrorCode_InternalError);
92 return NULL; 102 return NULL;
98 Py_DECREF(argList); 108 Py_DECREF(argList);
99 return python; 109 return python;
100 } 110 }
101 {{/return_object}} 111 {{/return_object}}
102 {{#return_bytes}} 112 {{#return_bytes}}
103 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
104 OrthancPlugins::MemoryBuffer buffer; 113 OrthancPlugins::MemoryBuffer buffer;
105 OrthancPluginErrorCode code = {{c_function}}(OrthancPlugins::GetGlobalContext(), *buffer{{self}}{{call_args}}); 114 OrthancPluginErrorCode code;
106 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 115 {
116 PythonThreadsAllower allower;
117 code = {{c_function}}(OrthancPlugins::GetGlobalContext(), *buffer{{self}}{{call_args}});
118 }
107 {{#args}}{{release}}{{/args}} 119 {{#args}}{{release}}{{/args}}
108 if (code == OrthancPluginErrorCode_Success) 120 if (code == OrthancPluginErrorCode_Success)
109 { 121 {
110 return PyBytes_FromStringAndSize(buffer.GetData(), buffer.GetSize()); 122 return PyBytes_FromStringAndSize(buffer.GetData(), buffer.GetSize());
111 } 123 }
114 PythonLock::RaiseException(code); 126 PythonLock::RaiseException(code);
115 return NULL; 127 return NULL;
116 } 128 }
117 {{/return_bytes}} 129 {{/return_bytes}}
118 {{#return_enumeration}} 130 {{#return_enumeration}}
119 {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}} 131 {{return_enumeration}} value;
120 {{return_enumeration}} value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}); 132 {
121 {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}} 133 PythonThreadsAllower allower;
134 value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
135 }
122 {{#args}}{{release}}{{/args}} 136 {{#args}}{{release}}{{/args}}
123 return PyLong_FromLong(value); 137 return PyLong_FromLong(value);
124 {{/return_enumeration}} 138 {{/return_enumeration}}