| Georg Brandl | 765812f | 2008-08-16 22:37:05 +0000 | [diff] [blame] | 1 | :mod:`symtable` --- Access to the compiler's symbol tables | 
|  | 2 | ========================================================== | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 3 |  | 
|  | 4 | .. module:: symtable | 
|  | 5 | :synopsis: Interface to the compiler's internal symbol tables. | 
|  | 6 |  | 
| Georg Brandl | c066105 | 2014-10-02 08:38:39 +0200 | [diff] [blame] | 7 | **Source code:** :source:`Lib/symtable.py` | 
|  | 8 |  | 
|  | 9 | -------------- | 
|  | 10 |  | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 11 | .. moduleauthor:: Jeremy Hylton <jeremy@alum.mit.edu> | 
| Benjamin Peterson | c84ebe7 | 2009-01-19 16:18:27 +0000 | [diff] [blame] | 12 | .. sectionauthor:: Benjamin Peterson <benjamin@python.org> | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 13 |  | 
|  | 14 |  | 
|  | 15 | Symbol tables are generated by the compiler from AST just before bytecode is | 
|  | 16 | generated.  The symbol table is responsible for calculating the scope of every | 
|  | 17 | identifier in the code.  :mod:`symtable` provides an interface to examine these | 
|  | 18 | tables. | 
|  | 19 |  | 
|  | 20 |  | 
|  | 21 | Generating Symbol Tables | 
|  | 22 | ------------------------ | 
|  | 23 |  | 
|  | 24 | .. function:: symtable(code, filename, compile_type) | 
|  | 25 |  | 
| Georg Brandl | 765812f | 2008-08-16 22:37:05 +0000 | [diff] [blame] | 26 | Return the toplevel :class:`SymbolTable` for the Python source *code*. | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 27 | *filename* is the name of the file containing the code.  *compile_type* is | 
|  | 28 | like the *mode* argument to :func:`compile`. | 
|  | 29 |  | 
|  | 30 |  | 
|  | 31 | Examining Symbol Tables | 
|  | 32 | ----------------------- | 
|  | 33 |  | 
|  | 34 | .. class:: SymbolTable | 
|  | 35 |  | 
|  | 36 | A namespace table for a block.  The constructor is not public. | 
|  | 37 |  | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 38 | .. method:: get_type() | 
|  | 39 |  | 
|  | 40 | Return the type of the symbol table.  Possible values are ``'class'``, | 
|  | 41 | ``'module'``, and ``'function'``. | 
|  | 42 |  | 
|  | 43 | .. method:: get_id() | 
|  | 44 |  | 
|  | 45 | Return the table's identifier. | 
|  | 46 |  | 
|  | 47 | .. method:: get_name() | 
|  | 48 |  | 
|  | 49 | Return the table's name.  This is the name of the class if the table is | 
|  | 50 | for a class, the name of the function if the table is for a function, or | 
| Benjamin Peterson | e3444c8 | 2008-08-17 01:17:15 +0000 | [diff] [blame] | 51 | ``'top'`` if the table is global (:meth:`get_type` returns ``'module'``). | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 52 |  | 
|  | 53 | .. method:: get_lineno() | 
|  | 54 |  | 
|  | 55 | Return the number of the first line in the block this table represents. | 
|  | 56 |  | 
|  | 57 | .. method:: is_optimized() | 
|  | 58 |  | 
|  | 59 | Return ``True`` if the locals in this table can be optimized. | 
|  | 60 |  | 
|  | 61 | .. method:: is_nested() | 
|  | 62 |  | 
| Benjamin Peterson | e3444c8 | 2008-08-17 01:17:15 +0000 | [diff] [blame] | 63 | Return ``True`` if the block is a nested class or function. | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 64 |  | 
|  | 65 | .. method:: has_children() | 
|  | 66 |  | 
|  | 67 | Return ``True`` if the block has nested namespaces within it.  These can | 
|  | 68 | be obtained with :meth:`get_children`. | 
|  | 69 |  | 
|  | 70 | .. method:: has_exec() | 
|  | 71 |  | 
|  | 72 | Return ``True`` if the block uses ``exec``. | 
|  | 73 |  | 
| Benjamin Peterson | a85bd06 | 2010-05-20 22:23:37 +0000 | [diff] [blame] | 74 | .. method:: has_import_star() | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 75 |  | 
| Georg Brandl | 765812f | 2008-08-16 22:37:05 +0000 | [diff] [blame] | 76 | Return ``True`` if the block uses a starred from-import. | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 77 |  | 
|  | 78 | .. method:: get_identifiers() | 
|  | 79 |  | 
|  | 80 | Return a list of names of symbols in this table. | 
|  | 81 |  | 
|  | 82 | .. method:: lookup(name) | 
|  | 83 |  | 
|  | 84 | Lookup *name* in the table and return a :class:`Symbol` instance. | 
|  | 85 |  | 
|  | 86 | .. method:: get_symbols() | 
|  | 87 |  | 
|  | 88 | Return a list of :class:`Symbol` instances for names in the table. | 
|  | 89 |  | 
|  | 90 | .. method:: get_children() | 
|  | 91 |  | 
|  | 92 | Return a list of the nested symbol tables. | 
|  | 93 |  | 
|  | 94 |  | 
|  | 95 | .. class:: Function | 
|  | 96 |  | 
|  | 97 | A namespace for a function or method.  This class inherits | 
|  | 98 | :class:`SymbolTable`. | 
|  | 99 |  | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 100 | .. method:: get_parameters() | 
|  | 101 |  | 
|  | 102 | Return a tuple containing names of parameters to this function. | 
|  | 103 |  | 
|  | 104 | .. method:: get_locals() | 
|  | 105 |  | 
|  | 106 | Return a tuple containing names of locals in this function. | 
|  | 107 |  | 
|  | 108 | .. method:: get_globals() | 
|  | 109 |  | 
|  | 110 | Return a tuple containing names of globals in this function. | 
|  | 111 |  | 
|  | 112 | .. method:: get_frees() | 
|  | 113 |  | 
|  | 114 | Return a tuple containing names of free variables in this function. | 
|  | 115 |  | 
|  | 116 |  | 
|  | 117 | .. class:: Class | 
|  | 118 |  | 
|  | 119 | A namespace of a class.  This class inherits :class:`SymbolTable`. | 
|  | 120 |  | 
|  | 121 | .. method:: get_methods() | 
|  | 122 |  | 
|  | 123 | Return a tuple containing the names of methods declared in the class. | 
|  | 124 |  | 
|  | 125 |  | 
|  | 126 | .. class:: Symbol | 
|  | 127 |  | 
| Georg Brandl | 765812f | 2008-08-16 22:37:05 +0000 | [diff] [blame] | 128 | An entry in a :class:`SymbolTable` corresponding to an identifier in the | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 129 | source.  The constructor is not public. | 
|  | 130 |  | 
|  | 131 | .. method:: get_name() | 
|  | 132 |  | 
|  | 133 | Return the symbol's name. | 
|  | 134 |  | 
|  | 135 | .. method:: is_referenced() | 
|  | 136 |  | 
|  | 137 | Return ``True`` if the symbol is used in its block. | 
|  | 138 |  | 
|  | 139 | .. method:: is_imported() | 
|  | 140 |  | 
|  | 141 | Return ``True`` if the symbol is created from an import statement. | 
|  | 142 |  | 
|  | 143 | .. method:: is_parameter() | 
|  | 144 |  | 
|  | 145 | Return ``True`` if the symbol is a parameter. | 
|  | 146 |  | 
|  | 147 | .. method:: is_global() | 
|  | 148 |  | 
|  | 149 | Return ``True`` if the symbol is global. | 
|  | 150 |  | 
| Benjamin Peterson | f36bebd | 2009-06-26 23:37:06 +0000 | [diff] [blame] | 151 | .. method:: is_declared_global() | 
|  | 152 |  | 
|  | 153 | Return ``True`` if the symbol is declared global with a global statement. | 
|  | 154 |  | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 155 | .. method:: is_local() | 
|  | 156 |  | 
|  | 157 | Return ``True`` if the symbol is local to its block. | 
|  | 158 |  | 
|  | 159 | .. method:: is_free() | 
|  | 160 |  | 
| Georg Brandl | 765812f | 2008-08-16 22:37:05 +0000 | [diff] [blame] | 161 | Return ``True`` if the symbol is referenced in its block, but not assigned | 
|  | 162 | to. | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 163 |  | 
|  | 164 | .. method:: is_assigned() | 
|  | 165 |  | 
|  | 166 | Return ``True`` if the symbol is assigned to in its block. | 
|  | 167 |  | 
|  | 168 | .. method:: is_namespace() | 
|  | 169 |  | 
|  | 170 | Return ``True`` if name binding introduces new namespace. | 
|  | 171 |  | 
|  | 172 | If the name is used as the target of a function or class statement, this | 
|  | 173 | will be true. | 
|  | 174 |  | 
| Benjamin Peterson | c51ec0a | 2009-03-05 00:17:57 +0000 | [diff] [blame] | 175 | For example:: | 
|  | 176 |  | 
|  | 177 | >>> table = symtable.symtable("def some_func(): pass", "string", "exec") | 
|  | 178 | >>> table.lookup("some_func").is_namespace() | 
|  | 179 | True | 
|  | 180 |  | 
| Benjamin Peterson | 1e296cc | 2008-08-16 21:04:16 +0000 | [diff] [blame] | 181 | Note that a single name can be bound to multiple objects.  If the result | 
|  | 182 | is ``True``, the name may also be bound to other objects, like an int or | 
|  | 183 | list, that does not introduce a new namespace. | 
|  | 184 |  | 
|  | 185 | .. method:: get_namespaces() | 
|  | 186 |  | 
|  | 187 | Return a list of namespaces bound to this name. | 
|  | 188 |  | 
|  | 189 | .. method:: get_namespace() | 
|  | 190 |  | 
|  | 191 | Return the namespace bound to this name.  If more than one namespace is | 
|  | 192 | bound, a :exc:`ValueError` is raised. |