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

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

27 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_lexer", "c_parser", "c_ast"] 

11__version__ = "3.00" 

12 

13import io 

14from subprocess import check_output 

15 

16from . import c_parser 

17 

18CParser = c_parser.CParser 

19 

20 

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

22 """Preprocess a file using cpp. 

23 

24 filename: 

25 Name of the file you want to preprocess. 

26 

27 cpp_path: 

28 cpp_args: 

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

30 arguments. 

31 

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

33 Errors from cpp will be printed out. 

34 """ 

35 path_list = [cpp_path] 

36 if isinstance(cpp_args, list): 

37 path_list += cpp_args 

38 elif cpp_args != "": 

39 path_list += [cpp_args] 

40 path_list += [filename] 

41 

42 try: 

43 # Note the use of universal_newlines to treat all newlines 

44 # as \n for Python's purpose 

45 text = check_output(path_list, universal_newlines=True) 

46 except OSError as e: 

47 raise RuntimeError( 

48 "Unable to invoke 'cpp'. " 

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

50 + f"Original error: {e}" 

51 ) 

52 

53 return text 

54 

55 

56def parse_file( 

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

58): 

59 """Parse a C file using pycparser. 

60 

61 filename: 

62 Name of the file you want to parse. 

63 

64 use_cpp: 

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

66 on the file prior to parsing it. 

67 

68 cpp_path: 

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

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

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

72 

73 cpp_args: 

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

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

76 (r'') here. For example: 

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

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

79 

80 encoding: 

81 Encoding to use for the file to parse 

82 

83 parser: 

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

85 

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

87 thrown if the file doesn't parse successfully. 

88 

89 Errors from cpp will be printed out. 

90 """ 

91 if use_cpp: 

92 text = preprocess_file(filename, cpp_path, cpp_args) 

93 else: 

94 with io.open(filename, encoding=encoding) as f: 

95 text = f.read() 

96 

97 if parser is None: 

98 parser = CParser() 

99 return parser.parse(text, filename)