[Python-checkins] r82383 - in python/branches/py3k/Demo/parser: test_unparse.py unparse.py
mark.dickinson
python-checkins at python.org
Wed Jun 30 10:32:11 CEST 2010
Author: mark.dickinson
Date: Wed Jun 30 10:32:11 2010
New Revision: 82383
Log:
Collapse else: if: ... into elif:
Modified:
python/branches/py3k/Demo/parser/test_unparse.py
python/branches/py3k/Demo/parser/unparse.py
Modified: python/branches/py3k/Demo/parser/test_unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/test_unparse.py (original)
+++ python/branches/py3k/Demo/parser/test_unparse.py Wed Jun 30 10:32:11 2010
@@ -64,6 +64,24 @@
class Foo: pass
"""
+elif1 = """\
+if cond1:
+ suite1
+elif cond2:
+ suite2
+else:
+ suite3
+"""
+
+elif2 = """\
+if cond1:
+ suite1
+elif cond2:
+ suite2
+"""
+
+
+
class ASTTestCase(unittest.TestCase):
def assertASTEqual(self, ast1, ast2):
self.assertEqual(ast.dump(ast1), ast.dump(ast2))
@@ -159,6 +177,10 @@
def test_class_definition(self):
self.check_roundtrip("class A(metaclass=type, *[], **{}): pass")
+ def test_elifs(self):
+ self.check_roundtrip(elif1)
+ self.check_roundtrip(elif2)
+
class DirectoryTestCase(ASTTestCase):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
Modified: python/branches/py3k/Demo/parser/unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/unparse.py (original)
+++ python/branches/py3k/Demo/parser/unparse.py Wed Jun 30 10:32:11 2010
@@ -256,9 +256,18 @@
self.fill("if ")
self.dispatch(t.test)
self.enter()
- # XXX elif?
self.dispatch(t.body)
self.leave()
+ # collapse nested ifs into equivalent elifs.
+ while (t.orelse and len(t.orelse) == 1 and
+ isinstance(t.orelse[0], ast.If)):
+ t = t.orelse[0]
+ self.fill("elif ")
+ self.dispatch(t.test)
+ self.enter()
+ self.dispatch(t.body)
+ self.leave()
+ # final else
if t.orelse:
self.fill("else")
self.enter()
More information about the Python-checkins
mailing list