[Python-checkins] r56387 - in sandbox/trunk/2to3: README fixes/fix_funcattrs.py tests/test_fixers.py

collin.winter python-checkins at python.org
Sun Jul 15 00:34:03 CEST 2007


Author: collin.winter
Date: Sun Jul 15 00:34:02 2007
New Revision: 56387

Added:
   sandbox/trunk/2to3/fixes/fix_funcattrs.py
Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/README
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add a fixer for function attributes (f.func_x -> f.__x__).


Modified: sandbox/trunk/2to3/README
==============================================================================
--- sandbox/trunk/2to3/README	(original)
+++ sandbox/trunk/2to3/README	Sun Jul 15 00:34:02 2007
@@ -45,6 +45,8 @@
 
 * **fix_filter** - changes filter(F, X) into list(filter(F, X)).
 
+* **fix_funcattrs** - fix function attribute names (f.func_x -> f.__x__).
+
 * **fix_has_key** - "d.has_key(x)" -> "x in d".
 
 * **fix_input** - "input()" -> "eval(input())" (PEP 3111).

Added: sandbox/trunk/2to3/fixes/fix_funcattrs.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_funcattrs.py	Sun Jul 15 00:34:02 2007
@@ -0,0 +1,19 @@
+"""Fix function attribute names (f.func_x -> f.__x__)."""
+# Author: Collin Winter
+
+# Local imports
+from fixes import basefix
+from fixes.util import Name
+
+
+class FixFuncattrs(basefix.BaseFix):
+    PATTERN = """
+    power< any trailer< '.' attr=('func_closure' | 'func_doc' | 'func_globals'
+                                  | 'func_name' | 'func_defaults' | 'func_code'
+                                  | 'func_dict') > any* >
+    """
+
+    def transform(self, node, results):
+        attr = results["attr"][0]
+        attr.replace(Name(("__%s__" % attr.value[5:]),
+                          prefix=attr.get_prefix()))

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Sun Jul 15 00:34:02 2007
@@ -1180,6 +1180,26 @@
         self.check(b, a)
 
 
+class Test_funcattrs(FixerTestCase):
+    fixer = "funcattrs"
+
+    attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
+
+    def test(self):
+        for attr in self.attrs:
+            b = "a.func_%s" % attr
+            a = "a.__%s__" % attr
+            self.check(b, a)
+
+    def test_unchanged(self):
+        for attr in self.attrs:
+            s = "foo(func_%s + 5)" % attr
+            self.check(s, s)
+
+            s = "f(foo.__%s__)" % attr
+            self.check(s, s)
+
+
 class Test_stringio(FixerTestCase):
     fixer = "stringio"
 


More information about the Python-checkins mailing list