Fixes pretty_symbology.py to work if sys.stdout.encoding doesn't exist (#737).
1 """Printing subsystem driver"""
4 """Generic printer driver
6 This is a generic printer driver.
7 It's job is to provide infrastructure for implementing new printers easily.
9 Basically, if you want to implement a printer, all you have to do is:
12 2. In your subclass, define _print_<CLASS> methods
14 For each class you want to provide printing to, define an appropriate
15 method how to do it. For example if you want a class FOO to be printed in
16 it's own way, define _print_FOO:
18 def _print_FOO(self, e):
21 this should return how FOO instance e is printed
23 Also, if BAR is a subclass of FOO, _print_FOO(bar) will be called for
24 instance of BAR, if no _print_BAR is provided. Thus, usually, we don't
25 need to provide prining routines for every class we want to support --
26 only generic routine has to be provided for a set of classes.
28 A good example for this are functions - for example PrettyPrinter only
29 defines _print_Function, and there is no _print_sin, _print_tan, etc...
31 On the other hand, a good printer will probably have to define separate
32 routines for Symbol, Atom, Number, Integral, Limit, etc...
34 3. If convenient, override self.emptyPrinter
36 This callable will be called to obtain printing result as a last resort,
37 that is when no appropriate _print_<CLASS> was found for an expression.
43 self.emptyPrinter = str
45 def doprint(self, expr):
46 """Returns printer's representation for expr (as a string)"""
47 return self._str(self._print(expr))
49 def _print(self, expr, *args):
50 """internal dispatcher
52 It's job is to loop through expr classes (class + it's bases), and
53 try to dispatch the work to _print_<EXPR_CLASS>
55 e.g., suppose we have the following class hierarcy::
65 then, for expr=Rational(...), in order to dispatch, we will try
66 calling printer methods as shown in the figure below:
70 |-- p._print_Rational(expr)
72 |-- p._print_Number(expr)
74 |-- p._print_Atom(expr)
76 `-- p._print_Basic(expr)
78 if ._print_Rational method exists in the printer, then it is called,
79 and the result is returned back.
81 otherwise, we proceed with trying Rational bases in the inheritance
84 if nothing exists, we just return:
90 # See if the class of expr is known, or if one of its super
91 # classes is known, and use that print function
93 for cls in expr.__class__.__mro__:
94 if hasattr(self, '_print_'+cls.__name__):
95 res = getattr(self, '_print_'+cls.__name__)(expr, *args)
98 # Unknown object, just use its string representation
100 res = self.emptyPrinter(expr)