refactor cpp invocation from parse_file into preprocess_file
diff --git a/CHANGES b/CHANGES
index c0d315e..8efc772 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,8 @@
Also fixes Issue 60.
- Issue 63: line endings cleanup for consistent LF ending.
- Issues 64 & 65: added some more headers and typedefs to fake includes.
+ - Refactoring the cpp invocation in parse_file into a separate function, which
+ can also be used as a utility.
+ Version 2.07 (16.06.2012)
diff --git a/pycparser/__init__.py b/pycparser/__init__.py
index 9e29099..a02351b 100644
--- a/pycparser/__init__.py
+++ b/pycparser/__init__.py
@@ -7,77 +7,87 @@
# Copyright (C) 2008-2012, Eli Bendersky
# License: BSD
#-----------------------------------------------------------------
-
__all__ = ['c_lexer', 'c_parser', 'c_ast']
-__version__ = '2.07'
+__version__ = '2.08'
from subprocess import Popen, PIPE
-
from .c_parser import CParser
-def parse_file( filename, use_cpp=False,
- cpp_path='cpp', cpp_args='', parser=None):
+def preprocess_file(filename, cpp_path='cpp', cpp_args=''):
+ """ Preprocess a file using cpp.
+
+ filename:
+ Name of the file you want to preprocess.
+
+ cpp_path:
+ cpp_args:
+ Refer to the documentation of parse_file for the meaning of these
+ arguments.
+
+ When successful, returns the preprocessed file's contents.
+ Errors from cpp will be printed out.
+ """
+ path_list = [cpp_path]
+ if isinstance(cpp_args, list):
+ path_list += cpp_args
+ elif cpp_args != '':
+ path_list += [cpp_args]
+ path_list += [filename]
+
+ try:
+ # Note the use of universal_newlines to treat all newlines
+ # as \n for Python's purpose
+ #
+ pipe = Popen( path_list,
+ stdout=PIPE,
+ universal_newlines=True)
+ text = pipe.communicate()[0]
+ except OSError as e:
+ raise RuntimeError("Unable to invoke 'cpp'. " +
+ 'Make sure its path was passed correctly\n' +
+ ('Original error: %s' % e))
+
+ return text
+
+
+def parse_file(filename, use_cpp=False, cpp_path='cpp', cpp_args='',
+ parser=None):
""" Parse a C file using pycparser.
-
+
filename:
Name of the file you want to parse.
-
+
use_cpp:
Set to True if you want to execute the C pre-processor
on the file prior to parsing it.
-
+
cpp_path:
If use_cpp is True, this is the path to 'cpp' on your
system. If no path is provided, it attempts to just
execute 'cpp', so it must be in your PATH.
-
+
cpp_args:
- If use_cpp is True, set this to the command line
- arguments strings to cpp. Be careful with quotes -
- it's best to pass a raw string (r'') here.
- For example:
+ If use_cpp is True, set this to the command line arguments strings
+ to cpp. Be careful with quotes - it's best to pass a raw string
+ (r'') here. For example:
r'-I../utils/fake_libc_include'
- If several arguments are required, pass a list of
- strings.
-
+ If several arguments are required, pass a list of strings.
+
parser:
Optional parser object to be used instead of the default CParser
When successful, an AST is returned. ParseError can be
thrown if the file doesn't parse successfully.
-
+
Errors from cpp will be printed out.
"""
if use_cpp:
- path_list = [cpp_path]
- if isinstance(cpp_args, list):
- path_list += cpp_args
- elif cpp_args != '':
- path_list += [cpp_args]
- path_list += [filename]
-
- try:
- # Note the use of universal_newlines to treat all newlines
- # as \n for Python's purpose
- #
- pipe = Popen( path_list,
- stdout=PIPE,
- universal_newlines=True)
- text = pipe.communicate()[0]
- except OSError as e:
- raise RuntimeError("Unable to invoke 'cpp'. " +
- 'Make sure its path was passed correctly\n' +
- ('Original error: %s' % e))
+ text = preprocess_file(filename, cpp_path, cpp_args)
else:
text = open(filename, 'rU').read()
-
+
if parser is None:
parser = CParser()
return parser.parse(text, filename)
-
-
-if __name__ == "__main__":
- pass
-