# Calculate format=diff
index cace43b..ab2acaf 100644
--- /usr/lib/python2.7/site-packages/pexpect/__init__.py
+++ /usr/lib/python2.7/site-packages/pexpect/__init__.py
@@ -490,10 +490,13 @@ class spawn(object):
         # inherit EOF and INTR definitions from controlling process.
         try:
             from termios import VEOF, VINTR
-            fd = sys.__stdin__.fileno()
+            try:
+                fd = sys.__stdin__.fileno()
+            except ValueError:
+                fd = sys.__stdout__.fileno()
             self._INTR = ord(termios.tcgetattr(fd)[6][VINTR])
             self._EOF = ord(termios.tcgetattr(fd)[6][VEOF])
-        except (ImportError, OSError, IOError, termios.error):
+        except (ImportError, OSError, ValueError, IOError, termios.error):
             # unless the controlling process is also not a terminal,
             # such as cron(1). Fall-back to using CEOF and CINTR.
             try:
