Permalink
Browse files

ensure a fallback exists, so use local std{in,out,err}

Since IOStream instances require a valid fallback stream, use the locally defined std{in,out,err}
instead of sys.std{in,out,err} in IOTerm's __init__ method.  Note that the local std{in,out,err}
are IOStream instances as well, that fall back to os.devnull
  • Loading branch information...
1 parent 40ec4c3 commit 183d9879955640c9b19ce74f1a1e5e9afecd16a1 @gbrandonp committed Jan 14, 2012
Showing with 9 additions and 8 deletions.
  1. +9 −8 IPython/utils/io.py
View
@@ -70,6 +70,11 @@ def closed(self):
def close(self):
pass
+# setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
+devnull = open(os.devnull, 'a')
+stdin = IOStream(sys.stdin, fallback=devnull)
+stdout = IOStream(sys.stdout, fallback=devnull)
+stderr = IOStream(sys.stderr, fallback=devnull)
class IOTerm:
""" Term holds the file or file-like objects for handling I/O operations.
@@ -82,14 +87,10 @@ class IOTerm:
# this class will make it easier to embed it into other environments which
# are not a normal terminal (such as a GUI-based shell)
def __init__(self, stdin=None, stdout=None, stderr=None):
- self.stdin = IOStream(stdin, sys.stdin)
- self.stdout = IOStream(stdout, sys.stdout)
- self.stderr = IOStream(stderr, sys.stderr)
-
-# setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
-stdin = sys.stdin if not sys.stdin else IOStream(sys.stdin)
-stdout = sys.stdout if not sys.stdout else IOStream(sys.stdout)
-stderr = sys.stderr if not sys.stderr else IOStream(sys.stderr)
+ mymodule = sys.modules[__name__]
+ self.stdin = IOStream(stdin, mymodule.stdin)
+ self.stdout = IOStream(stdout, mymodule.stdout)
+ self.stderr = IOStream(stderr, mymodule.stderr)
class Tee(object):

0 comments on commit 183d987

Please sign in to comment.