Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pycparser/__init__.py: 27%

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

26 statements  

1# ----------------------------------------------------------------- 

2# pycparser: __init__.py 

3# 

4# This package file exports some convenience functions for 

5# interacting with pycparser 

6# 

7# Eli Bendersky [https://eli.thegreenplace.net/] 

8# License: BSD 

9# ----------------------------------------------------------------- 

10__all__ = ["c_ast", "c_lexer", "c_parser"] 

11__version__ = "3.00" 

12 

13from subprocess import check_output 

14 

15from . import c_parser 

16 

17CParser = c_parser.CParser 

18 

19 

20def preprocess_file(filename, cpp_path="cpp", cpp_args=""): 

21 """Preprocess a file using cpp. 

22 

23 filename: 

24 Name of the file you want to preprocess. 

25 

26 cpp_path: 

27 cpp_args: 

28 Refer to the documentation of parse_file for the meaning of these 

29 arguments. 

30 

31 When successful, returns the preprocessed file's contents. 

32 Errors from cpp will be printed out. 

33 """ 

34 path_list = [cpp_path] 

35 if isinstance(cpp_args, list): 

36 path_list += cpp_args 

37 elif cpp_args != "": 

38 path_list += [cpp_args] 

39 path_list += [filename] 

40 

41 try: 

42 # Note the use of universal_newlines to treat all newlines 

43 # as \n for Python's purpose 

44 text = check_output(path_list, universal_newlines=True) 

45 except OSError as e: 

46 raise RuntimeError( 

47 "Unable to invoke 'cpp'. " 

48 + "Make sure its path was passed correctly\n" 

49 + f"Original error: {e}" 

50 ) 

51 

52 return text 

53 

54 

55def parse_file( 

56 filename, use_cpp=False, cpp_path="cpp", cpp_args="", parser=None, encoding=None 

57): 

58 """Parse a C file using pycparser. 

59 

60 filename: 

61 Name of the file you want to parse. 

62 

63 use_cpp: 

64 Set to True if you want to execute the C pre-processor 

65 on the file prior to parsing it. 

66 

67 cpp_path: 

68 If use_cpp is True, this is the path to 'cpp' on your 

69 system. If no path is provided, it attempts to just 

70 execute 'cpp', so it must be in your PATH. 

71 

72 cpp_args: 

73 If use_cpp is True, set this to the command line arguments strings 

74 to cpp. Be careful with quotes - it's best to pass a raw string 

75 (r'') here. For example: 

76 r'-I../utils/fake_libc_include' 

77 If several arguments are required, pass a list of strings. 

78 

79 encoding: 

80 Encoding to use for the file to parse 

81 

82 parser: 

83 Optional parser object to be used instead of the default CParser 

84 

85 When successful, an AST is returned. ParseError can be 

86 thrown if the file doesn't parse successfully. 

87 

88 Errors from cpp will be printed out. 

89 """ 

90 if use_cpp: 

91 text = preprocess_file(filename, cpp_path, cpp_args) 

92 else: 

93 with open(filename, encoding=encoding) as f: 

94 text = f.read() 

95 

96 if parser is None: 

97 parser = CParser() 

98 return parser.parse(text, filename)