Skip to content

Commit

Permalink
More sophisticated pre-processor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Feb 15, 2024
1 parent c298de5 commit fa3106a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
38 changes: 36 additions & 2 deletions src/ctypesgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,42 @@ def _is_relative_to(path, other):
return path == other or other in path.parents


# -- Argument Parser (Backports) --

if sys.version_info >= (3, 9):
from argparse import BooleanOptionalAction

else:
# backport, adapted from argparse sources
class BooleanOptionalAction (argparse.Action):
def __init__(self, option_strings, dest, **kwargs):

_option_strings = []
for option_string in option_strings:
_option_strings.append(option_string)

if option_string.startswith('--'):
option_string = '--no-' + option_string[2:]
_option_strings.append(option_string)

super().__init__(option_strings=_option_strings, dest=dest, nargs=0, **kwargs)

def __call__(self, parser, namespace, values, option_string=None):
if option_string in self.option_strings:
setattr(namespace, self.dest, not option_string.startswith('--no-'))

def format_usage(self):
return ' | '.join(self.option_strings)


# -- Argument Parser ---

def generic_path_t(p):
return Path(p).expanduser().resolve()

def checked_path_t(p, check, exc):
p = generic_path_t(p)
if not check(p): raise exc(f"{p}")
# if not check(p): raise exc(f"{p}")
return p

def input_file_t(p):
Expand All @@ -187,7 +215,7 @@ def input_dir_t(p):
def get_parser():

# FIXME argparse parameters are not ordered consistently...
# TODO consider BooleanOptionalAction (with compat backport)
# TODO expand use of BooleanOptionalAction

parser = argparse.ArgumentParser(prog="ctypesgen")

Expand Down Expand Up @@ -340,6 +368,12 @@ def __call__(self, parser, namespace, values, option_string=None):
metavar="FILENAME",
help="Save preprocessor output to the specified FILENAME",
)
parser.add_argument(
"--preproc-errcheck",
action=BooleanOptionalAction,
help="Whether to fail fast if the preprocessor returned a non-zero exit code. Defaults to True, unless on Windows.",
default=not sys.platform.startswith("win"),
)
parser.add_argument(
"--optimize-lexer",
action="store_true",
Expand Down
10 changes: 8 additions & 2 deletions src/ctypesgen/parser/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from ctypesgen.parser import pplexer, lex
from ctypesgen.parser.lex import LexError
from ctypesgen.messages import warning_message
from ctypesgen.messages import warning_message, status_message


IS_WINDOWS = sys.platform.startswith("win")
Expand Down Expand Up @@ -141,8 +141,14 @@ def parse(self, filename):
cmd,
universal_newlines=False, # binary
stdout=subprocess.PIPE,
check=not IS_WINDOWS,
)
status_message(f"Pre-processor returned exit code {pp.returncode}")
if pp.returncode != 0:
msg = f"Pre-processor returned non-zero exit code {pp.returncode}"
if self.options.preproc_errcheck:
assert False, msg
else:
warning_message(msg)

if IS_MAC:
ppout = pp.stdout.decode("utf-8", errors="replace")
Expand Down

0 comments on commit fa3106a

Please sign in to comment.