]> granicus.if.org Git - python/commitdiff
Merged revisions 77102 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Tue, 29 Dec 2009 00:06:20 +0000 (00:06 +0000)
committerBenjamin Peterson <benjamin@python.org>
Tue, 29 Dec 2009 00:06:20 +0000 (00:06 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

................
  r77102 | benjamin.peterson | 2009-12-28 17:50:41 -0600 (Mon, 28 Dec 2009) | 50 lines

  Merged revisions 76871-76872,77093-77095,77097-77101 via svnmerge from
  svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3

  ........
    r76871 | benjamin.peterson | 2009-12-17 20:49:21 -0600 (Thu, 17 Dec 2009) | 1 line

    handle unencodable diffs gracefully #5093
  ........
    r76872 | benjamin.peterson | 2009-12-17 20:51:37 -0600 (Thu, 17 Dec 2009) | 1 line

    fix emacs header
  ........
    r77093 | benjamin.peterson | 2009-12-28 14:43:32 -0600 (Mon, 28 Dec 2009) | 7 lines

    replace callable(x) with isinstance(x, collections.Callable) #7006

    This is a more accurate translation than hasattr(x, '__call__') which failed in
    the case that somebody had put __call__ in the instance dictionary.

    Patch mostly by Joe Amenta.
  ........
    r77094 | benjamin.peterson | 2009-12-28 14:45:13 -0600 (Mon, 28 Dec 2009) | 2 lines

    deuglify imports
  ........
    r77095 | benjamin.peterson | 2009-12-28 14:49:23 -0600 (Mon, 28 Dec 2009) | 1 line

    remove unused flag
  ........
    r77097 | benjamin.peterson | 2009-12-28 16:12:13 -0600 (Mon, 28 Dec 2009) | 2 lines

    clean up imports and whitespace
  ........
    r77098 | benjamin.peterson | 2009-12-28 16:43:35 -0600 (Mon, 28 Dec 2009) | 1 line

    *** empty log message ***
  ........
    r77099 | benjamin.peterson | 2009-12-28 16:45:10 -0600 (Mon, 28 Dec 2009) | 1 line

    revert unintended change
  ........
    r77100 | benjamin.peterson | 2009-12-28 16:53:21 -0600 (Mon, 28 Dec 2009) | 1 line

    revert unintended changes
  ........
    r77101 | benjamin.peterson | 2009-12-28 17:46:02 -0600 (Mon, 28 Dec 2009) | 1 line

    normalize whitespace
  ........
................

Lib/lib2to3/fixer_util.py
Lib/lib2to3/fixes/fix_callable.py
Lib/lib2to3/main.py
Lib/lib2to3/tests/test_all_fixers.py
Lib/lib2to3/tests/test_fixers.py
Lib/lib2to3/tests/test_main.py [new file with mode: 0644]

index 5c0a08858d9ef594b8177bcbd925b64d2d7baedd..e6f7cdff80114641920059f6d08c4210940bf5da 100644 (file)
@@ -291,8 +291,6 @@ def touch_import(package, name, node):
     if does_tree_import(package, name, root):
         return
 
-    add_newline_before = False
-
     # figure out where to insert the new import.  First try to find
     # the first import and then skip to the last one.
     insert_pos = offset = 0
@@ -312,7 +310,6 @@ def touch_import(package, name, node):
             if node.type == syms.simple_stmt and node.children and \
                node.children[0].type == token.STRING:
                 insert_pos = idx + 1
-                add_newline_before
                 break
 
     if package is None:
@@ -324,8 +321,6 @@ def touch_import(package, name, node):
         import_ = FromImport(package, [Leaf(token.NAME, name, prefix=' ')])
 
     children = [import_, Newline()]
-    if add_newline_before:
-        children.insert(0, Newline())
     root.insert_child(insert_pos, Node(syms.simple_stmt, children))
 
 
index 831b91358a92169e8fb488a78f23de595c0902c9..ed1cb373c0f245b9e4e1664a1602467d4f1b597c 100644 (file)
@@ -3,12 +3,12 @@
 
 """Fixer for callable().
 
-This converts callable(obj) into hasattr(obj, '__call__')."""
+This converts callable(obj) into isinstance(obj, collections.Callable), adding a
+collections import if needed."""
 
 # Local imports
-from .. import pytree
-from .. import fixer_base
-from ..fixer_util import Call, Name, String
+from lib2to3 import fixer_base
+from lib2to3.fixer_util import Call, Name, String, Attr, touch_import
 
 class FixCallable(fixer_base.BaseFix):
 
@@ -25,7 +25,10 @@ class FixCallable(fixer_base.BaseFix):
     """
 
     def transform(self, node, results):
-        func = results["func"]
+        func = results['func']
 
-        args = [func.clone(), String(', '), String("'__call__'")]
-        return Call(Name("hasattr"), args, prefix=node.prefix)
+        touch_import(None, 'collections', node=node)
+
+        args = [func.clone(), String(', ')]
+        args.extend(Attr(Name('collections'), Name('Callable')))
+        return Call(Name('isinstance'), args, prefix=node.prefix)
index 6e096933efc9cf26ed3a5bb56fa5ea3b2984e51b..6c57e67951ee5ee35eb0a0a508f04fe47a8f38f0 100644 (file)
@@ -60,8 +60,14 @@ class StdoutRefactoringTool(refactor.MultiprocessRefactoringTool):
         else:
             self.log_message("Refactored %s", filename)
             if self.show_diffs:
-                for line in diff_texts(old, new, filename):
-                    print(line)
+                diff_lines = diff_texts(old, new, filename)
+                try:
+                    for line in diff_lines:
+                        print(line)
+                except UnicodeEncodeError:
+                    warn("couldn't encode %s's diff for your terminal" %
+                         (filename,))
+                    return
 
 def warn(msg):
     print("WARNING: %s" % (msg,), file=sys.stderr)
index 61bcc542575ed8c73551f111eb9b66453c2479b0..f64b3d942af2f91e3791247436e020c33c111e1b 100644 (file)
@@ -9,12 +9,12 @@ running time.
 import unittest
 
 # Local imports
-from .. import pytree
-from .. import refactor
+from lib2to3 import refactor
 from . import support
 
 
 class Test_all(support.TestCase):
+
     def setUp(self):
         self.refactor = support.get_refactorer()
 
index ea361dec1f23edb63f78b52e8cf786e3bcde224d..2e0092fabdb935393065d611fb4e5af848ff593b 100755 (executable)
@@ -2725,16 +2725,79 @@ class Test_callable(FixerTestCase):
 
     def test_prefix_preservation(self):
         b = """callable(    x)"""
-        a = """hasattr(    x, '__call__')"""
+        a = """import collections\nisinstance(    x, collections.Callable)"""
         self.check(b, a)
 
         b = """if     callable(x): pass"""
-        a = """if     hasattr(x, '__call__'): pass"""
+        a = """import collections
+if     isinstance(x, collections.Callable): pass"""
         self.check(b, a)
 
     def test_callable_call(self):
         b = """callable(x)"""
-        a = """hasattr(x, '__call__')"""
+        a = """import collections\nisinstance(x, collections.Callable)"""
+        self.check(b, a)
+
+    def test_global_import(self):
+        b = """
+def spam(foo):
+    callable(foo)"""[1:]
+        a = """
+import collections
+def spam(foo):
+    isinstance(foo, collections.Callable)"""[1:]
+        self.check(b, a)
+
+        b = """
+import collections
+def spam(foo):
+    callable(foo)"""[1:]
+        # same output if it was already imported
+        self.check(b, a)
+
+        b = """
+from collections import *
+def spam(foo):
+    callable(foo)"""[1:]
+        a = """
+from collections import *
+import collections
+def spam(foo):
+    isinstance(foo, collections.Callable)"""[1:]
+        self.check(b, a)
+
+        b = """
+do_stuff()
+do_some_other_stuff()
+assert callable(do_stuff)"""[1:]
+        a = """
+import collections
+do_stuff()
+do_some_other_stuff()
+assert isinstance(do_stuff, collections.Callable)"""[1:]
+        self.check(b, a)
+
+        b = """
+if isinstance(do_stuff, Callable):
+    assert callable(do_stuff)
+    do_stuff(do_stuff)
+    if not callable(do_stuff):
+        exit(1)
+    else:
+        assert callable(do_stuff)
+else:
+    assert not callable(do_stuff)"""[1:]
+        a = """
+import collections
+if isinstance(do_stuff, Callable):
+    assert isinstance(do_stuff, collections.Callable)
+    do_stuff(do_stuff)
+    if not isinstance(do_stuff, collections.Callable):
+        exit(1)
+    else:
+        assert isinstance(do_stuff, collections.Callable)
+else:
+    assert not isinstance(do_stuff, collections.Callable)"""[1:]
         self.check(b, a)
 
     def test_callable_should_not_change(self):
diff --git a/Lib/lib2to3/tests/test_main.py b/Lib/lib2to3/tests/test_main.py
new file mode 100644 (file)
index 0000000..23d1b56
--- /dev/null
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+import sys
+import codecs
+import io
+import unittest
+
+from lib2to3 import main
+
+
+class TestMain(unittest.TestCase):
+
+    def run_2to3_capture(self, args, in_capture, out_capture, err_capture):
+        save_stdin = sys.stdin
+        save_stdout = sys.stdout
+        save_stderr = sys.stderr
+        sys.stdin = in_capture
+        sys.stdout = out_capture
+        sys.stderr = err_capture
+        try:
+            return main.main("lib2to3.fixes", args)
+        finally:
+            sys.stdin = save_stdin
+            sys.stdout = save_stdout
+            sys.stderr = save_stderr
+
+    def test_unencodable_diff(self):
+        input_stream = io.StringIO("print 'nothing'\nprint u'über'\n")
+        out = io.StringIO()
+        out_enc = codecs.getwriter("ascii")(out)
+        err = io.StringIO()
+        ret = self.run_2to3_capture(["-"], input_stream, out_enc, err)
+        self.assertEqual(ret, 0)
+        output = out.getvalue()
+        self.assertTrue("-print 'nothing'" in output)
+        self.assertTrue("WARNING: couldn't encode <stdin>'s diff for "
+                        "your terminal" in err.getvalue())