| Store | Cart

[Python-Dev] Exception for setting attributes of built-in type

From: Nick Coghlan <ncog...@gmail.com>
Tue, 16 Jun 2009 07:31:06 +1000
Benjamin Peterson wrote:
> 2009/6/15 Nick Coghlan <ncoghlan at 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   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------

Recent Messages in this Thread
Seo Sanghyeon Jun 14, 2009 07:09 pm
Terry Reedy Jun 14, 2009 10:40 pm
MRAB Jun 14, 2009 10:42 pm
Guido van Rossum Jun 14, 2009 11:19 pm
Nick Coghlan Jun 15, 2009 11:27 am
Benjamin Peterson Jun 15, 2009 07:09 pm
Nick Coghlan Jun 15, 2009 09:31 pm
Guido van Rossum Jun 15, 2009 02:48 pm
Dino Viehland Jun 15, 2009 03:54 pm
Michael Foord Jun 15, 2009 04:10 pm
Guido van Rossum Jun 15, 2009 05:10 pm
Michael Foord Jun 15, 2009 05:20 pm
Dino Viehland Jul 02, 2009 11:45 pm
Messages in this thread