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