|
1 Solaris doesn't deliver a /usr/bin/cpp. We have our own cpp in |
|
2 /usr/lib/cpp, and we deliver GNU cpp in /usr/bin/gcpp, but we might not |
|
3 find those simply by looking in $PATH. So if we fail, try extra hard to |
|
4 execute another cpp. We try GNU's first, since it's more likely what folks |
|
5 using pycparser will expect (for instance, it handles C++-style comments). |
|
6 |
|
7 --- pycparser-2.10/pycparser/__init__.py Sat Aug 3 06:05:22 2013 |
|
8 +++ pycparser-2.10/pycparser/__init__.py Fri Apr 11 14:58:36 2014 |
|
9 @@ -10,6 +10,7 @@ |
|
10 __all__ = ['c_lexer', 'c_parser', 'c_ast'] |
|
11 __version__ = '2.10' |
|
12 |
|
13 +import errno |
|
14 from subprocess import Popen, PIPE |
|
15 from .c_parser import CParser |
|
16 |
|
17 @@ -28,29 +29,41 @@ |
|
18 When successful, returns the preprocessed file's contents. |
|
19 Errors from cpp will be printed out. |
|
20 """ |
|
21 - path_list = [cpp_path] |
|
22 - if isinstance(cpp_args, list): |
|
23 - path_list += cpp_args |
|
24 - elif cpp_args != '': |
|
25 - path_list += [cpp_args] |
|
26 - path_list += [filename] |
|
27 + cpp_list = [cpp_path, "/usr/bin/gcpp", "/usr/lib/cpp"] |
|
28 |
|
29 - try: |
|
30 - # Note the use of universal_newlines to treat all newlines |
|
31 - # as \n for Python's purpose |
|
32 - # |
|
33 - pipe = Popen( path_list, |
|
34 - stdout=PIPE, |
|
35 - universal_newlines=True) |
|
36 - text = pipe.communicate()[0] |
|
37 - except OSError as e: |
|
38 + for cpp_path in cpp_list: |
|
39 + path_list = [cpp_path] |
|
40 + if isinstance(cpp_args, list): |
|
41 + path_list += cpp_args |
|
42 + elif cpp_args != '': |
|
43 + path_list += [cpp_args] |
|
44 + path_list += [filename] |
|
45 + |
|
46 + try: |
|
47 + # Note the use of universal_newlines to treat all newlines |
|
48 + # as \n for Python's purpose |
|
49 + # |
|
50 + pipe = Popen( path_list, |
|
51 + stdout=PIPE, |
|
52 + universal_newlines=True) |
|
53 + text = pipe.communicate()[0] |
|
54 + except OSError as e: |
|
55 + # If cpp couldn't be found, just try the next one. |
|
56 + if e.errno == errno.ENOENT: |
|
57 + continue |
|
58 + # If we ran into some other error, raise a RuntimeError. |
|
59 + raise RuntimeError("Unable to invoke 'cpp'.\n" + |
|
60 + ('Original error: %s' % e)) |
|
61 + |
|
62 + return text |
|
63 + |
|
64 + else: |
|
65 + # If we couldn't find a cpp, then raise a RuntimeError. |
|
66 raise RuntimeError("Unable to invoke 'cpp'. " + |
|
67 'Make sure its path was passed correctly\n' + |
|
68 ('Original error: %s' % e)) |
|
69 |
|
70 - return text |
|
71 |
|
72 - |
|
73 def parse_file(filename, use_cpp=False, cpp_path='cpp', cpp_args='', |
|
74 parser=None): |
|
75 """ Parse a C file using pycparser. |