Show More
@@ -2,6 +2,8 b'' | |||||
2 |
|
2 | |||
3 | """ |
|
3 | """ | |
4 | Stdout/stderr redirector, at the OS level, using file descriptors. |
|
4 | Stdout/stderr redirector, at the OS level, using file descriptors. | |
|
5 | ||||
|
6 | This also works under windows. | |||
5 | """ |
|
7 | """ | |
6 |
|
8 | |||
7 | __docformat__ = "restructuredtext en" |
|
9 | __docformat__ = "restructuredtext en" | |
@@ -31,8 +33,12 b' class FDRedirector(object):' | |||||
31 | """ |
|
33 | """ | |
32 | self.fd = fd |
|
34 | self.fd = fd | |
33 | self.started = False |
|
35 | self.started = False | |
|
36 | self.piper = None | |||
|
37 | self.pipew = None | |||
34 |
|
38 | |||
35 | def start(self): |
|
39 | def start(self): | |
|
40 | """ Setup the redirection. | |||
|
41 | """ | |||
36 | if not self.started: |
|
42 | if not self.started: | |
37 | self.oldhandle = os.dup(self.fd) |
|
43 | self.oldhandle = os.dup(self.fd) | |
38 | self.piper, self.pipew = os.pipe() |
|
44 | self.piper, self.pipew = os.pipe() | |
@@ -42,12 +48,17 b' class FDRedirector(object):' | |||||
42 | self.started = True |
|
48 | self.started = True | |
43 |
|
49 | |||
44 | def flush(self): |
|
50 | def flush(self): | |
|
51 | """ Flush the captured output, similar to the flush method of any | |||
|
52 | stream. | |||
|
53 | """ | |||
45 | if self.fd == STDOUT: |
|
54 | if self.fd == STDOUT: | |
46 | sys.stdout.flush() |
|
55 | sys.stdout.flush() | |
47 | elif self.fd == STDERR: |
|
56 | elif self.fd == STDERR: | |
48 | sys.stderr.flush() |
|
57 | sys.stderr.flush() | |
49 |
|
58 | |||
50 | def stop(self): |
|
59 | def stop(self): | |
|
60 | """ Unset the redirection and return the captured output. | |||
|
61 | """ | |||
51 | if self.started: |
|
62 | if self.started: | |
52 | self.flush() |
|
63 | self.flush() | |
53 | os.dup2(self.oldhandle, self.fd) |
|
64 | os.dup2(self.oldhandle, self.fd) | |
@@ -62,6 +73,9 b' class FDRedirector(object):' | |||||
62 | return '' |
|
73 | return '' | |
63 |
|
74 | |||
64 | def getvalue(self): |
|
75 | def getvalue(self): | |
|
76 | """ Return the output captured since the last getvalue, or the | |||
|
77 | start of the redirection. | |||
|
78 | """ | |||
65 | output = self.stop() |
|
79 | output = self.stop() | |
66 | self.start() |
|
80 | self.start() | |
67 | return output |
|
81 | return output |
@@ -25,25 +25,42 b' class FileLike(object):' | |||||
25 | self.write = write_callback |
|
25 | self.write = write_callback | |
26 |
|
26 | |||
27 | def flush(self): |
|
27 | def flush(self): | |
|
28 | """ This method is there for compatibility with other file-like | |||
|
29 | objects. | |||
|
30 | """ | |||
28 | pass |
|
31 | pass | |
29 |
|
32 | |||
30 | def close(self): |
|
33 | def close(self): | |
|
34 | """ This method is there for compatibility with other file-like | |||
|
35 | objects. | |||
|
36 | """ | |||
31 | pass |
|
37 | pass | |
32 |
|
38 | |||
33 | def writelines(self, lines): |
|
39 | def writelines(self, lines): | |
34 |
|
|
40 | map(self.write, lines) | |
35 | self.write(line) |
|
|||
36 |
|
41 | |||
37 | def isatty(self): |
|
42 | def isatty(self): | |
|
43 | """ This method is there for compatibility with other file-like | |||
|
44 | objects. | |||
|
45 | """ | |||
38 | return False |
|
46 | return False | |
39 |
|
47 | |||
40 | def getvalue(self): |
|
48 | def getvalue(self): | |
|
49 | """ This method is there for compatibility with other file-like | |||
|
50 | objects. | |||
|
51 | """ | |||
41 | return '' |
|
52 | return '' | |
42 |
|
53 | |||
43 | def reset(self): |
|
54 | def reset(self): | |
|
55 | """ This method is there for compatibility with other file-like | |||
|
56 | objects. | |||
|
57 | """ | |||
44 | pass |
|
58 | pass | |
45 |
|
59 | |||
46 | def truncate(self): |
|
60 | def truncate(self): | |
|
61 | """ This method is there for compatibility with other file-like | |||
|
62 | objects. | |||
|
63 | """ | |||
47 | pass |
|
64 | pass | |
48 |
|
65 | |||
49 |
|
66 |
@@ -31,6 +31,10 b' class RedirectorOutputTrap(OutputTrap):' | |||||
31 | # OutputTrap interface. |
|
31 | # OutputTrap interface. | |
32 | #------------------------------------------------------------------------ |
|
32 | #------------------------------------------------------------------------ | |
33 | def __init__(self, out_callback, err_callback): |
|
33 | def __init__(self, out_callback, err_callback): | |
|
34 | """ | |||
|
35 | out_callback : callable called when there is output in the stdout | |||
|
36 | err_callback : callable called when there is output in the stderr | |||
|
37 | """ | |||
34 | # Callback invoked on write to stdout and stderr |
|
38 | # Callback invoked on write to stdout and stderr | |
35 | self.out_callback = out_callback |
|
39 | self.out_callback = out_callback | |
36 | self.err_callback = err_callback |
|
40 | self.err_callback = err_callback |
@@ -21,9 +21,17 b' from traceback_trap import TracebackTrap' | |||||
21 | from IPython.ultraTB import ColorTB |
|
21 | from IPython.ultraTB import ColorTB | |
22 |
|
22 | |||
23 | class SyncTracebackTrap(TracebackTrap): |
|
23 | class SyncTracebackTrap(TracebackTrap): | |
|
24 | """ TracebackTrap that displays immediatly the traceback in addition | |||
|
25 | to capturing it. Useful in frontends, as without this traceback trap, | |||
|
26 | some tracebacks never get displayed. | |||
|
27 | """ | |||
24 |
|
28 | |||
25 | def __init__(self, sync_formatter=None, formatters=None, |
|
29 | def __init__(self, sync_formatter=None, formatters=None, | |
26 | raiseException=True): |
|
30 | raiseException=True): | |
|
31 | """ | |||
|
32 | sync_formatter: Callable to display the traceback. | |||
|
33 | formatters: A list of formatters to apply. | |||
|
34 | """ | |||
27 | TracebackTrap.__init__(self, formatters=formatters) |
|
35 | TracebackTrap.__init__(self, formatters=formatters) | |
28 | if sync_formatter is None: |
|
36 | if sync_formatter is None: | |
29 | sync_formatter = ColorTB(color_scheme='LightBG') |
|
37 | sync_formatter = ColorTB(color_scheme='LightBG') |
General Comments 0
You need to be logged in to leave comments.
Login now