@@ -356,7 +356,7 @@ class ErrorMinifier:
356356
357357 def __init__ (self , errors_list ):
358358 self ._errors_list = errors_list
359- self ._capacity = None
359+ self ._capacity = None # to how many symbols should minify
360360 self ._minified_error = "\n " .join (self ._errors_list )
361361
362362 def set_capacity (self , capacity ):
@@ -399,11 +399,13 @@ def _get_minified_errors(self): # pylint: disable=too-many-return-statements
399399 if len (errors_string ) <= self ._capacity :
400400 return errors_string
401401
402- # last resort keep removing middle lines
403- while len (errors_string ) > self ._capacity :
404- errors_string = self ._minify_by_removing_lines (errors_string )
402+ # remove some lines
403+ errors_string = self ._minify_by_removing_lines (errors_string , desired_length = self ._capacity )
404+ if len (errors_string ) <= self ._capacity :
405+ return errors_string
405406
406- return errors_string
407+ # last resort: strip off the suffix
408+ return self ._minify_by_removing_suffix (errors_string , desired_length = self ._capacity )
407409
408410 @staticmethod
409411 def _minify_by_shortening_file_names (error_string , levels = 5 ):
@@ -459,23 +461,41 @@ def _minify_by_removing_nested_exceptions(error_string):
459461 return "\n " .join (lines )
460462
461463 @staticmethod
462- def _minify_by_removing_lines (error_string ):
463- error_string = error_string .replace (ErrorMinifier ._CONTINUATION_STR , "" )
464- lines = error_string .splitlines ()
464+ def _minify_by_removing_lines (error_string , desired_length ):
465+ """
466+ Repeatedly remove the lines from the middle, as a last resort remove even the
467+ first line but keep the last one, in the effort to strip down the error string
468+ to the desired length.
469+ """
470+ if len (error_string ) <= desired_length :
471+ return error_string
465472
466- mid = int (len (lines ) / 2 ) + 1
467- if not (".py" in lines [mid ] and ", ln" in lines [mid ]):
468- mid -= 1
473+ symbols_to_remove = len (error_string ) - desired_length + len (ErrorMinifier ._CONTINUATION_STR )
474+ lines = error_string .splitlines (keepends = True )
469475
470- new_lines = []
471- for i , line in enumerate (lines ):
472- if i == mid :
473- new_lines .append (ErrorMinifier ._CONTINUATION_STR .strip ())
474- if i in range (mid , mid + 4 ):
475- continue
476- new_lines .append (line )
476+ if len (lines ) <= 1 or symbols_to_remove <= 0 :
477+ # nothing to remove
478+ return error_string
477479
478- return "\n " .join (new_lines )
480+ mid = 0
481+ while len (lines ) > 1 and symbols_to_remove > 0 :
482+ # remove the middle line, when even prefer to remove the one closer to the start
483+ mid = (len (lines ) - 1 ) // 2
484+ symbols_to_remove -= len (lines .pop (mid ))
485+
486+ lines .insert (mid , ErrorMinifier ._CONTINUATION_STR )
487+ return "" .join (lines )
488+
489+ @staticmethod
490+ def _minify_by_removing_suffix (error_string , desired_length ):
491+ """
492+ Strip off the suffix of the error string, force it to be <= desired length.
493+ """
494+ if len (error_string ) <= desired_length :
495+ return error_string
496+
497+ continuation = ErrorMinifier ._CONTINUATION_STR .strip ()[:desired_length ]
498+ return error_string [:desired_length - len (continuation )] + continuation
479499
480500 def __str__ (self ):
481501 if self ._minified_error :
0 commit comments