[Python-checkins] r65968 - in sandbox/trunk/2to3: README example.py lib2to3/fixes/fix_sys_exc.py lib2to3/tests/test_fixers.py

benjamin.peterson python-checkins at python.org
Fri Aug 22 01:45:14 CEST 2008


Author: benjamin.peterson
Date: Fri Aug 22 01:45:13 2008
New Revision: 65968

Log:
add a fixer for sys.exc_info etc by Jeff Balogh #2357

Added:
   sandbox/trunk/2to3/lib2to3/fixes/fix_sys_exc.py   (contents, props changed)
Modified:
   sandbox/trunk/2to3/README
   sandbox/trunk/2to3/example.py
   sandbox/trunk/2to3/lib2to3/tests/test_fixers.py

Modified: sandbox/trunk/2to3/README
==============================================================================
--- sandbox/trunk/2to3/README	(original)
+++ sandbox/trunk/2to3/README	Fri Aug 22 01:45:13 2008
@@ -100,6 +100,9 @@
 
 * **fix_standarderror** - StandardError -> Exception.
 
+* **fix_sys_exc** - Converts * **"sys.exc_info", "sys.exc_type", and
+  "sys.exc_value" to sys.exc_info()
+
 * **fix_throw** - fix generator.throw() calls to be 3.0-compliant (PEP 3109).
 
 * **fix_tuple_params** - remove tuple parameters from function, method and

Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py	(original)
+++ sandbox/trunk/2to3/example.py	Fri Aug 22 01:45:13 2008
@@ -362,6 +362,8 @@
 def buffer_examples():
     x = buffer(y)
 
+def sys_exc_examples():
+    print sys.exc_type, sys.exc_value, sys.exc_traceback
 
 class X:
     def maximum(self):

Added: sandbox/trunk/2to3/lib2to3/fixes/fix_sys_exc.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_sys_exc.py	Fri Aug 22 01:45:13 2008
@@ -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())

Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py	Fri Aug 22 01:45:13 2008
@@ -3343,6 +3343,39 @@
         """
         self.check_both(b, a)
 
+class Test_sys_exc(FixerTestCase):
+    fixer = "sys_exc"
+
+    def test_0(self):
+        b = "sys.exc_type"
+        a = "sys.exc_info()[0]"
+        self.check(b, a)
+
+    def test_1(self):
+        b = "sys.exc_value"
+        a = "sys.exc_info()[1]"
+        self.check(b, a)
+
+    def test_2(self):
+        b = "sys.exc_traceback"
+        a = "sys.exc_info()[2]"
+        self.check(b, a)
+
+    def test_3(self):
+        b = "sys.exc_type # Foo"
+        a = "sys.exc_info()[0] # Foo"
+        self.check(b, a)
+
+    def test_4(self):
+        b = "sys.  exc_type"
+        a = "sys.  exc_info()[0]"
+        self.check(b, a)
+
+    def test_5(self):
+        b = "sys  .exc_type"
+        a = "sys  .exc_info()[0]"
+        self.check(b, a)
+
 
 if __name__ == "__main__":
     import __main__


More information about the Python-checkins mailing list