Skip to content

Commit db42e09

Browse files
committed
py/obj: Remove mp_generic_unary_op().
Since converting to variable sized slots in mp_obj_type_t, we can now reduce the code size a bit by removing mp_generic_unary_op() and the corresponding slots where it is used. Instead we just implement the generic `__hash__` operation in the runtime. Signed-off-by: David Lechner <david@pybricks.com>
1 parent 699477d commit db42e09

File tree

9 files changed

+16
-39
lines changed

9 files changed

+16
-39
lines changed

py/obj.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,3 @@ void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flag
594594
mp_raise_TypeError(MP_ERROR_TEXT("object with buffer protocol required"));
595595
}
596596
}
597-
598-
mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
599-
switch (op) {
600-
case MP_UNARY_OP_HASH:
601-
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
602-
default:
603-
return MP_OBJ_NULL; // op not supported
604-
}
605-
}

py/obj.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ mp_obj_t mp_obj_id(mp_obj_t o_in);
10161016
mp_obj_t mp_obj_len(mp_obj_t o_in);
10171017
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
10181018
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
1019-
mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in);
10201019

10211020
// cell
10221021

py/objfun.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ STATIC mp_obj_t fun_builtin_0_call(mp_obj_t self_in, size_t n_args, size_t n_kw,
5858

5959
MP_DEFINE_CONST_OBJ_TYPE(
6060
mp_type_fun_builtin_0, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
61-
call, fun_builtin_0_call,
62-
unary_op, mp_generic_unary_op
61+
call, fun_builtin_0_call
6362
);
6463

6564
STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -71,8 +70,7 @@ STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw,
7170

7271
MP_DEFINE_CONST_OBJ_TYPE(
7372
mp_type_fun_builtin_1, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
74-
call, fun_builtin_1_call,
75-
unary_op, mp_generic_unary_op
73+
call, fun_builtin_1_call
7674
);
7775

7876
STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -84,8 +82,7 @@ STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw,
8482

8583
MP_DEFINE_CONST_OBJ_TYPE(
8684
mp_type_fun_builtin_2, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
87-
call, fun_builtin_2_call,
88-
unary_op, mp_generic_unary_op
85+
call, fun_builtin_2_call
8986
);
9087

9188
STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -97,8 +94,7 @@ STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw,
9794

9895
MP_DEFINE_CONST_OBJ_TYPE(
9996
mp_type_fun_builtin_3, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
100-
call, fun_builtin_3_call,
101-
unary_op, mp_generic_unary_op
97+
call, fun_builtin_3_call
10298
);
10399

104100
STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -126,8 +122,7 @@ STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_k
126122

127123
MP_DEFINE_CONST_OBJ_TYPE(
128124
mp_type_fun_builtin_var, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
129-
call, fun_builtin_var_call,
130-
unary_op, mp_generic_unary_op
125+
call, fun_builtin_var_call
131126
);
132127

133128
/******************************************************************************/
@@ -370,8 +365,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
370365
MP_TYPE_FLAG_BINDS_SELF,
371366
FUN_BC_TYPE_PRINT
372367
FUN_BC_TYPE_ATTR
373-
call, fun_bc_call,
374-
unary_op, mp_generic_unary_op
368+
call, fun_bc_call
375369
);
376370

377371
mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_module_context_t *context, struct _mp_raw_code_t *const *child_table) {
@@ -432,8 +426,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
432426
MP_TYPE_FLAG_BINDS_SELF,
433427
FUN_BC_TYPE_PRINT
434428
FUN_BC_TYPE_ATTR
435-
call, fun_native_call,
436-
unary_op, mp_generic_unary_op
429+
call, fun_native_call
437430
);
438431

439432
mp_obj_t mp_obj_new_fun_native(const mp_obj_t *def_args, const void *fun_data, const mp_module_context_t *mc, struct _mp_raw_code_t *const *child_table) {
@@ -540,8 +533,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
540533
mp_type_fun_asm,
541534
MP_QSTR_function,
542535
MP_TYPE_FLAG_BINDS_SELF,
543-
call, fun_asm_call,
544-
unary_op, mp_generic_unary_op
536+
call, fun_asm_call
545537
);
546538

547539
mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig) {

py/objgenerator.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
8181
MP_QSTR_generator,
8282
MP_TYPE_FLAG_BINDS_SELF,
8383
GEN_WRAP_TYPE_ATTR
84-
call, gen_wrap_call,
85-
unary_op, mp_generic_unary_op
84+
call, gen_wrap_call
8685
);
8786

8887
/******************************************************************************/
@@ -136,7 +135,7 @@ STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_k
136135
}
137136

138137
#if MICROPY_PY_FUNCTION_ATTRS
139-
#define NATIVE_GEN_WRAP_TYPE_ATTR attr, mp_obj_fun_bc_attr,
138+
#define NATIVE_GEN_WRAP_TYPE_ATTR , attr, mp_obj_fun_bc_attr
140139
#else
141140
#define NATIVE_GEN_WRAP_TYPE_ATTR
142141
#endif
@@ -145,9 +144,8 @@ MP_DEFINE_CONST_OBJ_TYPE(
145144
mp_type_native_gen_wrap,
146145
MP_QSTR_generator,
147146
MP_TYPE_FLAG_BINDS_SELF,
148-
call, native_gen_wrap_call,
147+
call, native_gen_wrap_call
149148
NATIVE_GEN_WRAP_TYPE_ATTR
150-
unary_op, mp_generic_unary_op
151149
);
152150

153151
#endif // MICROPY_EMIT_NATIVE
@@ -370,7 +368,6 @@ MP_DEFINE_CONST_OBJ_TYPE(
370368
MP_QSTR_generator,
371369
MP_TYPE_FLAG_ITER_IS_ITERNEXT,
372370
print, gen_instance_print,
373-
unary_op, mp_generic_unary_op,
374371
iter, gen_instance_iternext,
375372
locals_dict, &gen_instance_locals_dict
376373
);

py/objnone.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
4747
mp_type_NoneType,
4848
MP_QSTR_NoneType,
4949
MP_TYPE_FLAG_NONE,
50-
print, none_print,
51-
unary_op, mp_generic_unary_op
50+
print, none_print
5251
);
5352

5453
#if !MICROPY_OBJ_IMMEDIATE_OBJS

py/objsingleton.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ STATIC void singleton_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
4545

4646
MP_DEFINE_CONST_OBJ_TYPE(
4747
mp_type_singleton, MP_QSTR_, MP_TYPE_FLAG_NONE,
48-
print, singleton_print,
49-
unary_op, mp_generic_unary_op
48+
print, singleton_print
5049
);
5150

5251
const mp_obj_singleton_t mp_const_ellipsis_obj = {{&mp_type_singleton}, MP_QSTR_Ellipsis};

py/objtype.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,6 @@ MP_DEFINE_CONST_OBJ_TYPE(
11121112
make_new, type_make_new,
11131113
print, type_print,
11141114
call, type_call,
1115-
unary_op, mp_generic_unary_op,
11161115
attr, type_attr
11171116
);
11181117

py/profile.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ MP_DEFINE_CONST_OBJ_TYPE(
177177
MP_QSTR_code,
178178
MP_TYPE_FLAG_NONE,
179179
print, code_print,
180-
unary_op, mp_generic_unary_op,
181180
attr, code_attr
182181
);
183182

@@ -247,7 +246,6 @@ MP_DEFINE_CONST_OBJ_TYPE(
247246
MP_QSTR_frame,
248247
MP_TYPE_FLAG_NONE,
249248
print, frame_print,
250-
unary_op, mp_generic_unary_op,
251249
attr, frame_attr
252250
);
253251

py/runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
314314
if (result != MP_OBJ_NULL) {
315315
return result;
316316
}
317+
} else if (op == MP_UNARY_OP_HASH) {
318+
// Type doesn't have unary_op so use hash of object instance.
319+
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)arg);
317320
}
318321
if (op == MP_UNARY_OP_BOOL) {
319322
// Type doesn't have unary_op (or didn't handle MP_UNARY_OP_BOOL),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy