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

Comprehensibility patch (typeobject.c) #36348

Closed
davidabrahams mannequin opened this issue Mar 28, 2002 · 5 comments
Closed

Comprehensibility patch (typeobject.c) #36348

davidabrahams mannequin opened this issue Mar 28, 2002 · 5 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@davidabrahams
Copy link
Mannequin

davidabrahams mannequin commented Mar 28, 2002

BPO 536407
Nosy @gvanrossum, @nascheme
Files
  • patch: patch contents
  • 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/gvanrossum'
    closed_at = <Date 2002-04-04.17:51:46.000>
    created_at = <Date 2002-03-28.18:56:33.000>
    labels = ['interpreter-core']
    title = 'Comprehensibility patch (typeobject.c)'
    updated_at = <Date 2002-04-04.17:51:46.000>
    user = 'https://bugs.python.org/davidabrahams'

    bugs.python.org fields:

    activity = <Date 2002-04-04.17:51:46.000>
    actor = 'gvanrossum'
    assignee = 'gvanrossum'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2002-03-28.18:56:33.000>
    creator = 'david_abrahams'
    dependencies = []
    files = ['4093']
    hgrepos = []
    issue_num = 536407
    keywords = ['patch']
    message_count = 5.0
    messages = ['39401', '39402', '39403', '39404', '39405']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'nascheme', 'david_abrahams']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue536407'
    versions = []

    @davidabrahams
    Copy link
    Mannequin Author

    davidabrahams mannequin commented Mar 28, 2002

    --- typeobject.c	Mon Dec 17 12:14:22 2001
    +++ typeobject.c.new	Thu Mar 28 13:46:03 2002
    @@ -1186,8 +1186,8 @@
     type_getattro(PyTypeObject *type, PyObject *name)
     {
     	PyTypeObject *metatype = type->ob_type;
    -	PyObject *descr, *res;
    -	descrgetfunc f;
    +	PyObject *meta_attribute, *attribute;
    +	descrgetfunc meta_get;
     
     	/* Initialize this type (we'll assume the 
    metatype is initialized) */
     	if (type->tp_dict == NULL) {
    @@ -1195,34 +1195,50 @@
     			return NULL;
     	}
     
    -	/* Get a descriptor from the metatype */
    -	descr = _PyType_Lookup(metatype, name);
    -	f = NULL;
    -	if (descr != NULL) {
    -		f = descr->ob_type->tp_descr_get;
    -		if (f != NULL && PyDescr_IsData
    (descr))
    -			return f(descr,
    -				 (PyObject *)type, 
    (PyObject *)metatype);
    -	}
    +	/* No readable descriptor found yet */
    +	meta_get = NULL;
    +        
    +	/* Look for the attribute in the metatype */
    +	meta_attribute = _PyType_Lookup(metatype, 
    name);
     
    -	/* Look in tp_dict of this type and its bases 
    */
    -	res = _PyType_Lookup(type, name);
    -	if (res != NULL) {
    -		f = res->ob_type->tp_descr_get;
    -		if (f != NULL)
    -			return f(res, (PyObject *)
    NULL, (PyObject *)type);
    -		Py_INCREF(res);
    -		return res;
    +	if (meta_attribute != NULL) {
    +		meta_get = meta_attribute->ob_type-
    >tp_descr_get;
    +                
    +		if (meta_get != NULL && PyDescr_IsData
    (meta_attribute)) {
    +            /* Data descriptors implement 
    tp_descr_set to intercept
    +             * writes. Assume the attribute is not 
    overridden in
    +             * type's tp_dict (and bases): call the 
    descriptor now.
    +             */
    +			return meta_get
    (meta_attribute,
    +                            (PyObject *)type, 
    (PyObject *)metatype);
    +        }
     	}
     
    -	/* Use the descriptor from the metatype */
    -	if (f != NULL) {
    -		res = f(descr, (PyObject *)type, 
    (PyObject *)metatype);
    -		return res;
    +	/* No data descriptor found on metatype. Look 
    in tp_dict of this
    +     * type and its bases */
    +	attribute = _PyType_Lookup(type, name);
    +	if (attribute != NULL) {
    +        /* Implement descriptor functionality, if 
    any */
    +		descrgetfunc local_get = attribute-
    >ob_type->tp_descr_get;
    +		if (local_get != NULL) {
    +            /* NULL 2nd argument indicates the 
    descriptor was found on
    +             * the target object itself (or a base)  
    */
    +			return local_get(attribute, 
    (PyObject *)NULL, (PyObject *)type);
    +        }
    +        
    +		Py_INCREF(attribute);
    +		return attribute;
     	}
    -	if (descr != NULL) {
    -		Py_INCREF(descr);
    -		return descr;
    +
    +	/* No attribute found in local __dict__ (or 
    bases): use the
    +     * descriptor from the metatype, if any */
    +	if (meta_get != NULL)
    +		return meta_get(meta_attribute, 
    (PyObject *)type, (PyObject *)metatype);
    +
    +    /* If an ordinary attribute was found on the 
    metatype, return it now. */
    +	if (meta_attribute != NULL) {
    +		Py_INCREF(meta_attribute);
    +		return meta_attribute;
     	}
     
     	/* Give up */

    @davidabrahams davidabrahams mannequin closed this as completed Mar 28, 2002
    @davidabrahams davidabrahams mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 28, 2002
    @davidabrahams
    Copy link
    Mannequin Author

    davidabrahams mannequin commented Mar 29, 2002

    Logged In: YES
    user_id=52572

    I have updated the patch so that it is made against the
    current sources.

    -------

    --- typeobject.c	Thu Mar 28 00:33:33 2002
    +++ typeobject.c.new	Fri Mar 29 16:20:12 2002
    @@ -1237,8 +1237,8 @@
     type_getattro(PyTypeObject *type, PyObject *name)
     {
     	PyTypeObject *metatype = type->ob_type;
    -	PyObject *descr, *res;
    -	descrgetfunc f;
    +	PyObject *meta_attribute, *attribute;
    +	descrgetfunc meta_get;
     
     	/* Initialize this type (we'll assume the metatype 
    is initialized) */
     	if (type->tp_dict == NULL) {
    @@ -1246,40 +1246,56 @@
     			return NULL;
     	}
     
    -	/* Get a descriptor from the metatype */
    -	descr = _PyType_Lookup(metatype, name);
    -	f = NULL;
    -	if (descr != NULL) {
    -		f = descr->ob_type->tp_descr_get;
    -		if (f != NULL && PyDescr_IsData(descr))
    -			return f(descr,
    -				 (PyObject *)type, 
    (PyObject *)metatype);
    -	}
    +	/* No readable descriptor found yet */
    +	meta_get = NULL;
    +		
    +	/* Look for the attribute in the metatype */
    +	meta_attribute = _PyType_Lookup(metatype, name);
     
    -	/* Look in tp_dict of this type and its bases */
    -	res = _PyType_Lookup(type, name);
    -	if (res != NULL) {
    -		f = res->ob_type->tp_descr_get;
    -		if (f != NULL)
    -			return f(res, (PyObject *)NULL, 
    (PyObject *)type);
    -		Py_INCREF(res);
    -		return res;
    +	if (meta_attribute != NULL) {
    +		meta_get = meta_attribute->ob_type-
    >tp_descr_get;
    +				
    +		if (meta_get != NULL && PyDescr_IsData
    (meta_attribute)) {
    +			/* Data descriptors implement 
    tp_descr_set to intercept
    +			 * writes. Assume the attribute is 
    not overridden in
    +			 * type's tp_dict (and bases): 
    call the descriptor now.
    +			 */
    +			return meta_get(meta_attribute,
    +						
    	(PyObject *)type, (PyObject *)metatype);
    +		}
     	}
     
    -	/* Use the descriptor from the metatype */
    -	if (f != NULL) {
    -		res = f(descr, (PyObject *)type, (PyObject 
    *)metatype);
    -		return res;
    +	/* No data descriptor found on metatype. Look in 
    tp_dict of this
    +	 * type and its bases */
    +	attribute = _PyType_Lookup(type, name);
    +	if (attribute != NULL) {
    +		/* Implement descriptor functionality, if 
    any */
    +		descrgetfunc local_get = attribute-
    >ob_type->tp_descr_get;
    +		if (local_get != NULL) {
    +			/* NULL 2nd argument indicates the 
    descriptor was found on
    +			 * the target object itself (or a 
    base)  */
    +			return local_get(attribute, 
    (PyObject *)NULL, (PyObject *)type);
    +		}
    +		
    +		Py_INCREF(attribute);
    +		return attribute;
     	}
    -	if (descr != NULL) {
    -		Py_INCREF(descr);
    -		return descr;
    +
    +	/* No attribute found in local __dict__ (or 
    bases): use the
    +	 * descriptor from the metatype, if any */
    +	if (meta_get != NULL)
    +		return meta_get(meta_attribute, (PyObject 
    *)type, (PyObject *)metatype);
    +
    +	/* If an ordinary attribute was found on the 
    metatype, return it now. */
    +	if (meta_attribute != NULL) {
    +		Py_INCREF(meta_attribute);
    +		return meta_attribute;
     	}
     
     	/* Give up */
     	PyErr_Format(PyExc_AttributeError,
    -		     "type object '%.50s' has no 
    attribute '%.400s'",
    -		     type->tp_name, PyString_AS_STRING
    (name));
    +			 "type object '%.50s' has no 
    attribute '%.400s'",
    +			 type->tp_name, PyString_AS_STRING
    (name));
     	return NULL;
     }

    @nascheme
    Copy link
    Member

    Logged In: YES
    user_id=35752

    Don't paste the patch in the comment box.

    @davidabrahams
    Copy link
    Mannequin Author

    davidabrahams mannequin commented Mar 29, 2002

    Logged In: YES
    user_id=52572

    Thanks, Neil, I think I got the picture already (see
    Python-Dev).

    @gvanrossum
    Copy link
    Member

    Logged In: YES
    user_id=6380

    Thanks, applied (after folding some long lines).

    Next time, please don't call the patch "patch". Call it
    something like "typeobject.patch".

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 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