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 

2from typing import Dict, List, Tuple, Union # pragma: no cover 

3 

4import atheris # pragma: no cover 

5 

6 

7@atheris.instrument_func 

8def is_expected_error( 

9 exception: Exception, expected_errors: Dict[str, List[Tuple[str, Union[int]]]] 

10): # pragma: no cover 

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

12 

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

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

15 the exception message contains a specified substring. 

16 

17 Args: 

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

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

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

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

22 match any line number). 

23 

24 Returns: 

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

26 """ 

27 tb = traceback.extract_tb(exception.__traceback__) 

28 if not tb: 

29 return False 

30 

31 last_frame = tb[-1] 

32 exception_origin_file = last_frame.filename 

33 

34 if exception_origin_file in expected_errors: 

35 origin_line = int(last_frame.lineno) 

36 exception_message = str(exception).lower() 

37 for message, line in expected_errors[exception_origin_file]: 

38 if message.lower() in exception_message and ( 

39 line == -1 or line == origin_line 

40 ): 

41 return True 

42 return False