py_assert.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * This file is part of the OpenMV project.
  3. *
  4. * Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io>
  5. * Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io>
  6. *
  7. * This work is licensed under the MIT license, see the file LICENSE for details.
  8. *
  9. * MP assertions.
  10. */
  11. #ifndef __PY_ASSERT_H__
  12. #define __PY_ASSERT_H__
  13. #define PY_ASSERT_TRUE(cond) \
  14. do { \
  15. if ((cond) == 0) { \
  16. mp_raise_msg(&mp_type_OSError, \
  17. MP_ERROR_TEXT( \
  18. "Operation not supported")); \
  19. } \
  20. } while(0)
  21. #define PY_ASSERT_TRUE_MSG(cond, msg) \
  22. do { \
  23. if ((cond) == 0) { \
  24. mp_raise_msg(&mp_type_OSError, \
  25. MP_ERROR_TEXT(msg)); \
  26. } \
  27. } while(0)
  28. #define PY_ASSERT_FALSE_MSG(cond, msg) \
  29. do { \
  30. if ((cond) == 1) { \
  31. mp_raise_msg(&mp_type_OSError, \
  32. MP_ERROR_TEXT(msg)); \
  33. } \
  34. } while(0)
  35. #define PY_ASSERT_TYPE(obj, type) \
  36. do { \
  37. __typeof__ (obj) _a = (obj); \
  38. __typeof__ (type) _b = (type); \
  39. if (!MP_OBJ_IS_TYPE(_a, _b)) { \
  40. mp_raise_msg_varg(&mp_type_TypeError, \
  41. MP_ERROR_TEXT( \
  42. "Can't convert %s to %s"), \
  43. mp_obj_get_type_str(_a), \
  44. mp_obj_get_type_str(_b)); \
  45. } \
  46. } while(0)
  47. /* IS_TYPE doesn't work for str objs */
  48. #define PY_ASSERT_STR(obj) \
  49. do { \
  50. __typeof__ (obj) _a = (obj); \
  51. if (!MP_OBJ_IS_STR(_a)) { \
  52. mp_raise_msg_varg( \
  53. &mp_type_TypeError, \
  54. MP_ERROR_TEXT( \
  55. "Can't convert %s to %s"), \
  56. mp_obj_get_type_str(_a), \
  57. str_type.name); \
  58. } \
  59. } while(0)
  60. #endif // __PY_ASSERT_H__