Repository: minrk/wurlitzer

Issue:
pipes are not redirected back in 3.1.0
```python
import wurlitzer
with open("log", "w") as f, wurlitzer.pipes(f):
        print("foo")
print("bar")
```

In 3.0.3, it will print `bar`. But in 3.1.0 it will print nothing and `bar` is also in `log`. Tested on MacOS and Linux.

Discussion:

Patch:
diff --git a/README.md b/README.md
index 2780dde..e365664 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@ with pipes() as (out, err):
 stdout = out.read()
 ```
 
-Capture both stdout and stderr in a single StringIO object:
+Capture stdout/stderr in StringIO:
 
 ```python
 from io import StringIO
diff --git a/wurlitzer.py b/wurlitzer.py
index 39089a7..75efe68 100644
--- a/wurlitzer.py
+++ b/wurlitzer.py
@@ -209,6 +209,7 @@ class Wurlitzer:
         real_fd = getattr(sys, '__%s__' % name).fileno()
         save_fd = os.dup(real_fd)
         self._save_fds[name] = save_fd
+        self._real_fds[name] = real_fd
 
         try:
             capture_fd = getattr(self, "_" + name).fileno()
@@ -233,7 +234,6 @@ class Wurlitzer:
 
         dup2(pipe_in, real_fd)
         os.close(pipe_in)
-        self._real_fds[name] = real_fd
 
         # make pipe_out non-blocking
         flags = fcntl(pipe_out, F_GETFL)


Test patch:
diff --git a/test.py b/test.py
index 78bdada..366f313 100644
--- a/test.py
+++ b/test.py
@@ -229,6 +229,9 @@ def test_two_file_pipes(tmpdir):
             assert w.thread is None
             printf("some stdout")
             printf_err("some stderr")
+        # make sure capture stopped
+        printf("after stdout")
+        printf_err("after stderr")
 
     with test_stdout.open() as f:
         assert f.read() == "some stdout\n"