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