A visitor that checks if a name is accessed without being declared. This is different from the frame visitor as it will not stop at closure frames.
| 213 | |
| 214 | |
| 215 | class UndeclaredNameVisitor(NodeVisitor): |
| 216 | """A visitor that checks if a name is accessed without being |
| 217 | declared. This is different from the frame visitor as it will |
| 218 | not stop at closure frames. |
| 219 | """ |
| 220 | |
| 221 | def __init__(self, names): |
| 222 | self.names = set(names) |
| 223 | self.undeclared = set() |
| 224 | |
| 225 | def visit_Name(self, node): |
| 226 | if node.ctx == 'load' and node.name in self.names: |
| 227 | self.undeclared.add(node.name) |
| 228 | if self.undeclared == self.names: |
| 229 | raise VisitorExit() |
| 230 | else: |
| 231 | self.names.discard(node.name) |
| 232 | |
| 233 | def visit_Block(self, node): |
| 234 | """Stop visiting a blocks.""" |
| 235 | |
| 236 | |
| 237 | class CompilerExit(Exception): |