Mercurial > hg > orthanc-python
annotate Sources/PythonModule.cpp @ 101:eb6ac5a801d1
update to year 2022
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 23 Feb 2022 07:35:37 +0100 |
parents | 0b3ef425db31 |
children | 65ec5597ec70 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
101 | 3 * Copyright (C) 2020-2022 Osimis S.A., Belgium |
4 * Copyright (C) 2021-2022 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 "PythonModule.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 "PythonString.h" |
25 | |
26 #include <boost/thread/mutex.hpp> | |
27 #include <set> | |
28 | |
29 | |
30 #if PY_VERSION_HEX < 0x03070000 /* 3.7.0 */ | |
31 static PythonObject* GetModule(PythonLock& lock, | |
32 const std::string& utf8Name) | |
33 { | |
34 static boost::mutex mutex_; | |
35 static std::set<std::string> loadedModules_; | |
36 | |
37 boost::mutex::scoped_lock l(mutex_); | |
38 | |
39 if (loadedModules_.find(utf8Name) == loadedModules_.end()) | |
40 { | |
41 PythonString tmp(lock, utf8Name); | |
42 | |
43 // The module is not imported yet | |
44 loadedModules_.insert(utf8Name); | |
45 return new PythonObject(lock, PyImport_Import(tmp.GetPyObject()), false); | |
46 } | |
47 else | |
48 { | |
49 // PyImport_AddModule returns a borrowed reference | |
50 return new PythonObject(lock, PyImport_AddModule(utf8Name.c_str()), true); | |
51 } | |
52 } | |
53 | |
54 #else | |
55 // The "PyImport_GetModule()" function was introduced in Python | |
56 // 3.7.0 | |
57 static PythonObject* GetModule(PythonLock& lock, | |
58 const std::string& utf8Name) | |
59 { | |
60 PythonString tmp(lock, utf8Name); | |
61 | |
62 PyObject* module = PyImport_GetModule(tmp.GetPyObject()); | |
63 if (module != NULL) | |
64 { | |
65 // The module was already imported by a previous call: Reuse it | |
66 return new PythonObject(lock, module); | |
67 } | |
68 else | |
69 { | |
70 // This is the first time this module is used: Import it | |
71 return new PythonObject(lock, PyImport_Import(tmp.GetPyObject())); | |
72 } | |
73 } | |
74 #endif | |
75 | |
76 | |
77 PythonModule::PythonModule(PythonLock& lock, | |
78 const std::string& utf8Name) : | |
79 lock_(lock), | |
80 name_(utf8Name) | |
81 { | |
82 module_.reset(GetModule(lock, utf8Name)); | |
83 } | |
84 | |
85 | |
86 bool PythonModule::IsValid() const | |
87 { | |
88 return (module_.get() != NULL && | |
89 module_->IsValid()); | |
90 } | |
91 | |
92 | |
93 PythonObject& PythonModule::GetObject() const | |
94 { | |
95 if (!IsValid()) | |
96 { | |
97 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
98 } | |
99 else | |
100 { | |
101 return *module_; | |
102 } | |
103 } | |
104 | |
105 | |
106 PyObject* PythonModule::GetPyObject() const | |
107 { | |
108 if (!IsValid()) | |
109 { | |
110 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
111 } | |
112 else | |
113 { | |
114 return module_->GetPyObject(); | |
115 } | |
116 } |