pycompat.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. // Defines for Python 2/3 compatibility.
  43. #ifndef __PYCOMPAT_HPP__
  44. #define __PYCOMPAT_HPP__
  45. #include <string>
  46. #if PY_MAJOR_VERSION >= 3
  47. // Python3 treats all ints as longs, PyInt_X functions have been removed.
  48. #define PyInt_Check PyLong_Check
  49. #define PyInt_CheckExact PyLong_CheckExact
  50. #define PyInt_AsLong PyLong_AsLong
  51. #define PyInt_AS_LONG PyLong_AS_LONG
  52. #define PyInt_FromLong PyLong_FromLong
  53. #define PyNumber_Int PyNumber_Long
  54. #define PyString_FromString PyUnicode_FromString
  55. #define PyString_FromStringAndSize PyUnicode_FromStringAndSize
  56. #endif // PY_MAJOR >=3
  57. static inline bool getUnicodeString(PyObject * obj, std::string &str)
  58. {
  59. bool res = false;
  60. if (PyUnicode_Check(obj))
  61. {
  62. PyObject * bytes = PyUnicode_AsUTF8String(obj);
  63. if (PyBytes_Check(bytes))
  64. {
  65. const char * raw = PyBytes_AsString(bytes);
  66. if (raw)
  67. {
  68. str = std::string(raw);
  69. res = true;
  70. }
  71. }
  72. Py_XDECREF(bytes);
  73. }
  74. #if PY_MAJOR_VERSION < 3
  75. else if (PyString_Check(obj))
  76. {
  77. const char * raw = PyString_AsString(obj);
  78. if (raw)
  79. {
  80. str = std::string(raw);
  81. res = true;
  82. }
  83. }
  84. #endif
  85. return res;
  86. }
  87. //==================================================================================================
  88. #define CV_PY_FN_WITH_KW_(fn, flags) (PyCFunction)(void*)(PyCFunctionWithKeywords)(fn), (flags) | METH_VARARGS | METH_KEYWORDS
  89. #define CV_PY_FN_NOARGS_(fn, flags) (PyCFunction)(fn), (flags) | METH_NOARGS
  90. #define CV_PY_FN_WITH_KW(fn) CV_PY_FN_WITH_KW_(fn, 0)
  91. #define CV_PY_FN_NOARGS(fn) CV_PY_FN_NOARGS_(fn, 0)
  92. #define CV_PY_TO_CLASS(TYPE) \
  93. template<> \
  94. bool pyopencv_to(PyObject* dst, TYPE& src, const ArgInfo& info) \
  95. { \
  96. if (!dst || dst == Py_None) \
  97. return true; \
  98. Ptr<TYPE> ptr; \
  99. \
  100. if (!pyopencv_to(dst, ptr, info)) return false; \
  101. src = *ptr; \
  102. return true; \
  103. }
  104. #define CV_PY_FROM_CLASS(TYPE) \
  105. template<> \
  106. PyObject* pyopencv_from(const TYPE& src) \
  107. { \
  108. Ptr<TYPE> ptr(new TYPE()); \
  109. \
  110. *ptr = src; \
  111. return pyopencv_from(ptr); \
  112. }
  113. #define CV_PY_TO_CLASS_PTR(TYPE) \
  114. template<> \
  115. bool pyopencv_to(PyObject* dst, TYPE*& src, const ArgInfo& info) \
  116. { \
  117. if (!dst || dst == Py_None) \
  118. return true; \
  119. Ptr<TYPE> ptr; \
  120. \
  121. if (!pyopencv_to(dst, ptr, info)) return false; \
  122. src = ptr; \
  123. return true; \
  124. }
  125. #define CV_PY_FROM_CLASS_PTR(TYPE) \
  126. static PyObject* pyopencv_from(TYPE*& src) \
  127. { \
  128. return pyopencv_from(Ptr<TYPE>(src)); \
  129. }
  130. #define CV_PY_TO_ENUM(TYPE) \
  131. template<> \
  132. bool pyopencv_to(PyObject* dst, TYPE& src, const ArgInfo& info) \
  133. { \
  134. if (!dst || dst == Py_None) \
  135. return true; \
  136. int underlying = 0; \
  137. \
  138. if (!pyopencv_to(dst, underlying, info)) return false; \
  139. src = static_cast<TYPE>(underlying); \
  140. return true; \
  141. }
  142. #define CV_PY_FROM_ENUM(TYPE) \
  143. template<> \
  144. PyObject* pyopencv_from(const TYPE& src) \
  145. { \
  146. return pyopencv_from(static_cast<int>(src)); \
  147. }
  148. //==================================================================================================
  149. #if PY_MAJOR_VERSION >= 3
  150. #define CVPY_TYPE_HEAD PyVarObject_HEAD_INIT(&PyType_Type, 0)
  151. #define CVPY_TYPE_INCREF(T) Py_INCREF(T)
  152. #else
  153. #define CVPY_TYPE_HEAD PyObject_HEAD_INIT(&PyType_Type) 0,
  154. #define CVPY_TYPE_INCREF(T) _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA (T)->ob_refcnt++
  155. #endif
  156. #define CVPY_TYPE_DECLARE(WNAME, NAME, STORAGE, SNAME) \
  157. struct pyopencv_##NAME##_t \
  158. { \
  159. PyObject_HEAD \
  160. STORAGE v; \
  161. }; \
  162. static PyTypeObject pyopencv_##NAME##_TypeXXX = \
  163. { \
  164. CVPY_TYPE_HEAD \
  165. MODULESTR"."#WNAME, \
  166. sizeof(pyopencv_##NAME##_t), \
  167. }; \
  168. static PyTypeObject * pyopencv_##NAME##_TypePtr = &pyopencv_##NAME##_TypeXXX; \
  169. static bool pyopencv_##NAME##_getp(PyObject * self, STORAGE * & dst) \
  170. { \
  171. if (PyObject_TypeCheck(self, pyopencv_##NAME##_TypePtr)) \
  172. { \
  173. dst = &(((pyopencv_##NAME##_t*)self)->v); \
  174. return true; \
  175. } \
  176. return false; \
  177. } \
  178. static PyObject * pyopencv_##NAME##_Instance(const STORAGE &r) \
  179. { \
  180. pyopencv_##NAME##_t *m = PyObject_NEW(pyopencv_##NAME##_t, pyopencv_##NAME##_TypePtr); \
  181. new (&(m->v)) STORAGE(r); \
  182. return (PyObject*)m; \
  183. } \
  184. static void pyopencv_##NAME##_dealloc(PyObject* self) \
  185. { \
  186. ((pyopencv_##NAME##_t*)self)->v.STORAGE::~SNAME(); \
  187. PyObject_Del(self); \
  188. } \
  189. static PyObject* pyopencv_##NAME##_repr(PyObject* self) \
  190. { \
  191. char str[1000]; \
  192. sprintf(str, "<"#WNAME" %p>", self); \
  193. return PyString_FromString(str); \
  194. }
  195. #define CVPY_TYPE_INIT_STATIC(WNAME, NAME, ERROR_HANDLER, BASE, CONSTRUCTOR) \
  196. { \
  197. pyopencv_##NAME##_TypePtr->tp_base = pyopencv_##BASE##_TypePtr; \
  198. pyopencv_##NAME##_TypePtr->tp_dealloc = pyopencv_##NAME##_dealloc; \
  199. pyopencv_##NAME##_TypePtr->tp_repr = pyopencv_##NAME##_repr; \
  200. pyopencv_##NAME##_TypePtr->tp_getset = pyopencv_##NAME##_getseters; \
  201. pyopencv_##NAME##_TypePtr->tp_init = (initproc) CONSTRUCTOR; \
  202. pyopencv_##NAME##_TypePtr->tp_methods = pyopencv_##NAME##_methods; \
  203. pyopencv_##NAME##_TypePtr->tp_alloc = PyType_GenericAlloc; \
  204. pyopencv_##NAME##_TypePtr->tp_new = PyType_GenericNew; \
  205. pyopencv_##NAME##_TypePtr->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; \
  206. if (PyType_Ready(pyopencv_##NAME##_TypePtr) != 0) \
  207. { \
  208. ERROR_HANDLER; \
  209. } \
  210. CVPY_TYPE_INCREF(pyopencv_##NAME##_TypePtr); \
  211. if (PyModule_AddObject(m, #WNAME, (PyObject *)pyopencv_##NAME##_TypePtr) < 0) \
  212. { \
  213. printf("Failed to register a new type: " #WNAME ", base (" #BASE ")\n"); \
  214. Py_DECREF(pyopencv_##NAME##_TypePtr); \
  215. ERROR_HANDLER; \
  216. } \
  217. }
  218. //==================================================================================================
  219. #define CVPY_TYPE_DECLARE_DYNAMIC(WNAME, NAME, STORAGE, SNAME) \
  220. struct pyopencv_##NAME##_t \
  221. { \
  222. PyObject_HEAD \
  223. STORAGE v; \
  224. }; \
  225. static PyObject * pyopencv_##NAME##_TypePtr = 0; \
  226. static bool pyopencv_##NAME##_getp(PyObject * self, STORAGE * & dst) \
  227. { \
  228. if (PyObject_TypeCheck(self, (PyTypeObject*)pyopencv_##NAME##_TypePtr)) \
  229. { \
  230. dst = &(((pyopencv_##NAME##_t*)self)->v); \
  231. return true; \
  232. } \
  233. return false; \
  234. } \
  235. static PyObject * pyopencv_##NAME##_Instance(const STORAGE &r) \
  236. { \
  237. pyopencv_##NAME##_t *m = PyObject_New(pyopencv_##NAME##_t, (PyTypeObject*)pyopencv_##NAME##_TypePtr); \
  238. new (&(m->v)) STORAGE(r); \
  239. return (PyObject*)m; \
  240. } \
  241. static void pyopencv_##NAME##_dealloc(PyObject* self) \
  242. { \
  243. ((pyopencv_##NAME##_t*)self)->v.STORAGE::~SNAME(); \
  244. PyObject_Del(self); \
  245. } \
  246. static PyObject* pyopencv_##NAME##_repr(PyObject* self) \
  247. { \
  248. char str[1000]; \
  249. sprintf(str, "<"#WNAME" %p>", self); \
  250. return PyString_FromString(str); \
  251. } \
  252. static PyType_Slot pyopencv_##NAME##_Slots[] = \
  253. { \
  254. {Py_tp_dealloc, 0}, \
  255. {Py_tp_repr, 0}, \
  256. {Py_tp_getset, 0}, \
  257. {Py_tp_init, 0}, \
  258. {Py_tp_methods, 0}, \
  259. {Py_tp_alloc, 0}, \
  260. {Py_tp_new, 0}, \
  261. {0, 0} \
  262. }; \
  263. static PyType_Spec pyopencv_##NAME##_Spec = \
  264. { \
  265. MODULESTR"."#WNAME, \
  266. sizeof(pyopencv_##NAME##_t), \
  267. 0, \
  268. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
  269. pyopencv_##NAME##_Slots \
  270. };
  271. #define CVPY_TYPE_INIT_DYNAMIC(WNAME, NAME, ERROR_HANDLER, BASE, CONSTRUCTOR) \
  272. { \
  273. pyopencv_##NAME##_Slots[0].pfunc /*tp_dealloc*/ = (void*)pyopencv_##NAME##_dealloc; \
  274. pyopencv_##NAME##_Slots[1].pfunc /*tp_repr*/ = (void*)pyopencv_##NAME##_repr; \
  275. pyopencv_##NAME##_Slots[2].pfunc /*tp_getset*/ = (void*)pyopencv_##NAME##_getseters; \
  276. pyopencv_##NAME##_Slots[3].pfunc /*tp_init*/ = (void*) CONSTRUCTOR; \
  277. pyopencv_##NAME##_Slots[4].pfunc /*tp_methods*/ = pyopencv_##NAME##_methods; \
  278. pyopencv_##NAME##_Slots[5].pfunc /*tp_alloc*/ = (void*)PyType_GenericAlloc; \
  279. pyopencv_##NAME##_Slots[6].pfunc /*tp_new*/ = (void*)PyType_GenericNew; \
  280. PyObject * bases = 0; \
  281. if (pyopencv_##BASE##_TypePtr) \
  282. bases = PyTuple_Pack(1, pyopencv_##BASE##_TypePtr); \
  283. pyopencv_##NAME##_TypePtr = PyType_FromSpecWithBases(&pyopencv_##NAME##_Spec, bases); \
  284. if (!pyopencv_##NAME##_TypePtr) \
  285. { \
  286. printf("Failed to create type from spec: " #WNAME ", base (" #BASE ")\n"); \
  287. ERROR_HANDLER; \
  288. } \
  289. if (PyModule_AddObject(m, #WNAME, (PyObject *)pyopencv_##NAME##_TypePtr) < 0) \
  290. { \
  291. printf("Failed to register a new type: " #WNAME ", base (" #BASE ")\n"); \
  292. Py_DECREF(pyopencv_##NAME##_TypePtr); \
  293. ERROR_HANDLER; \
  294. } \
  295. }
  296. // Debug module load:
  297. //
  298. // else \
  299. // { \
  300. // printf("Init: " #NAME ", base (" #BASE ") -> %p" "\n", pyopencv_##NAME##_TypePtr); \
  301. // } \
  302. #endif // END HEADER GUARD