diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-04-09 00:25:28 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-04-09 01:07:37 +0300 |
commit | 3aa8ee7c9e2e9fd50ffb4b588518bd84b40fef84 (patch) | |
tree | 98cce37eac4fbb744c8c1841fae47e735e0466a4 /py/obj.c | |
parent | 2b0091983ff88272314ab69aaceb3c69f88c4518 (diff) | |
download | micropython-3aa8ee7c9e2e9fd50ffb4b588518bd84b40fef84.tar.gz micropython-3aa8ee7c9e2e9fd50ffb4b588518bd84b40fef84.zip |
py: Add mp_get_buffer(), mp_get_buffer_raise() convenience functions to API.
Diffstat (limited to 'py/obj.c')
-rw-r--r-- | py/obj.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -330,3 +330,21 @@ mp_obj_t mp_identity(mp_obj_t self) { return self; } MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity); + +bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) { + mp_obj_base_t *o = (mp_obj_base_t *)obj; + if (o->type->buffer_p.get_buffer == NULL) { + return false; + } + o->type->buffer_p.get_buffer(o, bufinfo, BUFFER_READ); + if (bufinfo->buf == NULL) { + return false; + } + return true; +} + +void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo) { + if (!mp_get_buffer(obj, bufinfo)) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Object with buffer protocol required")); + } +} |