Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show function name in error message #165

Merged
merged 3 commits into from
Oct 21, 2023
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
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ jobs:
path: "./tests/stubs/python-${{ matrix.python }}/pybind11-${{ matrix.pybind11-branch }}.patch"
retention-days: 30
if-no-files-found: ignore

- name: Check error generation
if: ${{ !contains(fromJson('["3.7", "3.8"]'), matrix.python) }}
shell: bash
run: ./tests/check-demo-errors-generation.sh

test-cli-options:
name: "Runs on 🐍 ${{ matrix.python }} • ${{ matrix.test-package }}"
runs-on: ubuntu-latest
Expand Down
58 changes: 36 additions & 22 deletions pybind11_stubgen/parser/mixins/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,59 @@
ParserError,
)
from pybind11_stubgen.parser.interface import IParser
from pybind11_stubgen.structs import Class, Module, QualifiedName
from pybind11_stubgen.structs import Class, Function, Method, Module, QualifiedName


class LocalErrors:
def __init__(self, path: QualifiedName, errors: set[str], stack: list[LocalErrors]):
self.path: QualifiedName = path
self.errors: set[str] = errors
self._stack: list[LocalErrors] = stack

def __enter__(self) -> LocalErrors:
self._stack.append(self)
return self

def __exit__(self, exc_type, exc_val, exc_tb):
top = self._stack.pop()
assert top == self


class LoggerData(IParser):
def __init__(self):
super().__init__()
self._seen_errors: set[str] = set()
self.__current_path: QualifiedName | None = None
self.stack: list[LocalErrors] = []

def __new_layer(self, path: QualifiedName) -> LocalErrors:
return LocalErrors(path, errors=set(), stack=self.stack)

def handle_module(
self, path: QualifiedName, module: types.ModuleType
) -> Module | None:
old_errors = self._seen_errors
old_module = self.__current_path
self._seen_errors = set()
self.__current_path = path
result = super().handle_module(path, module)
self._seen_errors = old_errors
self.__current_path = old_module
return result
with self.__new_layer(path):
return super().handle_module(path, module)

def handle_class(self, path: QualifiedName, class_: type) -> Class | None:
old_errors = self._seen_errors
old_module = self.__current_path
self._seen_errors = set()
self.__current_path = path
result = super().handle_class(path, class_)
self._seen_errors = old_errors
self.__current_path = old_module
return result
with self.__new_layer(path):
return super().handle_class(path, class_)

def handle_function(self, path: QualifiedName, class_: type) -> list[Function]:
with self.__new_layer(path):
return super().handle_function(path, class_)

def handle_method(self, path: QualifiedName, class_: type) -> list[Method]:
with self.__new_layer(path):
return super().handle_method(path, class_)

@property
def current_path(self) -> QualifiedName:
assert self.__current_path is not None
return self.__current_path
assert len(self.stack) != 0
return self.stack[-1].path

@property
def reported_errors(self) -> set[str]:
return self._seen_errors
assert len(self.stack) != 0
return self.stack[-1].errors


logger = getLogger("pybind11_stubgen")
Expand Down
45 changes: 45 additions & 0 deletions tests/check-demo-errors-generation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

set -e

TESTS_ROOT="$(readlink -m "$(dirname "$0")")"
DEMO_ERRORS_FILE="${TESTS_ROOT}/demo.errors.stderr.txt"
STUBS_DIR="/tmp/out" # Stubs should never be actually written

remove_demo_errors() {
rm -rf "${DEMO_ERRORS_FILE}";
}

check_error_messages() {
(
set -o pipefail ;
git diff --exit-code HEAD -- "${DEMO_ERRORS_FILE}";
)
}
run_stubgen() {
(
set +e ;
pybind11-stubgen \
demo \
--output-dir=${STUBS_DIR} \
--exit-code \
2> "${DEMO_ERRORS_FILE}" \
|| exit 0
) || (
echo "'pybind11-stubgen demo --exit-code' did not exit with code 1"
exit 1
)
}

remove_randomness_in_errors (){
sed -i 's/0x[0-9a-f]*/0x1234abcd5678/gi' "${DEMO_ERRORS_FILE}"
}

main () {
remove_demo_errors
run_stubgen
remove_randomness_in_errors
check_error_messages
}

main "$@"
15 changes: 15 additions & 0 deletions tests/demo.errors.stderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pybind11_stubgen - [ ERROR] In demo._bindings.aliases.foreign_enum_default : Invalid expression '<ConsoleForegroundColor.Blue: 34>'
pybind11_stubgen - [ ERROR] In demo._bindings.enum.accept_defaulted_enum : Invalid expression '<ConsoleForegroundColor.None_: -1>'
pybind11_stubgen - [ ERROR] In demo._bindings.flawed_bindings.accept_unbound_enum : Invalid expression '(anonymous namespace)::Enum'
pybind11_stubgen - [ ERROR] In demo._bindings.flawed_bindings.accept_unbound_enum_defaulted : Invalid expression '<demo._bindings.flawed_bindings.Enum object at 0x1234abcd5678>'
pybind11_stubgen - [ ERROR] In demo._bindings.flawed_bindings.accept_unbound_type : Invalid expression '(anonymous namespace)::Unbound'
pybind11_stubgen - [ ERROR] In demo._bindings.flawed_bindings.accept_unbound_type_defaulted : Invalid expression '<demo._bindings.flawed_bindings.Unbound object at 0x1234abcd5678>'
pybind11_stubgen - [ ERROR] In demo._bindings.flawed_bindings.get_unbound_type : Invalid expression '(anonymous namespace)::Unbound'
pybind11_stubgen - [WARNING] Enum-like str representations were found with no matching mapping to the enum class location.
Use `--enum-class-locations` to specify full path to the following enum(s):
- ConsoleForegroundColor
pybind11_stubgen - [WARNING] Raw C++ types/values were found in signatures extracted from docstrings.
Please check the corresponding sections of pybind11 documentation to avoid common mistakes in binding code:
- https://pybind11.readthedocs.io/en/latest/advanced/misc.html#avoiding-cpp-types-in-docstrings
- https://pybind11.readthedocs.io/en/latest/advanced/functions.html#default-arguments-revisited
pybind11_stubgen - [ INFO] Terminating due to previous errors