|
12 | 12 | ParserError, |
13 | 13 | ) |
14 | 14 | from pybind11_stubgen.parser.interface import IParser |
15 | | -from pybind11_stubgen.structs import Class, Module, QualifiedName |
| 15 | +from pybind11_stubgen.structs import Class, Function, Method, Module, QualifiedName |
| 16 | + |
| 17 | + |
| 18 | +class LocalErrors: |
| 19 | + def __init__(self, path: QualifiedName, errors: set[str], stack: list[LocalErrors]): |
| 20 | + self.path: QualifiedName = path |
| 21 | + self.errors: set[str] = errors |
| 22 | + self._stack: list[LocalErrors] = stack |
| 23 | + |
| 24 | + def __enter__(self) -> LocalErrors: |
| 25 | + self._stack.append(self) |
| 26 | + return self |
| 27 | + |
| 28 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 29 | + top = self._stack.pop() |
| 30 | + assert top == self |
16 | 31 |
|
17 | 32 |
|
18 | 33 | class LoggerData(IParser): |
19 | 34 | def __init__(self): |
20 | 35 | super().__init__() |
21 | | - self._seen_errors: set[str] = set() |
22 | | - self.__current_path: QualifiedName | None = None |
| 36 | + self.stack: list[LocalErrors] = [] |
| 37 | + |
| 38 | + def __new_layer(self, path: QualifiedName) -> LocalErrors: |
| 39 | + return LocalErrors(path, errors=set(), stack=self.stack) |
23 | 40 |
|
24 | 41 | def handle_module( |
25 | 42 | self, path: QualifiedName, module: types.ModuleType |
26 | 43 | ) -> Module | None: |
27 | | - old_errors = self._seen_errors |
28 | | - old_module = self.__current_path |
29 | | - self._seen_errors = set() |
30 | | - self.__current_path = path |
31 | | - result = super().handle_module(path, module) |
32 | | - self._seen_errors = old_errors |
33 | | - self.__current_path = old_module |
34 | | - return result |
| 44 | + with self.__new_layer(path): |
| 45 | + return super().handle_module(path, module) |
35 | 46 |
|
36 | 47 | def handle_class(self, path: QualifiedName, class_: type) -> Class | None: |
37 | | - old_errors = self._seen_errors |
38 | | - old_module = self.__current_path |
39 | | - self._seen_errors = set() |
40 | | - self.__current_path = path |
41 | | - result = super().handle_class(path, class_) |
42 | | - self._seen_errors = old_errors |
43 | | - self.__current_path = old_module |
44 | | - return result |
| 48 | + with self.__new_layer(path): |
| 49 | + return super().handle_class(path, class_) |
| 50 | + |
| 51 | + def handle_function(self, path: QualifiedName, class_: type) -> list[Function]: |
| 52 | + with self.__new_layer(path): |
| 53 | + return super().handle_function(path, class_) |
| 54 | + |
| 55 | + def handle_method(self, path: QualifiedName, class_: type) -> list[Method]: |
| 56 | + with self.__new_layer(path): |
| 57 | + return super().handle_method(path, class_) |
45 | 58 |
|
46 | 59 | @property |
47 | 60 | def current_path(self) -> QualifiedName: |
48 | | - assert self.__current_path is not None |
49 | | - return self.__current_path |
| 61 | + assert len(self.stack) != 0 |
| 62 | + return self.stack[-1].path |
50 | 63 |
|
51 | 64 | @property |
52 | 65 | def reported_errors(self) -> set[str]: |
53 | | - return self._seen_errors |
| 66 | + assert len(self.stack) != 0 |
| 67 | + return self.stack[-1].errors |
54 | 68 |
|
55 | 69 |
|
56 | 70 | logger = getLogger("pybind11_stubgen") |
|
0 commit comments