summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/obj.c18
-rw-r--r--py/obj.h2
2 files changed, 20 insertions, 0 deletions
diff --git a/py/obj.c b/py/obj.c
index 8d5467c5e7..d1db53690f 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -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"));
+ }
+}
diff --git a/py/obj.h b/py/obj.h
index 92dd6a8a0b..ab1685d4d0 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -196,6 +196,8 @@ typedef struct _buffer_info_t {
typedef struct _mp_buffer_p_t {
machine_int_t (*get_buffer)(mp_obj_t obj, buffer_info_t *bufinfo, int flags);
} mp_buffer_p_t;
+bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo);
+void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo);
// Stream protocol
typedef struct _mp_stream_p_t {