openmv/src/omv/py/py_assert.h
Kwabena W. Agyeman e0e112468e Argument Parsing Done Correctly
All of our argument parsing code has now been updated to handle
positional as well as keyword arguments in our python libraries.

Basically, python allows you to pass some number of positional arguments
to functions/methods followed by keyword arguments (you cannot have more
positional arguments after keyword arguments). Previously, our code
would only look for keyword arguments. Now, it works better and will
grab as many positional arguments as it can followed by processing
keyword arguments. Note: If the case of a positional argument value for
a parameter being passed followed by a keyword for that same parameter
the keyword value is taken (since it comes aftward).

Because arguments were passed in keyword form before this update has no
affect on current code. However, moving forward, argument positions are
now locked and cannot be moved around.
2018-03-05 00:49:36 -05:00

63 lines
2.7 KiB
C

/*
* This file is part of the OpenMV project.
* Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com>
* This work is licensed under the MIT license, see the file LICENSE for details.
*
* MP assertions.
*
*/
#ifndef __PY_ASSERT_H__
#define __PY_ASSERT_H__
#include "mp.h"
#define PY_ASSERT_TRUE(cond) \
do { \
if ((cond) == 0) { \
nlr_jump(mp_obj_new_exception_msg( \
&mp_type_OSError, \
"Operation not supported")); \
} \
} while(0)
#define PY_ASSERT_TRUE_MSG(cond, msg) \
do { \
if ((cond) == 0) { \
nlr_jump(mp_obj_new_exception_msg( \
&mp_type_OSError, msg)); \
} \
} while(0)
#define PY_ASSERT_FALSE_MSG(cond, msg) \
do { \
if ((cond) == 1) { \
nlr_jump(mp_obj_new_exception_msg( \
&mp_type_OSError, msg)); \
} \
} while(0)
#define PY_ASSERT_TYPE(obj, type) \
do { \
__typeof__ (obj) _a = (obj); \
__typeof__ (type) _b = (type); \
if (!MP_OBJ_IS_TYPE(_a, _b)) { \
nlr_jump(mp_obj_new_exception_msg_varg( \
&mp_type_TypeError, \
"Can't convert %s to %s", \
mp_obj_get_type_str(_a), \
mp_obj_get_type_str(_b))); \
} \
} while(0)
/* IS_TYPE doesn't work for str objs */
#define PY_ASSERT_STR(obj) \
do { \
__typeof__ (obj) _a = (obj); \
if (!MP_OBJ_IS_STR(_a)) { \
nlr_jump(mp_obj_new_exception_msg_varg( \
&mp_type_TypeError, \
"Can't convert %s to %s", \
mp_obj_get_type_str(_a), \
str_type.name)); \
} \
} while(0)
#endif // __PY_ASSERT_H__