summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_compiler.py
diff options
context:
space:
mode:
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")