|
1 | | -"""Find functions that call the native module and swallow its failures.""" |
| 1 | +"""Find code that calls native functions and swallows their failures.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import ast |
6 | 6 | from pathlib import Path |
7 | 7 |
|
8 | | -from scripts.python_boundary.constants import NATIVE_MODULE, NATIVE_NAME, NATIVE_PACKAGE |
| 8 | +from scripts.python_boundary.constants import ( |
| 9 | + MODULE_SCOPE, |
| 10 | + NATIVE_MARKER, |
| 11 | + NATIVE_PACKAGE, |
| 12 | + SUPPRESS_NAME, |
| 13 | +) |
9 | 14 | from scripts.python_boundary.models import NativeErrorHandler |
10 | 15 |
|
11 | 16 |
|
12 | 17 | def find_native_error_handlers( |
13 | 18 | *, source: Path, paths: tuple[Path, ...] |
14 | 19 | ) -> tuple[NativeErrorHandler, ...]: |
15 | | - """Each `try` around a native call whose handler neither raises nor re-raises.""" |
| 20 | + """Each handler or `suppress` around a native call that does not unconditionally raise.""" |
16 | 21 | handlers: list[NativeErrorHandler] = [] |
17 | 22 | for path in paths: |
18 | 23 | tree: ast.Module = ast.parse(path.read_text(encoding="utf-8"), filename=str(path)) |
19 | 24 | native: frozenset[str] = _native_names(tree) |
20 | 25 | if not native: |
21 | 26 | continue |
22 | 27 | relative: str = path.relative_to(source).as_posix() |
23 | | - for function, statement in _try_statements(tree): |
24 | | - if not _calls_native(body=statement.body, native=native): |
25 | | - continue |
26 | | - for handler in statement.handlers: |
27 | | - if not any(isinstance(node, ast.Raise) for node in ast.walk(handler)): |
28 | | - handlers.append( |
29 | | - NativeErrorHandler(location=f"{relative}::{function}", line=handler.lineno) |
30 | | - ) |
| 28 | + for scope, node in _guarded_blocks(tree): |
| 29 | + for line in _swallowing_lines(node=node, native=native): |
| 30 | + handlers.append(NativeErrorHandler(location=f"{relative}::{scope}", line=line)) |
31 | 31 | return tuple(handlers) |
32 | 32 |
|
33 | 33 |
|
34 | 34 | def _native_names(tree: ast.Module) -> frozenset[str]: |
| 35 | + """Names bound to native code: imports from native modules or of native-named functions.""" |
35 | 36 | names: set[str] = set() |
36 | 37 | for node in ast.walk(tree): |
37 | 38 | if isinstance(node, ast.ImportFrom) and node.module is not None: |
38 | | - if node.module.startswith(NATIVE_MODULE): |
39 | | - names.update(alias.asname or alias.name for alias in node.names) |
40 | | - elif node.module == NATIVE_PACKAGE: |
41 | | - names.update( |
42 | | - alias.asname or alias.name for alias in node.names if alias.name == NATIVE_NAME |
43 | | - ) |
| 39 | + native_module: bool = _is_native_module(node.module) |
| 40 | + names.update( |
| 41 | + alias.asname or alias.name |
| 42 | + for alias in node.names |
| 43 | + if native_module or NATIVE_MARKER in alias.name |
| 44 | + ) |
44 | 45 | elif isinstance(node, ast.Import): |
45 | 46 | names.update( |
46 | 47 | (alias.asname or alias.name).split(".")[0] |
47 | 48 | for alias in node.names |
48 | | - if alias.name.startswith(NATIVE_MODULE) |
| 49 | + if _is_native_module(alias.name) |
49 | 50 | ) |
50 | 51 | return frozenset(names) |
51 | 52 |
|
52 | 53 |
|
53 | | -def _try_statements(tree: ast.Module) -> list[tuple[str, ast.Try]]: |
54 | | - found: list[tuple[str, ast.Try]] = [] |
55 | | - for node in ast.walk(tree): |
56 | | - if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef): |
57 | | - found.extend( |
58 | | - (node.name, child) for child in ast.walk(node) if isinstance(child, ast.Try) |
59 | | - ) |
| 54 | +def _is_native_module(module: str) -> bool: |
| 55 | + parts: list[str] = module.split(".") |
| 56 | + return parts[0] == NATIVE_PACKAGE and any(NATIVE_MARKER in part for part in parts[1:]) |
| 57 | + |
| 58 | + |
| 59 | +def _guarded_blocks(tree: ast.Module) -> list[tuple[str, ast.stmt]]: |
| 60 | + """Every `try` and `with` statement, named by its innermost enclosing function.""" |
| 61 | + return _collect(node=tree, scope=MODULE_SCOPE, owner=None) |
| 62 | + |
| 63 | + |
| 64 | +def _collect(*, node: ast.AST, scope: str, owner: str | None) -> list[tuple[str, ast.stmt]]: |
| 65 | + found: list[tuple[str, ast.stmt]] = [] |
| 66 | + for child in ast.iter_child_nodes(node): |
| 67 | + if isinstance(child, ast.ClassDef): |
| 68 | + found.extend(_collect(node=child, scope=scope, owner=child.name)) |
| 69 | + elif isinstance(child, ast.FunctionDef | ast.AsyncFunctionDef): |
| 70 | + name: str = child.name if owner is None else f"{owner}.{child.name}" |
| 71 | + found.extend(_collect(node=child, scope=name, owner=None)) |
| 72 | + else: |
| 73 | + if isinstance(child, ast.Try | ast.TryStar | ast.With | ast.AsyncWith): |
| 74 | + found.append((scope, child)) |
| 75 | + found.extend(_collect(node=child, scope=scope, owner=owner)) |
60 | 76 | return found |
61 | 77 |
|
62 | 78 |
|
| 79 | +def _swallowing_lines(*, node: ast.stmt, native: frozenset[str]) -> list[int]: |
| 80 | + if isinstance(node, ast.Try | ast.TryStar): |
| 81 | + if not _calls_native(body=node.body, native=native): |
| 82 | + return [] |
| 83 | + return [handler.lineno for handler in node.handlers if not _always_raises(handler)] |
| 84 | + if isinstance(node, ast.With | ast.AsyncWith) and any( |
| 85 | + _is_suppress(item.context_expr) for item in node.items |
| 86 | + ): |
| 87 | + return [node.lineno] if _calls_native(body=node.body, native=native) else [] |
| 88 | + return [] |
| 89 | + |
| 90 | + |
| 91 | +def _always_raises(handler: ast.ExceptHandler) -> bool: |
| 92 | + return any(isinstance(statement, ast.Raise) for statement in handler.body) |
| 93 | + |
| 94 | + |
| 95 | +def _is_suppress(expression: ast.expr) -> bool: |
| 96 | + if not isinstance(expression, ast.Call): |
| 97 | + return False |
| 98 | + function: ast.expr = expression.func |
| 99 | + if isinstance(function, ast.Attribute): |
| 100 | + return function.attr == SUPPRESS_NAME |
| 101 | + return isinstance(function, ast.Name) and function.id == SUPPRESS_NAME |
| 102 | + |
| 103 | + |
63 | 104 | def _calls_native(*, body: list[ast.stmt], native: frozenset[str]) -> bool: |
64 | 105 | for statement in body: |
65 | 106 | for node in ast.walk(statement): |
|
0 commit comments