Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions pybind11_mkdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import argparse
import os
import re
import sys
from pathlib import Path

from pybind11_mkdoc.mkdoc_lib import mkdoc
Expand Down Expand Up @@ -34,15 +34,16 @@ 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):
"""
Add a compiler definition to an argument list.

The definition is expected to be given in the format '<macro>=<value>',
which will define <macro> to <value> (or 1 if <value> is omitted).
which will define <macro> to <value> (or 1 if the '=<value>' part is
omitted). An explicit empty value ('<macro>=') defines <macro> to nothing.

Parameters
----------
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pybind11_mkdoc/mkdoc_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down