abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
redirect Namespace Reference

Functions

def _dup
 
def save_stdout
 
def redirect
 
def start_redirect
 
def end_redirect
 

Variables

tuple null_file = open( os.devnull, "w" )
 

Detailed Description

A simple context manager for redirecting streams in Python. 
The streams are redirected at the the C runtime level so that the output of C extensions
that use stdio will also be redirected.

null_file : a stream representing the null device (e.g. /dev/null on Unix)
redirect: a context manager for redirecting streams

Author: Baruch Sterin (sterin@berkeley.edu)

Function Documentation

def redirect._dup (   f)
private

Definition at line 22 of file redirect.py.

22 
23 def _dup( f ):
24  fd = os.dup( f.fileno() )
25  yield fd
26  os.close(fd)
27 
@contextmanager
def _dup
Definition: redirect.py:22
def redirect.end_redirect (   src,
  fd_dup_src 
)
End redirection of stream src.Redirect the src stream into dst. src is the source stream and fd_dup_src is the value returned by
start_redirect()

Definition at line 97 of file redirect.py.

97 
98 def end_redirect(src, fd_dup_src):
99 
100  """
101  End redirection of stream src.Redirect the src stream into dst. src is the source stream and fd_dup_src is the value returned by
102  start_redirect()
103  """
104 
105  if fd_dup_src is None:
106  return
107 
108  src.flush()
109  os.close( src.fileno() )
110  os.dup2( fd_dup_src, src.fileno() )
111 
112  os.close(fd_dup_src)
def end_redirect
Definition: redirect.py:97
def redirect.redirect (   dst = null_file,
  src = sys.stdout 
)
Redirect the src stream into dst.

Example:
    with redirect( open("somefile.txt", sys.stdout ) ):
        do some stuff ...        

Definition at line 45 of file redirect.py.

45 
46 def redirect(dst = null_file, src = sys.stdout):
47 
48  """
49  Redirect the src stream into dst.
50 
51  Example:
52  with redirect( open("somefile.txt", sys.stdout ) ):
53  do some stuff ...
54  """
55 
56  if src.fileno() == dst.fileno():
57  yield
58  return
59 
60  with _dup( src ) as fd_dup_src:
61 
62  dst.flush()
63 
64  src.flush()
65  os.close( src.fileno() )
66  os.dup2( dst.fileno(), src.fileno() )
67 
68  yield
69 
70  src.flush()
71  os.close( src.fileno() )
72  os.dup2( fd_dup_src, src.fileno() )
def _dup
Definition: redirect.py:22
def redirect
Definition: redirect.py:45
def redirect.save_stdout (   src = sys.stdout)
Redirect 

Definition at line 28 of file redirect.py.

28 
29 def save_stdout( src = sys.stdout ):
30  """
31  Redirect
32  """
33  fd = os.dup( src.fileno() )
34  own = True
35 
36  try:
37  with os.fdopen( fd, "w", 0) as f:
38  own = False
39  yield f
40  except:
41  if own:
42  os.close(fd)
43  raise
44 
@contextmanager
def save_stdout
Definition: redirect.py:28
def redirect.start_redirect (   dst = null_file,
  src = sys.stdout 
)
Start redirection of src stream into dst. Return the duplicated file handle of the source.

Example:
    fd = start_redirect( open("somefile.txt"), sys.stdout )
        ... do some stuff ...        
    end_redirect(sys.stdout, fd)

Definition at line 73 of file redirect.py.

73 
74 def start_redirect(dst = null_file, src = sys.stdout):
75 
76  """
77  Start redirection of src stream into dst. Return the duplicated file handle of the source.
78 
79  Example:
80  fd = start_redirect( open("somefile.txt"), sys.stdout )
81  ... do some stuff ...
82  end_redirect(sys.stdout, fd)
83  """
84 
85  if src.fileno() == dst.fileno():
86  return None
87 
88  fd_dup_src = os.dup( src.fileno() )
89 
90  dst.flush()
91  src.flush()
92 
93  os.close( src.fileno() )
94  os.dup2( dst.fileno(), src.fileno() )
95 
96  return fd_dup_src
def start_redirect
Definition: redirect.py:73

Variable Documentation

tuple redirect.null_file = open( os.devnull, "w" )

Definition at line 19 of file redirect.py.