summaryrefslogtreecommitdiff
path: root/py/objstr.c
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2019-10-15 15:55:21 -0400
committerDan Halbert <halbert@halwitz.org>2019-10-15 15:55:21 -0400
commitbe8136dc6d09e2c3af5c20c881662ebf51a778d8 (patch)
treed365470b40d1877449c929dce43c55a288550437 /py/objstr.c
parentfc19e03128aeeb94a22fdff097e4d0576d768986 (diff)
parent63790f01f833ff197d92f80816f21a61e924bd72 (diff)
Merge remote-tracking branch 'adafruit/master' into bonding1
Diffstat (limited to 'py/objstr.c')
-rw-r--r--py/objstr.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 6ef9a15b5..fde264681 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -31,6 +31,7 @@
#include "py/unicode.h"
#include "py/objstr.h"
#include "py/objlist.h"
+#include "py/objtype.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
@@ -226,10 +227,6 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons
return MP_OBJ_FROM_PTR(o);
}
- if (n_args > 1) {
- goto wrong_args;
- }
-
if (MP_OBJ_IS_SMALL_INT(args[0])) {
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(args[0]);
if (len < 0) {
@@ -241,6 +238,17 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
+ if (n_args > 1) {
+ goto wrong_args;
+ }
+
+ // check if __bytes__ exists, and if so delegate to it
+ mp_obj_t dest[2];
+ mp_load_method_maybe(args[0], MP_QSTR___bytes__, dest);
+ if (dest[0] != MP_OBJ_NULL) {
+ return mp_call_method_n_kw(0, 0, dest);
+ }
+
// check if argument has the buffer protocol
mp_buffer_info_t bufinfo;
if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ)) {