Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding __dir__ #44203

Closed
gangesmaster mannequin opened this issue Nov 6, 2006 · 12 comments
Closed

adding __dir__ #44203

gangesmaster mannequin opened this issue Nov 6, 2006 · 12 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@gangesmaster
Copy link
Mannequin

gangesmaster mannequin commented Nov 6, 2006

BPO 1591665
Nosy @arigo, @birkenfeld, @ncoghlan
Files
  • demo.py: a little test/demo
  • dir.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/birkenfeld'
    closed_at = <Date 2007-03-12.13:18:22.000>
    created_at = <Date 2006-11-06.21:52:36.000>
    labels = ['interpreter-core']
    title = 'adding __dir__'
    updated_at = <Date 2007-03-12.13:18:22.000>
    user = 'https://bugs.python.org/gangesmaster'

    bugs.python.org fields:

    activity = <Date 2007-03-12.13:18:22.000>
    actor = 'georg.brandl'
    assignee = 'georg.brandl'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2006-11-06.21:52:36.000>
    creator = 'gangesmaster'
    dependencies = []
    files = ['7605', '7606']
    hgrepos = []
    issue_num = 1591665
    keywords = ['patch']
    message_count = 12.0
    messages = ['51325', '51326', '51327', '51328', '51329', '51330', '51331', '51332', '51333', '51334', '51335', '51336']
    nosy_count = 5.0
    nosy_names = ['nnorwitz', 'arigo', 'georg.brandl', 'ncoghlan', 'gangesmaster']
    pr_nums = []
    priority = 'high'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1591665'
    versions = ['Python 2.6']

    @gangesmaster
    Copy link
    Mannequin Author

    gangesmaster mannequin commented Nov 6, 2006

    in accordance with
    http://mail.python.org/pipermail/python-dev/2006-November/069865.html

    i've written a patch that allows objects to define their
    own introspection mechanisms, by providing __dir__.
    with this patch:

    • dir() returns the locals. this is done in builtin_dir()
    • dir(obj) returns the attributes of obj,
      by invoking PyObject_Dir()
    • if obj->ob_type has "__dir__", it is used.
      note that it must return a list!
    • otherwise, use default the mechanism of collecting
      attributes
    • for module objects, return __dict__.keys()
    • for type objects, return __dict__.keys() +
      dir(obj.__base__)
    • for all other objects, return __dict__.keys() +
      __members__ + __methods__ + dir(obj.__class__)
    • builtin_dir takes care of sorting the list

    @gangesmaster gangesmaster mannequin closed this as completed Nov 6, 2006
    @gangesmaster gangesmaster mannequin assigned birkenfeld Nov 6, 2006
    @gangesmaster gangesmaster mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 6, 2006
    @gangesmaster gangesmaster mannequin closed this as completed Nov 6, 2006
    @gangesmaster gangesmaster mannequin assigned birkenfeld Nov 6, 2006
    @gangesmaster gangesmaster mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 6, 2006
    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Nov 6, 2006

    Logged In: YES
    user_id=1038590

    The retrieval of locals on a NULL argument and the sorting
    step need to move back inside PyObject_Dir to avoid changing
    the C API.

    If the standard library's current C API tests didn't break
    on this version of the patch, then the final version of the
    patch should include enhanced tests for PyObject_Dir that
    pass both before and after the patch is applied to PyObject_Dir.

    Other than that, I didn't see any major problems on reading
    the code (i.e. refcounts and error handling looked pretty
    reasonable). I haven't actually run it though.

    @gangesmaster
    Copy link
    Mannequin Author

    gangesmaster mannequin commented Nov 7, 2006

    Logged In: YES
    user_id=1406776

    okay:

    • builtin_dir directly calls PyObject_Dir
    • PyObject_Dir handles NULL argument and sorting
    • it is now completely compatible with the 2.5 API
    • fixed several refcount bugs (i wish we had a tracing gc :)

    @nnorwitz
    Copy link
    Mannequin

    nnorwitz mannequin commented Nov 8, 2006

    Logged In: YES
    user_id=33168

    tomer, do you know about configuring with --pydebug? That
    helps track down refleaks when running regrtest -R ::.

    object.c:
    _dir_locals: result is not necessary and locals doesn't
    need to be initialized as it's set on the next line. You
    could just declare and set it all on one line.

    _specialized_dir_type should be static. No need to init
    dict. Either don't init result or remove else result =
    NULL. I'd prefer removing the else and leaving the init.

    _specialized_dir_module should be static. No need to init
    dict. Can you get the name of the module and use that in
    the error msg: PyModule_GetName()? That would hopefully
    provide a nicer error msg.

    _generic_dir: No need to init dict.

    + /* XXX api change: how about falling to
    obj->ob_type
    + XXX if no __class__ exists? */

    Do you mean falling *back*? Also, we've been using
    XXX(username): as the format for such comments. So this
    would be better as:

    /* XXX(tomer): Perhaps fall back to obj->ob_type if no
    __class__ exists? */

    _dir_object: No need to init dirfunc.

    PyObject_Dir: No need to init result.

    Are there tests for all conditions? At least:

    • dir()
    • dir(obj)
    • dir(obj_with_no_dict)
    • dir(obj_with_no__class__)
    • dir(obj_with__methods__)
    • dir(obj_with__members__)
    • dir(module)
    • dir(module_with_no__dict__)
    • dir(module_with_invalid__dict__)

    There also need to be updates to Doc/lib/libfuncs.tex. If
    you can't deal with the markup, just do the best you can in
    text and someone else will fixup the markup.

    Thanks for attaching the patch as a single file, it's easier
    to deal with.

    @gangesmaster
    Copy link
    Mannequin Author

    gangesmaster mannequin commented Nov 8, 2006

    Logged In: YES
    user_id=1406776

    i like to init all my locals ("just in case"), but
    if the rest of the code does not adhere my style,
    i'll change that.
    anyway, i made the changes to the code, updated the docs,
    and added full tests (the original dir() wasn't test
    so thoroughly)

    -tomer

    @birkenfeld
    Copy link
    Member

    Logged In: YES
    user_id=849994

    • Instead of doing PyObject_CallFunction(dirfunc, "O", obj)
      you should
      do PyObject_CallFunctionObjArgs(dirfunc, obj, NULL).
    • Couldn't __dir__ also be allowed to return a tuple?

    @gangesmaster
    Copy link
    Mannequin Author

    gangesmaster mannequin commented Nov 11, 2006

    Logged In: YES
    user_id=1406776

    PyObject_CallFunctionObjArgs(dirfunc, obj, NULL)
    done

    Couldn't __dir__ also be allowed to return a tuple?
    no, because tuples are not sortable, and i don't want to
    over complicate the c-side code of PyObject_Dir.
    having __dir__ returning only a list is equivalent to
    __repr__ returning only strings.

    @arigo
    Copy link
    Mannequin

    arigo mannequin commented Nov 23, 2006

    Line 20 in demo.py:

    assert "__getitem__" in dir(x)
    

    looks strange to me... Foo doesn't inherit from
    any sequence or mapping type.

    @gangesmaster
    Copy link
    Mannequin Author

    gangesmaster mannequin commented Dec 19, 2006

    i guess the demo isn't updated/relevant anymore.
    instead, concrete tests were added to lib/tests/test_builtin.py

    @birkenfeld
    Copy link
    Member

    Applied to Python 3000 branch in rev. 54265.
    Leaving open for the case that there's interest in backporting to 2.6.

    @nnorwitz
    Copy link
    Mannequin

    nnorwitz mannequin commented Mar 11, 2007

    I think this should be backported to 2.6.

    @birkenfeld
    Copy link
    Member

    Done in rev. 54287.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants