-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgather_globals.py
More file actions
31 lines (24 loc) · 1.19 KB
/
gather_globals.py
File metadata and controls
31 lines (24 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Finds names that are global to a module, e.g., builtins, functions, and classes.
Any references to these identifiers need not be captured by a continuation because they're available globally within
the module. This optimization not only reduces the capture, but also deals with objects that pickle cannot handle in
the first place, e.g., module names.
When omitting captured variables, keep in mind that global names can be shadowed inside functions.
"""
# TODO(zhangwen): this optimization assumes that global variables cannot be mutated by functions.
import ast
import builtins
from typing import Set
from .util import find_variables_by_usage
def gather_global_names(mod: ast.Module) -> Set[str]:
"""Returns names of globals, whose values presumably don't change."""
names = set(dir(builtins)) # Start with the builtins.
for stmt in mod.body:
if isinstance(stmt, (ast.ClassDef, ast.FunctionDef)):
names.add(stmt.name)
elif isinstance(stmt, (ast.Import, ast.ImportFrom)):
for alias in stmt.names:
names.add(alias.asname or alias.name)
else:
names.update(find_variables_by_usage(stmt)[ast.Store])
return names