Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42bd99a

Browse files
anntzertacaswell
authored andcommitted
ENH: backport cbook._setattr_cm
This context manager was added to master in 690b213 via matplotlib#10314. We do not want to backport that entire commit, however the backport of matplotlib#11407 requires this context manger. It is private and self contained to low-risk to backport
1 parent 648a04e commit 42bd99a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,3 +2832,21 @@ def _str_lower_equal(obj, s):
28322832
cannot be used in a boolean context.
28332833
"""
28342834
return isinstance(obj, six.string_types) and obj.lower() == s
2835+
2836+
2837+
@contextlib.contextmanager
2838+
def _setattr_cm(obj, **kwargs):
2839+
"""Temporarily set some attributes; restore original state at context exit.
2840+
"""
2841+
sentinel = object()
2842+
origs = [(attr, getattr(obj, attr, sentinel)) for attr in kwargs]
2843+
try:
2844+
for attr, val in kwargs.items():
2845+
setattr(obj, attr, val)
2846+
yield
2847+
finally:
2848+
for attr, orig in origs:
2849+
if orig is sentinel:
2850+
delattr(obj, attr)
2851+
else:
2852+
setattr(obj, attr, orig)

0 commit comments

Comments
 (0)