Created
November 26, 2012 22:43
-
-
Save saghul/4151154 to your computer and use it in GitHub Desktop.
greenlet locals for greenlet >= 0.4.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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