Mercurial > hg > orthanc-python
annotate Sources/PythonLock.cpp @ 164:519e285ce627 OrthancPython-4.2
hotfix to add Windows builders for Python 3.12
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 16 May 2024 23:09:09 +0200 |
parents | 71d305c29cfa |
children | 6fada29b6759 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
155
71d305c29cfa
updated year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
114
diff
changeset
|
3 * Copyright (C) 2020-2024 Osimis S.A., Belgium |
71d305c29cfa
updated year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
114
diff
changeset
|
4 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU Affero General Public License | |
8 * as published by the Free Software Foundation, either version 3 of | |
9 * the License, or (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "PythonLock.h" | |
22 | |
23 #include "PythonFunction.h" | |
24 #include "PythonModule.h" | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
25 #include "PythonString.h" |
0 | 26 |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
27 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
28 |
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
29 #include <Compatibility.h> // For std::unique_ptr<> |
0 | 30 |
31 #include <boost/thread/mutex.hpp> | |
32 | |
33 static boost::mutex mutex_; | |
34 static PyThreadState* interpreterState_ = NULL; | |
35 static PythonLock::ModuleFunctionsInstaller moduleFunctions_ = NULL; | |
36 static PythonLock::ModuleClassesInstaller moduleClasses_ = NULL; | |
37 static std::string moduleName_; | |
38 static std::string exceptionName_; | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
39 static bool verbose_ = false; |
0 | 40 |
41 | |
42 struct module_state | |
43 { | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
44 PyObject *exceptionObject_ = NULL; |
0 | 45 }; |
46 | |
47 #if PY_MAJOR_VERSION >= 3 | |
48 # define GETSTATE(module) ((struct module_state*) PyModule_GetState(module)) | |
49 #else | |
50 # define GETSTATE(module) (&_state) | |
51 static struct module_state _state; | |
52 #endif | |
53 | |
54 | |
13 | 55 PythonLock::PythonLock() : |
56 gstate_(PyGILState_Ensure()) | |
0 | 57 { |
58 //OrthancPlugins::LogInfo("Python lock (GIL) acquired"); | |
59 } | |
60 | |
61 | |
62 PythonLock::~PythonLock() | |
63 { | |
64 PyGILState_Release(gstate_); | |
65 //OrthancPlugins::LogInfo("Python lock (GIL) released"); | |
66 } | |
67 | |
68 | |
69 void PythonLock::ExecuteCommand(const std::string& s) | |
70 { | |
71 if (PyRun_SimpleString(s.c_str()) != 0) | |
72 { | |
73 OrthancPlugins::LogError("Error while executing a Python command"); | |
74 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
75 } | |
76 } | |
77 | |
78 | |
79 void PythonLock::ExecuteFile(const std::string& path) | |
80 { | |
81 OrthancPlugins::MemoryBuffer buffer; | |
82 buffer.ReadFile(path); | |
83 | |
84 std::string script; | |
85 buffer.ToString(script); | |
86 | |
87 ExecuteCommand(script); | |
88 } | |
89 | |
90 | |
91 bool PythonLock::HasErrorOccurred(std::string& target) | |
92 { | |
93 if (PyErr_Occurred()) | |
94 { | |
95 PyObject *exceptionType = NULL; | |
96 PyObject *exceptionValue = NULL; | |
97 PyObject *traceback = NULL; | |
98 PyErr_Fetch(&exceptionType, &exceptionValue, &traceback); | |
99 | |
100 if (exceptionType == NULL) | |
101 { | |
102 return false; | |
103 } | |
104 | |
105 PyErr_NormalizeException(&exceptionType, &exceptionValue, &traceback); | |
106 | |
107 #if PY_MAJOR_VERSION >= 3 | |
108 if (traceback != NULL) | |
109 { | |
110 PyException_SetTraceback(exceptionValue, traceback); | |
111 } | |
112 #endif | |
113 | |
114 if (exceptionType != NULL) | |
115 { | |
116 PythonObject temp(*this, PyObject_Str(exceptionType)); | |
117 std::string s; | |
118 if (temp.ToUtf8String(s)) | |
119 { | |
120 target += s + "\n"; | |
121 } | |
122 } | |
123 | |
124 if (exceptionValue != NULL) | |
125 { | |
126 PythonObject temp(*this, PyObject_Str(exceptionValue)); | |
127 std::string s; | |
128 if (temp.ToUtf8String(s)) | |
129 { | |
130 target += s + "\n"; | |
131 } | |
132 } | |
133 | |
134 { | |
135 PythonModule module(*this, "traceback"); | |
136 PythonFunction f(*this, module, "format_tb"); | |
137 | |
138 if (traceback != NULL && | |
139 f.IsValid()) | |
140 { | |
141 PythonObject args(*this, PyTuple_New(1)); | |
142 PyTuple_SetItem(args.GetPyObject(), 0, traceback); | |
143 | |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
144 std::unique_ptr<PythonObject> value(f.CallUnchecked(args.GetPyObject())); |
0 | 145 |
146 if (value->IsValid()) | |
147 { | |
148 Py_ssize_t len = PyList_Size(value->GetPyObject()); | |
149 for (Py_ssize_t i = 0; i < len; i++) | |
150 { | |
151 PythonObject item(*this, PyList_GetItem(value->GetPyObject(), i), true /* borrowed */); | |
152 std::string line; | |
153 if (item.ToUtf8String(line)) | |
154 { | |
155 target += "\n" + line; | |
156 } | |
157 } | |
158 } | |
159 } | |
160 } | |
161 | |
162 | |
163 /** | |
164 * "This call takes away a reference to each object: you must own | |
165 * a reference to each object before the call and after the call | |
166 * you no longer own these references. (If you don't understand | |
167 * this, don't use this function. I warned you.)" | |
168 * => I don't use PyErr_Restore() | |
169 **/ | |
170 | |
171 //PyErr_Restore(exceptionType, exceptionValue, traceback); | |
172 //PyErr_Clear(); | |
173 | |
174 return true; | |
175 } | |
176 else | |
177 { | |
178 return false; | |
179 } | |
180 } | |
181 | |
182 | |
183 static void RegisterException(PyObject* module, | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
184 const std::string& fqnName, |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
185 const std::string& shortName) |
0 | 186 { |
187 struct module_state *state = GETSTATE(module); | |
188 | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
189 state->exceptionObject_ = PyErr_NewException(const_cast<char*>(fqnName.c_str()), NULL, NULL); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
190 if (state->exceptionObject_ == NULL) |
0 | 191 { |
192 Py_DECREF(module); | |
193 OrthancPlugins::LogError("Cannot create the Python exception class"); | |
194 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
195 } | |
196 | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
197 Py_XINCREF(state->exceptionObject_); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
198 if (PyModule_AddObject(module, shortName.c_str(), state->exceptionObject_) < 0) |
0 | 199 { |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
200 Py_XDECREF(state->exceptionObject_); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
201 Py_CLEAR(state->exceptionObject_); |
0 | 202 OrthancPlugins::LogError("Cannot create the Python exception class"); |
203 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
204 } | |
205 } | |
206 | |
207 | |
208 | |
209 #if PY_MAJOR_VERSION >= 3 | |
210 | |
211 static int sdk_traverse(PyObject *module, visitproc visit, void *arg) | |
212 { | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
213 Py_VISIT(GETSTATE(module)->exceptionObject_); |
0 | 214 return 0; |
215 } | |
216 | |
217 static int sdk_clear(PyObject *module) | |
218 { | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
219 Py_CLEAR(GETSTATE(module)->exceptionObject_); |
0 | 220 return 0; |
221 } | |
222 | |
223 static struct PyModuleDef moduledef = | |
224 { | |
225 PyModuleDef_HEAD_INIT, | |
226 NULL, /* m_name => TO BE FILLED */ | |
227 NULL, | |
228 sizeof(struct module_state), | |
229 NULL, /* m_methods => TO BE FILLED */ | |
230 NULL, | |
231 sdk_traverse, | |
232 sdk_clear, | |
233 NULL | |
234 }; | |
235 | |
236 | |
237 PyMODINIT_FUNC InitializeModule() | |
238 { | |
239 if (moduleFunctions_ == NULL || | |
240 moduleClasses_ == NULL || | |
241 moduleName_.empty() || | |
242 exceptionName_.empty()) | |
243 { | |
244 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
245 } | |
246 | |
247 moduledef.m_name = moduleName_.c_str(); | |
248 moduledef.m_methods = moduleFunctions_(); | |
249 | |
250 PyObject *module = PyModule_Create(&moduledef); | |
251 if (module == NULL) | |
252 { | |
253 OrthancPlugins::LogError("Cannot create a Python module"); | |
254 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
255 } | |
256 | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
257 RegisterException(module, moduleName_ + "." + exceptionName_, exceptionName_); |
0 | 258 moduleClasses_(module); |
259 | |
260 return module; | |
261 } | |
262 | |
263 #else | |
264 | |
265 void InitializeModule() | |
266 { | |
267 if (moduleFunctions_ == NULL || | |
268 moduleClasses_ == NULL || | |
269 moduleName_.empty() || | |
270 exceptionName_.empty()) | |
271 { | |
272 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
273 } | |
274 | |
275 PyObject *module = Py_InitModule(moduleName_.c_str(), moduleFunctions_()); | |
276 if (module == NULL) | |
277 { | |
278 OrthancPlugins::LogError("Cannot create a Python module"); | |
279 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
280 } | |
281 | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
282 RegisterException(module, moduleName_ + "." + exceptionName_, exceptionName_); |
0 | 283 moduleClasses_(module); |
284 } | |
285 | |
286 #endif | |
287 | |
288 | |
289 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
290 OrthancPluginErrorCode PythonLock::CheckCallbackSuccess(const std::string& callbackDetails) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
291 { |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
292 std::string traceback; |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
293 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
294 if (HasErrorOccurred(traceback)) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
295 { |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
296 OrthancPlugins::LogError("Error in the " + callbackDetails + ", traceback:\n" + traceback); |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
297 return OrthancPluginErrorCode_Plugin; |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
298 } |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
299 else |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
300 { |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
301 return OrthancPluginErrorCode_Success; |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
302 } |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
303 } |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
304 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
305 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
306 |
0 | 307 void PythonLock::GlobalInitialize(const std::string& moduleName, |
308 const std::string& exceptionName, | |
309 ModuleFunctionsInstaller moduleFunctions, | |
310 ModuleClassesInstaller moduleClasses, | |
311 bool verbose) | |
312 { | |
313 boost::mutex::scoped_lock lock(mutex_); | |
314 | |
315 if (interpreterState_ != NULL) | |
316 { | |
317 OrthancPlugins::LogError("Cannot initialize twice the Python interpreter"); | |
318 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
319 } | |
320 | |
321 if (moduleClasses == NULL || | |
322 moduleFunctions == NULL) | |
323 { | |
324 ORTHANC_PLUGINS_THROW_EXCEPTION(NullPointer); | |
325 } | |
326 | |
327 if (moduleName.empty() || | |
328 exceptionName.empty()) | |
329 { | |
330 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
331 } | |
332 | |
333 if (exceptionName.find('.') != std::string::npos) | |
334 { | |
335 OrthancPlugins::LogError("The name of the exception cannot contain \".\", found: " + | |
336 exceptionName); | |
337 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
338 } | |
339 | |
340 moduleClasses_ = moduleClasses; | |
341 moduleFunctions_ = moduleFunctions; | |
342 moduleName_ = moduleName; | |
343 exceptionName_ = exceptionName; | |
344 | |
345 std::string executable; | |
346 | |
347 { | |
348 OrthancPlugins::OrthancString str; | |
349 str.Assign(OrthancPluginGetOrthancPath(OrthancPlugins::GetGlobalContext())); | |
350 str.ToString(executable); | |
351 } | |
352 | |
353 OrthancPlugins::LogWarning("Program name: " + executable); | |
354 | |
355 #if PY_MAJOR_VERSION == 2 | |
356 Py_SetProgramName(&executable[0]); /* optional but recommended */ | |
357 #else | |
358 std::wstring wide(executable.begin(), executable.end()); | |
359 Py_SetProgramName(&wide[0]); /* optional but recommended */ | |
360 Py_UnbufferedStdioFlag = 1; // Write immediately to stdout after "sys.stdout.flush()" (only in Python 3.x) | |
361 #endif | |
362 | |
363 Py_InspectFlag = 1; // Don't exit the Orthanc process on Python error | |
364 | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
365 verbose_ = verbose; |
0 | 366 if (verbose) |
367 { | |
368 Py_VerboseFlag = 1; | |
369 } | |
370 | |
371 #if PY_MAJOR_VERSION >= 3 | |
372 PyImport_AppendInittab(moduleName_.c_str(), InitializeModule); | |
373 #endif | |
374 | |
375 #if PY_VERSION_HEX < 0x03020000 /* 3.2.0 */ | |
376 /** | |
377 * "Changed in version 3.2: This function cannot be called before | |
378 * Py_Initialize() anymore." | |
379 **/ | |
380 if (!PyEval_ThreadsInitialized()) | |
381 { | |
382 PyEval_InitThreads(); | |
383 } | |
384 #endif | |
385 | |
386 Py_InitializeEx(0 /* Python is embedded, skip signal handlers */); | |
387 | |
12 | 388 #if 0 |
0 | 389 #if PY_MAJOR_VERSION == 2 |
390 std::cout << Py_GetPath() << std::endl; | |
391 std::cout << Py_GetProgramName() << std::endl; | |
392 std::cout << Py_GetProgramFullPath() << std::endl; | |
393 #else | |
394 std::wcout << Py_GetPath() << std::endl; | |
395 std::wcout << Py_GetProgramName() << std::endl; | |
396 std::wcout << Py_GetProgramFullPath() << std::endl; | |
397 #endif | |
12 | 398 #endif |
0 | 399 |
400 #if (PY_VERSION_HEX >= 0x03020000 /* 3.2.0 */ && \ | |
401 PY_VERSION_HEX < 0x03070000 /* 3.7.0 */) | |
402 /** | |
403 * Changed in version 3.7: This function is now called by | |
404 * Py_Initialize(), so you don't have to call it yourself anymore. | |
405 **/ | |
406 if (!PyEval_ThreadsInitialized()) | |
407 { | |
408 PyEval_InitThreads(); | |
409 } | |
410 #endif | |
411 | |
412 | |
413 #if PY_MAJOR_VERSION == 2 | |
414 InitializeModule(); | |
415 #endif | |
416 | |
417 // https://fr.slideshare.net/YiLungTsai/embed-python => slide 26 | |
418 interpreterState_ = PyEval_SaveThread(); | |
419 } | |
420 | |
421 | |
422 void PythonLock::AddSysPath(const std::string& path) | |
423 { | |
424 /** | |
425 * "It is recommended that applications embedding the Python | |
426 * interpreter for purposes other than executing a single script | |
427 * pass 0 as updatepath, and update sys.path themselves if | |
428 * desired. See CVE-2008-5983." | |
429 * => Set "sys.path.append()" to the directory of the configuration file by default | |
430 **/ | |
431 | |
432 PythonLock lock; | |
433 | |
434 /** | |
435 * Initialization of "sys.path.append()" must be done before loading | |
436 * any module. | |
437 **/ | |
438 | |
439 PyObject *sysPath = PySys_GetObject(const_cast<char*>("path")); | |
440 if (sysPath == NULL) | |
441 { | |
442 OrthancPlugins::LogError("Cannot find sys.path"); | |
443 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
444 } | |
445 | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
446 PythonString pyPath(lock, path); |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
447 int result = PyList_Insert(sysPath, 0, pyPath.Release()); |
0 | 448 |
449 if (result != 0) | |
450 { | |
451 OrthancPlugins::LogError("Cannot run sys.path.append()"); | |
452 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
453 } | |
454 } | |
455 | |
456 | |
457 void PythonLock::GlobalFinalize() | |
458 { | |
459 boost::mutex::scoped_lock lock(mutex_); | |
460 | |
461 if (interpreterState_ != NULL) | |
462 { | |
463 PyEval_RestoreThread(interpreterState_); | |
464 interpreterState_ = NULL; | |
465 } | |
466 | |
467 Py_Finalize(); | |
468 } | |
469 | |
470 | |
75
cbfc72a53970
refactoring calls to PythonLock::RaiseException()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
471 void PythonLock::RaiseException(OrthancPluginErrorCode code) |
0 | 472 { |
473 if (code != OrthancPluginErrorCode_Success) | |
474 { | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
475 if (0) |
0 | 476 { |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
477 // This was the implementation in versions <= 3.2 of the Python plugin |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
478 PyErr_SetString(PyExc_ValueError, "Internal error"); |
0 | 479 } |
480 else | |
481 { | |
77
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
482 // "Python custom exceptions in C(++) extensions" |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
483 // https://www.pierov.org/2020/03/01/python-custom-exceptions-c-extensions/ |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
484 |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
485 const char* message = OrthancPluginGetErrorDescription(OrthancPlugins::GetGlobalContext(), code); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
486 |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
487 PythonLock lock; |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
488 |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
489 #if PY_MAJOR_VERSION >= 3 |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
490 PythonModule module(lock, moduleName_); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
491 #endif |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
492 |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
493 struct module_state *state = GETSTATE(module.GetPyObject()); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
494 if (state->exceptionObject_ == NULL) |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
495 { |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
496 OrthancPlugins::LogError("No Python exception has been registered"); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
497 } |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
498 else |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
499 { |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
500 PythonString str(lock, message); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
501 |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
502 PyObject *exception = PyTuple_New(2); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
503 PyTuple_SetItem(exception, 0, PyLong_FromLong(code)); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
504 PyTuple_SetItem(exception, 1, str.Release()); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
505 PyErr_SetObject(state->exceptionObject_, exception); |
e7ff5efb100d
Custom exception "orthanc.OrthancException" is raised instead of "ValueError"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
506 } |
0 | 507 } |
508 } | |
509 } | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
510 |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
511 |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
512 void PythonLock::LogCall(const std::string& message) |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
513 { |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
514 /** |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
515 * For purity, this function should lock the global "mutex_", but |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
516 * "verbose_" cannot change after the initial call to |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
517 * "GlobalInitialize()". |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
518 **/ |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
519 |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
520 if (verbose_) |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
521 { |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
522 OrthancPlugins::LogInfo(message); |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
523 } |
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
524 } |