Mercurial > hg > orthanc-python
annotate Sources/PythonObject.cpp @ 39:c6cdc28d5878
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 06 Jul 2020 21:32:56 +0200 |
parents | fd58eb5749ed |
children | 3dc37a5af1b1 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
3 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
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 "PythonObject.h" | |
21 | |
22 | |
23 #include "PythonLock.h" | |
24 | |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
25 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
0 | 26 |
27 | |
28 PythonObject::PythonObject(PythonLock& lock, | |
29 PyObject *object, | |
30 bool borrowed) : | |
31 lock_(lock), | |
32 object_(object), | |
33 borrowed_(borrowed) | |
34 { | |
35 } | |
36 | |
37 | |
38 PythonObject::~PythonObject() | |
39 { | |
40 if (!borrowed_ && | |
41 object_ != NULL) | |
42 { | |
43 Py_DECREF(object_); | |
44 } | |
45 } | |
46 | |
47 | |
48 PyObject* PythonObject::GetPyObject() const | |
49 { | |
50 if (object_ == NULL) | |
51 { | |
52 // "IsValid()" should have been called | |
53 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
54 } | |
55 else | |
56 { | |
57 return object_; | |
58 } | |
59 } | |
60 | |
61 | |
62 PythonObject* PythonObject::GetAttribute(const std::string& name) | |
63 { | |
64 return new PythonObject(lock_, PyObject_GetAttrString(GetPyObject(), name.c_str())); | |
65 } | |
66 | |
67 | |
68 bool PythonObject::ToUtf8String(std::string& target) | |
69 { | |
70 PyObject* value = GetPyObject(); // Raises an exception if needed | |
71 | |
72 if (PyUnicode_Check(value)) | |
73 { | |
74 PythonObject encoded(lock_, PyUnicode_AsEncodedString(value, "utf-8", "replace")); | |
75 if (encoded.IsValid()) | |
76 { | |
77 target = PyBytes_AS_STRING(encoded.GetPyObject()); | |
78 return true; | |
79 } | |
80 else | |
81 { | |
82 target.clear(); | |
83 return false; | |
84 } | |
85 } | |
86 #if PY_MAJOR_VERSION == 2 | |
87 else if (PyString_Check(value)) | |
88 { | |
89 target = PyString_AS_STRING(value); | |
90 return true; | |
91 } | |
92 #endif | |
93 else | |
94 { | |
95 target.clear(); | |
96 return false; | |
97 } | |
98 } | |
99 | |
100 | |
101 void PythonObject::Format(std::ostream& os) | |
102 { | |
103 std::string s; | |
104 if (object_ == NULL) | |
105 { | |
106 os << "Can't format a NULL Python object" << std::endl; | |
107 } | |
108 else if (ToUtf8String(s)) | |
109 { | |
110 os << s; | |
111 } | |
112 else | |
113 { | |
114 os << "Can't format this Python object" << std::endl; | |
115 } | |
116 } | |
117 | |
118 | |
119 PyObject* PythonObject::Release() | |
120 { | |
121 if (!borrowed_ && | |
122 object_ != NULL) | |
123 { | |
124 PyObject* value = object_; | |
125 object_ = NULL; | |
126 return value; | |
127 } | |
128 else | |
129 { | |
130 OrthancPlugins::LogError("Cannot release a NULL or borrowed reference"); | |
131 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
132 } | |
133 } | |
134 |