
![]() | NumPy array code in C can be simplified using macros |
![]() | First, a smart macro wrapping an argument in quotes:
#define QUOTE(s) # s /* turn s into string "s" */ |
![]() | Check the type of the array data:
#define TYPECHECK(a, tp) \
if (a->descr->type_num != tp) { \
PyErr_Format(PyExc_TypeError, \
"%s array is not of correct type (%d)", QUOTE(a), tp); \
return NULL; \
}
|
![]() | PyErr_Format is a flexible way of raising exceptions in C (must return NULL afterwards!) |