diff -r 1f02dc050855 Lib/idlelib/CallTips.py --- a/Lib/idlelib/CallTips.py Sat Jan 04 13:06:59 2014 +0100 +++ b/Lib/idlelib/CallTips.py Sat Jan 04 18:19:54 2014 +0200 @@ -78,7 +78,7 @@ self.active_calltip = self._calltip_window() self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1]) - def fetch_tip(self, expression): + def fetch_tip(self, expression, *, _namespace=None): """Return the argument list and docstring of a function or class. If there is a Python subprocess, get the calltip there. Otherwise, @@ -100,15 +100,17 @@ return rpcclt.remotecall("exec", "get_the_calltip", (expression,), {}) else: - return get_argspec(get_entity(expression)) + return get_argspec(get_entity(expression, _namespace=_namespace)) -def get_entity(expression): +def get_entity(expression, *, _namespace=None): """Return the object corresponding to expression evaluated in a namespace spanning sys.modules and __main.dict__. """ if expression: namespace = sys.modules.copy() - namespace.update(__main__.__dict__) + if _namespace is None: + _namespace = __main__.__dict__ + namespace.update(_namespace) try: return eval(expression, namespace) except BaseException: @@ -158,112 +160,3 @@ if not argspec: argspec = _default_callable_argspec return argspec - -################################################# -# -# Test code tests CallTips.fetch_tip, get_entity, and get_argspec - -def main(): - # Putting expected in docstrings results in doubled tips for test - def t1(): "()" - def t2(a, b=None): "(a, b=None)" - def t3(a, *args): "(a, *args)" - def t4(*args): "(*args)" - def t5(a, b=None, *args, **kw): "(a, b=None, *args, **kw)" - - class TC(object): - "(ai=None, *b)" - def __init__(self, ai=None, *b): "(self, ai=None, *b)" - def t1(self): "(self)" - def t2(self, ai, b=None): "(self, ai, b=None)" - def t3(self, ai, *args): "(self, ai, *args)" - def t4(self, *args): "(self, *args)" - def t5(self, ai, b=None, *args, **kw): "(self, ai, b=None, *args, **kw)" - def t6(no, self): "(no, self)" - @classmethod - def cm(cls, a): "(cls, a)" - @staticmethod - def sm(b): "(b)" - def __call__(self, ci): "(self, ci)" - - tc = TC() - - # Python classes that inherit builtin methods - class Int(int): "Int(x[, base]) -> integer" - class List(list): "List() -> new empty list" - # Simulate builtin with no docstring for default argspec test - class SB: __call__ = None - - __main__.__dict__.update(locals()) # required for get_entity eval() - - num_tests = num_fail = 0 - tip = CallTips().fetch_tip - - def test(expression, expected): - nonlocal num_tests, num_fail - num_tests += 1 - argspec = tip(expression) - if argspec != expected: - num_fail += 1 - fmt = "%s - expected\n%r\n - but got\n%r" - print(fmt % (expression, expected, argspec)) - - def test_builtins(): - # if first line of a possibly multiline compiled docstring changes, - # must change corresponding test string - test('int', "int(x=0) -> integer") - test('Int', Int.__doc__) - test('types.MethodType', "method(function, instance)") - test('list', "list() -> new empty list") - test('List', List.__doc__) - test('list.__new__', - 'T.__new__(S, ...) -> a new object with type S, a subtype of T') - test('list.__init__', - 'x.__init__(...) initializes x; see help(type(x)) for signature') - append_doc = "L.append(object) -> None -- append object to end" - test('list.append', append_doc) - test('[].append', append_doc) - test('List.append', append_doc) - test('SB()', _default_callable_argspec) - - def test_funcs(): - for func in (t1, t2, t3, t4, t5, TC,): - fdoc = func.__doc__ - test(func.__name__, fdoc + "\n" + fdoc) - for func in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.sm, - TC.__call__): - fdoc = func.__doc__ - test('TC.'+func.__name__, fdoc + "\n" + fdoc) - fdoc = TC.cm.__func__.__doc__ - test('TC.cm.__func__', fdoc + "\n" + fdoc) - - def test_methods(): - # test that first parameter is correctly removed from argspec - # using _first_param re to calculate expected masks re errors - for meth, mdoc in ((tc.t1, "()"), (tc.t4, "(*args)"), (tc.t6, "(self)"), - (TC.cm, "(a)"),): - test('tc.'+meth.__name__, mdoc + "\n" + meth.__doc__) - test('tc', "(ci)" + "\n" + tc.__call__.__doc__) - # directly test that re works to delete unicode parameter name - uni = "(A\u0391\u0410\u05d0\u0627\u0905\u1e00\u3042, a)" # various As - assert _first_param.sub('', uni) == '(a)' - - def test_non_callables(): - # expression evaluates, but not to a callable - for expr in ('0', '0.0' 'num_tests', b'num_tests', '[]', '{}'): - test(expr, '') - # expression does not evaluate, but raises an exception - for expr in ('1a', 'xyx', 'num_tests.xyz', '[int][1]', '{0:int}[1]'): - test(expr, '') - - test_builtins() - test_funcs() - test_non_callables() - test_methods() - - print("%d of %d tests failed" % (num_fail, num_tests)) - -if __name__ == '__main__': - #main() - from unittest import main - main('idlelib.idle_test.test_calltips', verbosity=2, exit=False) diff -r 1f02dc050855 Lib/idlelib/idle_test/test_calltips.py --- a/Lib/idlelib/idle_test/test_calltips.py Sat Jan 04 13:06:59 2014 +0100 +++ b/Lib/idlelib/idle_test/test_calltips.py Sat Jan 04 18:19:54 2014 +0200 @@ -1,6 +1,97 @@ import unittest import idlelib.CallTips as ct +# Putting expected in docstrings results in doubled tips for test +def t1(): "()" +def t2(a, b=None): "(a, b=None)" +def t3(a, *args): "(a, *args)" +def t4(*args): "(*args)" +def t5(a, b=None, *args, **kw): "(a, b=None, *args, **kw)" + +class TC(object): + "(ai=None, *b)" + def __init__(self, ai=None, *b): "(self, ai=None, *b)" + def t1(self): "(self)" + def t2(self, ai, b=None): "(self, ai, b=None)" + def t3(self, ai, *args): "(self, ai, *args)" + def t4(self, *args): "(self, *args)" + def t5(self, ai, b=None, *args, **kw): "(self, ai, b=None, *args, **kw)" + def t6(no, self): "(no, self)" + @classmethod + def cm(cls, a): "(cls, a)" + @staticmethod + def sm(b): "(b)" + def __call__(self, ci): "(self, ci)" + +tc = TC() + +# Python classes that inherit builtin methods +class Int(int): "Int(x[, base]) -> integer" +class List(list): "List() -> new empty list" +# Simulate builtin with no docstring for default argspec test +class SB: __call__ = None + +class CallTipsTest(unittest.TestCase): + def setUp(self): + self.tip = ct.CallTips().fetch_tip + + def checkTip(self, expression, expected): + with self.subTest(expression=expression): + self.assertEqual(self.tip(expression, _namespace=globals()), + expected) + + def test_builtins(self): + test = self.checkTip + # if first line of a possibly multiline compiled docstring changes, + # must change corresponding test string + test('int', "int(x=0) -> integer") + test('Int', Int.__doc__) + test('types.MethodType', "method(function, instance)") + test('list', "list() -> new empty list") + test('List', List.__doc__) + test('list.__new__', + 'T.__new__(S, ...) -> a new object with type S, a subtype of T') + test('list.__init__', + 'x.__init__(...) initializes x; see help(type(x)) for signature') + append_doc = "L.append(object) -> None -- append object to end" + test('list.append', append_doc) + test('[].append', append_doc) + test('List.append', append_doc) + test('SB()', ct._default_callable_argspec) + + def test_funcs(self): + test = self.checkTip + for func in (t1, t2, t3, t4, t5, TC,): + fdoc = func.__doc__ + test(func.__name__, fdoc + "\n" + fdoc) + for func in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.sm, + TC.__call__): + fdoc = func.__doc__ + test('TC.'+func.__name__, fdoc + "\n" + fdoc) + fdoc = TC.cm.__func__.__doc__ + test('TC.cm.__func__', fdoc + "\n" + fdoc) + + def test_methods(self): + test = self.checkTip + # test that first parameter is correctly removed from argspec + # using _first_param re to calculate expected masks re errors + for meth, mdoc in ((tc.t1, "()"), (tc.t4, "(*args)"), (tc.t6, "(self)"), + (TC.cm, "(a)"),): + test('tc.'+meth.__name__, mdoc + "\n" + meth.__doc__) + test('tc', "(ci)" + "\n" + tc.__call__.__doc__) + # directly test that re works to delete unicode parameter name + uni = "(A\u0391\u0410\u05d0\u0627\u0905\u1e00\u3042, a)" # various As + assert ct._first_param.sub('', uni) == '(a)' + + def test_non_callables(self): + test = self.checkTip + # expression evaluates, but not to a callable + for expr in ('0', '0.0' 'num_tests', b'num_tests', '[]', '{}'): + test(expr, '') + # expression does not evaluate, but raises an exception + for expr in ('1a', 'xyx', 'num_tests.xyz', '[int][1]', '{0:int}[1]'): + test(expr, '') + class Get_entityTest(unittest.TestCase): def test_bad_entity(self): self.assertIsNone(ct.get_entity('1/0'))