Coverage for /pythoncovmergedfiles/medio/medio/src/jsonpickle/fuzzing/fuzz-targets/utils.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1 statements  

1import traceback # pragma: no cover 

2 

3import atheris # pragma: no cover 

4 

5 

6@atheris.instrument_func 

7def is_expected_error( 

8 exception: Exception, expected_errors: dict[str, list[tuple[str, int]]] 

9): # pragma: no cover 

10 """Checks if a given exception matches any of the expected errors. 

11 

12 This function inspects the traceback of the provided exception to determine if 

13 it originated from a file and line number listed in the expected errors, and if 

14 the exception message contains a specified substring. 

15 

16 Args: 

17 exception (Exception): The exception to be checked. 

18 expected_errors (Dict[str, List[Tuple[str, Union[int]]]]): A dictionary where 

19 keys are filenames and values are lists of tuples. Each tuple contains a 

20 substring of an expected exception message and a line number (or -1 to 

21 match any line number). 

22 

23 Returns: 

24 bool: True if the exception matches any of the expected errors, False otherwise. 

25 """ 

26 tb = traceback.extract_tb(exception.__traceback__) 

27 if not tb: 

28 return False 

29 

30 last_frame = tb[-1] 

31 exception_origin_file = last_frame.filename 

32 

33 if exception_origin_file in expected_errors: 

34 origin_line = int(last_frame.lineno) 

35 exception_message = str(exception).lower() 

36 for message, line in expected_errors[exception_origin_file]: 

37 if message.lower() in exception_message and ( 

38 line == -1 or line == origin_line 

39 ): 

40 return True 

41 return False