@@ -468,6 +468,7 @@ def iter_stacks(tb):
468
468
def get_lines_from_file (
469
469
filename , # type: str
470
470
lineno , # type: int
471
+ max_length , # type: int
471
472
loader = None , # type: Optional[Any]
472
473
module = None , # type: Optional[str]
473
474
):
@@ -496,11 +497,12 @@ def get_lines_from_file(
496
497
497
498
try :
498
499
pre_context = [
499
- strip_string (line .strip ("\r \n " )) for line in source [lower_bound :lineno ]
500
+ strip_string (line .strip ("\r \n " ), max_length = max_length )
501
+ for line in source [lower_bound :lineno ]
500
502
]
501
- context_line = strip_string (source [lineno ].strip ("\r \n " ))
503
+ context_line = strip_string (source [lineno ].strip ("\r \n " ), max_length = max_length )
502
504
post_context = [
503
- strip_string (line .strip ("\r \n " ))
505
+ strip_string (line .strip ("\r \n " ), max_length = max_length )
504
506
for line in source [(lineno + 1 ) : upper_bound ]
505
507
]
506
508
return pre_context , context_line , post_context
@@ -512,6 +514,7 @@ def get_lines_from_file(
512
514
def get_source_context (
513
515
frame , # type: FrameType
514
516
tb_lineno , # type: int
517
+ max_string_length , # type: int
515
518
):
516
519
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
517
520
try :
@@ -528,7 +531,9 @@ def get_source_context(
528
531
loader = None
529
532
lineno = tb_lineno - 1
530
533
if lineno is not None and abs_path :
531
- return get_lines_from_file (abs_path , lineno , loader , module )
534
+ return get_lines_from_file (
535
+ abs_path , lineno , max_string_length , loader = loader , module = module
536
+ )
532
537
return [], None , []
533
538
534
539
@@ -602,7 +607,11 @@ def filename_for_module(module, abs_path):
602
607
603
608
604
609
def serialize_frame (
605
- frame , tb_lineno = None , include_local_variables = True , include_source_context = True
610
+ frame ,
611
+ tb_lineno = None ,
612
+ include_local_variables = True ,
613
+ include_source_context = True ,
614
+ max_string_length = None ,
606
615
):
607
616
# type: (FrameType, Optional[int], bool, bool) -> Dict[str, Any]
608
617
f_code = getattr (frame , "f_code" , None )
@@ -630,7 +639,7 @@ def serialize_frame(
630
639
631
640
if include_source_context :
632
641
rv ["pre_context" ], rv ["context_line" ], rv ["post_context" ] = get_source_context (
633
- frame , tb_lineno
642
+ frame , tb_lineno , max_string_length
634
643
)
635
644
636
645
if include_local_variables :
@@ -639,7 +648,9 @@ def serialize_frame(
639
648
return rv
640
649
641
650
642
- def current_stacktrace (include_local_variables = True , include_source_context = True ):
651
+ def current_stacktrace (
652
+ max_string_length , include_local_variables = True , include_source_context = True
653
+ ):
643
654
# type: (bool, bool) -> Any
644
655
__tracebackhide__ = True
645
656
frames = []
@@ -652,6 +663,7 @@ def current_stacktrace(include_local_variables=True, include_source_context=True
652
663
f ,
653
664
include_local_variables = include_local_variables ,
654
665
include_source_context = include_source_context ,
666
+ max_string_length = max_string_length ,
655
667
)
656
668
)
657
669
f = f .f_back
@@ -724,16 +736,19 @@ def single_exception_from_error_tuple(
724
736
if client_options is None :
725
737
include_local_variables = True
726
738
include_source_context = True
739
+ max_string_length = MAX_STRING_LENGTH # fallback
727
740
else :
728
741
include_local_variables = client_options ["include_local_variables" ]
729
742
include_source_context = client_options ["include_source_context" ]
743
+ max_string_length = client_options ["max_string_length" ]
730
744
731
745
frames = [
732
746
serialize_frame (
733
747
tb .tb_frame ,
734
748
tb_lineno = tb .tb_lineno ,
735
749
include_local_variables = include_local_variables ,
736
750
include_source_context = include_source_context ,
751
+ max_string_length = max_string_length ,
737
752
)
738
753
for tb in iter_stacks (tb )
739
754
]
@@ -819,9 +834,7 @@ def exceptions_from_error(
819
834
parent_id = exception_id
820
835
exception_id += 1
821
836
822
- should_supress_context = (
823
- hasattr (exc_value , "__suppress_context__" ) and exc_value .__suppress_context__ # type: ignore
824
- )
837
+ should_supress_context = hasattr (exc_value , "__suppress_context__" ) and exc_value .__suppress_context__ # type: ignore
825
838
if should_supress_context :
826
839
# Add direct cause.
827
840
# The field `__cause__` is set when raised with the exception (using the `from` keyword).
@@ -1082,7 +1095,6 @@ def _is_in_project_root(abs_path, project_root):
1082
1095
1083
1096
def strip_string (value , max_length = None ):
1084
1097
# type: (str, Optional[int]) -> Union[AnnotatedValue, str]
1085
- # TODO: read max_length from config
1086
1098
if not value :
1087
1099
return value
1088
1100
0 commit comments