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

Descriptor instance attributes not interpreted consistently #46857

Closed
PiDelport mannequin opened this issue Apr 10, 2008 · 4 comments
Closed

Descriptor instance attributes not interpreted consistently #46857

PiDelport mannequin opened this issue Apr 10, 2008 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@PiDelport
Copy link
Mannequin

PiDelport mannequin commented Apr 10, 2008

BPO 2605
Nosy @gvanrossum, @PiDelport

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 = None
closed_at = <Date 2008-06-11.16:46:33.965>
created_at = <Date 2008-04-10.10:15:08.122>
labels = ['interpreter-core', 'type-bug', 'library']
title = 'Descriptor instance attributes not interpreted consistently'
updated_at = <Date 2008-06-11.16:46:33.912>
user = 'https://github.com/PiDelport'

bugs.python.org fields:

activity = <Date 2008-06-11.16:46:33.912>
actor = 'gvanrossum'
assignee = 'none'
closed = True
closed_date = <Date 2008-06-11.16:46:33.965>
closer = 'gvanrossum'
components = ['Interpreter Core', 'Library (Lib)']
creation = <Date 2008-04-10.10:15:08.122>
creator = 'PiDelport'
dependencies = []
files = []
hgrepos = []
issue_num = 2605
keywords = []
message_count = 4.0
messages = ['65289', '65574', '65578', '67993']
nosy_count = 2.0
nosy_names = ['gvanrossum', 'PiDelport']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2605'
versions = ['Python 2.6', 'Python 2.5', 'Python 2.4', 'Python 2.3', 'Python 2.2.3', 'Python 2.2.2', 'Python 2.2.1', 'Python 2.2', 'Python 3.0']

@PiDelport
Copy link
Mannequin Author

PiDelport mannequin commented Apr 10, 2008

Short version: __get__/set/delete attributes on descriptor objects
(as opposed to their types) are treated inconsistently as part of the
descriptor
protocol: the documentation and support code includes them; the core
implementation doesn't.

Example:

    class D(object):
        __get__ = lambda self, i, o: 'D'

    class C(object):
        d = D()
        d.__get__ = lambda i, o: 'd'
        d.__set__ = lambda i, v: 1/0

    c = C()

According to pydoc and inspect, and the description in the reference manual
(section 3.4.2.3), d's __get__ and __set__ override D's:

    >>> inspect.isdatadescriptor(C.__dict__['d'])
    True
    >>> help(C)
    class C(__builtin__.object)
    |  Data descriptors defined here:
    ...
    |  d

    >>> type(c).__dict__['d'].__get__(c, type(c))
    'd'
    >>> type(c).__dict__['d'].__set__(c, 5)
    ZeroDivisionError: integer division or modulo by zero

According to CPython, they have no effect:

    >>> c.d
    'D'
    >>> c.d = 5; c.d
    5

PEP-252 notes: "For speed, the get and set methods are type slots", which
points to the CPython behavior being an intentional concession for
performance.

Should CPython respect descriptor object attributes, if reasonable
performance
can be maintained? Otherwise, should the documentation and support code be
changed to ignore them?

@PiDelport PiDelport mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 10, 2008
@PiDelport
Copy link
Mannequin Author

PiDelport mannequin commented Apr 17, 2008

Related: bpo-643841 (new-style special method lookup)

@PiDelport
Copy link
Mannequin Author

PiDelport mannequin commented Apr 17, 2008

From the Py3K list:
http://mail.python.org/pipermail/python-3000/2007-March/006304.html

The sentiment appears to be to leave the behavior
implementation-defined. It seems straightforward to update inspect and
pydoc to mirror typeobject.c, but i'm not sure where this leaves the
documentation.

@gvanrossum
Copy link
Member

The behavior observed is intentional. The docs should be updated.

@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) stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant