what's unsafe to do in a __getattr__?

Mats Wichmann mats at wichmann.us
Mon Oct 25 13:05:23 EDT 2021


On 10/25/21 10:48, Dieter Maurer wrote:
> Mats Wichmann wrote at 2021-10-24 19:10 -0600:
>> Have run into a problem on a "mature" project I work on (there are many
>> years of history before I joined), there are a combination of factors
>> that combine to trigger a KeyError when using copy.copy().
>> ...
>> There's a class that's a kind of proxy ...
> 
> "Proxying" has become very difficult since the introduction
> of Python's "new style classes" and looking up "special methods"
> on the type rather then the type instance.
> 
> This essentially means that the proxy must implement all
> "special methods" that may be relevant to the application.
> 
> All special methods start and end with `"__"`.
> Your `__getattr__` could raise an `AttributeError` as soon as
> it is called with such a name.
> 

Thanks. Raising an AttributeError here was what I was going to propose 
so maybe I'll take your reply as validation I was on the right track.

This stuff was definitely defined in an era before the new-style 
classes, might have to examine some other proxying classes to make sure 
there aren't other holes...

thanks again,

-- mats


More information about the Python-list mailing list