Skip to content

Commit

Permalink
Inline simple/func macro handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Dec 22, 2024
1 parent ebd495b commit b8d7e23
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions src/ctypesgen/printer_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,27 +263,19 @@ def print_typedef(self, typedef):
self._srcinfo(typedef.src)
self.file.write(f"{typedef.name} = {typedef.ctype.py_string()}")


def print_macro(self, macro):
self._srcinfo(macro.src)
# important: must check precisely against None because params may be an empty list for a func macro
if macro.params is None:
self._print_simple_macro(macro)
else:
self._print_func_macro(macro)

def _print_simple_macro(self, macro):
entry = f"{macro.name} = {macro.expr.py_string(True)}"
if self.opts.guard_macros:
entry = self._try_except_wrap(entry)
self.file.write(entry)

def _print_func_macro(self, macro):
self.file.write(
f"def {macro.name}({', '.join(macro.params)}):"
f"\n return {macro.expr.py_string(True)}"
)

if macro.params is None: # simple macro
entry = f"{macro.name} = {macro.expr.py_string(True)}"
if self.opts.guard_macros:
entry = self._try_except_wrap(entry)
self.file.write(entry)
else: # func macro
self.file.write(
f"def {macro.name}({', '.join(macro.params)}):"
f"\n return {macro.expr.py_string(True)}"
)

def print_undef(self, undef):
self._srcinfo(undef.src)
Expand Down

0 comments on commit b8d7e23

Please sign in to comment.