Repository: ipython/ipython

Issue:
SciPy.weave broken in IPython notebook/ qtconsole 
# Problem

Compiling and running of compiled scipy.weave code is not possible with IPython notebook and IPython console. However, it works with standalone, plain IPython.
# Minimal Example

http://projects.scipy.org/scipy/browser/trunk/Lib/weave/examples/array3d.py?rev=1558
# Error
## compiling code

find the traceback here:
http://pastebin.com/FdSadJtV
final error:
AttributeError: 'OutStream' object has no attribute 'fileno'
## executing code

For both notebook and qtconsole, the output is printed in the terminal that was used to start them.
# tested version
## failure in

ipython 0.13.1
scipy  '0.11.0' 
numpy '1.7.0'
## working in
### compilation

Apparently it is working with numpy 1.6.0. I did not test it by myself, but that was a response on IRC. It was there also reproduced using numpy 1.7.0. I could compile it in an virtualenv using numpy 1.6 using the notebook. (Still trying to get the qtconsole to run so that I can test that)
### output

Even with numpy 1.6 the output is directed to the terminal that was used to start the IPython kernel



Discussion:
That awful exec_command should really be ditched, subprocess does the job fine.

Something like:

```
-        if _with_python and sys.stdout.fileno() == -1:
+        if _with_python and (not hasattr(sys.stdout, 'fileno') or sys.stdout.fileno() == -1):
```

(untested) should make things work in the notebook.

Sorry, I basically started last week with IPython and dont know much about the scripts it involves. Where about do I have to look for that line? Or is it in numpy?

It's in numpy, look at the "juliantaylor referenced this issue" link. Here is the [current exec_command.py](https://github.com/numpy/numpy/blob/master/numpy/distutils/exec_command.py).

The overridden iostreams don't have fileno attribute defined, because there is no fd associated with them.
So the question is what is the best way to indicate this:
1. have no `fileno` method (current)
2. `fileno()` returns None
3. `fileno()` returns special 'no FD' integer value (e.g. `-1`)

Is there an official / common behavior for file-like objects that don't have an FD associated with them?  It looks like weave expects returning `-1` to be this indicator - is that a general behavior documented anywhere?

Based on [IOBase](http://docs.python.org/2/library/io.html?highlight=fileno#io.IOBase.fileno), it looks like we should be raising an IOError when `stream.fileno()` is called.

I was going to [link that](http://docs.python.org/dev/library/io.html#io.IOBase.fileno). Also you could create a file descriptor (by making a pipe, or maybe using zeromq).

I don't think we want to create an fd, just express the exceptional state appropriately.

`io.BytesIO` and `io.StringIO` raise `io.UnsupportedOperation` when you try to call `fileno()`. According to the docs, that subclasses IOError and ValueError.

As far as the breakage is concerned, there is a fix: numpy/numpy#3128 and an 1.7.x backport: numpy/numpy#3136 . It handles IOError as it should.

Both fixes merged into numpy.


Resolving patch:
diff --git a/IPython/kernel/zmq/iostream.py b/IPython/kernel/zmq/iostream.py
--- a/IPython/kernel/zmq/iostream.py
+++ b/IPython/kernel/zmq/iostream.py
@@ -8,12 +8,11 @@
 #  the file COPYING, distributed as part of this software.
 #-----------------------------------------------------------------------------
 
-import sys
-import time
 import os
 import threading
+import time
 import uuid
-from io import StringIO
+from io import StringIO, UnsupportedOperation
 
 import zmq
 
@@ -171,6 +170,9 @@ def read(self, size=-1):
 
     def readline(self, size=-1):
         raise IOError('Read not supported on a write only stream.')
+    
+    def fileno(self):
+        raise UnsupportedOperation("IOStream has no fileno.")
 
     def write(self, string):
         if self.pub_socket is None:


Test patch: