1616# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
1818import pprint
19+ from typing import Callable
20+
1921from columnize import columnize
2022
2123# Maximum length of strings
@@ -44,18 +46,21 @@ def truncate_length(obj, length=MAX_PP_COUNT):
4446
4547class SafePP (pprint .PrettyPrinter ):
4648 def _format (self , obj , * args , ** kwargs ):
47- if isinstance (obj , str ):
48- if len (obj ) > MAX_PP_STRLEN :
49- obj = obj [:MAX_PP_STRLEN ] + "..."
49+ try :
50+ if isinstance (obj , str ):
51+ if len (obj ) > MAX_PP_STRLEN :
52+ obj = obj [:MAX_PP_STRLEN ] + "..."
53+ pass
5054 pass
55+ elif hasattr (obj , "__len__" ) and len (obj ) > MAX_PP_COUNT :
56+ obj = truncate_length (obj )
57+ except Exception :
5158 pass
52- elif hasattr (obj , "__len__" ) and len (obj ) > MAX_PP_COUNT :
53- obj = truncate_length (obj )
5459
5560 return pprint .PrettyPrinter ._format (self , obj , * args , ** kwargs )
5661
5762
58- def pp (val , display_width , msg_nocr , msg , prefix = None ):
63+ def pp (val , display_width , msg_nocr : Callable , msg : Callable , prefix = None ):
5964 if prefix is not None :
6065 val_len = len (repr (val ))
6166 if val_len + len (prefix ) < display_width - 1 :
@@ -77,7 +82,9 @@ def pp(val, display_width, msg_nocr, msg, prefix=None):
7782
7883# Actually... code like this should go in pformat.
7984# Possibly some will go into columnize.
80- def pprint_simple_array (val , displaywidth , msg_nocr , msg , lineprefix = "" ):
85+ def pprint_simple_array (
86+ val , displaywidth , msg_nocr : Callable , msg : Callable , lineprefix = ""
87+ ) -> bool :
8188 """Try to pretty print a simple case where a list is not nested.
8289 Return True if we can do it and False if not."""
8390
@@ -116,7 +123,7 @@ def msg_nocr(m):
116123 def msg (m ):
117124 print (m )
118125
119- pprint_simple_array (range (50 ), 50 , msg_nocr , msg )
126+ assert pprint_simple_array (range (50 ), 50 , msg_nocr , msg ) is False
120127 pp ([i for i in range (10 )], 50 , msg_nocr , msg )
121128 pp (locals (), 50 , msg_nocr , msg )
122129 x = [i for i in range (10 )]
0 commit comments