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

collin.winter python-checkins at python.org
Tue Mar 20 05:25:02 CET 2007


Author: collin.winter
Date: Tue Mar 20 05:24:56 2007
New Revision: 54455

Added:
   sandbox/trunk/2to3/fixes/fix_nonzero.py   (contents, props changed)
Modified:
   sandbox/trunk/2to3/README
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add a fixer for __nonzero__ methods.

Modified: sandbox/trunk/2to3/README
==============================================================================
--- sandbox/trunk/2to3/README	(original)
+++ sandbox/trunk/2to3/README	Tue Mar 20 05:24:56 2007
@@ -52,6 +52,8 @@
 
 * **fix_next** - fixer for it.next() -> next(it) (PEP 3114).
 
+* **fix_nonzero** - convert __nonzero__() methods to __bool__() methods.
+
 * **fix_print** - convert "print" statements to print() function calls.
 
 * **fix_raise** - convert "raise" statements to Python 3 syntax (PEP 3109).

Added: sandbox/trunk/2to3/fixes/fix_nonzero.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_nonzero.py	Tue Mar 20 05:24:56 2007
@@ -0,0 +1,27 @@
+"""Fixer for __nonzero__ -> __bool__ methods."""
+# Author: Collin Winter
+
+# Local imports
+import pytree
+from pgen2 import token
+from pygram import python_symbols as syms
+from fixes import basefix
+from fixes.macros import Name
+
+class FixNonzero(basefix.BaseFix):
+    PATTERN = """
+    classdef< 'class' any+ ':'
+              suite< any*
+                     funcdef< 'def' name='__nonzero__'
+                              parameters< '(' NAME ')' > any+ >
+                     any* > >
+    """
+    
+    def transform(self, node):
+        results = self.match(node)
+        assert results
+
+        name = results["name"]
+        new = Name("__bool__")
+        new.set_prefix(name.get_prefix())
+        name.replace(new)

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Tue Mar 20 05:24:56 2007
@@ -1638,6 +1638,58 @@
         b = """f(g().next + 5)"""
         a = """f(g().__next__ + 5)"""
         self.check(b, a)
+        
+class Test_nonzero(FixerTestCase):
+    fixer = "nonzero"
+        
+    def test_1(self):
+        b = """
+            class A:
+                def __nonzero__(self):
+                    pass
+            """
+        a = """
+            class A:
+                def __bool__(self):
+                    pass
+            """
+        self.check(b, a)
+        
+    def test_2(self):
+        b = """
+            class A(object):
+                def __nonzero__(self):
+                    pass
+            """
+        a = """
+            class A(object):
+                def __bool__(self):
+                    pass
+            """
+        self.check(b, a)
+        
+    def test_unchanged_1(self):
+        s = """
+            class A(object):
+                def __bool__(self):
+                    pass
+            """
+        self.check(s, s)
+        
+    def test_unchanged_2(self):
+        s = """
+            class A(object):
+                def __nonzero__(self, a):
+                    pass
+            """
+        self.check(s, s)
+        
+    def test_unchanged_func(self):
+        s = """
+            def __nonzero__(self):
+                pass
+            """
+        self.check(s, s)
 
 
 if __name__ == "__main__":


More information about the Python-checkins mailing list