Changeset View
Changeset View
Standalone View
Standalone View
plugins/extensions/pykrita/plugin/utilities.cpp
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | |||||
43 | #include <kis_debug.h> | 43 | #include <kis_debug.h> | ||
44 | 44 | | |||
45 | #define THREADED 1 | 45 | #define THREADED 1 | ||
46 | 46 | | |||
47 | namespace PyKrita | 47 | namespace PyKrita | ||
48 | { | 48 | { | ||
49 | namespace | 49 | namespace | ||
50 | { | 50 | { | ||
51 | #ifndef Q_OS_WIN | ||||
51 | QLibrary* s_pythonLibrary = 0; | 52 | QLibrary* s_pythonLibrary = 0; | ||
53 | #endif | ||||
52 | PyThreadState* s_pythonThreadState = 0; | 54 | PyThreadState* s_pythonThreadState = 0; | ||
53 | } // anonymous namespace | 55 | } // anonymous namespace | ||
54 | 56 | | |||
55 | const char* Python::PYKRITA_ENGINE = "pykrita"; | 57 | const char* Python::PYKRITA_ENGINE = "pykrita"; | ||
56 | 58 | | |||
57 | Python::Python() | 59 | Python::Python() | ||
58 | { | 60 | { | ||
59 | #if THREADED | 61 | #if THREADED | ||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Line(s) | |||||
160 | { | 162 | { | ||
161 | QString result; | 163 | QString result; | ||
162 | result.swap(m_traceback); | 164 | result.swap(m_traceback); | ||
163 | return result; | 165 | return result; | ||
164 | } | 166 | } | ||
165 | 167 | | |||
166 | void Python::libraryLoad() | 168 | void Python::libraryLoad() | ||
167 | { | 169 | { | ||
170 | #ifdef Q_OS_WIN | ||||
171 | if (Py_IsInitialized()) { | ||||
172 | dbgScript << "Python interpreter is already initialized"; | ||||
173 | } else { | ||||
174 | dbgScript << "Initializing Python interpreter"; | ||||
175 | #else | ||||
168 | if (!s_pythonLibrary) { | 176 | if (!s_pythonLibrary) { | ||
169 | dbgScript << "Creating s_pythonLibrary" << PYKRITA_PYTHON_LIBRARY; | 177 | dbgScript << "Creating s_pythonLibrary" << PYKRITA_PYTHON_LIBRARY; | ||
170 | s_pythonLibrary = new QLibrary(PYKRITA_PYTHON_LIBRARY); | 178 | s_pythonLibrary = new QLibrary(PYKRITA_PYTHON_LIBRARY); | ||
171 | if (!s_pythonLibrary) | 179 | if (!s_pythonLibrary) | ||
172 | errScript << "Could not create" << PYKRITA_PYTHON_LIBRARY; | 180 | errScript << "Could not create" << PYKRITA_PYTHON_LIBRARY; | ||
173 | 181 | | |||
174 | s_pythonLibrary->setLoadHints(QLibrary::ExportExternalSymbolsHint); | 182 | s_pythonLibrary->setLoadHints(QLibrary::ExportExternalSymbolsHint); | ||
175 | if (!s_pythonLibrary->load()) | 183 | if (!s_pythonLibrary->load()) | ||
176 | errScript << "Could not load" << PYKRITA_PYTHON_LIBRARY; | 184 | errScript << "Could not load" << PYKRITA_PYTHON_LIBRARY; | ||
185 | #endif | ||||
177 | 186 | | |||
178 | Py_InitializeEx(0); | 187 | Py_InitializeEx(0); | ||
179 | if (!Py_IsInitialized()) | 188 | if (!Py_IsInitialized()) { | ||
189 | #ifdef Q_OS_WIN | ||||
190 | errScript << "Could not initialise Python interpreter"; | ||||
191 | #else | ||||
180 | errScript << "Could not initialise" << PYKRITA_PYTHON_LIBRARY; | 192 | errScript << "Could not initialise" << PYKRITA_PYTHON_LIBRARY; | ||
193 | #endif | ||||
194 | } | ||||
181 | #if THREADED | 195 | #if THREADED | ||
182 | PyEval_InitThreads(); | 196 | PyEval_InitThreads(); | ||
183 | s_pythonThreadState = PyGILState_GetThisThreadState(); | 197 | s_pythonThreadState = PyGILState_GetThisThreadState(); | ||
184 | PyEval_ReleaseThread(s_pythonThreadState); | 198 | PyEval_ReleaseThread(s_pythonThreadState); | ||
185 | #endif | 199 | #endif | ||
186 | } | 200 | } | ||
187 | } | 201 | } | ||
188 | 202 | | |||
189 | void Python::libraryUnload() | 203 | void Python::libraryUnload() | ||
190 | { | 204 | { | ||
205 | #ifdef Q_OS_WIN | ||||
206 | warnScript << "Explicitly unloading Python interpreter isn't supported for Windows"; | ||||
207 | { | ||||
208 | #else | ||||
191 | if (s_pythonLibrary) { | 209 | if (s_pythonLibrary) { | ||
210 | #endif | ||||
192 | // Shut the interpreter down if it has been started. | 211 | // Shut the interpreter down if it has been started. | ||
193 | if (Py_IsInitialized()) { | 212 | if (Py_IsInitialized()) { | ||
194 | #if THREADED | 213 | #if THREADED | ||
195 | PyEval_AcquireThread(s_pythonThreadState); | 214 | PyEval_AcquireThread(s_pythonThreadState); | ||
196 | #endif | 215 | #endif | ||
197 | //Py_Finalize(); | 216 | //Py_Finalize(); | ||
198 | } | 217 | } | ||
218 | #ifndef Q_OS_WIN | ||||
199 | if (s_pythonLibrary->isLoaded()) { | 219 | if (s_pythonLibrary->isLoaded()) { | ||
200 | s_pythonLibrary->unload(); | 220 | s_pythonLibrary->unload(); | ||
201 | } | 221 | } | ||
202 | delete s_pythonLibrary; | 222 | delete s_pythonLibrary; | ||
203 | s_pythonLibrary = 0; | 223 | s_pythonLibrary = 0; | ||
224 | #endif | ||||
204 | } | 225 | } | ||
205 | } | 226 | } | ||
206 | 227 | | |||
207 | PyObject* Python::moduleActions(const char* moduleName) | 228 | PyObject* Python::moduleActions(const char* moduleName) | ||
208 | { | 229 | { | ||
209 | return kritaHandler(moduleName, "moduleGetActions"); | 230 | return kritaHandler(moduleName, "moduleGetActions"); | ||
210 | } | 231 | } | ||
211 | 232 | | |||
▲ Show 20 Lines • Show All 314 Lines • Show Last 20 Lines |