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