Mercurial > cpython-lru_cache-weakref
annotate Lib/compiler/syntax.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 4572fb4dfbb3 |
| children | 4cdf21a0dbbc |
| rev | line source |
|---|---|
| 19939 | 1 """Check for errs in the AST. |
| 2 | |
| 3 The Python parser does not catch all syntax errors. Others, like | |
| 4 assignments with invalid targets, are caught in the code generation | |
| 5 phase. | |
| 6 | |
| 7 The compiler package catches some errors in the transformer module. | |
| 8 But it seems clearer to write checkers that use the AST to detect | |
| 9 errors. | |
| 10 """ | |
| 11 | |
| 12 from compiler import ast, walk | |
| 13 | |
| 14 def check(tree, multi=None): | |
| 15 v = SyntaxErrorChecker(multi) | |
| 16 walk(tree, v) | |
| 17 return v.errors | |
| 18 | |
| 19 class SyntaxErrorChecker: | |
| 20 """A visitor to find syntax errors in the AST.""" | |
| 21 | |
| 22 def __init__(self, multi=None): | |
| 23 """Create new visitor object. | |
| 24 | |
| 25 If optional argument multi is not None, then print messages | |
| 26 for each error rather than raising a SyntaxError for the | |
| 27 first. | |
| 28 """ | |
| 29 self.multi = multi | |
| 30 self.errors = 0 | |
| 31 | |
| 32 def error(self, node, msg): | |
| 33 self.errors = self.errors + 1 | |
| 34 if self.multi is not None: | |
| 35 print "%s:%s: %s" % (node.filename, node.lineno, msg) | |
| 36 else: | |
| 37 raise SyntaxError, "%s (%s:%s)" % (msg, node.filename, node.lineno) | |
| 38 | |
| 39 def visitAssign(self, node): | |
| 40 # the transformer module handles many of these | |
|
35947
4572fb4dfbb3
Stop looping to do nothing, just pass.
Neal Norwitz <nnorwitz@gmail.com>
parents:
19943
diff
changeset
|
41 pass |
|
4572fb4dfbb3
Stop looping to do nothing, just pass.
Neal Norwitz <nnorwitz@gmail.com>
parents:
19943
diff
changeset
|
42 ## for target in node.nodes: |
|
19943
6773dcfe34a3
Last set of change to get regression tests to pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19939
diff
changeset
|
43 ## if isinstance(target, ast.AssList): |
|
6773dcfe34a3
Last set of change to get regression tests to pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19939
diff
changeset
|
44 ## if target.lineno is None: |
|
6773dcfe34a3
Last set of change to get regression tests to pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19939
diff
changeset
|
45 ## target.lineno = node.lineno |
|
6773dcfe34a3
Last set of change to get regression tests to pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19939
diff
changeset
|
46 ## self.error(target, "can't assign to list comprehension") |
