[IPython-dev] IPython 0.9.1 and sys.stdin

Nicolas Rougier Nicolas.Rougier at loria.fr
Wed Apr 8 02:13:36 EDT 2009



Thanks, I found a work-around by first redirecting stdin and then import
IPython but I did not find where exactly IPython messes with stdin.

Nicolas


On Tue, 2009-04-07 at 16:46 -0700, Fernando Perez wrote:
> On Mon, Apr 6, 2009 at 7:10 AM, Nicolas Rougier
> <Nicolas.Rougier at loria.fr> wrote:
> >
> > Hi,
> >
> > I've question regarding stdin ipython handling. If I try to run
> > interactively the following script:
> >
> > import sys
> > #import IPython
> >
> > class TerminalIn:
> >     def __init__(self):  pass
> >     def close(self):     pass
> >     flush = close
> >     def isatty(self):    return True
> >     def read(self, a):   return self.readline()
> >     def readline(self):  return '"q"'
> >     def readlines(self): return []
> >     def write(self, s):  return None
> >     def writelines(self, l): return None
> >
> > sys.stdin = TerminalIn()
> >
> > and then type 'help()' in the console, depending on the IPython
> > import, I get different results. If I do not include IPython, the help
> > exists immediately (as expected) while if I import IPython, the help
> > does not exit immediately, making me think that IPython is changing
> > something without any kind of warning.
> 
> Mmh, I'm not quite finding right now exactly what it is that may be
> changing.  For one thing, ipython does load help() by itself in
> ipmaker, but this is just the same _Helper object that plain python
> uses:
> 
>     # Put 'help' in the user namespace
>     try:
>         from site import _Helper
>         IP.user_ns['help'] = _Helper()
>     except ImportError:
>         warn('help() not available - check site.py')
> 
> 
> You may also try to manually make changes in genutils.py, where the
> system-wide Term object is created:
> 
> class IOTerm:
>     """ Term holds the file or file-like objects for handling I/O operations.
> 
>     These are normally just sys.stdin, sys.stdout and sys.stderr but for
>     Windows they can can replaced to allow editing the strings before they are
>     displayed."""
> 
>     # In the future, having IPython channel all its I/O operations through
>     # 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,cin=None,cout=None,cerr=None):
>         self.cin  = IOStream(cin,sys.stdin)
>         self.cout = IOStream(cout,sys.stdout)
>         self.cerr = IOStream(cerr,sys.stderr)
> 
> # Global variable to be used for all I/O
> Term = IOTerm()
> 
> 
> In principle this is just a wrapper, but it might be causing some side
> effects...
> 
> Just some starting pointers, not a full solution I'm afraid.
> 
> Cheers,
> 
> f




More information about the IPython-dev mailing list