aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2013-11-03 17:00:51 +1000
committerNick Coghlan <ncoghlan@gmail.com>2013-11-03 17:00:51 +1000
commit8e113b418df7d0c8480e1e2de29a385e1f31b15b (patch)
treed57fd872f36da8ecfc0626358667550f4e768070 /Lib/contextlib.py
parentClose #19439: execute embedding tests on Windows (diff)
downloadcpython-8e113b418df7d0c8480e1e2de29a385e1f31b15b.tar.gz
cpython-8e113b418df7d0c8480e1e2de29a385e1f31b15b.tar.bz2
cpython-8e113b418df7d0c8480e1e2de29a385e1f31b15b.zip
Close #19403: make contextlib.redirect_stdout reentrant
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 fb89118052e..d3219f6c150 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: