Show More
@@ -20,15 +20,14 b' Authors:' | |||||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | from pprint import PrettyPrinter |
|
|||
24 | pformat = PrettyPrinter().pformat |
|
|||
25 |
|
23 | |||
26 | from IPython.config.configurable import Configurable |
|
24 | from IPython.config.configurable import Configurable | |
27 | from IPython.core import prompts |
|
25 | from IPython.core import prompts | |
28 | import IPython.utils.generics |
|
26 | import IPython.utils.generics | |
29 | import IPython.utils.io |
|
27 | import IPython.utils.io | |
30 |
from IPython.utils.traitlets import Instance, |
|
28 | from IPython.utils.traitlets import Instance, List | |
31 | from IPython.utils.warn import warn |
|
29 | from IPython.utils.warn import warn | |
|
30 | from IPython.core.formatters import DefaultFormatter | |||
32 |
|
31 | |||
33 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
34 | # Main displayhook class |
|
33 | # Main displayhook class | |
@@ -55,6 +54,16 b' class DisplayHook(Configurable):' | |||||
55 |
|
54 | |||
56 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
55 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
57 |
|
56 | |||
|
57 | # The default formatter. | |||
|
58 | default_formatter = Instance('IPython.core.formatters.FormatterABC') | |||
|
59 | def _default_formatter_default(self): | |||
|
60 | # FIXME: backwards compatibility for the InteractiveShell.pprint option? | |||
|
61 | return DefaultFormatter(config=self.config) | |||
|
62 | ||||
|
63 | # Any additional FormatterABC instances we use. | |||
|
64 | # FIXME: currently unused. | |||
|
65 | extra_formatters = List(config=True) | |||
|
66 | ||||
58 | # Each call to the In[] prompt raises it by 1, even the first. |
|
67 | # Each call to the In[] prompt raises it by 1, even the first. | |
59 | #prompt_count = Int(0) |
|
68 | #prompt_count = Int(0) | |
60 |
|
69 | |||
@@ -109,7 +118,6 b' class DisplayHook(Configurable):' | |||||
109 | self.output_sep = output_sep |
|
118 | self.output_sep = output_sep | |
110 | self.output_sep2 = output_sep2 |
|
119 | self.output_sep2 = output_sep2 | |
111 | self._,self.__,self.___ = '','','' |
|
120 | self._,self.__,self.___ = '','','' | |
112 | self.pprint_types = map(type,[(),[],{}]) |
|
|||
113 |
|
121 | |||
114 | # these are deliberately global: |
|
122 | # these are deliberately global: | |
115 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} |
|
123 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} | |
@@ -184,37 +192,23 b' class DisplayHook(Configurable):' | |||||
184 | if self.do_full_cache: |
|
192 | if self.do_full_cache: | |
185 | IPython.utils.io.Term.cout.write(outprompt) |
|
193 | IPython.utils.io.Term.cout.write(outprompt) | |
186 |
|
194 | |||
187 | # TODO: Make this method an extension point. The previous implementation |
|
|||
188 | # has both a result_display hook as well as a result_display generic |
|
|||
189 | # function to customize the repr on a per class basis. We need to rethink |
|
|||
190 | # the hooks mechanism before doing this though. |
|
|||
191 | def compute_result_repr(self, result): |
|
195 | def compute_result_repr(self, result): | |
192 | """Compute and return the repr of the object to be displayed. |
|
196 | """Compute and return the repr of the object to be displayed. | |
193 |
|
197 | |||
194 | This method only compute the string form of the repr and should NOT |
|
198 | This method only compute the string form of the repr and should NOT | |
195 |
actual print or write that to a stream. |
|
199 | actual print or write that to a stream. | |
196 | the result itself, but the default implementation passes the original |
|
|||
197 | through. |
|
|||
198 | """ |
|
200 | """ | |
199 | try: |
|
201 | result_repr = self.default_formatter(result) | |
200 | if self.shell.pprint: |
|
202 | if '\n' in result_repr: | |
201 | try: |
|
203 | # So that multi-line strings line up with the left column of | |
202 | result_repr = pformat(result) |
|
204 | # the screen, instead of having the output prompt mess up | |
203 | except: |
|
205 | # their first line. | |
204 | # Work around possible bugs in pformat |
|
206 | outprompt = str(self.prompt_out) | |
205 | result_repr = repr(result) |
|
207 | if outprompt and not outprompt.endswith('\n'): | |
206 | if '\n' in result_repr: |
|
208 | # But avoid extraneous empty lines. | |
207 | # So that multi-line strings line up with the left column of |
|
209 | result_repr = '\n' + result_repr | |
208 | # the screen, instead of having the output prompt mess up |
|
210 | ||
209 | # their first line. |
|
211 | return result_repr | |
210 | result_repr = '\n' + result_repr |
|
|||
211 | else: |
|
|||
212 | result_repr = repr(result) |
|
|||
213 | except TypeError: |
|
|||
214 | # This happens when result.__repr__ doesn't return a string, |
|
|||
215 | # such as when it returns None. |
|
|||
216 | result_repr = '\n' |
|
|||
217 | return result, result_repr |
|
|||
218 |
|
212 | |||
219 | def write_result_repr(self, result_repr): |
|
213 | def write_result_repr(self, result_repr): | |
220 | # We want to print because we want to always make sure we have a |
|
214 | # We want to print because we want to always make sure we have a | |
@@ -271,7 +265,7 b' class DisplayHook(Configurable):' | |||||
271 | if result is not None and not self.quiet(): |
|
265 | if result is not None and not self.quiet(): | |
272 | self.start_displayhook() |
|
266 | self.start_displayhook() | |
273 | self.write_output_prompt() |
|
267 | self.write_output_prompt() | |
274 |
|
|
268 | result_repr = self.compute_result_repr(result) | |
275 | self.write_result_repr(result_repr) |
|
269 | self.write_result_repr(result_repr) | |
276 | self.update_user_ns(result) |
|
270 | self.update_user_ns(result) | |
277 | self.log_output(result) |
|
271 | self.log_output(result) |
@@ -25,32 +25,32 b' class DefaultFormatter(Configurable):' | |||||
25 | format = Str('text') |
|
25 | format = Str('text') | |
26 |
|
26 | |||
27 | # Whether to pretty-print or not. |
|
27 | # Whether to pretty-print or not. | |
28 | pprint = Bool(True) |
|
28 | pprint = Bool(True, config=True) | |
29 |
|
29 | |||
30 | # Whether to be verbose or not. |
|
30 | # Whether to be verbose or not. | |
31 | verbose = Bool(False) |
|
31 | verbose = Bool(False, config=True) | |
32 |
|
32 | |||
33 | # The maximum width. |
|
33 | # The maximum width. | |
34 | max_width = Int(79) |
|
34 | max_width = Int(79, config=True) | |
35 |
|
35 | |||
36 | # The newline character. |
|
36 | # The newline character. | |
37 | newline = Str('\n') |
|
37 | newline = Str('\n', config=True) | |
38 |
|
38 | |||
39 | # The singleton prettyprinters. |
|
39 | # The singleton prettyprinters. | |
40 | # Maps the IDs of the builtin singleton objects to the format functions. |
|
40 | # Maps the IDs of the builtin singleton objects to the format functions. | |
41 | singleton_pprinters = Dict() |
|
41 | singleton_pprinters = Dict(config=True) | |
42 | def _singleton_pprinters_default(self): |
|
42 | def _singleton_pprinters_default(self): | |
43 | return pretty._singleton_pprinters.copy() |
|
43 | return pretty._singleton_pprinters.copy() | |
44 |
|
44 | |||
45 | # The type-specific prettyprinters. |
|
45 | # The type-specific prettyprinters. | |
46 | # Map type objects to the format functions. |
|
46 | # Map type objects to the format functions. | |
47 | type_pprinters = Dict() |
|
47 | type_pprinters = Dict(config=True) | |
48 | def _type_pprinters_default(self): |
|
48 | def _type_pprinters_default(self): | |
49 | return pretty._type_pprinters.copy() |
|
49 | return pretty._type_pprinters.copy() | |
50 |
|
50 | |||
51 | # The deferred-import type-specific prettyprinters. |
|
51 | # The deferred-import type-specific prettyprinters. | |
52 | # Map (modulename, classname) pairs to the format functions. |
|
52 | # Map (modulename, classname) pairs to the format functions. | |
53 | deferred_pprinters = Dict() |
|
53 | deferred_pprinters = Dict(config=True) | |
54 | def _deferred_pprinters_default(self): |
|
54 | def _deferred_pprinters_default(self): | |
55 | return pretty._deferred_type_pprinters.copy() |
|
55 | return pretty._deferred_type_pprinters.copy() | |
56 |
|
56 | |||
@@ -60,11 +60,10 b' class DefaultFormatter(Configurable):' | |||||
60 | """ Format the object. |
|
60 | """ Format the object. | |
61 | """ |
|
61 | """ | |
62 | if not self.pprint: |
|
62 | if not self.pprint: | |
63 |
|
|
63 | try: | |
64 | if r is None: |
|
64 | return repr(obj) | |
65 | # It can happen. |
|
65 | except TypeError: | |
66 |
r |
|
66 | return '' | |
67 | return r |
|
|||
68 | else: |
|
67 | else: | |
69 | stream = StringIO() |
|
68 | stream = StringIO() | |
70 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
|
69 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
@@ -475,6 +475,7 b' class InteractiveShell(Configurable, Magic):' | |||||
475 | def init_displayhook(self): |
|
475 | def init_displayhook(self): | |
476 | # Initialize displayhook, set in/out prompts and printing system |
|
476 | # Initialize displayhook, set in/out prompts and printing system | |
477 | self.displayhook = self.displayhook_class( |
|
477 | self.displayhook = self.displayhook_class( | |
|
478 | config=self.config, | |||
478 | shell=self, |
|
479 | shell=self, | |
479 | cache_size=self.cache_size, |
|
480 | cache_size=self.cache_size, | |
480 | input_sep = self.separate_in, |
|
481 | input_sep = self.separate_in, |
General Comments 0
You need to be logged in to leave comments.
Login now