summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_compiler.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2004-09-07 15:28:01 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2004-09-07 15:28:01 (GMT)
commit566d934745378df72fbbf7d3f834e7d08f7e42d1 (patch)
tree495591bd3b0dfcc198f36c5a904dae9cac617036 /Lib/test/test_compiler.py
parent2ad68e69b975b1714d3da1cd5e419c8294a5f2c1 (diff)
downloadcpython-566d934745378df72fbbf7d3f834e7d08f7e42d1.zip
cpython-566d934745378df72fbbf7d3f834e7d08f7e42d1.tar.gz
cpython-566d934745378df72fbbf7d3f834e7d08f7e42d1.tar.bz2
compiler.transformer: correct lineno attribute when possible
SF patch #1015989 The basic idea of this patch is to compute lineno attributes for all AST nodes. The actual implementation lead to a lot of restructing and code cleanup. The generated AST nodes now have an optional lineno argument to constructor. Remove the top-level asList(), since it didn't seem to serve any purpose. Add an __iter__ to ast nodes. Use isinstance() instead of explicit type tests. Change transformer to use the new lineno attribute, which replaces three lines of code with one. Use universal newlines so that we can get rid of special-case code for line endings. Use lookup_node() in a few more frequently called, but simple com_xxx methods(). Change string exception to class exception.
Diffstat (limited to 'Lib/test/test_compiler.py')
-rw-r--r--Lib/test/test_compiler.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/Lib/test/test_compiler.py b/Lib/test/test_compiler.py
index 63cbc00..1d77318 100644
--- a/Lib/test/test_compiler.py
+++ b/Lib/test/test_compiler.py
@@ -33,6 +33,65 @@ class CompilerTest(unittest.TestCase):
else:
compiler.compile(buf, basename, "exec")
+ def testLineNo(self):
+ # Test that all nodes except Module have a correct lineno attribute.
+ filename = __file__
+ if filename.endswith(".pyc") or filename.endswith(".pyo"):
+ filename = filename[:-1]
+ tree = compiler.parseFile(filename)
+ self.check_lineno(tree)
+
+ def check_lineno(self, node):
+ try:
+ self._check_lineno(node)
+ except AssertionError:
+ print node.__class__, node.lineno
+ raise
+
+ def _check_lineno(self, node):
+ if not node.__class__ in NOLINENO:
+ self.assert_(isinstance(node.lineno, int),
+ "lineno=%s on %s" % (node.lineno, node.__class__))
+ self.assert_(node.lineno > 0,
+ "lineno=%s on %s" % (node.lineno, node.__class__))
+ for child in node.getChildNodes():
+ self.check_lineno(child)
+
+NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
+
+###############################################################################
+# code below is just used to trigger some possible errors, for the benefit of
+# testLineNo
+###############################################################################
+
+class Toto:
+ """docstring"""
+ pass
+
+a, b = 2, 3
+[c, d] = 5, 6
+l = [(x, y) for x, y in zip(range(5), range(5,10))]
+l[0]
+l[3:4]
+if l:
+ pass
+else:
+ a, b = b, a
+
+try:
+ print yo
+except:
+ yo = 3
+else:
+ yo += 3
+
+try:
+ a += b
+finally:
+ b = 0
+
+###############################################################################
+
def test_main():
global TEST_ALL
TEST_ALL = test.test_support.is_resource_enabled("compiler")