Skip to content

delattr: use python code to add back missing builtin from cpython? #1251

Description

@TG-Techie

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

Activity

  1. dhalbert commented on Oct 8, 2018

    @dhalbert
    Collaborator

    The presence or absence of delattr() is determined by MPY_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 of delattr() is quite small in the current py/modbuiltins.c

  2. TG-Techie commented on Oct 8, 2018

    @TG-Techie
    Author

    checking out: py/modbuiltins.c

  3. TG-Techie commented on Oct 8, 2018

    @TG-Techie
    Author

    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?

  4. dhalbert commented on Oct 8, 2018

    @dhalbert
    Collaborator

    The presence of delattr is controlled by MICROPY_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);
    #endif
    

    We could control that instead with a new MICROPY_PY_DELATTR flag. But note that, confusingly, there's already a MICROPY_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.

  5. dhalbert commented on Oct 8, 2018

    @dhalbert
    Collaborator

    Or maybe we should just remove the #if, since I'm not sure why delattr() is conditionalized but getattr(), setattr(), and hasattr() are not.

  6. TG-Techie commented on Oct 8, 2018

    @TG-Techie
    Author

    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

  7. tannewt commented on Oct 8, 2018

    @tannewt
    Member

    @TG-Techie Do you have a link to your code? I'm surprised you need to use the attr functions directly.

  8. added this to the Long term milestone on Oct 8, 2018
  9. tannewt commented on Aug 11, 2021

    @tannewt
    Member

    What do we want done here?

  10. dhalbert commented on Aug 11, 2021

    @dhalbert
    Collaborator

    Is MICROPY_CPYTHON_COMPAT turned on for all boards now? I'd start there. I'm not sure whether turning it on will bloat the tiniest builds too much.

  11. tannewt commented on Aug 11, 2021

    @tannewt
    Member

    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?

  12. dhalbert commented on Jan 31, 2025

    @dhalbert
    Collaborator

    Closing because it can be fixed by turning feature on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions