Re: [Python-Dev] Exception for setting attributes of built-in type
On Sun, Jun 14, 2009 at 4:19 PM, Guido van Rossumgu...@python.org wrote: In general, CPython isn't always consistent in raising AttributeError and TypeError when it comes to such policy issues: there are various places that raise TypeError in typeobject.c (and probably elsewhere) that simply forbid setting a specific attribute (another example is __name__). I should add that this policy is also forced somewhat by the existence of the multiple interpreters in one address space feature, which is used e.g. by mod_python. This feature attempts to provide isolation between interpreters to the point that each one can have a completely different set of modules loaded and can be working on a totally different application. The implementation of CPython shares built-in types between multiple interpreters (and it wouldn't be easy to change this); if you were able to modify a built-in type from one interpreter, all other interpreters would see that same modification. -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Exception for setting attributes of built-in type
2009/6/15 Nick Coghlan ncogh...@gmail.com: Guido van Rossum wrote: In general, CPython isn't always consistent in raising AttributeError and TypeError when it comes to such policy issues: there are various places that raise TypeError in typeobject.c (and probably elsewhere) that simply forbid setting a specific attribute (another example is __name__). We're pretty inconsistent when it comes to looking up special methods as well - those that are looked up through dedicated slots in abstract.c usually raise TypeError, while those that are looked up via a PyType method usually raise AttributeError. What's a PyType method? -- Regards, Benjamin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Exception for setting attributes of built-in type
Benjamin Peterson wrote: 2009/6/15 Nick Coghlan ncogh...@gmail.com: Guido van Rossum wrote: In general, CPython isn't always consistent in raising AttributeError and TypeError when it comes to such policy issues: there are various places that raise TypeError in typeobject.c (and probably elsewhere) that simply forbid setting a specific attribute (another example is __name__). We're pretty inconsistent when it comes to looking up special methods as well - those that are looked up through dedicated slots in abstract.c usually raise TypeError, while those that are looked up via a PyType method usually raise AttributeError. What's a PyType method? I was misremembering - for some reason I thought: a) _PyObject_LookupSpecial was a PyType method b) That it raised AttributeError itself instead of letting the caller decide what error to raise It's still the case that (e.g.) special_lookup() in ceval.c raises an AttributeError, as do special method lookups from Python of the form type(obj).__method__(obj). Whether CPython raises TypeError or AttributeError when it comes to special methods is really pretty arbitrary rather than a matter of any grand design. Cheers, Nick. P.S. If anyone feels like digging into the archives, the last time I can recall this topic coming up is when I was concerned about the original with statement implementation raising AttributeError rather than TypeError when it couldn't find an __enter__ or __exit__ method. Guido chimed in then (as now) to say that either exception was fine. -- Nick Coghlan | ncogh...@gmail.com | Brisbane, Australia --- ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Exception for setting attributes of built-in type
Exception for setting attributes of built-in type differs between CPython and IronPython. This is not purely theoretical, as zope.interface tries to set Implements declaration as __implemented__ attribute of built-in type object, and excepts TypeError. Python 2.6.1 object.flag = True TypeError: can't set attributes of built-in/extension type 'object' IronPython 2.6 object.flag = True AttributeError: 'object' object has no attribute 'flag' I was surprised that CPython raises TypeError. Library Reference seems to mention it here: exception AttributeError Raised when an attribute reference or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.) http://docs.python.org/library/exceptions.html What does it mean that an object does not support attribute references or attribute assignments at all? -- Seo Sanghyeon ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Exception for setting attributes of built-in type
Seo Sanghyeon wrote: Exception for setting attributes of built-in type differs between CPython and IronPython. This is not purely theoretical, as zope.interface tries to set Implements declaration as __implemented__ attribute of built-in type object, and excepts TypeError. Python 2.6.1 object.flag = True TypeError: can't set attributes of built-in/extension type 'object' IronPython 2.6 object.flag = True AttributeError: 'object' object has no attribute 'flag' I was surprised that CPython raises TypeError. Library Reference seems to mention it here: exception AttributeError Raised when an attribute reference or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.) http://docs.python.org/library/exceptions.html What does it mean that an object does not support attribute references or attribute assignments at all? I see it as slightly ambiguous: 1. It neither supports references nor assignments. 2. It either does not support reference or it does not support assignments. 1 could hardly apply any more, certainly to built-ins since everything now has attributes. 2 covers object, so if that is meant, then TypeError is appropriate. Or were you unclear about 'at all', which means 'never'? Terry Jan Reedy ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Exception for setting attributes of built-in type
Seo Sanghyeon wrote: Exception for setting attributes of built-in type differs between CPython and IronPython. This is not purely theoretical, as zope.interface tries to set Implements declaration as __implemented__ attribute of built-in type object, and excepts TypeError. Python 2.6.1 object.flag = True TypeError: can't set attributes of built-in/extension type 'object' IronPython 2.6 object.flag = True AttributeError: 'object' object has no attribute 'flag' I was surprised that CPython raises TypeError. Library Reference seems to mention it here: exception AttributeError Raised when an attribute reference or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.) http://docs.python.org/library/exceptions.html What does it mean that an object does not support attribute references or attribute assignments at all? Here's my guess: Some objects have a fixed (and possibly empty) set of attributes. Attempting to add a new attribute or get a non-existent attribute raises an AttributeError. Other objects, such as types (are there any that aren't types?), don't have attributes at all. Attempting to add or get an attribute raises a TypeError. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Exception for setting attributes of built-in type
On Sun, Jun 14, 2009 at 3:42 PM, MRABpyt...@mrabarnett.plus.com wrote: Seo Sanghyeon wrote: Exception for setting attributes of built-in type differs between CPython and IronPython. This is not purely theoretical, as zope.interface tries to set Implements declaration as __implemented__ attribute of built-in type object, and excepts TypeError. Python 2.6.1 object.flag = True TypeError: can't set attributes of built-in/extension type 'object' IronPython 2.6 object.flag = True AttributeError: 'object' object has no attribute 'flag' I was surprised that CPython raises TypeError. Library Reference seems to mention it here: exception AttributeError Raised when an attribute reference or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.) http://docs.python.org/library/exceptions.html What does it mean that an object does not support attribute references or attribute assignments at all? Here's my guess: Some objects have a fixed (and possibly empty) set of attributes. Attempting to add a new attribute or get a non-existent attribute raises an AttributeError. Other objects, such as types (are there any that aren't types?), don't have attributes at all. Attempting to add or get an attribute raises a TypeError. This particular error comes (grep tells me :-) from type_setattro() in Objects/typeobject.c. It makes a specific check whether the type object whose attribute you want to set is a built-in type (this is done by checking the HEAPTYPE flag, which is never set for built-in types and always for user-defined types). For built-in types it disallows setting attributes. This is a pure policy issue: it prevents different 3rd party modules to make incompatible modifications of built-in types. In general, CPython isn't always consistent in raising AttributeError and TypeError when it comes to such policy issues: there are various places that raise TypeError in typeobject.c (and probably elsewhere) that simply forbid setting a specific attribute (another example is __name__). Given how poorly Python exceptions are specified in general, I don't want to hold IronPython to this standard (nor CPython :-). The reason this breaks zope.interfaces in IronPython is probably just that zope.interfaces was primarily written/tested against CPython. Going back to the phrase quoted from the reference manual, it's probably referring to the possibility that a type's tp_getattro slot is NULL; but even so it's wrong: PyObject_GetAttr() raises AttributeError in this case. Uselessly y'rs, -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com