summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2010-10-20 18:30:59 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2010-10-20 18:30:59 +0200
commit0285a5cf5fad710dc5cd17a332662ac09e0e7861 (patch)
treed8ea06fc7b8d9a5f11f96d11382fe1787cf10ddb
parentee67b24ad9baaf38ef03c8e6cd6d5351e5d8e1cd (diff)
downloadlogilab-common-0285a5cf5fad710dc5cd17a332662ac09e0e7861.tar.gz
new tempattr context manager
-rw-r--r--__init__.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/__init__.py b/__init__.py
index 558868e..d9c5e64 100644
--- a/__init__.py
+++ b/__init__.py
@@ -60,6 +60,22 @@ class nullobject(object):
def __nonzero__(self):
return False
+class tempattr(object):
+ def __init__(self, obj, attr, value):
+ self.obj = obj
+ self.attr = attr
+ self.value = value
+
+ def __enter__(self):
+ self.oldvalue = getattr(self.obj, self.attr)
+ setattr(self.obj, self.attr, self.value)
+ return self.obj
+
+ def __exit__(self, exctype, value, traceback):
+ setattr(self.obj, self.attr, self.oldvalue)
+
+
+
# flatten -----
# XXX move in a specific module and use yield instead
# do not mix flatten and translate