Names a Python function body's OWN `import`/`from ... import` statements bind, not descending into nested `function_definition`/`class_definition`/ `lambda` subtrees (their imports belong to a different scope). A narrower, import-only analogue of `_python_local_bound_names`: kept se
(func_def_node, source: bytes)
| 3454 | |
| 3455 | |
| 3456 | def _python_local_import_names(func_def_node, source: bytes) -> frozenset[str]: |
| 3457 | """Names a Python function body's OWN `import`/`from ... import` statements |
| 3458 | bind, not descending into nested `function_definition`/`class_definition`/ |
| 3459 | `lambda` subtrees (their imports belong to a different scope). |
| 3460 | |
| 3461 | A narrower, import-only analogue of `_python_local_bound_names`: kept |
| 3462 | separate so the bare-call lexical resolution guard built on it (#5421) |
| 3463 | does not also start suppressing the parameter-shadowing case, which is a |
| 3464 | distinct, already-tracked issue (#5418). |
| 3465 | |
| 3466 | Memoized per (scope byte range) for the file being extracted, exactly like |
| 3467 | `_js_local_bound_names`. The guard asks for the ENCLOSING scope's set once |
| 3468 | per bare call site, so a function holding N call sites walked its whole body |
| 3469 | N times — quadratic in function size, and the dominant cost of a cold Python |
| 3470 | extraction (49 M walk frames, 38% of the per-file CPU on graphify's own |
| 3471 | source, concentrated in the handful of big files that set the pool's tail: |
| 3472 | one 318 KB module took 61 s of a 385 s serial run). Byte ranges are unique |
| 3473 | only within one file, so `_extract_generic` clears the memo per file. |
| 3474 | """ |
| 3475 | key = (func_def_node.start_byte, func_def_node.end_byte) |
| 3476 | cached = _PYTHON_LOCAL_IMPORT_CACHE.get(key) |
| 3477 | if cached is None: |
| 3478 | cached = frozenset(_python_local_import_names_uncached(func_def_node, source)) |
| 3479 | _PYTHON_LOCAL_IMPORT_CACHE[key] = cached |
| 3480 | return cached |
| 3481 | |
| 3482 | |
| 3483 | # (scope start, scope end) -> the names its own import statements bind, for the |