Summary
Evaluate the invalid-input return value in pylong_as_bool in plugins/python/src/modulewrap.cpp.
The function sets a Python ValueError and currently returns static_cast<bool>(-1). In C++, that conversion yields true, which makes the returned value look like a successful boolean result if it is observed independently of the pending Python exception.
Required changes
Define and document the intended error-path contract for pylong_as_bool, then update the implementation accordingly. Consider whether the return should be simply true, false, or whether the function/API should be structured so that an error cannot be represented by a boolean return value.
Rationale
PR #771 replaced a C-style cast with static_cast<bool>(-1) to satisfy modernize-avoid-c-style-cast. The modernization preserves existing behavior, but the resulting expression exposes an unclear sentinel/error convention that should be reviewed separately.
Affected area
plugins/python/src/modulewrap.cpp
pylong_as_bool(PyObject*)
- Python-to-C++ boolean conversion through the
BASIC_CONVERTER machinery
Backlinks
Requested by @knoepfel.
Summary
Evaluate the invalid-input return value in
pylong_as_boolinplugins/python/src/modulewrap.cpp.The function sets a Python
ValueErrorand currently returnsstatic_cast<bool>(-1). In C++, that conversion yieldstrue, which makes the returned value look like a successful boolean result if it is observed independently of the pending Python exception.Required changes
Define and document the intended error-path contract for
pylong_as_bool, then update the implementation accordingly. Consider whether the return should be simplytrue,false, or whether the function/API should be structured so that an error cannot be represented by a boolean return value.Rationale
PR #771 replaced a C-style cast with
static_cast<bool>(-1)to satisfymodernize-avoid-c-style-cast. The modernization preserves existing behavior, but the resulting expression exposes an unclear sentinel/error convention that should be reviewed separately.Affected area
plugins/python/src/modulewrap.cpppylong_as_bool(PyObject*)BASIC_CONVERTERmachineryBacklinks
modernize-avoid-c-style-cast#771: clang-tidy: resolvemodernize-avoid-c-style-cast#771modernize-avoid-c-style-cast#771 (comment)Requested by @knoepfel.