Mercurial > hg > orthanc-python
annotate Sources/PythonFunction.cpp @ 155:71d305c29cfa
updated year to 2024
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 16 Mar 2024 11:58:51 +0100 |
parents | 65ec5597ec70 |
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 "PythonFunction.h" | |
22 | |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
23 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
0 | 24 #include "PythonModule.h" |
25 | |
26 | |
27 PythonObject* PythonFunction::CallUnchecked(PyObject* args) | |
28 { | |
29 if (!IsValid()) | |
30 { | |
31 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
32 } | |
33 else | |
34 { | |
35 PyObject* obj = PyObject_CallObject(func_->GetPyObject(), args); | |
36 return new PythonObject(lock_, obj); | |
37 } | |
38 } | |
39 | |
40 | |
41 PythonFunction::PythonFunction(PythonLock& lock, | |
13 | 42 const PythonModule& module, |
0 | 43 const std::string& name) : |
44 lock_(lock) | |
45 { | |
46 if (module.IsValid() && | |
47 // This check is necessary in Python 2.7, otherwise garbage collector might crash | |
48 PyObject_HasAttrString(module.GetPyObject(), name.c_str())) | |
49 { | |
50 func_.reset(module.GetObject().GetAttribute(name)); | |
51 | |
52 if (func_.get() == NULL || | |
53 !func_->IsValid() || | |
54 !PyCallable_Check(func_->GetPyObject())) | |
55 { | |
56 func_.reset(); // Not such a function | |
57 OrthancPlugins::LogWarning("Missing Python function: " + module.GetName() + | |
58 "." + name + "()"); | |
59 } | |
60 } | |
61 } | |
62 | |
63 | |
64 PythonObject* PythonFunction::Call() | |
65 { | |
66 std::unique_ptr<PythonObject> result(CallUnchecked(NULL)); | |
67 | |
68 std::string error; | |
69 if (lock_.HasErrorOccurred(error)) | |
70 { | |
71 OrthancPlugins::LogError("Python exception has occurred, traceback:\n" + error); | |
72 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
73 } | |
74 else | |
75 { | |
76 return result.release(); | |
77 } | |
78 } | |
79 | |
80 | |
13 | 81 PythonObject* PythonFunction::Call(const PythonObject& args) |
0 | 82 { |
83 std::unique_ptr<PythonObject> result(CallUnchecked(args.GetPyObject())); | |
84 | |
85 std::string error; | |
86 if (lock_.HasErrorOccurred(error)) | |
87 { | |
88 OrthancPlugins::LogError("Python exception has occurred, traceback:\n" + error); | |
89 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
90 } | |
91 else | |
92 { | |
93 return result.release(); | |
94 } | |
95 } |