Skip to content

Instantly share code, notes, and snippets.

@saghul
Created November 26, 2012 22:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saghul/4151154 to your computer and use it in GitHub Desktop.
Save saghul/4151154 to your computer and use it in GitHub Desktop.
greenlet locals for greenlet >= 0.4.0
from greenlet import getcurrent
__all__ = ['local']
def _get_local_dict():
current = getcurrent()
s = '_%s__local_dict__' % current.__class__.__name__
if not hasattr(current, s):
setattr(current, s, {})
return getattr(current, s)
class local(object):
def __getattribute__(self, attr):
local_dict = _get_local_dict()
try:
return local_dict[attr]
except KeyError:
raise AttributeError("'local' object has no attribute '%s'" % attr)
def __setattr__(self, attr, value):
local_dict = _get_local_dict()
local_dict[attr] = value
def __delattr__(self, attr):
local_dict = _get_local_dict()
try:
del local_dict[attr]
except KeyError:
raise AttributeError(attr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment