Repository: xonsh/xonsh

Issue:
timeit - OSError Inappropriate ioctl for device
A new introduced issue.

```
$ timeit ls
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 12437, in run
    r = self.f(self.args, sp_stdin, sp_stdout, sp_stderr, spec)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 12266, in proxy_two
    return f(args, stdin)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 13794, in timeit_alias
    time_number = timer.timeit(number)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 13747, in timeit
    timing = self.inner(it, self.timer)
  File "<xonsh-timeit>", line 6, in inner
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 18340, in subproc_captured_hiddenobject
    return run_subproc(cmds, captured='hiddenobject')
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 18281, in run_subproc
    command = HiddenCommandPipeline(specs)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 12859, in __init__
    if update_fg_process_group(pipeline_group, background):
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 12795, in update_fg_process_group
    return give_terminal_to(pipeline_group)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 8901, in give_terminal_to
    os.tcsetpgrp(sys.stderr.fileno(), pgid)
OSError: [Errno 25] Inappropriate ioctl for device
```


Discussion:
The fix is probably to replace `sys.stderr.fileno()` with `2`
Thanks for reporting @mitnk 

Resolving patch:
diff --git a/xonsh/jobs.py b/xonsh/jobs.py
--- a/xonsh/jobs.py
+++ b/xonsh/jobs.py
@@ -10,7 +10,7 @@
 import collections
 
 from xonsh.lazyasd import LazyObject
-from xonsh.platform import ON_DARWIN, ON_WINDOWS, ON_CYGWIN, LIBC
+from xonsh.platform import FD_STDERR, ON_DARWIN, ON_WINDOWS, ON_CYGWIN, LIBC
 from xonsh.tools import unthreadable
 
 
@@ -127,8 +127,7 @@ def give_terminal_to(pgid):
             LIBC.sigprocmask(ctypes.c_int(signal.SIG_BLOCK),
                              ctypes.byref(mask),
                              ctypes.byref(omask))
-            st = sys.stderr.fileno()
-            LIBC.tcsetpgrp(ctypes.c_int(st), ctypes.c_int(pgid))
+            LIBC.tcsetpgrp(ctypes.c_int(FD_STDERR), ctypes.c_int(pgid))
             LIBC.sigprocmask(ctypes.c_int(signal.SIG_SETMASK),
                              ctypes.byref(omask), None)
             return True
@@ -136,7 +135,7 @@ def give_terminal_to(pgid):
         def give_terminal_to(pgid):
             oldmask = signal.pthread_sigmask(signal.SIG_BLOCK,
                                              _block_when_giving)
-            os.tcsetpgrp(sys.stderr.fileno(), pgid)
+            os.tcsetpgrp(FD_STDERR, pgid)
             signal.pthread_sigmask(signal.SIG_SETMASK, oldmask)
             return True
 
diff --git a/xonsh/platform.py b/xonsh/platform.py
--- a/xonsh/platform.py
+++ b/xonsh/platform.py
@@ -14,6 +14,11 @@
 import importlib.util
 
 from xonsh.lazyasd import LazyBool, lazyobject, lazybool
+# do not import any xonsh-modules here to avoid circular dependencies
+
+FD_STDIN = 0
+FD_STDOUT = 1
+FD_STDERR = 2
 
 
 @lazyobject
@@ -27,9 +32,6 @@ def distro():
     return d
 
 
-# do not import any xonsh-modules here to avoid circular dependencies
-
-
 #
 # OS
 #
@@ -176,6 +178,7 @@ def pathbasename(p):
     """
     return pathsplit(p)[-1]
 
+
 # termios tc(get|set)attr indexes.
 IFLAG = 0
 OFLAG = 1


Test patch: