[Python-checkins] cpython (2.7): Issue #20122: Idlelib: Move tests in 3.x CallTips.py to test_calltips.py.

terry.reedy python-checkins at python.org
Tue Jan 21 06:31:31 CET 2014


http://hg.python.org/cpython/rev/f6f2d9d04cd0
changeset:   88600:f6f2d9d04cd0
branch:      2.7
parent:      88588:208fa45672c2
user:        Terry Jan Reedy <tjreedy at udel.edu>
date:        Tue Jan 21 00:26:10 2014 -0500
summary:
  Issue #20122: Idlelib: Move tests in 3.x CallTips.py to test_calltips.py.
For 2.7 backport, fix get_arg_spec as needed to make expanded tests pass.

files:
  Lib/idlelib/CallTips.py                |   67 +------
  Lib/idlelib/idle_test/test_calltips.py |  102 ++++++++++++-
  2 files changed, 114 insertions(+), 55 deletions(-)


diff --git a/Lib/idlelib/CallTips.py b/Lib/idlelib/CallTips.py
--- a/Lib/idlelib/CallTips.py
+++ b/Lib/idlelib/CallTips.py
@@ -143,10 +143,15 @@
                 fob = lambda: None
             else:
                 arg_offset = 1
-        elif type(ob)==types.MethodType:
+        elif type(ob) == types.MethodType:
             # bit of a hack for methods - turn it into a function
-            # but we drop the "self" param.
+            # and drop the "self" param for bound methods
             fob = ob.im_func
+            if ob.im_self:
+                arg_offset = 1
+        elif type(ob.__call__) == types.MethodType:
+            # a callable class instance
+            fob = ob.__call__.im_func
             arg_offset = 1
         else:
             fob = ob
@@ -159,13 +164,16 @@
             defaults = [""] * (len(real_args) - len(defaults)) + defaults
             items = map(lambda arg, dflt: arg + dflt, real_args, defaults)
             if fob.func_code.co_flags & 0x4:
-                items.append("...")
+                items.append("*args")
             if fob.func_code.co_flags & 0x8:
-                items.append("***")
+                items.append("**kwds")
             arg_text = ", ".join(items)
             arg_text = "(%s)" % re.sub("(?<!\d)\.\d+", "<tuple>", arg_text)
         # See if we can use the docstring
-        doc = getattr(ob, "__doc__", "")
+        if isinstance(ob.__call__, types.MethodType):
+            doc = ob.__call__.__doc__
+        else:
+            doc = getattr(ob, "__doc__", "")
         if doc:
             doc = doc.lstrip()
             pos = doc.find("\n")
@@ -176,53 +184,6 @@
             arg_text += doc[:pos]
     return arg_text
 
-#################################################
-#
-# Test code
-#
-if __name__=='__main__':
-
-    def t1(): "()"
-    def t2(a, b=None): "(a, b=None)"
-    def t3(a, *args): "(a, ...)"
-    def t4(*args): "(...)"
-    def t5(a, *args): "(a, ...)"
-    def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
-    def t7((a, b), c, (d, e)): "(<tuple>, c, <tuple>)"
-
-    class TC(object):
-        "(ai=None, ...)"
-        def __init__(self, ai=None, *b): "(ai=None, ...)"
-        def t1(self): "()"
-        def t2(self, ai, b=None): "(ai, b=None)"
-        def t3(self, ai, *args): "(ai, ...)"
-        def t4(self, *args): "(...)"
-        def t5(self, ai, *args): "(ai, ...)"
-        def t6(self, ai, b=None, *args, **kw): "(ai, b=None, ..., ***)"
-        def t7(self, (ai, b), c, (d, e)): "(<tuple>, c, <tuple>)"
-
-    def test(tests):
-        ct = CallTips()
-        failed=[]
-        for t in tests:
-            expected = t.__doc__ + "\n" + t.__doc__
-            name = t.__name__
-            # exercise fetch_tip(), not just get_arg_text()
-            try:
-                qualified_name = "%s.%s" % (t.im_class.__name__, name)
-            except AttributeError:
-                qualified_name = name
-            arg_text = ct.fetch_tip(qualified_name)
-            if arg_text != expected:
-                failed.append(t)
-                fmt = "%s - expected %s, but got %s"
-                print  fmt % (t.__name__, expected, get_arg_text(t))
-        print "%d of %d tests failed" % (len(failed), len(tests))
-
-    tc = TC()
-    tests = (t1, t2, t3, t4, t5, t6, t7,
-             TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6, tc.t7)
-
-    # test(tests)
+if __name__ == '__main__':
     from unittest import main
     main('idlelib.idle_test.test_calltips', verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_calltips.py b/Lib/idlelib/idle_test/test_calltips.py
--- a/Lib/idlelib/idle_test/test_calltips.py
+++ b/Lib/idlelib/idle_test/test_calltips.py
@@ -1,6 +1,104 @@
 import unittest
 import idlelib.CallTips as ct
-CTi = ct.CallTips()
+CTi = ct.CallTips()  # needed for get_entity test in 2.7
+import types
+
+
+# Test Class TC is used in multiple get_argspec test methods
+class TC(object):
+    'doc'
+    tip = "(ai=None, *args)"
+    def __init__(self, ai=None, *b): 'doc'
+    __init__.tip = "(self, ai=None, *args)"
+    def t1(self): 'doc'
+    t1.tip = "(self)"
+    def t2(self, ai, b=None): 'doc'
+    t2.tip = "(self, ai, b=None)"
+    def t3(self, ai, *args): 'doc'
+    t3.tip = "(self, ai, *args)"
+    def t4(self, *args): 'doc'
+    t4.tip = "(self, *args)"
+    def t5(self, ai, b=None, *args, **kw): 'doc'
+    t5.tip = "(self, ai, b=None, *args, **kwds)"
+    def t6(no, self): 'doc'
+    t6.tip = "(no, self)"
+    def __call__(self, ci): 'doc'
+    __call__.tip = "(self, ci)"
+    # attaching .tip to wrapped methods does not work
+    @classmethod
+    def cm(cls, a): 'doc'
+    @staticmethod
+    def sm(b): 'doc'
+
+tc = TC()
+
+signature = ct.get_arg_text  # 2.7 and 3.x use different functions
+class Get_signatureTest(unittest.TestCase):
+
+    def test_builtins(self):
+        # 2.7 puts '()\n' where 3.x does not, other minor differences
+
+        # Python class that inherits builtin methods
+        class List(list): "List() doc"
+        # Simulate builtin with no docstring for default argspec test
+        class SB:  __call__ = None
+
+        def gtest(obj, out):
+            self.assertEqual(signature(obj), out)
+
+        gtest(list, "()\nlist() -> new empty list")
+        gtest(List, '()\n' + List.__doc__)
+        gtest(list.__new__,
+               'T.__new__(S, ...) -> a new object with type S, a subtype of T')
+        gtest(list.__init__,
+               'x.__init__(...) initializes x; see help(type(x)) for signature')
+        append_doc =  "L.append(object) -- append object to end"
+        gtest(list.append, append_doc)
+        gtest([].append, append_doc)
+        gtest(List.append, append_doc)
+
+        gtest(types.MethodType, '()\ninstancemethod(function, instance, class)')
+        gtest(SB(), '')
+
+
+    def test_functions(self):
+        def t1(): 'doc'
+        t1.tip = "()"
+        def t2(a, b=None): 'doc'
+        t2.tip = "(a, b=None)"
+        def t3(a, *args): 'doc'
+        t3.tip = "(a, *args)"
+        def t4(*args): 'doc'
+        t4.tip = "(*args)"
+        def t5(a, b=None, *args, **kwds): 'doc'
+        t5.tip = "(a, b=None, *args, **kwds)"
+
+        for func in (t1, t2, t3, t4, t5, TC):
+            self.assertEqual(signature(func), func.tip + '\ndoc')
+
+    def test_methods(self):
+        for meth in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.__call__):
+            self.assertEqual(signature(meth), meth.tip + "\ndoc")
+        self.assertEqual(signature(TC.cm), "(a)\ndoc")
+        self.assertEqual(signature(TC.sm), "(b)\ndoc")
+
+    def test_bound_methods(self):
+        # test that first parameter is correctly removed from argspec
+        for meth, mtip  in ((tc.t1, "()"), (tc.t4, "(*args)"), (tc.t6, "(self)"),
+                            (TC.cm, "(a)"),):
+            self.assertEqual(signature(meth), mtip + "\ndoc")
+        self.assertEqual(signature(tc), "(ci)\ndoc")
+        
+    def test_no_docstring(self):
+        def nd(s): pass
+        TC.nd = nd
+        self.assertEqual(signature(nd), "(s)")
+        self.assertEqual(signature(TC.nd), "(s)")
+        self.assertEqual(signature(tc.nd), "()")
+
+    def test_non_callables(self):
+        for obj in (0, 0.0, '0', b'0', [], {}):
+            self.assertEqual(signature(obj), '')
 
 class Get_entityTest(unittest.TestCase):
     # In 3.x, get_entity changed from 'instance method' to module function
@@ -14,7 +112,7 @@
     def test_paramtuple_float(self):
         # 18539: (a,b) becomes '.0' in code object; change that but not float
         def f((a,b), c=0.0): pass
-        self.assertEqual(ct.get_arg_text(f), '(<tuple>, c=0.0)')
+        self.assertEqual(signature(f), '(<tuple>, c=0.0)')
 
 if __name__ == '__main__':
     unittest.main(verbosity=2, exit=False)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list