cv2_util.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef CV2_UTIL_HPP
  2. #define CV2_UTIL_HPP
  3. #include "cv2.hpp"
  4. #include "opencv2/core.hpp"
  5. #include "opencv2/core/utils/tls.hpp"
  6. #include <vector>
  7. #include <string>
  8. //======================================================================================================================
  9. bool isPythonBindingsDebugEnabled();
  10. void emit_failmsg(PyObject * exc, const char *msg);
  11. int failmsg(const char *fmt, ...);
  12. PyObject* failmsgp(const char *fmt, ...);;
  13. //======================================================================================================================
  14. class PyAllowThreads
  15. {
  16. public:
  17. PyAllowThreads() : _state(PyEval_SaveThread()) {}
  18. ~PyAllowThreads()
  19. {
  20. PyEval_RestoreThread(_state);
  21. }
  22. private:
  23. PyThreadState* _state;
  24. };
  25. class PyEnsureGIL
  26. {
  27. public:
  28. PyEnsureGIL() : _state(PyGILState_Ensure()) {}
  29. ~PyEnsureGIL()
  30. {
  31. PyGILState_Release(_state);
  32. }
  33. private:
  34. PyGILState_STATE _state;
  35. };
  36. /**
  37. * Light weight RAII wrapper for `PyObject*` owning references.
  38. * In comparisson to C++11 `std::unique_ptr` with custom deleter, it provides
  39. * implicit conversion functions that might be useful to initialize it with
  40. * Python functions those returns owning references through the `PyObject**`
  41. * e.g. `PyErr_Fetch` or directly pass it to functions those want to borrow
  42. * reference to object (doesn't extend object lifetime) e.g. `PyObject_Str`.
  43. */
  44. class PySafeObject
  45. {
  46. public:
  47. PySafeObject() : obj_(NULL) {}
  48. explicit PySafeObject(PyObject* obj) : obj_(obj) {}
  49. ~PySafeObject()
  50. {
  51. Py_CLEAR(obj_);
  52. }
  53. operator PyObject*()
  54. {
  55. return obj_;
  56. }
  57. operator PyObject**()
  58. {
  59. return &obj_;
  60. }
  61. PyObject* release()
  62. {
  63. PyObject* obj = obj_;
  64. obj_ = NULL;
  65. return obj;
  66. }
  67. private:
  68. PyObject* obj_;
  69. // Explicitly disable copy operations
  70. PySafeObject(const PySafeObject*); // = delete
  71. PySafeObject& operator=(const PySafeObject&); // = delete
  72. };
  73. //======================================================================================================================
  74. extern PyObject* opencv_error;
  75. void pyRaiseCVException(const cv::Exception &e);
  76. #define ERRWRAP2(expr) \
  77. try \
  78. { \
  79. PyAllowThreads allowThreads; \
  80. expr; \
  81. } \
  82. catch (const cv::Exception &e) \
  83. { \
  84. pyRaiseCVException(e); \
  85. return 0; \
  86. } \
  87. catch (const std::exception &e) \
  88. { \
  89. PyErr_SetString(opencv_error, e.what()); \
  90. return 0; \
  91. } \
  92. catch (...) \
  93. { \
  94. PyErr_SetString(opencv_error, "Unknown C++ exception from OpenCV code"); \
  95. return 0; \
  96. }
  97. //======================================================================================================================
  98. extern cv::TLSData<std::vector<std::string> > conversionErrorsTLS;
  99. inline void pyPrepareArgumentConversionErrorsStorage(std::size_t size)
  100. {
  101. std::vector<std::string>& conversionErrors = conversionErrorsTLS.getRef();
  102. conversionErrors.clear();
  103. conversionErrors.reserve(size);
  104. }
  105. void pyRaiseCVOverloadException(const std::string& functionName);
  106. void pyPopulateArgumentConversionErrors();
  107. //======================================================================================================================
  108. PyObject *pycvRedirectError(PyObject*, PyObject *args, PyObject *kw);
  109. #endif // CV2_UTIL_HPP