##// END OF EJS Templates
Make set_term_title() default to no-op, as it can cause problems....
Make set_term_title() default to no-op, as it can cause problems. In embedded contexts this can corrupt stdout (e.g. gedit ipython plugin), by default ipython should be 'safe' to use in all contexts. The user-facing terminal app can activate more aggressive configurations as needed. Added an API call to actually toggle the state, and deprecated the old one (which could only disable but not enable).

File last commit:

r1459:8d08214a
r1852:37edbe78
Show More
fd_redirector.py
81 lines | 2.1 KiB | text/x-python | PythonLexer
Gael Varoquaux
Add OS-level output capture, using file-descriptor redirection.
r1422 # encoding: utf-8
"""
Stdout/stderr redirector, at the OS level, using file descriptors.
gvaroquaux
More docstring work.
r1459
This also works under windows.
Gael Varoquaux
Add OS-level output capture, using file-descriptor redirection.
r1422 """
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
import os
import sys
STDOUT = 1
STDERR = 2
class FDRedirector(object):
""" Class to redirect output (stdout or stderr) at the OS level using
file descriptors.
"""
def __init__(self, fd=STDOUT):
""" fd is the file descriptor of the outpout you want to capture.
It can be STDOUT or STERR.
"""
self.fd = fd
self.started = False
gvaroquaux
More docstring work.
r1459 self.piper = None
self.pipew = None
Gael Varoquaux
Add OS-level output capture, using file-descriptor redirection.
r1422
def start(self):
gvaroquaux
More docstring work.
r1459 """ Setup the redirection.
"""
Gael Varoquaux
Add OS-level output capture, using file-descriptor redirection.
r1422 if not self.started:
self.oldhandle = os.dup(self.fd)
self.piper, self.pipew = os.pipe()
os.dup2(self.pipew, self.fd)
os.close(self.pipew)
self.started = True
def flush(self):
gvaroquaux
More docstring work.
r1459 """ Flush the captured output, similar to the flush method of any
stream.
"""
Gael Varoquaux
Add OS-level output capture, using file-descriptor redirection.
r1422 if self.fd == STDOUT:
sys.stdout.flush()
elif self.fd == STDERR:
sys.stderr.flush()
def stop(self):
gvaroquaux
More docstring work.
r1459 """ Unset the redirection and return the captured output.
"""
Gael Varoquaux
Add OS-level output capture, using file-descriptor redirection.
r1422 if self.started:
self.flush()
os.dup2(self.oldhandle, self.fd)
os.close(self.oldhandle)
f = os.fdopen(self.piper, 'r')
output = f.read()
f.close()
self.started = False
return output
else:
return ''
def getvalue(self):
gvaroquaux
More docstring work.
r1459 """ Return the output captured since the last getvalue, or the
start of the redirection.
"""
Gael Varoquaux
Add OS-level output capture, using file-descriptor redirection.
r1422 output = self.stop()
self.start()
return output