Repository navigation
delattr: use python code to add back missing builtin from cpython? #1251
Description
Activity
The presence or absence of
delattr()is determined byMPY_CPYTHON_COMPAT, which turns a lot of things on and off, costing about 800 bytes in the build. We can refactor this to break that big on/off switch into several smaller ones. The implementation ofdelattr()is quite small in the currentpy/modbuiltins.cchecking out: py/modbuiltins.c
I think I am missing a good bit of how cp is organised.
why is py/modbuiltins.c not implemented normally if it is in py?
best place to learn?The presence of
delattris controlled byMICROPY_CPYTHON_COMPAT#if MICROPY_CPYTHON_COMPAT { MP_ROM_QSTR(MP_QSTR_delattr), MP_ROM_PTR(&mp_builtin_delattr_obj) }, #endif#if MICROPY_CPYTHON_COMPAT STATIC mp_obj_t mp_builtin_delattr(mp_obj_t base, mp_obj_t attr) { return mp_builtin_setattr(base, attr, MP_OBJ_NULL); } MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_delattr_obj, mp_builtin_delattr); #endifWe could control that instead with a new
MICROPY_PY_DELATTRflag. But note that, confusingly, there's already aMICROPY_PY_DELATTR_SETATTR, which implements__delattr__and__setattr__, which are hook functions that get called when an attribute is deleted or set. So maybe we need a clearer new flag name.Or maybe we should just remove the
#if, since I'm not sure whydelattr()is conditionalized butgetattr(),setattr(), andhasattr()are not.Mr. Hablert, (Dan or Mr.Halbert)as more of a user than developer, hopefully just for nowcrosses fingers, I did find it is confusing that three are included yet the other isn't
@TG-Techie Do you have a link to your code? I'm surprised you need to use the attr functions directly.
What do we want done here?
Is
MICROPY_CPYTHON_COMPATturned on for all boards now? I'd start there. I'm not sure whether turning it on will bloat the tiniest builds too much.Nope, it looks like it's tied to FULL_BUILD. There isn't really anything to do for this specific issue. Do we want to have a new issue to enable CPYTHON_COMPAT on all boards?
Closing because it can be fixed by turning feature on.
dear developers,
I am a huge fan of cp and am loving using it!
please hear me out:
was making a program loader and bufferer for my gui/user_thing by making classes and after asking around why I was having a problem Dan Halbert pointed out that delattr isn't in cp.
however with Mr.Halbert's help and a pile of dead bugs, I mean debugs :-), I found this works:
def delattr(target,name): exec('del str_target.'+name, {'str_target':target})and am using it
and if you check the cpython page about builtin functions it says:
"For example, delattr(x, 'foobar') is equivalent to del x.foobar."
see: https://docs.python.org/3/library/functions.html#delattr
I would love to investigate implementing this at a c level or a python level!? (if small size # of bytes)
is that "kosher" to ask to do?
if it is okay for my to try this can i ask where the builtins are? I looked through repo but could not find it. in tools? (also tried finding getattr, search did not yield its location)
thanks for considering it!,
~TG-Techie