| 111 | |
| 112 | |
| 113 | class ConstexprDetector(ast.NodeVisitor): |
| 114 | def __init__(self) -> None: |
| 115 | self.is_constexpr = True |
| 116 | self._allow_builtins_exceptions = True |
| 117 | |
| 118 | @contextlib.contextmanager |
| 119 | def disallow_builtins_exceptions(self) -> Generator[None, None, None]: |
| 120 | prev_allow = self._allow_builtins_exceptions |
| 121 | self._allow_builtins_exceptions = False |
| 122 | try: |
| 123 | yield |
| 124 | finally: |
| 125 | self._allow_builtins_exceptions = prev_allow |
| 126 | |
| 127 | def visit_Attribute(self, node: ast.Attribute) -> None: |
| 128 | with self.disallow_builtins_exceptions(): |
| 129 | self.visit(node.value) |
| 130 | |
| 131 | def visit_Name(self, node: ast.Name) -> None: |
| 132 | if self._allow_builtins_exceptions and hasattr(builtins, node.id): |
| 133 | return |
| 134 | self.is_constexpr = False |
| 135 | |
| 136 | def visit(self, node: ast.AST) -> None: |
| 137 | if not self.is_constexpr: |
| 138 | # can short-circuit if we've already detected that it's not a constexpr |
| 139 | return |
| 140 | super().visit(node) |
| 141 | |
| 142 | def __call__(self, node: ast.AST) -> bool: |
| 143 | self.is_constexpr = True |
| 144 | self.visit(node) |
| 145 | return self.is_constexpr |
| 146 | |
| 147 | |
| 148 | class AutoreloadTree: |
no outgoing calls
no test coverage detected
searching dependent graphs…