Mercurial > hg > orthanc-python
annotate Sources/PythonFunction.cpp @ 95:c17cdaf687e3
use debian:buster-slim to compile Windows binaries
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 15 Sep 2021 11:52:30 +0200 |
parents | 0b3ef425db31 |
children | eb6ac5a801d1 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
56
23f3099bed47
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
3 * Copyright (C) 2020-2021 Osimis S.A., Belgium |
0 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
20 #include "PythonFunction.h" | |
21 | |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
0 | 23 #include "PythonModule.h" |
24 | |
25 | |
26 PythonObject* PythonFunction::CallUnchecked(PyObject* args) | |
27 { | |
28 if (!IsValid()) | |
29 { | |
30 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
31 } | |
32 else | |
33 { | |
34 PyObject* obj = PyObject_CallObject(func_->GetPyObject(), args); | |
35 return new PythonObject(lock_, obj); | |
36 } | |
37 } | |
38 | |
39 | |
40 PythonFunction::PythonFunction(PythonLock& lock, | |
13 | 41 const PythonModule& module, |
0 | 42 const std::string& name) : |
43 lock_(lock) | |
44 { | |
45 if (module.IsValid() && | |
46 // This check is necessary in Python 2.7, otherwise garbage collector might crash | |
47 PyObject_HasAttrString(module.GetPyObject(), name.c_str())) | |
48 { | |
49 func_.reset(module.GetObject().GetAttribute(name)); | |
50 | |
51 if (func_.get() == NULL || | |
52 !func_->IsValid() || | |
53 !PyCallable_Check(func_->GetPyObject())) | |
54 { | |
55 func_.reset(); // Not such a function | |
56 OrthancPlugins::LogWarning("Missing Python function: " + module.GetName() + | |
57 "." + name + "()"); | |
58 } | |
59 } | |
60 } | |
61 | |
62 | |
63 PythonObject* PythonFunction::Call() | |
64 { | |
65 std::unique_ptr<PythonObject> result(CallUnchecked(NULL)); | |
66 | |
67 std::string error; | |
68 if (lock_.HasErrorOccurred(error)) | |
69 { | |
70 OrthancPlugins::LogError("Python exception has occurred, traceback:\n" + error); | |
71 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
72 } | |
73 else | |
74 { | |
75 return result.release(); | |
76 } | |
77 } | |
78 | |
79 | |
13 | 80 PythonObject* PythonFunction::Call(const PythonObject& args) |
0 | 81 { |
82 std::unique_ptr<PythonObject> result(CallUnchecked(args.GetPyObject())); | |
83 | |
84 std::string error; | |
85 if (lock_.HasErrorOccurred(error)) | |
86 { | |
87 OrthancPlugins::LogError("Python exception has occurred, traceback:\n" + error); | |
88 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
89 } | |
90 else | |
91 { | |
92 return result.release(); | |
93 } | |
94 } |