annotate Sources/PythonModule.cpp @ 93:28f354fd36fa OrthancPython-3.4

OrthancPython-3.4
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 31 Aug 2021 14:08:43 +0200
parents 0b3ef425db31
children eb6ac5a801d1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 *
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * modify it under the terms of the GNU Affero General Public License
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * as published by the Free Software Foundation, either version 3 of
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * the License, or (at your option) any later version.
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 *
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful, but
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * Affero General Public License for more details.
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 *
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU Affero General Public License
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 **/
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 #include "PythonModule.h"
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "PythonString.h"
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <boost/thread/mutex.hpp>
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <set>
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #if PY_VERSION_HEX < 0x03070000 /* 3.7.0 */
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 static PythonObject* GetModule(PythonLock& lock,
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 const std::string& utf8Name)
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 static boost::mutex mutex_;
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 static std::set<std::string> loadedModules_;
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 boost::mutex::scoped_lock l(mutex_);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 if (loadedModules_.find(utf8Name) == loadedModules_.end())
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 PythonString tmp(lock, utf8Name);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 // The module is not imported yet
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 loadedModules_.insert(utf8Name);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 return new PythonObject(lock, PyImport_Import(tmp.GetPyObject()), false);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 else
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 // PyImport_AddModule returns a borrowed reference
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 return new PythonObject(lock, PyImport_AddModule(utf8Name.c_str()), true);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 #else
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 // The "PyImport_GetModule()" function was introduced in Python
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 // 3.7.0
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 static PythonObject* GetModule(PythonLock& lock,
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 const std::string& utf8Name)
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 PythonString tmp(lock, utf8Name);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 PyObject* module = PyImport_GetModule(tmp.GetPyObject());
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 if (module != NULL)
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 // The module was already imported by a previous call: Reuse it
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 return new PythonObject(lock, module);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 else
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 // This is the first time this module is used: Import it
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 return new PythonObject(lock, PyImport_Import(tmp.GetPyObject()));
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 #endif
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 PythonModule::PythonModule(PythonLock& lock,
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 const std::string& utf8Name) :
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 lock_(lock),
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 name_(utf8Name)
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 module_.reset(GetModule(lock, utf8Name));
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 bool PythonModule::IsValid() const
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 return (module_.get() != NULL &&
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 module_->IsValid());
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 PythonObject& PythonModule::GetObject() const
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 if (!IsValid())
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 else
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 return *module_;
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 PyObject* PythonModule::GetPyObject() const
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 if (!IsValid())
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls);
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 else
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 return module_->GetPyObject();
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
7ed502b17b8f initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }