[Python-Dev] Proposal: thread.get_dict

Armin Rigo arigo at tunes.org
Mon Jun 28 12:16:19 EDT 2004


Hello Jim,

On Mon, Jun 28, 2004 at 10:50:08AM -0400, Jim Fulton wrote:
> I propose, for 2.4, to add a get_dict method to the thread
> module that will return this dictionary.  This will be
> implemented via a call to PyThreadState_GetDict.

I am pretty sure that some extension modules would badly crash if arbitrary
Python code would be allowed to temper with the data that they store there.  

Actually, to encourage encapsulation I'd push for another interface.  For
example there could be a constructor thread.Locals which you invoke once at a
global level; it returns an object whose attributes are then local to the
thread.  An unrelated module's call to thread.Locals returns an unrelated
object.

To polish the idea more, thread.Locals would be a class that you can also
inherit from to use the functionality in your own classes.  Something along
the lines of the C version of

class Locals(object):
  def __getattribute__(self, attr):
    d = PyThreadState_GetDict()
    attrs = d.setdefault(self, {})   # use 'self' as the key
    try:
      return attrs[attr]
    except KeyError:
      ...
  def __setattr__(self, attr, value):
    ...
  def __delattr__(self, attr):
    ...


Armin



More information about the Python-Dev mailing list