From 2d26aff82f82a154e6cca7716baad5bb19fa518b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 6 Aug 2026 14:11:53 -0400 Subject: [PATCH] fix: keep explicit empty value in -D definitions `-DFOO=` now defines FOO to nothing, as clang does, instead of 1. Only a bare `-DFOO` becomes `FOO=1`. Also simplifies `__init__.py`: removes the unreachable `except` blocks in `_append_definition`, prints the promised warning for a missing include directory, and drops some redundant code in `main`. Assisted-by: ClaudeCode:claude-fable-5 --- pybind11_mkdoc/__init__.py | 33 ++++++++++----------------------- pybind11_mkdoc/mkdoc_lib.py | 2 +- tests/cli_test.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/pybind11_mkdoc/__init__.py b/pybind11_mkdoc/__init__.py index c12d3c7..21ad05d 100644 --- a/pybind11_mkdoc/__init__.py +++ b/pybind11_mkdoc/__init__.py @@ -6,7 +6,7 @@ import argparse import os -import re +import sys from pathlib import Path from pybind11_mkdoc.mkdoc_lib import mkdoc @@ -34,7 +34,7 @@ def _append_include_dir(args: list, include_dir: str, *, verbose: bool = True): if os.path.isdir(include_dir): args.append(f"-I{include_dir}") elif verbose: - pass + print(f"Include directory '{include_dir}' does not exist!", file=sys.stderr) # noqa: T201 def _append_definition(args: list, definition: str): @@ -42,7 +42,8 @@ def _append_definition(args: list, definition: str): Add a compiler definition to an argument list. The definition is expected to be given in the format '=', - which will define to (or 1 if is omitted). + which will define to (or 1 if the '=' part is + omitted). An explicit empty value ('=') defines to nothing. Parameters ---------- @@ -52,26 +53,12 @@ def _append_definition(args: list, definition: str): definition: str The definition to append. - - verbose: bool - Whether to print a warning for invalid definition strings. """ - try: - macro, _, value = definition.partition("=") - macro = macro.strip() - value = value.strip() if value else "1" - - args.append(f"-D{macro}={value}") - except ValueError: - # most likely means there was no '=' given - # check if argument is valid identifier - if re.search(r"^[A-Za-z_][A-Za-z0-9_]*", definition): - args.append(f"-D{definition}") - else: - pass - except Exception: - pass + macro, sep, value = definition.partition("=") + value = value.strip() if sep else "1" + + args.append(f"-D{macro.strip()}={value}") def get_cmake_dir() -> Path: @@ -142,7 +129,7 @@ def main(): parser.add_argument("header", type=str, nargs="+", help="A header file to process.") - [parsed_args, unparsed_args] = parser.parse_known_args() + parsed_args, unparsed_args = parser.parse_known_args() mkdoc_args = [] mkdoc_out = parsed_args.output @@ -165,7 +152,7 @@ def main(): # append argument as is and hope for the best mkdoc_args.append(arg) - mkdoc_args.extend(header for header in parsed_args.header) + mkdoc_args.extend(parsed_args.header) mkdoc(mkdoc_args, docstring_width, mkdoc_out) diff --git a/pybind11_mkdoc/mkdoc_lib.py b/pybind11_mkdoc/mkdoc_lib.py index 0a553d7..3b8442f 100755 --- a/pybind11_mkdoc/mkdoc_lib.py +++ b/pybind11_mkdoc/mkdoc_lib.py @@ -744,7 +744,7 @@ def write_header(comments, out_file=sys.stdout): def mkdoc(args, width, output=None): if width is not None: global docstring_width - docstring_width = int(width) + docstring_width = width comments = extract_all(args) if output: diff --git a/tests/cli_test.py b/tests/cli_test.py index 0abf36d..fccfa79 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -4,6 +4,8 @@ import pytest +from pybind11_mkdoc import _append_definition + DIR = Path(__file__).resolve().parent with open(DIR / "sample_header_docs" / "sample_header_truth.h") as f: @@ -27,6 +29,22 @@ def test_simple_header_cli(tmp_path: Path, name: str) -> None: assert res == expected +@pytest.mark.parametrize( + ("definition", "expected_arg"), + [ + ("FOO", "-DFOO=1"), + ("FOO=", "-DFOO="), + ("FOO=2", "-DFOO=2"), + (" FOO = 2 ", "-DFOO=2"), + ], +) +def test_append_definition(definition: str, expected_arg: str) -> None: + args: list[str] = [] + _append_definition(args, definition) + + assert args == [expected_arg] + + def test_parse_failure_sets_exit_code(tmp_path: Path) -> None: tf = tmp_path / "tmp.h" result = subprocess.run(