Mercurial > hg > orthanc-python
comparison Sources/PythonLock.cpp @ 171:c8de83fe7faa
removed deprecation warnings
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Jun 2024 15:52:51 +0200 |
parents | 6fada29b6759 |
children | 3678a028f1f6 |
comparison
equal
deleted
inserted
replaced
170:b49eeb36cd0d | 171:c8de83fe7faa |
---|---|
54 | 54 |
55 | 55 |
56 PythonLock::PythonLock() : | 56 PythonLock::PythonLock() : |
57 gstate_(PyGILState_Ensure()) | 57 gstate_(PyGILState_Ensure()) |
58 { | 58 { |
59 //OrthancPlugins::LogInfo("Python lock (GIL) acquired"); | 59 //ORTHANC_PLUGINS_LOG_INFO("Python lock (GIL) acquired"); |
60 } | 60 } |
61 | 61 |
62 | 62 |
63 PythonLock::~PythonLock() | 63 PythonLock::~PythonLock() |
64 { | 64 { |
65 PyGILState_Release(gstate_); | 65 PyGILState_Release(gstate_); |
66 //OrthancPlugins::LogInfo("Python lock (GIL) released"); | 66 //ORTHANC_PLUGINS_LOG_INFO("Python lock (GIL) released"); |
67 } | 67 } |
68 | 68 |
69 | 69 |
70 void PythonLock::ExecuteCommand(const std::string& s) | 70 void PythonLock::ExecuteCommand(const std::string& s) |
71 { | 71 { |
72 if (PyRun_SimpleString(s.c_str()) != 0) | 72 if (PyRun_SimpleString(s.c_str()) != 0) |
73 { | 73 { |
74 OrthancPlugins::LogError("Error while executing a Python command"); | 74 ORTHANC_PLUGINS_LOG_ERROR("Error while executing a Python command"); |
75 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | 75 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 | 79 |
189 | 189 |
190 state->exceptionObject_ = PyErr_NewException(const_cast<char*>(fqnName.c_str()), NULL, NULL); | 190 state->exceptionObject_ = PyErr_NewException(const_cast<char*>(fqnName.c_str()), NULL, NULL); |
191 if (state->exceptionObject_ == NULL) | 191 if (state->exceptionObject_ == NULL) |
192 { | 192 { |
193 Py_DECREF(module); | 193 Py_DECREF(module); |
194 OrthancPlugins::LogError("Cannot create the Python exception class"); | 194 ORTHANC_PLUGINS_LOG_ERROR("Cannot create the Python exception class"); |
195 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 195 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
196 } | 196 } |
197 | 197 |
198 Py_XINCREF(state->exceptionObject_); | 198 Py_XINCREF(state->exceptionObject_); |
199 if (PyModule_AddObject(module, shortName.c_str(), state->exceptionObject_) < 0) | 199 if (PyModule_AddObject(module, shortName.c_str(), state->exceptionObject_) < 0) |
200 { | 200 { |
201 Py_XDECREF(state->exceptionObject_); | 201 Py_XDECREF(state->exceptionObject_); |
202 Py_CLEAR(state->exceptionObject_); | 202 Py_CLEAR(state->exceptionObject_); |
203 OrthancPlugins::LogError("Cannot create the Python exception class"); | 203 ORTHANC_PLUGINS_LOG_ERROR("Cannot create the Python exception class"); |
204 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 204 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
205 } | 205 } |
206 } | 206 } |
207 | 207 |
208 | 208 |
249 moduledef.m_methods = moduleFunctions_(); | 249 moduledef.m_methods = moduleFunctions_(); |
250 | 250 |
251 PyObject *module = PyModule_Create(&moduledef); | 251 PyObject *module = PyModule_Create(&moduledef); |
252 if (module == NULL) | 252 if (module == NULL) |
253 { | 253 { |
254 OrthancPlugins::LogError("Cannot create a Python module"); | 254 ORTHANC_PLUGINS_LOG_ERROR("Cannot create a Python module"); |
255 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 255 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
256 } | 256 } |
257 | 257 |
258 RegisterException(module, moduleName_ + "." + exceptionName_, exceptionName_); | 258 RegisterException(module, moduleName_ + "." + exceptionName_, exceptionName_); |
259 moduleClasses_(module); | 259 moduleClasses_(module); |
274 } | 274 } |
275 | 275 |
276 PyObject *module = Py_InitModule(moduleName_.c_str(), moduleFunctions_()); | 276 PyObject *module = Py_InitModule(moduleName_.c_str(), moduleFunctions_()); |
277 if (module == NULL) | 277 if (module == NULL) |
278 { | 278 { |
279 OrthancPlugins::LogError("Cannot create a Python module"); | 279 ORTHANC_PLUGINS_LOG_ERROR("Cannot create a Python module"); |
280 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 280 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
281 } | 281 } |
282 | 282 |
283 RegisterException(module, moduleName_ + "." + exceptionName_, exceptionName_); | 283 RegisterException(module, moduleName_ + "." + exceptionName_, exceptionName_); |
284 moduleClasses_(module); | 284 moduleClasses_(module); |
292 { | 292 { |
293 std::string traceback; | 293 std::string traceback; |
294 | 294 |
295 if (HasErrorOccurred(traceback)) | 295 if (HasErrorOccurred(traceback)) |
296 { | 296 { |
297 OrthancPlugins::LogError("Error in the " + callbackDetails + ", traceback:\n" + traceback); | 297 ORTHANC_PLUGINS_LOG_ERROR("Error in the " + callbackDetails + ", traceback:\n" + traceback); |
298 return OrthancPluginErrorCode_Plugin; | 298 return OrthancPluginErrorCode_Plugin; |
299 } | 299 } |
300 else | 300 else |
301 { | 301 { |
302 return OrthancPluginErrorCode_Success; | 302 return OrthancPluginErrorCode_Success; |
313 { | 313 { |
314 boost::mutex::scoped_lock lock(mutex_); | 314 boost::mutex::scoped_lock lock(mutex_); |
315 | 315 |
316 if (interpreterState_ != NULL) | 316 if (interpreterState_ != NULL) |
317 { | 317 { |
318 OrthancPlugins::LogError("Cannot initialize twice the Python interpreter"); | 318 ORTHANC_PLUGINS_LOG_ERROR("Cannot initialize twice the Python interpreter"); |
319 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | 319 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); |
320 } | 320 } |
321 | 321 |
322 if (moduleClasses == NULL || | 322 if (moduleClasses == NULL || |
323 moduleFunctions == NULL) | 323 moduleFunctions == NULL) |
331 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | 331 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); |
332 } | 332 } |
333 | 333 |
334 if (exceptionName.find('.') != std::string::npos) | 334 if (exceptionName.find('.') != std::string::npos) |
335 { | 335 { |
336 OrthancPlugins::LogError("The name of the exception cannot contain \".\", found: " + | 336 ORTHANC_PLUGINS_LOG_ERROR("The name of the exception cannot contain \".\", found: " + exceptionName); |
337 exceptionName); | |
338 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | 337 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); |
339 } | 338 } |
340 | 339 |
341 moduleClasses_ = moduleClasses; | 340 moduleClasses_ = moduleClasses; |
342 moduleFunctions_ = moduleFunctions; | 341 moduleFunctions_ = moduleFunctions; |
349 OrthancPlugins::OrthancString str; | 348 OrthancPlugins::OrthancString str; |
350 str.Assign(OrthancPluginGetOrthancPath(OrthancPlugins::GetGlobalContext())); | 349 str.Assign(OrthancPluginGetOrthancPath(OrthancPlugins::GetGlobalContext())); |
351 str.ToString(executable); | 350 str.ToString(executable); |
352 } | 351 } |
353 | 352 |
354 OrthancPlugins::LogWarning("Program name: " + executable); | 353 ORTHANC_PLUGINS_LOG_WARNING("Program name: " + executable); |
355 | 354 |
356 #if PY_MAJOR_VERSION == 2 | 355 #if PY_MAJOR_VERSION == 2 |
357 Py_SetProgramName(&executable[0]); /* optional but recommended */ | 356 Py_SetProgramName(&executable[0]); /* optional but recommended */ |
358 #else | 357 #else |
359 std::wstring wide(executable.begin(), executable.end()); | 358 std::wstring wide(executable.begin(), executable.end()); |
438 **/ | 437 **/ |
439 | 438 |
440 PyObject *sysPath = PySys_GetObject(const_cast<char*>("path")); | 439 PyObject *sysPath = PySys_GetObject(const_cast<char*>("path")); |
441 if (sysPath == NULL) | 440 if (sysPath == NULL) |
442 { | 441 { |
443 OrthancPlugins::LogError("Cannot find sys.path"); | 442 ORTHANC_PLUGINS_LOG_ERROR("Cannot find sys.path"); |
444 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 443 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
445 } | 444 } |
446 | 445 |
447 PythonString pyPath(lock, path); | 446 PythonString pyPath(lock, path); |
448 int result = PyList_Insert(sysPath, 0, pyPath.Release()); | 447 int result = PyList_Insert(sysPath, 0, pyPath.Release()); |
449 | 448 |
450 if (result != 0) | 449 if (result != 0) |
451 { | 450 { |
452 OrthancPlugins::LogError("Cannot run sys.path.append()"); | 451 ORTHANC_PLUGINS_LOG_ERROR("Cannot run sys.path.append()"); |
453 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 452 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
454 } | 453 } |
455 } | 454 } |
456 | 455 |
457 | 456 |
492 #endif | 491 #endif |
493 | 492 |
494 struct module_state *state = GETSTATE(module.GetPyObject()); | 493 struct module_state *state = GETSTATE(module.GetPyObject()); |
495 if (state->exceptionObject_ == NULL) | 494 if (state->exceptionObject_ == NULL) |
496 { | 495 { |
497 OrthancPlugins::LogError("No Python exception has been registered"); | 496 ORTHANC_PLUGINS_LOG_ERROR("No Python exception has been registered"); |
498 } | 497 } |
499 else | 498 else |
500 { | 499 { |
501 PythonString str(lock, message); | 500 PythonString str(lock, message); |
502 | 501 |
518 * "GlobalInitialize()". | 517 * "GlobalInitialize()". |
519 **/ | 518 **/ |
520 | 519 |
521 if (verbose_) | 520 if (verbose_) |
522 { | 521 { |
523 OrthancPlugins::LogInfo(message); | 522 ORTHANC_PLUGINS_LOG_INFO(message); |
524 } | 523 } |
525 } | 524 } |