| 1 | n/a | """Interface to the compiler's internal symbol tables""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import _symtable |
|---|
| 4 | n/a | from _symtable import (USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM, |
|---|
| 5 | n/a | DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE, |
|---|
| 6 | n/a | LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL) |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | import weakref |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | __all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"] |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | def symtable(code, filename, compile_type): |
|---|
| 13 | n/a | top = _symtable.symtable(code, filename, compile_type) |
|---|
| 14 | n/a | return _newSymbolTable(top, filename) |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | class SymbolTableFactory: |
|---|
| 17 | n/a | def __init__(self): |
|---|
| 18 | n/a | self.__memo = weakref.WeakValueDictionary() |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | def new(self, table, filename): |
|---|
| 21 | n/a | if table.type == _symtable.TYPE_FUNCTION: |
|---|
| 22 | n/a | return Function(table, filename) |
|---|
| 23 | n/a | if table.type == _symtable.TYPE_CLASS: |
|---|
| 24 | n/a | return Class(table, filename) |
|---|
| 25 | n/a | return SymbolTable(table, filename) |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def __call__(self, table, filename): |
|---|
| 28 | n/a | key = table, filename |
|---|
| 29 | n/a | obj = self.__memo.get(key, None) |
|---|
| 30 | n/a | if obj is None: |
|---|
| 31 | n/a | obj = self.__memo[key] = self.new(table, filename) |
|---|
| 32 | n/a | return obj |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | _newSymbolTable = SymbolTableFactory() |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | class SymbolTable(object): |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def __init__(self, raw_table, filename): |
|---|
| 40 | n/a | self._table = raw_table |
|---|
| 41 | n/a | self._filename = filename |
|---|
| 42 | n/a | self._symbols = {} |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def __repr__(self): |
|---|
| 45 | n/a | if self.__class__ == SymbolTable: |
|---|
| 46 | n/a | kind = "" |
|---|
| 47 | n/a | else: |
|---|
| 48 | n/a | kind = "%s " % self.__class__.__name__ |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | if self._table.name == "global": |
|---|
| 51 | n/a | return "<{0}SymbolTable for module {1}>".format(kind, self._filename) |
|---|
| 52 | n/a | else: |
|---|
| 53 | n/a | return "<{0}SymbolTable for {1} in {2}>".format(kind, |
|---|
| 54 | n/a | self._table.name, |
|---|
| 55 | n/a | self._filename) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def get_type(self): |
|---|
| 58 | n/a | if self._table.type == _symtable.TYPE_MODULE: |
|---|
| 59 | n/a | return "module" |
|---|
| 60 | n/a | if self._table.type == _symtable.TYPE_FUNCTION: |
|---|
| 61 | n/a | return "function" |
|---|
| 62 | n/a | if self._table.type == _symtable.TYPE_CLASS: |
|---|
| 63 | n/a | return "class" |
|---|
| 64 | n/a | assert self._table.type in (1, 2, 3), \ |
|---|
| 65 | n/a | "unexpected type: {0}".format(self._table.type) |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | def get_id(self): |
|---|
| 68 | n/a | return self._table.id |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def get_name(self): |
|---|
| 71 | n/a | return self._table.name |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def get_lineno(self): |
|---|
| 74 | n/a | return self._table.lineno |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def is_optimized(self): |
|---|
| 77 | n/a | return bool(self._table.type == _symtable.TYPE_FUNCTION) |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def is_nested(self): |
|---|
| 80 | n/a | return bool(self._table.nested) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def has_children(self): |
|---|
| 83 | n/a | return bool(self._table.children) |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | def has_exec(self): |
|---|
| 86 | n/a | """Return true if the scope uses exec. Deprecated method.""" |
|---|
| 87 | n/a | return False |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | def get_identifiers(self): |
|---|
| 90 | n/a | return self._table.symbols.keys() |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | def lookup(self, name): |
|---|
| 93 | n/a | sym = self._symbols.get(name) |
|---|
| 94 | n/a | if sym is None: |
|---|
| 95 | n/a | flags = self._table.symbols[name] |
|---|
| 96 | n/a | namespaces = self.__check_children(name) |
|---|
| 97 | n/a | sym = self._symbols[name] = Symbol(name, flags, namespaces) |
|---|
| 98 | n/a | return sym |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | def get_symbols(self): |
|---|
| 101 | n/a | return [self.lookup(ident) for ident in self.get_identifiers()] |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | def __check_children(self, name): |
|---|
| 104 | n/a | return [_newSymbolTable(st, self._filename) |
|---|
| 105 | n/a | for st in self._table.children |
|---|
| 106 | n/a | if st.name == name] |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | def get_children(self): |
|---|
| 109 | n/a | return [_newSymbolTable(st, self._filename) |
|---|
| 110 | n/a | for st in self._table.children] |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | class Function(SymbolTable): |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | # Default values for instance variables |
|---|
| 116 | n/a | __params = None |
|---|
| 117 | n/a | __locals = None |
|---|
| 118 | n/a | __frees = None |
|---|
| 119 | n/a | __globals = None |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def __idents_matching(self, test_func): |
|---|
| 122 | n/a | return tuple([ident for ident in self.get_identifiers() |
|---|
| 123 | n/a | if test_func(self._table.symbols[ident])]) |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | def get_parameters(self): |
|---|
| 126 | n/a | if self.__params is None: |
|---|
| 127 | n/a | self.__params = self.__idents_matching(lambda x:x & DEF_PARAM) |
|---|
| 128 | n/a | return self.__params |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | def get_locals(self): |
|---|
| 131 | n/a | if self.__locals is None: |
|---|
| 132 | n/a | locs = (LOCAL, CELL) |
|---|
| 133 | n/a | test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs |
|---|
| 134 | n/a | self.__locals = self.__idents_matching(test) |
|---|
| 135 | n/a | return self.__locals |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | def get_globals(self): |
|---|
| 138 | n/a | if self.__globals is None: |
|---|
| 139 | n/a | glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT) |
|---|
| 140 | n/a | test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob |
|---|
| 141 | n/a | self.__globals = self.__idents_matching(test) |
|---|
| 142 | n/a | return self.__globals |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | def get_frees(self): |
|---|
| 145 | n/a | if self.__frees is None: |
|---|
| 146 | n/a | is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE |
|---|
| 147 | n/a | self.__frees = self.__idents_matching(is_free) |
|---|
| 148 | n/a | return self.__frees |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | class Class(SymbolTable): |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | __methods = None |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | def get_methods(self): |
|---|
| 156 | n/a | if self.__methods is None: |
|---|
| 157 | n/a | d = {} |
|---|
| 158 | n/a | for st in self._table.children: |
|---|
| 159 | n/a | d[st.name] = 1 |
|---|
| 160 | n/a | self.__methods = tuple(d) |
|---|
| 161 | n/a | return self.__methods |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | class Symbol(object): |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | def __init__(self, name, flags, namespaces=None): |
|---|
| 167 | n/a | self.__name = name |
|---|
| 168 | n/a | self.__flags = flags |
|---|
| 169 | n/a | self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope() |
|---|
| 170 | n/a | self.__namespaces = namespaces or () |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def __repr__(self): |
|---|
| 173 | n/a | return "<symbol {0!r}>".format(self.__name) |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | def get_name(self): |
|---|
| 176 | n/a | return self.__name |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | def is_referenced(self): |
|---|
| 179 | n/a | return bool(self.__flags & _symtable.USE) |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | def is_parameter(self): |
|---|
| 182 | n/a | return bool(self.__flags & DEF_PARAM) |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | def is_global(self): |
|---|
| 185 | n/a | return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)) |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | def is_declared_global(self): |
|---|
| 188 | n/a | return bool(self.__scope == GLOBAL_EXPLICIT) |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | def is_local(self): |
|---|
| 191 | n/a | return bool(self.__flags & DEF_BOUND) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | def is_annotated(self): |
|---|
| 194 | n/a | return bool(self.__flags & DEF_ANNOT) |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | def is_free(self): |
|---|
| 197 | n/a | return bool(self.__scope == FREE) |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | def is_imported(self): |
|---|
| 200 | n/a | return bool(self.__flags & DEF_IMPORT) |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | def is_assigned(self): |
|---|
| 203 | n/a | return bool(self.__flags & DEF_LOCAL) |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | def is_namespace(self): |
|---|
| 206 | n/a | """Returns true if name binding introduces new namespace. |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | If the name is used as the target of a function or class |
|---|
| 209 | n/a | statement, this will be true. |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | Note that a single name can be bound to multiple objects. If |
|---|
| 212 | n/a | is_namespace() is true, the name may also be bound to other |
|---|
| 213 | n/a | objects, like an int or list, that does not introduce a new |
|---|
| 214 | n/a | namespace. |
|---|
| 215 | n/a | """ |
|---|
| 216 | n/a | return bool(self.__namespaces) |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | def get_namespaces(self): |
|---|
| 219 | n/a | """Return a list of namespaces bound to this name""" |
|---|
| 220 | n/a | return self.__namespaces |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | def get_namespace(self): |
|---|
| 223 | n/a | """Returns the single namespace bound to this name. |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | Raises ValueError if the name is bound to multiple namespaces. |
|---|
| 226 | n/a | """ |
|---|
| 227 | n/a | if len(self.__namespaces) != 1: |
|---|
| 228 | n/a | raise ValueError("name is bound to multiple namespaces") |
|---|
| 229 | n/a | return self.__namespaces[0] |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | if __name__ == "__main__": |
|---|
| 232 | n/a | import os, sys |
|---|
| 233 | n/a | with open(sys.argv[0]) as f: |
|---|
| 234 | n/a | src = f.read() |
|---|
| 235 | n/a | mod = symtable(src, os.path.split(sys.argv[0])[1], "exec") |
|---|
| 236 | n/a | for ident in mod.get_identifiers(): |
|---|
| 237 | n/a | info = mod.lookup(ident) |
|---|
| 238 | n/a | print(info, info.is_local(), info.is_namespace()) |
|---|