summaryrefslogtreecommitdiffstats
path: root/Lib/contextlib.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r--Lib/contextlib.py12
1 files changed, 4 insertions, 8 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index fb89118..d3219f6 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -166,20 +166,16 @@ class redirect_stdout:
def __init__(self, new_target):
self._new_target = new_target
- self._old_target = self._sentinel = object()
+ # We use a list of old targets to make this CM re-entrant
+ self._old_targets = []
def __enter__(self):
- if self._old_target is not self._sentinel:
- raise RuntimeError("Cannot reenter {!r}".format(self))
- self._old_target = sys.stdout
+ self._old_targets.append(sys.stdout)
sys.stdout = self._new_target
return self._new_target
def __exit__(self, exctype, excinst, exctb):
- restore_stdout = self._old_target
- self._old_target = self._sentinel
- sys.stdout = restore_stdout
-
+ sys.stdout = self._old_targets.pop()
class suppress: