Show More
@@ -32,7 +32,7 b' import sys' | |||||
32 | from IPython.utils import PyColorize |
|
32 | from IPython.utils import PyColorize | |
33 | from IPython.core import ipapi |
|
33 | from IPython.core import ipapi | |
34 | from IPython.utils import coloransi |
|
34 | from IPython.utils import coloransi | |
35 |
|
|
35 | import IPython.utils.io | |
36 | from IPython.core.excolors import exception_colors |
|
36 | from IPython.core.excolors import exception_colors | |
37 |
|
37 | |||
38 | # See if we can use pydb. |
|
38 | # See if we can use pydb. | |
@@ -171,7 +171,7 b' class Pdb(OldPdb):' | |||||
171 |
|
171 | |||
172 | # Parent constructor: |
|
172 | # Parent constructor: | |
173 | if has_pydb and completekey is None: |
|
173 | if has_pydb and completekey is None: | |
174 | OldPdb.__init__(self,stdin=stdin,stdout=Term.cout) |
|
174 | OldPdb.__init__(self,stdin=stdin,stdout=IPython.utils.io.Term.cout) | |
175 | else: |
|
175 | else: | |
176 | OldPdb.__init__(self,completekey,stdin,stdout) |
|
176 | OldPdb.__init__(self,completekey,stdin,stdout) | |
177 |
|
177 | |||
@@ -279,7 +279,7 b' class Pdb(OldPdb):' | |||||
279 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', |
|
279 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', | |
280 | context = 3): |
|
280 | context = 3): | |
281 | #frame, lineno = frame_lineno |
|
281 | #frame, lineno = frame_lineno | |
282 | print >>Term.cout, self.format_stack_entry(frame_lineno, '', context) |
|
282 | print >>IPython.utils.io.Term.cout, self.format_stack_entry(frame_lineno, '', context) | |
283 |
|
283 | |||
284 | # vds: >> |
|
284 | # vds: >> | |
285 | frame, lineno = frame_lineno |
|
285 | frame, lineno = frame_lineno | |
@@ -419,7 +419,7 b' class Pdb(OldPdb):' | |||||
419 | src.append(line) |
|
419 | src.append(line) | |
420 | self.lineno = lineno |
|
420 | self.lineno = lineno | |
421 |
|
421 | |||
422 | print >>Term.cout, ''.join(src) |
|
422 | print >>IPython.utils.io.Term.cout, ''.join(src) | |
423 |
|
423 | |||
424 | except KeyboardInterrupt: |
|
424 | except KeyboardInterrupt: | |
425 | pass |
|
425 | pass |
@@ -5,7 +5,8 b'' | |||||
5 | import fnmatch |
|
5 | import fnmatch | |
6 | import os |
|
6 | import os | |
7 |
|
7 | |||
8 | from IPython.utils.io import Term, ask_yes_no |
|
8 | import IPython.utils.io | |
|
9 | from IPython.utils.io import ask_yes_no | |||
9 | from IPython.utils.warn import warn |
|
10 | from IPython.utils.warn import warn | |
10 | from IPython.core import ipapi |
|
11 | from IPython.core import ipapi | |
11 |
|
12 | |||
@@ -62,7 +63,7 b" def magic_history(self, parameter_s = ''):" | |||||
62 | try: |
|
63 | try: | |
63 | outfname = opts['f'] |
|
64 | outfname = opts['f'] | |
64 | except KeyError: |
|
65 | except KeyError: | |
65 | outfile = Term.cout # default |
|
66 | outfile = IPython.utils.io.Term.cout # default | |
66 | # We don't want to close stdout at the end! |
|
67 | # We don't want to close stdout at the end! | |
67 | close_at_end = False |
|
68 | close_at_end = False | |
68 | else: |
|
69 | else: | |
@@ -101,7 +102,7 b" def magic_history(self, parameter_s = ''):" | |||||
101 | init, final = map(int, args) |
|
102 | init, final = map(int, args) | |
102 | else: |
|
103 | else: | |
103 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') |
|
104 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') | |
104 | print >> Term.cout, self.magic_hist.__doc__ |
|
105 | print >> IPython.utils.io.Term.cout, self.magic_hist.__doc__ | |
105 | return |
|
106 | return | |
106 |
|
107 | |||
107 | width = len(str(final)) |
|
108 | width = len(str(final)) |
@@ -46,7 +46,7 b' import sys' | |||||
46 |
|
46 | |||
47 | from pprint import PrettyPrinter |
|
47 | from pprint import PrettyPrinter | |
48 |
|
48 | |||
49 |
|
|
49 | import IPython.utils.io | |
50 | from IPython.utils.process import shell |
|
50 | from IPython.utils.process import shell | |
51 |
|
51 | |||
52 | from IPython.core.error import TryNext |
|
52 | from IPython.core.error import TryNext | |
@@ -175,13 +175,13 b' def result_display(self,arg):' | |||||
175 | # So that multi-line strings line up with the left column of |
|
175 | # So that multi-line strings line up with the left column of | |
176 | # the screen, instead of having the output prompt mess up |
|
176 | # the screen, instead of having the output prompt mess up | |
177 | # their first line. |
|
177 | # their first line. | |
178 | Term.cout.write('\n') |
|
178 | IPython.utils.io.Term.cout.write('\n') | |
179 | print >>Term.cout, out |
|
179 | print >>IPython.utils.io.Term.cout, out | |
180 | else: |
|
180 | else: | |
181 | # By default, the interactive prompt uses repr() to display results, |
|
181 | # By default, the interactive prompt uses repr() to display results, | |
182 | # so we should honor this. Users who'd rather use a different |
|
182 | # so we should honor this. Users who'd rather use a different | |
183 | # mechanism can easily override this hook. |
|
183 | # mechanism can easily override this hook. | |
184 | print >>Term.cout, repr(arg) |
|
184 | print >>IPython.utils.io.Term.cout, repr(arg) | |
185 | # the default display hook doesn't manipulate the value to put in history |
|
185 | # the default display hook doesn't manipulate the value to put in history | |
186 | return None |
|
186 | return None | |
187 |
|
187 |
@@ -53,7 +53,8 b' from IPython.utils import PyColorize' | |||||
53 | from IPython.utils import pickleshare |
|
53 | from IPython.utils import pickleshare | |
54 | from IPython.utils.doctestreload import doctest_reload |
|
54 | from IPython.utils.doctestreload import doctest_reload | |
55 | from IPython.utils.ipstruct import Struct |
|
55 | from IPython.utils.ipstruct import Struct | |
56 | from IPython.utils.io import Term, ask_yes_no |
|
56 | import IPython.utils.io | |
|
57 | from IPython.utils.io import ask_yes_no | |||
57 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError |
|
58 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError | |
58 | from IPython.utils.process import getoutput, getoutputerror |
|
59 | from IPython.utils.process import getoutput, getoutputerror | |
59 | from IPython.utils.strdispatch import StrDispatch |
|
60 | from IPython.utils.strdispatch import StrDispatch | |
@@ -250,6 +251,10 b' class InteractiveShell(Configurable, Magic):' | |||||
250 | self.init_syntax_highlighting() |
|
251 | self.init_syntax_highlighting() | |
251 | self.init_hooks() |
|
252 | self.init_hooks() | |
252 | self.init_pushd_popd_magic() |
|
253 | self.init_pushd_popd_magic() | |
|
254 | # TODO: init_io() needs to happen before init_traceback handlers | |||
|
255 | # because the traceback handlers hardcode the stdout/stderr streams. | |||
|
256 | # This logic in in debugger.Pdb and should eventually be changed. | |||
|
257 | self.init_io() | |||
253 | self.init_traceback_handlers(custom_exceptions) |
|
258 | self.init_traceback_handlers(custom_exceptions) | |
254 | self.init_user_ns() |
|
259 | self.init_user_ns() | |
255 | self.init_logger() |
|
260 | self.init_logger() | |
@@ -411,6 +416,17 b' class InteractiveShell(Configurable, Magic):' | |||||
411 | 'NoColor', |
|
416 | 'NoColor', | |
412 | self.object_info_string_level) |
|
417 | self.object_info_string_level) | |
413 |
|
418 | |||
|
419 | def init_io(self): | |||
|
420 | import IPython.utils.io | |||
|
421 | if sys.platform == 'win32' and readline.have_readline and \ | |||
|
422 | self.readline_use: | |||
|
423 | Term = IPython.utils.io.IOTerm( | |||
|
424 | cout=readline._outputfile,cerr=readline._outputfile | |||
|
425 | ) | |||
|
426 | else: | |||
|
427 | Term = IPython.utils.io.IOTerm() | |||
|
428 | IPython.utils.io.Term = Term | |||
|
429 | ||||
414 | def init_prompts(self): |
|
430 | def init_prompts(self): | |
415 | # Initialize cache, set in/out prompts and printing system |
|
431 | # Initialize cache, set in/out prompts and printing system | |
416 | self.outputcache = CachedOutput(self, |
|
432 | self.outputcache = CachedOutput(self, | |
@@ -1997,12 +2013,12 b' class InteractiveShell(Configurable, Magic):' | |||||
1997 | # TODO: This should be removed when Term is refactored. |
|
2013 | # TODO: This should be removed when Term is refactored. | |
1998 | def write(self,data): |
|
2014 | def write(self,data): | |
1999 | """Write a string to the default output""" |
|
2015 | """Write a string to the default output""" | |
2000 | Term.cout.write(data) |
|
2016 | IPython.utils.io.Term.cout.write(data) | |
2001 |
|
2017 | |||
2002 | # TODO: This should be removed when Term is refactored. |
|
2018 | # TODO: This should be removed when Term is refactored. | |
2003 | def write_err(self,data): |
|
2019 | def write_err(self,data): | |
2004 | """Write a string to the default error output""" |
|
2020 | """Write a string to the default error output""" | |
2005 | Term.cerr.write(data) |
|
2021 | IPython.utils.io.Term.cerr.write(data) | |
2006 |
|
2022 | |||
2007 | def ask_yes_no(self,prompt,default=True): |
|
2023 | def ask_yes_no(self,prompt,default=True): | |
2008 | if self.quiet: |
|
2024 | if self.quiet: |
@@ -7,7 +7,7 b'' | |||||
7 | # the file COPYING, distributed as part of this software. |
|
7 | # the file COPYING, distributed as part of this software. | |
8 | #***************************************************************************** |
|
8 | #***************************************************************************** | |
9 |
|
9 | |||
10 |
|
|
10 | import IPython.utils.io | |
11 | from IPython.core.autocall import IPyAutocall |
|
11 | from IPython.core.autocall import IPyAutocall | |
12 |
|
12 | |||
13 | class Macro(IPyAutocall): |
|
13 | class Macro(IPyAutocall): | |
@@ -32,7 +32,7 b' class Macro(IPyAutocall):' | |||||
32 | return 'IPython.macro.Macro(%s)' % repr(self.value) |
|
32 | return 'IPython.macro.Macro(%s)' % repr(self.value) | |
33 |
|
33 | |||
34 | def __call__(self,*args): |
|
34 | def __call__(self,*args): | |
35 | Term.cout.flush() |
|
35 | IPython.utils.io.Term.cout.flush() | |
36 | self._ip.user_ns['_margv'] = args |
|
36 | self._ip.user_ns['_margv'] = args | |
37 | self._ip.runlines(self.value) |
|
37 | self._ip.runlines(self.value) | |
38 |
|
38 |
@@ -58,7 +58,8 b' from IPython.lib.pylabtools import mpl_runner' | |||||
58 | from IPython.lib.inputhook import enable_gui |
|
58 | from IPython.lib.inputhook import enable_gui | |
59 | from IPython.external.Itpl import itpl, printpl |
|
59 | from IPython.external.Itpl import itpl, printpl | |
60 | from IPython.testing import decorators as testdec |
|
60 | from IPython.testing import decorators as testdec | |
61 |
from IPython.utils.io import |
|
61 | from IPython.utils.io import file_read, nlprint | |
|
62 | import IPython.utils.io | |||
62 | from IPython.utils.path import get_py_filename |
|
63 | from IPython.utils.path import get_py_filename | |
63 | from IPython.utils.process import arg_split, abbrev_cwd |
|
64 | from IPython.utils.process import arg_split, abbrev_cwd | |
64 | from IPython.utils.terminal import set_term_title |
|
65 | from IPython.utils.terminal import set_term_title | |
@@ -3093,7 +3094,7 b' Defaulting color scheme to \'NoColor\'"""' | |||||
3093 | # If all looks ok, proceed |
|
3094 | # If all looks ok, proceed | |
3094 | out,err = self.shell.getoutputerror(cmd) |
|
3095 | out,err = self.shell.getoutputerror(cmd) | |
3095 | if err: |
|
3096 | if err: | |
3096 | print >> Term.cerr,err |
|
3097 | print >> IPython.utils.io.Term.cerr, err | |
3097 | if opts.has_key('l'): |
|
3098 | if opts.has_key('l'): | |
3098 | out = SList(out.split('\n')) |
|
3099 | out = SList(out.split('\n')) | |
3099 | else: |
|
3100 | else: | |
@@ -3143,7 +3144,7 b' Defaulting color scheme to \'NoColor\'"""' | |||||
3143 | if parameter_s: |
|
3144 | if parameter_s: | |
3144 | out,err = self.shell.getoutputerror(parameter_s) |
|
3145 | out,err = self.shell.getoutputerror(parameter_s) | |
3145 | if err: |
|
3146 | if err: | |
3146 | print >> Term.cerr,err |
|
3147 | print >> IPython.utils.io.Term.cerr, err | |
3147 | return SList(out.split('\n')) |
|
3148 | return SList(out.split('\n')) | |
3148 |
|
3149 | |||
3149 | def magic_r(self, parameter_s=''): |
|
3150 | def magic_r(self, parameter_s=''): |
@@ -30,7 +30,7 b' import types' | |||||
30 | from IPython.core.page import page |
|
30 | from IPython.core.page import page | |
31 | from IPython.external.Itpl import itpl |
|
31 | from IPython.external.Itpl import itpl | |
32 | from IPython.utils import PyColorize |
|
32 | from IPython.utils import PyColorize | |
33 |
|
|
33 | import IPython.utils.io | |
34 | from IPython.utils.text import indent |
|
34 | from IPython.utils.text import indent | |
35 | from IPython.utils.wildcard import list_namespace |
|
35 | from IPython.utils.wildcard import list_namespace | |
36 | from IPython.utils.coloransi import * |
|
36 | from IPython.utils.coloransi import * | |
@@ -249,7 +249,7 b' class Inspector:' | |||||
249 | if output is None: |
|
249 | if output is None: | |
250 | self.noinfo('definition header',oname) |
|
250 | self.noinfo('definition header',oname) | |
251 | else: |
|
251 | else: | |
252 | print >>Term.cout, header,self.format(output), |
|
252 | print >>IPython.utils.io.Term.cout, header,self.format(output), | |
253 |
|
253 | |||
254 | def pdoc(self,obj,oname='',formatter = None): |
|
254 | def pdoc(self,obj,oname='',formatter = None): | |
255 | """Print the docstring for any object. |
|
255 | """Print the docstring for any object. |
@@ -36,7 +36,7 b' from IPython.core import ipapi' | |||||
36 | from IPython.core.error import TryNext |
|
36 | from IPython.core.error import TryNext | |
37 | from IPython.utils.cursesimport import use_curses |
|
37 | from IPython.utils.cursesimport import use_curses | |
38 | from IPython.utils.data import chop |
|
38 | from IPython.utils.data import chop | |
39 |
|
|
39 | import IPython.utils.io | |
40 | from IPython.utils.process import xsys |
|
40 | from IPython.utils.process import xsys | |
41 | from IPython.utils.terminal import get_terminal_size |
|
41 | from IPython.utils.terminal import get_terminal_size | |
42 |
|
42 | |||
@@ -56,18 +56,18 b' def page_dumb(strng, start=0, screen_lines=25):' | |||||
56 | out_ln = strng.splitlines()[start:] |
|
56 | out_ln = strng.splitlines()[start:] | |
57 | screens = chop(out_ln,screen_lines-1) |
|
57 | screens = chop(out_ln,screen_lines-1) | |
58 | if len(screens) == 1: |
|
58 | if len(screens) == 1: | |
59 | print >>Term.cout, os.linesep.join(screens[0]) |
|
59 | print >>IPython.utils.io.Term.cout, os.linesep.join(screens[0]) | |
60 | else: |
|
60 | else: | |
61 | last_escape = "" |
|
61 | last_escape = "" | |
62 | for scr in screens[0:-1]: |
|
62 | for scr in screens[0:-1]: | |
63 | hunk = os.linesep.join(scr) |
|
63 | hunk = os.linesep.join(scr) | |
64 | print >>Term.cout, last_escape + hunk |
|
64 | print >>IPython.utils.io.Term.cout, last_escape + hunk | |
65 | if not page_more(): |
|
65 | if not page_more(): | |
66 | return |
|
66 | return | |
67 | esc_list = esc_re.findall(hunk) |
|
67 | esc_list = esc_re.findall(hunk) | |
68 | if len(esc_list) > 0: |
|
68 | if len(esc_list) > 0: | |
69 | last_escape = esc_list[-1] |
|
69 | last_escape = esc_list[-1] | |
70 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) |
|
70 | print >>IPython.utils.io.Term.cout, last_escape + os.linesep.join(screens[-1]) | |
71 |
|
71 | |||
72 |
|
72 | |||
73 | def page(strng, start=0, screen_lines=0, pager_cmd=None): |
|
73 | def page(strng, start=0, screen_lines=0, pager_cmd=None): | |
@@ -156,7 +156,7 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):' | |||||
156 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
156 | #print 'numlines',numlines,'screenlines',screen_lines # dbg | |
157 | if numlines <= screen_lines : |
|
157 | if numlines <= screen_lines : | |
158 | #print '*** normal print' # dbg |
|
158 | #print '*** normal print' # dbg | |
159 | print >>Term.cout, str_toprint |
|
159 | print >>IPython.utils.io.Term.cout, str_toprint | |
160 | else: |
|
160 | else: | |
161 | # Try to open pager and default to internal one if that fails. |
|
161 | # Try to open pager and default to internal one if that fails. | |
162 | # All failure modes are tagged as 'retval=1', to match the return |
|
162 | # All failure modes are tagged as 'retval=1', to match the return | |
@@ -262,13 +262,13 b" if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':" | |||||
262 |
|
262 | |||
263 | @return: True if need print more lines, False if quit |
|
263 | @return: True if need print more lines, False if quit | |
264 | """ |
|
264 | """ | |
265 | Term.cout.write('---Return to continue, q to quit--- ') |
|
265 | IPython.utils.io.Term.cout.write('---Return to continue, q to quit--- ') | |
266 | ans = msvcrt.getch() |
|
266 | ans = msvcrt.getch() | |
267 | if ans in ("q", "Q"): |
|
267 | if ans in ("q", "Q"): | |
268 | result = False |
|
268 | result = False | |
269 | else: |
|
269 | else: | |
270 | result = True |
|
270 | result = True | |
271 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) |
|
271 | IPython.utils.io.Term.cout.write("\b"*37 + " "*37 + "\b"*37) | |
272 | return result |
|
272 | return result | |
273 | else: |
|
273 | else: | |
274 | def page_more(): |
|
274 | def page_more(): |
@@ -36,7 +36,7 b' from IPython.core.splitinput import split_user_input' | |||||
36 | from IPython.core.page import page |
|
36 | from IPython.core.page import page | |
37 |
|
37 | |||
38 | from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool, Instance |
|
38 | from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool, Instance | |
39 |
|
|
39 | import IPython.utils.io | |
40 | from IPython.utils.text import make_quoted_expr |
|
40 | from IPython.utils.text import make_quoted_expr | |
41 | from IPython.utils.autoattr import auto_attr |
|
41 | from IPython.utils.autoattr import auto_attr | |
42 |
|
42 | |||
@@ -922,7 +922,7 b' class AutoHandler(PrefilterHandler):' | |||||
922 | # plain ascii works better w/ pyreadline, on some machines, so |
|
922 | # plain ascii works better w/ pyreadline, on some machines, so | |
923 | # we use it and only print uncolored rewrite if we have unicode |
|
923 | # we use it and only print uncolored rewrite if we have unicode | |
924 | rw = str(rw) |
|
924 | rw = str(rw) | |
925 | print >>Term.cout, rw |
|
925 | print >>IPython.utils.io.Term.cout, rw | |
926 | except UnicodeEncodeError: |
|
926 | except UnicodeEncodeError: | |
927 | print "-------------->" + newcmd |
|
927 | print "-------------->" + newcmd | |
928 |
|
928 |
@@ -25,7 +25,7 b' from IPython.core.error import TryNext' | |||||
25 | from IPython.utils import coloransi |
|
25 | from IPython.utils import coloransi | |
26 | import IPython.utils.generics |
|
26 | import IPython.utils.generics | |
27 | from IPython.utils.warn import warn |
|
27 | from IPython.utils.warn import warn | |
28 |
|
|
28 | import IPython.utils.io | |
29 |
|
29 | |||
30 | #**************************************************************************** |
|
30 | #**************************************************************************** | |
31 | #Color schemes for Prompts. |
|
31 | #Color schemes for Prompts. | |
@@ -537,7 +537,7 b' class CachedOutput:' | |||||
537 | except KeyError: |
|
537 | except KeyError: | |
538 | pass |
|
538 | pass | |
539 | if arg is not None: |
|
539 | if arg is not None: | |
540 | cout_write = Term.cout.write # fast lookup |
|
540 | cout_write = IPython.utils.io.Term.cout.write # fast lookup | |
541 | # first handle the cache and counters |
|
541 | # first handle the cache and counters | |
542 |
|
542 | |||
543 | # do not print output if input ends in ';' |
|
543 | # do not print output if input ends in ';' | |
@@ -577,7 +577,7 b' class CachedOutput:' | |||||
577 | if self.logger.log_output: |
|
577 | if self.logger.log_output: | |
578 | self.logger.log_write(repr(arg),'output') |
|
578 | self.logger.log_write(repr(arg),'output') | |
579 | cout_write(self.output_sep2) |
|
579 | cout_write(self.output_sep2) | |
580 | Term.cout.flush() |
|
580 | IPython.utils.io.Term.cout.flush() | |
581 |
|
581 | |||
582 | def _display(self,arg): |
|
582 | def _display(self,arg): | |
583 | """Default printer method, uses pprint. |
|
583 | """Default printer method, uses pprint. |
@@ -95,7 +95,7 b' from IPython.core import debugger, ipapi' | |||||
95 | from IPython.core.display_trap import DisplayTrap |
|
95 | from IPython.core.display_trap import DisplayTrap | |
96 | from IPython.core.excolors import exception_colors |
|
96 | from IPython.core.excolors import exception_colors | |
97 | from IPython.utils.data import uniq_stable |
|
97 | from IPython.utils.data import uniq_stable | |
98 |
|
|
98 | import IPython.utils.io | |
99 | from IPython.utils.warn import info, error |
|
99 | from IPython.utils.warn import info, error | |
100 |
|
100 | |||
101 | # Globals |
|
101 | # Globals | |
@@ -313,14 +313,11 b' def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None):' | |||||
313 | class TBTools: |
|
313 | class TBTools: | |
314 | """Basic tools used by all traceback printer classes.""" |
|
314 | """Basic tools used by all traceback printer classes.""" | |
315 |
|
315 | |||
316 | #: Default output stream, can be overridden at call time. A special value |
|
316 | # This attribute us used in globalipapp.py to have stdout used for | |
317 | #: of 'stdout' *as a string* can be given to force extraction of sys.stdout |
|
317 | # writting exceptions. This is needed so nose can trap them. This attribute | |
318 | #: at runtime. This allows testing exception printing with doctests, that |
|
318 | # should be None (the default, which will use IPython.utils.io.Term) or | |
319 | #: swap sys.stdout just at execution time. |
|
319 | # the string 'stdout' which will cause the override to sys.stdout. | |
320 | #: Warning: be VERY careful to set this to one of the Term streams, NEVER |
|
320 | out_stream = None | |
321 | #: directly to sys.stdout/err, because under win32 the Term streams come from |
|
|||
322 | #: pyreadline and know how to handle color correctly, whie stdout/err don't. |
|
|||
323 | out_stream = Term.cerr |
|
|||
324 |
|
321 | |||
325 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): |
|
322 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): | |
326 | # Whether to call the interactive pdb debugger after printing |
|
323 | # Whether to call the interactive pdb debugger after printing | |
@@ -384,9 +381,9 b' class ListTB(TBTools):' | |||||
384 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) |
|
381 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) | |
385 |
|
382 | |||
386 | def __call__(self, etype, value, elist): |
|
383 | def __call__(self, etype, value, elist): | |
387 | Term.cout.flush() |
|
384 | IPython.utils.io.Term.cout.flush() | |
388 | Term.cerr.write(self.text(etype,value,elist)) |
|
385 | IPython.utils.io.Term.cerr.write(self.text(etype,value,elist)) | |
389 | Term.cerr.write('\n') |
|
386 | IPython.utils.io.Term.cerr.write('\n') | |
390 |
|
387 | |||
391 | def text(self, etype, value, elist, context=5): |
|
388 | def text(self, etype, value, elist, context=5): | |
392 | """Return a color formatted string with the traceback info. |
|
389 | """Return a color formatted string with the traceback info. | |
@@ -535,10 +532,13 b' class ListTB(TBTools):' | |||||
535 | """ |
|
532 | """ | |
536 | # This method needs to use __call__ from *this* class, not the one from |
|
533 | # This method needs to use __call__ from *this* class, not the one from | |
537 | # a subclass whose signature or behavior may be different |
|
534 | # a subclass whose signature or behavior may be different | |
538 | Term.cout.flush() |
|
535 | if self.out_stream == 'stdout': | |
539 | ostream = sys.stdout if self.out_stream == 'stdout' else Term.cerr |
|
536 | ostream = sys.stdout | |
|
537 | else: | |||
|
538 | ostream = IPython.utils.io.Term.cerr | |||
|
539 | ostream.flush() | |||
540 | ostream.write(ListTB.text(self, etype, value, [])) |
|
540 | ostream.write(ListTB.text(self, etype, value, [])) | |
541 |
ostream.flush() |
|
541 | ostream.flush() | |
542 |
|
542 | |||
543 | def _some_str(self, value): |
|
543 | def _some_str(self, value): | |
544 | # Lifted from traceback.py |
|
544 | # Lifted from traceback.py | |
@@ -659,7 +659,7 b' class VerboseTB(TBTools):' | |||||
659 | # So far, I haven't been able to find an isolated example to |
|
659 | # So far, I haven't been able to find an isolated example to | |
660 | # reproduce the problem. |
|
660 | # reproduce the problem. | |
661 | inspect_error() |
|
661 | inspect_error() | |
662 | traceback.print_exc(file=Term.cerr) |
|
662 | traceback.print_exc(file=IPython.utils.io.Term.cerr) | |
663 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
663 | info('\nUnfortunately, your original traceback can not be constructed.\n') | |
664 | return '' |
|
664 | return '' | |
665 |
|
665 | |||
@@ -696,7 +696,7 b' class VerboseTB(TBTools):' | |||||
696 | # able to remove this try/except when 2.4 becomes a |
|
696 | # able to remove this try/except when 2.4 becomes a | |
697 | # requirement. Bug details at http://python.org/sf/1005466 |
|
697 | # requirement. Bug details at http://python.org/sf/1005466 | |
698 | inspect_error() |
|
698 | inspect_error() | |
699 | traceback.print_exc(file=Term.cerr) |
|
699 | traceback.print_exc(file=IPython.utils.io.Term.cerr) | |
700 | info("\nIPython's exception reporting continues...\n") |
|
700 | info("\nIPython's exception reporting continues...\n") | |
701 |
|
701 | |||
702 | if func == '?': |
|
702 | if func == '?': | |
@@ -717,7 +717,7 b' class VerboseTB(TBTools):' | |||||
717 | # and barfs out. At some point I should dig into this one |
|
717 | # and barfs out. At some point I should dig into this one | |
718 | # and file a bug report about it. |
|
718 | # and file a bug report about it. | |
719 | inspect_error() |
|
719 | inspect_error() | |
720 | traceback.print_exc(file=Term.cerr) |
|
720 | traceback.print_exc(file=IPython.utils.io.Term.cerr) | |
721 | info("\nIPython's exception reporting continues...\n") |
|
721 | info("\nIPython's exception reporting continues...\n") | |
722 | call = tpl_call_fail % func |
|
722 | call = tpl_call_fail % func | |
723 |
|
723 | |||
@@ -910,9 +910,9 b' class VerboseTB(TBTools):' | |||||
910 | def handler(self, info=None): |
|
910 | def handler(self, info=None): | |
911 | (etype, evalue, etb) = info or sys.exc_info() |
|
911 | (etype, evalue, etb) = info or sys.exc_info() | |
912 | self.tb = etb |
|
912 | self.tb = etb | |
913 | Term.cout.flush() |
|
913 | IPython.utils.io.Term.cout.flush() | |
914 | Term.cerr.write(self.text(etype, evalue, etb)) |
|
914 | IPython.utils.io.Term.cerr.write(self.text(etype, evalue, etb)) | |
915 | Term.cerr.write('\n') |
|
915 | IPython.utils.io.Term.cerr.write('\n') | |
916 |
|
916 | |||
917 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
917 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
918 | # out the right info on its own. |
|
918 | # out the right info on its own. | |
@@ -1032,8 +1032,11 b' class AutoFormattedTB(FormattedTB):' | |||||
1032 | given at initialization time. """ |
|
1032 | given at initialization time. """ | |
1033 |
|
1033 | |||
1034 | if out is None: |
|
1034 | if out is None: | |
1035 |
|
|
1035 | if self.out_stream == 'stdout': | |
1036 | Term.cout.flush() |
|
1036 | out = sys.stdout | |
|
1037 | else: | |||
|
1038 | out = IPython.utils.io.Term.cerr | |||
|
1039 | out.flush() | |||
1037 | if tb_offset is not None: |
|
1040 | if tb_offset is not None: | |
1038 | tb_offset, self.tb_offset = self.tb_offset, tb_offset |
|
1041 | tb_offset, self.tb_offset = self.tb_offset, tb_offset | |
1039 | out.write(self.text(etype, evalue, etb)) |
|
1042 | out.write(self.text(etype, evalue, etb)) |
@@ -24,7 +24,7 b' import locale' | |||||
24 | from thread_ex import ThreadEx |
|
24 | from thread_ex import ThreadEx | |
25 |
|
25 | |||
26 | from IPython.core import iplib |
|
26 | from IPython.core import iplib | |
27 |
|
|
27 | import IPython.utils.io | |
28 |
|
28 | |||
29 | ############################################################################## |
|
29 | ############################################################################## | |
30 | class _Helper(object): |
|
30 | class _Helper(object): |
@@ -133,7 +133,7 b' from IPython.external import simplegeneric' | |||||
133 | from IPython.external import path |
|
133 | from IPython.external import path | |
134 |
|
134 | |||
135 | try: |
|
135 | try: | |
136 |
|
|
136 | import IPython.utils.io | |
137 | from IPython.utils import generics |
|
137 | from IPython.utils import generics | |
138 | except ImportError: |
|
138 | except ImportError: | |
139 | Term = None |
|
139 | Term = None |
@@ -31,7 +31,7 b' from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap' | |||||
31 |
|
31 | |||
32 | from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap |
|
32 | from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap | |
33 |
|
33 | |||
34 |
|
|
34 | import IPython.utils.io | |
35 |
|
35 | |||
36 | from linefrontendbase import LineFrontEndBase, common_prefix |
|
36 | from linefrontendbase import LineFrontEndBase, common_prefix | |
37 |
|
37 |
@@ -14,7 +14,7 b' from IPython.core.iplib import InteractiveShell' | |||||
14 | from IPython.utils.ipstruct import Struct |
|
14 | from IPython.utils.ipstruct import Struct | |
15 | import Queue,thread,threading,signal |
|
15 | import Queue,thread,threading,signal | |
16 | from signal import signal, SIGINT |
|
16 | from signal import signal, SIGINT | |
17 |
|
|
17 | import IPython.utils.io, ask_yes_no | |
18 | from IPython.utils.warn import warn, error |
|
18 | from IPython.utils.warn import warn, error | |
19 | from IPython.utils.decorators import flag_calls |
|
19 | from IPython.utils.decorators import flag_calls | |
20 | from IPython.core import shellglobals |
|
20 | from IPython.core import shellglobals |
@@ -39,7 +39,7 b' from IPython.core.error import TryNext' | |||||
39 | from IPython.external import pretty |
|
39 | from IPython.external import pretty | |
40 | from IPython.core.plugin import Plugin |
|
40 | from IPython.core.plugin import Plugin | |
41 | from IPython.utils.traitlets import Bool, List, Instance |
|
41 | from IPython.utils.traitlets import Bool, List, Instance | |
42 |
|
|
42 | import IPython.utils.io | |
43 | from IPython.utils.autoattr import auto_attr |
|
43 | from IPython.utils.autoattr import auto_attr | |
44 | from IPython.utils.importstring import import_item |
|
44 | from IPython.utils.importstring import import_item | |
45 |
|
45 | |||
@@ -100,8 +100,8 b' class PrettyResultDisplay(Plugin):' | |||||
100 | # So that multi-line strings line up with the left column of |
|
100 | # So that multi-line strings line up with the left column of | |
101 | # the screen, instead of having the output prompt mess up |
|
101 | # the screen, instead of having the output prompt mess up | |
102 | # their first line. |
|
102 | # their first line. | |
103 | Term.cout.write('\n') |
|
103 | IPython.utils.io.Term.cout.write('\n') | |
104 | print >>Term.cout, out |
|
104 | print >>IPython.utils.io.Term.cout, out | |
105 | else: |
|
105 | else: | |
106 | raise TryNext |
|
106 | raise TryNext | |
107 |
|
107 |
@@ -32,7 +32,7 b' from IPython.external.Itpl import ItplNS' | |||||
32 | from IPython.utils import coloransi |
|
32 | from IPython.utils import coloransi | |
33 | from IPython.core import release |
|
33 | from IPython.core import release | |
34 | from IPython.core.error import TryNext |
|
34 | from IPython.core.error import TryNext | |
35 |
|
|
35 | import IPython.utils.io | |
36 | from IPython.utils.warn import warn |
|
36 | from IPython.utils.warn import warn | |
37 | import IPython.utils.generics |
|
37 | import IPython.utils.generics | |
38 |
|
38 |
@@ -176,7 +176,8 b' import shlex' | |||||
176 | import sys |
|
176 | import sys | |
177 |
|
177 | |||
178 | from IPython.utils.PyColorize import Parser |
|
178 | from IPython.utils.PyColorize import Parser | |
179 |
from IPython.utils.io import file_read, file_readlines |
|
179 | from IPython.utils.io import file_read, file_readlines | |
|
180 | import IPython.utils.io | |||
180 | from IPython.utils.text import marquee |
|
181 | from IPython.utils.text import marquee | |
181 |
|
182 | |||
182 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] |
|
183 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] | |
@@ -318,7 +319,7 b' class Demo(object):' | |||||
318 |
|
319 | |||
319 | if index is None: |
|
320 | if index is None: | |
320 | if self.finished: |
|
321 | if self.finished: | |
321 | print >>Term.cout, 'Demo finished. Use <demo_name>.reset() if you want to rerun it.' |
|
322 | print >>IPython.utils.io.Term.cout, 'Demo finished. Use <demo_name>.reset() if you want to rerun it.' | |
322 | return None |
|
323 | return None | |
323 | index = self.block_index |
|
324 | index = self.block_index | |
324 | else: |
|
325 | else: | |
@@ -387,9 +388,9 b' class Demo(object):' | |||||
387 | if index is None: |
|
388 | if index is None: | |
388 | return |
|
389 | return | |
389 |
|
390 | |||
390 | print >>Term.cout, self.marquee('<%s> block # %s (%s remaining)' % |
|
391 | print >>IPython.utils.io.Term.cout, self.marquee('<%s> block # %s (%s remaining)' % | |
391 | (self.title,index,self.nblocks-index-1)) |
|
392 | (self.title,index,self.nblocks-index-1)) | |
392 | print >>Term.cout,(self.src_blocks_colored[index]) |
|
393 | print >>IPython.utils.io.Term.cout,(self.src_blocks_colored[index]) | |
393 | sys.stdout.flush() |
|
394 | sys.stdout.flush() | |
394 |
|
395 | |||
395 | def show_all(self): |
|
396 | def show_all(self): | |
@@ -402,12 +403,12 b' class Demo(object):' | |||||
402 | marquee = self.marquee |
|
403 | marquee = self.marquee | |
403 | for index,block in enumerate(self.src_blocks_colored): |
|
404 | for index,block in enumerate(self.src_blocks_colored): | |
404 | if silent[index]: |
|
405 | if silent[index]: | |
405 | print >>Term.cout, marquee('<%s> SILENT block # %s (%s remaining)' % |
|
406 | print >>IPython.utils.io.Term.cout, marquee('<%s> SILENT block # %s (%s remaining)' % | |
406 | (title,index,nblocks-index-1)) |
|
407 | (title,index,nblocks-index-1)) | |
407 | else: |
|
408 | else: | |
408 | print >>Term.cout, marquee('<%s> block # %s (%s remaining)' % |
|
409 | print >>IPython.utils.io.Term.cout, marquee('<%s> block # %s (%s remaining)' % | |
409 | (title,index,nblocks-index-1)) |
|
410 | (title,index,nblocks-index-1)) | |
410 | print >>Term.cout, block, |
|
411 | print >>IPython.utils.io.Term.cout, block, | |
411 | sys.stdout.flush() |
|
412 | sys.stdout.flush() | |
412 |
|
413 | |||
413 | def runlines(self,source): |
|
414 | def runlines(self,source): | |
@@ -432,18 +433,18 b' class Demo(object):' | |||||
432 | next_block = self.src_blocks[index] |
|
433 | next_block = self.src_blocks[index] | |
433 | self.block_index += 1 |
|
434 | self.block_index += 1 | |
434 | if self._silent[index]: |
|
435 | if self._silent[index]: | |
435 | print >>Term.cout, marquee('Executing silent block # %s (%s remaining)' % |
|
436 | print >>IPython.utils.io.Term.cout, marquee('Executing silent block # %s (%s remaining)' % | |
436 | (index,self.nblocks-index-1)) |
|
437 | (index,self.nblocks-index-1)) | |
437 | else: |
|
438 | else: | |
438 | self.pre_cmd() |
|
439 | self.pre_cmd() | |
439 | self.show(index) |
|
440 | self.show(index) | |
440 | if self.auto_all or self._auto[index]: |
|
441 | if self.auto_all or self._auto[index]: | |
441 | print >>Term.cout, marquee('output:') |
|
442 | print >>IPython.utils.io.Term.cout, marquee('output:') | |
442 | else: |
|
443 | else: | |
443 | print >>Term.cout, marquee('Press <q> to quit, <Enter> to execute...'), |
|
444 | print >>IPython.utils.io.Term.cout, marquee('Press <q> to quit, <Enter> to execute...'), | |
444 | ans = raw_input().strip() |
|
445 | ans = raw_input().strip() | |
445 | if ans: |
|
446 | if ans: | |
446 | print >>Term.cout, marquee('Block NOT executed') |
|
447 | print >>IPython.utils.io.Term.cout, marquee('Block NOT executed') | |
447 | return |
|
448 | return | |
448 | try: |
|
449 | try: | |
449 | save_argv = sys.argv |
|
450 | save_argv = sys.argv | |
@@ -461,10 +462,10 b' class Demo(object):' | |||||
461 | if self.block_index == self.nblocks: |
|
462 | if self.block_index == self.nblocks: | |
462 | mq1 = self.marquee('END OF DEMO') |
|
463 | mq1 = self.marquee('END OF DEMO') | |
463 | if mq1: |
|
464 | if mq1: | |
464 | # avoid spurious print >>Term.cout,s if empty marquees are used |
|
465 | # avoid spurious print >>IPython.utils.io.Term.cout,s if empty marquees are used | |
465 | print >>Term.cout |
|
466 | print >>IPython.utils.io.Term.cout | |
466 | print >>Term.cout, mq1 |
|
467 | print >>IPython.utils.io.Term.cout, mq1 | |
467 | print >>Term.cout, self.marquee('Use <demo_name>.reset() if you want to rerun it.') |
|
468 | print >>IPython.utils.io.Term.cout, self.marquee('Use <demo_name>.reset() if you want to rerun it.') | |
468 | self.finished = True |
|
469 | self.finished = True | |
469 |
|
470 | |||
470 | # These methods are meant to be overridden by subclasses who may wish to |
|
471 | # These methods are meant to be overridden by subclasses who may wish to |
@@ -65,21 +65,10 b' class IOTerm:' | |||||
65 | # In the future, having IPython channel all its I/O operations through |
|
65 | # In the future, having IPython channel all its I/O operations through | |
66 | # this class will make it easier to embed it into other environments which |
|
66 | # this class will make it easier to embed it into other environments which | |
67 | # are not a normal terminal (such as a GUI-based shell) |
|
67 | # are not a normal terminal (such as a GUI-based shell) | |
68 | def __init__(self,cin=None,cout=None,cerr=None): |
|
68 | def __init__(self, cin=None, cout=None, cerr=None): | |
69 | self.cin = IOStream(cin,sys.stdin) |
|
69 | self.cin = IOStream(cin, sys.stdin) | |
70 | self.cout = IOStream(cout,sys.stdout) |
|
70 | self.cout = IOStream(cout, sys.stdout) | |
71 | self.cerr = IOStream(cerr,sys.stderr) |
|
71 | self.cerr = IOStream(cerr, sys.stderr) | |
72 |
|
||||
73 |
|
||||
74 | # Global variable to be used for all I/O |
|
|||
75 | Term = IOTerm() |
|
|||
76 |
|
||||
77 |
|
||||
78 | import IPython.utils.rlineimpl as readline |
|
|||
79 | # Remake Term to use the readline i/o facilities |
|
|||
80 | if sys.platform == 'win32' and readline.have_readline: |
|
|||
81 |
|
||||
82 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) |
|
|||
83 |
|
72 | |||
84 |
|
73 | |||
85 | class Tee(object): |
|
74 | class Tee(object): |
@@ -16,7 +16,7 b" Utilities for warnings. Shoudn't we just use the built in warnings module." | |||||
16 |
|
16 | |||
17 | import sys |
|
17 | import sys | |
18 |
|
18 | |||
19 |
|
|
19 | import IPython.utils.io | |
20 |
|
20 | |||
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | # Code |
|
22 | # Code | |
@@ -25,7 +25,7 b' from IPython.utils.io import Term' | |||||
25 | def warn(msg,level=2,exit_val=1): |
|
25 | def warn(msg,level=2,exit_val=1): | |
26 | """Standard warning printer. Gives formatting consistency. |
|
26 | """Standard warning printer. Gives formatting consistency. | |
27 |
|
27 | |||
28 | Output is sent to Term.cerr (sys.stderr by default). |
|
28 | Output is sent to IPython.utils.io.Term.cerr (sys.stderr by default). | |
29 |
|
29 | |||
30 | Options: |
|
30 | Options: | |
31 |
|
31 | |||
@@ -41,9 +41,9 b' def warn(msg,level=2,exit_val=1):' | |||||
41 |
|
41 | |||
42 | if level>0: |
|
42 | if level>0: | |
43 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
43 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] | |
44 | print >> Term.cerr, '%s%s' % (header[level],msg) |
|
44 | print >> IPython.utils.io.Term.cerr, '%s%s' % (header[level],msg) | |
45 | if level == 4: |
|
45 | if level == 4: | |
46 | print >> Term.cerr,'Exiting.\n' |
|
46 | print >> IPython.utils.io.Term.cerr,'Exiting.\n' | |
47 | sys.exit(exit_val) |
|
47 | sys.exit(exit_val) | |
48 |
|
48 | |||
49 |
|
49 |
@@ -266,9 +266,12 b' def main():' | |||||
266 | req_port = bind_port(req_socket, namespace.ip, namespace.req) |
|
266 | req_port = bind_port(req_socket, namespace.ip, namespace.req) | |
267 | print >>sys.__stdout__, "REQ Channel on port", req_port |
|
267 | print >>sys.__stdout__, "REQ Channel on port", req_port | |
268 |
|
268 | |||
269 |
# Redirect input streams |
|
269 | # Redirect input streams. This needs to be done before the Kernel is done | |
|
270 | # because currently the Kernel creates a ZMQInteractiveShell, which | |||
|
271 | # holds references to sys.stdout and sys.stderr. | |||
270 | sys.stdout = OutStream(session, pub_socket, u'stdout') |
|
272 | sys.stdout = OutStream(session, pub_socket, u'stdout') | |
271 | sys.stderr = OutStream(session, pub_socket, u'stderr') |
|
273 | sys.stderr = OutStream(session, pub_socket, u'stderr') | |
|
274 | # Set a displayhook. | |||
272 | sys.displayhook = DisplayHook(session, pub_socket) |
|
275 | sys.displayhook = DisplayHook(session, pub_socket) | |
273 |
|
276 | |||
274 | # Create the kernel. |
|
277 | # Create the kernel. |
@@ -19,4 +19,13 b' class ZMQInteractiveShell(InteractiveShell):' | |||||
19 | print line |
|
19 | print line | |
20 | return p.wait() |
|
20 | return p.wait() | |
21 |
|
21 | |||
|
22 | def init_io(self): | |||
|
23 | # This will just use sys.stdout and sys.stderr. If you want to | |||
|
24 | # override sys.stdout and sys.stderr themselves, you need to do that | |||
|
25 | # *before* instantiating this class, because Term holds onto | |||
|
26 | # references to the underlying streams. | |||
|
27 | import IPython.utils.io | |||
|
28 | Term = IPython.utils.io.IOTerm() | |||
|
29 | IPython.utils.io.Term = Term | |||
|
30 | ||||
22 | InteractiveShellABC.register(ZMQInteractiveShell) |
|
31 | InteractiveShellABC.register(ZMQInteractiveShell) |
General Comments 0
You need to be logged in to leave comments.
Login now