summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/fixes
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2008-09-01 19:56:06 (GMT)
committerBenjamin Peterson <benjamin@python.org>2008-09-01 19:56:06 (GMT)
commitcf60382420f849881c3e1e9d691d9da2d4e95bc7 (patch)
treefb6dcd7d4a7c3d92e5735dff0615eef5a87dafae /Lib/lib2to3/fixes
parent2cb598f13101f5997755e15251d41f64da5905bf (diff)
downloadcpython-cf60382420f849881c3e1e9d691d9da2d4e95bc7.zip
cpython-cf60382420f849881c3e1e9d691d9da2d4e95bc7.tar.gz
cpython-cf60382420f849881c3e1e9d691d9da2d4e95bc7.tar.bz2
Merged revisions 66117 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ................ r66117 | benjamin.peterson | 2008-09-01 12:17:22 -0500 (Mon, 01 Sep 2008) | 25 lines Merged revisions 65887,65889,65967-65968,65981 via svnmerge from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r65887 | benjamin.peterson | 2008-08-19 17:45:04 -0500 (Tue, 19 Aug 2008) | 1 line allow the raw_input fixer to handle calls after the raw_input (ie. raw_input().split()) ........ r65889 | benjamin.peterson | 2008-08-19 18:11:03 -0500 (Tue, 19 Aug 2008) | 1 line no need for 2.4 compatibility now ........ r65967 | benjamin.peterson | 2008-08-21 18:43:37 -0500 (Thu, 21 Aug 2008) | 1 line allow a Call to have no arguments ........ r65968 | benjamin.peterson | 2008-08-21 18:45:13 -0500 (Thu, 21 Aug 2008) | 1 line add a fixer for sys.exc_info etc by Jeff Balogh #2357 ........ r65981 | benjamin.peterson | 2008-08-22 15:41:30 -0500 (Fri, 22 Aug 2008) | 1 line add a fixer to add parenthese for list and gen comps #2367 ........ ................
Diffstat (limited to 'Lib/lib2to3/fixes')
-rw-r--r--Lib/lib2to3/fixes/fix_paren.py42
-rw-r--r--Lib/lib2to3/fixes/fix_raw_input.py2
-rw-r--r--Lib/lib2to3/fixes/fix_sys_exc.py29
3 files changed, 72 insertions, 1 deletions
diff --git a/Lib/lib2to3/fixes/fix_paren.py b/Lib/lib2to3/fixes/fix_paren.py
new file mode 100644
index 0000000..0b72bb1
--- /dev/null
+++ b/Lib/lib2to3/fixes/fix_paren.py
@@ -0,0 +1,42 @@
+"""Fixer that addes parentheses where they are required
+
+This converts ``[x for x in 1, 2]`` to ``[x for x in (1, 2)]``."""
+
+# By Taek Joo Kim and Benjamin Peterson
+
+# Local imports
+from .. import fixer_base
+from ..fixer_util import LParen, RParen
+
+# XXX This doesn't support nested for loops like [x for x in 1, 2 for x in 1, 2]
+class FixParen(fixer_base.BaseFix):
+ PATTERN = """
+ atom< ('[' | '(')
+ (listmaker< any
+ comp_for<
+ 'for' NAME 'in'
+ target=testlist_safe< any (',' any)+ [',']
+ >
+ [any]
+ >
+ >
+ |
+ testlist_gexp< any
+ comp_for<
+ 'for' NAME 'in'
+ target=testlist_safe< any (',' any)+ [',']
+ >
+ [any]
+ >
+ >)
+ (']' | ')') >
+ """
+
+ def transform(self, node, results):
+ target = results["target"]
+
+ lparen = LParen()
+ lparen.set_prefix(target.get_prefix())
+ target.set_prefix("") # Make it hug the parentheses
+ target.insert_child(0, lparen)
+ target.append_child(RParen())
diff --git a/Lib/lib2to3/fixes/fix_raw_input.py b/Lib/lib2to3/fixes/fix_raw_input.py
index d9130d0..53e7a32 100644
--- a/Lib/lib2to3/fixes/fix_raw_input.py
+++ b/Lib/lib2to3/fixes/fix_raw_input.py
@@ -8,7 +8,7 @@ from ..fixer_util import Name
class FixRawInput(fixer_base.BaseFix):
PATTERN = """
- power< name='raw_input' trailer< '(' [any] ')' > >
+ power< name='raw_input' trailer< '(' [any] ')' > any* >
"""
def transform(self, node, results):
diff --git a/Lib/lib2to3/fixes/fix_sys_exc.py b/Lib/lib2to3/fixes/fix_sys_exc.py
new file mode 100644
index 0000000..18d9363
--- /dev/null
+++ b/Lib/lib2to3/fixes/fix_sys_exc.py
@@ -0,0 +1,29 @@
+"""Fixer for sys.exc_{type, value, traceback}
+
+sys.exc_type -> sys.exc_info()[0]
+sys.exc_value -> sys.exc_info()[1]
+sys.exc_traceback -> sys.exc_info()[2]
+"""
+
+# By Jeff Balogh and Benjamin Peterson
+
+# Local imports
+from .. import fixer_base
+from ..fixer_util import Attr, Call, Name, Number, Subscript, Node, syms
+
+class FixSysExc(fixer_base.BaseFix):
+ # This order matches the ordering of sys.exc_info().
+ exc_info = ["exc_type", "exc_value", "exc_traceback"]
+ PATTERN = """
+ power< 'sys' trailer< dot='.' attribute=(%s) > >
+ """ % '|'.join("'%s'" % e for e in exc_info)
+
+ def transform(self, node, results):
+ sys_attr = results["attribute"][0]
+ index = Number(self.exc_info.index(sys_attr.value))
+
+ call = Call(Name("exc_info"), prefix=sys_attr.get_prefix())
+ attr = Attr(Name("sys"), call)
+ attr[1].children[0].set_prefix(results["dot"].get_prefix())
+ attr.append(Subscript(index))
+ return Node(syms.power, attr, prefix=node.get_prefix())