31
31
#include "py/unicode.h"
32
32
#include "py/objstr.h"
33
33
#include "py/objlist.h"
34
+ #include "py/objtype.h"
34
35
#include "py/runtime.h"
35
36
#include "py/stackctrl.h"
36
37
@@ -211,6 +212,10 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons
211
212
return mp_const_empty_bytes ;
212
213
}
213
214
215
+ if (n_args > 1 ) {
216
+ goto wrong_args ;
217
+ }
218
+
214
219
if (MP_OBJ_IS_STR (args [0 ])) {
215
220
if (n_args < 2 || n_args > 3 ) {
216
221
goto wrong_args ;
@@ -226,10 +231,6 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons
226
231
return MP_OBJ_FROM_PTR (o );
227
232
}
228
233
229
- if (n_args > 1 ) {
230
- goto wrong_args ;
231
- }
232
-
233
234
if (MP_OBJ_IS_SMALL_INT (args [0 ])) {
234
235
mp_int_t len = MP_OBJ_SMALL_INT_VALUE (args [0 ]);
235
236
if (len < 0 ) {
@@ -241,6 +242,13 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons
241
242
return mp_obj_new_str_from_vstr (& mp_type_bytes , & vstr );
242
243
}
243
244
245
+ // check if __bytes__ exists, and if so delegate to it
246
+ mp_obj_t dest [2 ];
247
+ mp_load_method_maybe (args [0 ], MP_QSTR___bytes__ , dest );
248
+ if (dest [0 ] != MP_OBJ_NULL ) {
249
+ return mp_call_method_n_kw (0 , 0 , dest );
250
+ }
251
+
244
252
// check if argument has the buffer protocol
245
253
mp_buffer_info_t bufinfo ;
246
254
if (mp_get_buffer (args [0 ], & bufinfo , MP_BUFFER_READ )) {
0 commit comments