[Python-Dev] Revised Proposal: thread.get_dict

Armin Rigo arigo at tunes.org
Wed Jun 30 14:18:32 EDT 2004


Hello Jim,

On Wed, Jun 30, 2004 at 10:24:26AM -0400, Jim Fulton wrote:
> Here is a demonstration Python implementation:
> 
>   from threading import currentThread
> 
>   class local(object):
>
>     ...

The instance should clear the entry from all the per-thread dictionaries when
it is deallocated, to prevent data from outliving it -- and also to prevent
accidental reuse of the same data by a future instance.


Descriptor fun!  I think there is a way to write this class so that "regular"
attributes like __class__ still work, and without having to special-case
__dict__.  Something along the lines of:

    def __patch(self):
        key = "thread-local-%x" % id(self)
        d = currentThread().__dict__.setdefault(key, {})
        object.__setattr__(self, '__dict__', d)

    def __getattribute__(self, name):
        self.__patch()
        return super(local, self).__getattribute__(name)

    def __setattr__(self, name, value):
        self.__patch()
        super(local, self).__setattr__(name, value)

I am unclear about how C types should do the equivalent of the 'super' call,
though.  But I'm ok with declaring that we don't care and use
PyObject_Generic[GS]etAttr().


Finally, don't forget __delattr__.


A bientôt,

Armin.



More information about the Python-Dev mailing list