Exemplo n.º 1
0
def test_comprehensions_with_locals_in_pdb(kernel):
    """
    Test that evaluating comprehensions with locals works in Pdb.

    Also test that we use the right frame globals, in case the user
    wants to work with them.

    This is a regression test for spyder-ide/spyder#13909.
    """
    pdb_obj = SpyderPdb()
    pdb_obj.curframe = inspect.currentframe()
    pdb_obj.curframe_locals = pdb_obj.curframe.f_locals
    kernel._pdb_obj = pdb_obj

    # Create a local variable.
    kernel._pdb_obj.default('zz = 10')
    assert kernel.get_value('zz') == 10

    # Run a list comprehension with this variable.
    kernel._pdb_obj.default("compr = [zz * i for i in [1, 2, 3]]")
    assert kernel.get_value('compr') == [10, 20, 30]

    # Check that the variable is not reported as being part of globals.
    kernel._pdb_obj.default("in_globals = 'zz' in globals()")
    assert kernel.get_value('in_globals') == False
def test_do_complete(kernel):
    """
    Check do complete works in normal and debugging mode.
    """
    kernel.do_execute('abba = 1', True)
    assert kernel.get_value('abba') == 1
    match = kernel.do_complete('ab', 2)
    assert 'abba' in match['matches']

    # test pdb
    pdb_obj = SpyderPdb()
    pdb_obj.curframe = inspect.currentframe()
    pdb_obj.completenames = lambda *ignore: ['baba']
    kernel._pdb_obj = pdb_obj
    match = kernel.do_complete('ba', 2)
    assert 'baba' in match['matches']
def test_do_complete(kernel):
    """
    Check do complete works in normal and debugging mode.
    """
    if IPYKERNEL_6:
        asyncio.run(kernel.do_execute('abba = 1', True))
    else:
        kernel.do_execute('abba = 1', True)
    assert kernel.get_value('abba') == 1
    match = kernel.do_complete('ab', 2)
    assert 'abba' in match['matches']

    # test pdb
    pdb_obj = SpyderPdb()
    pdb_obj.curframe = inspect.currentframe()
    pdb_obj.completenames = lambda *ignore: ['baba']
    kernel.shell.pdb_session = pdb_obj
    match = kernel.do_complete('ba', 2)
    assert 'baba' in match['matches']
    pdb_obj.curframe = None
def test_functions_with_locals_in_pdb(kernel):
    """
    Test that functions with locals work in Pdb.

    This is a regression test for spyder-ide/spyder-kernels#345
    """
    pdb_obj = SpyderPdb()
    Frame = namedtuple("Frame", ["f_globals"])
    pdb_obj.curframe = Frame(f_globals=kernel.shell.user_ns)
    pdb_obj.curframe_locals = kernel.shell.user_ns
    kernel.shell.pdb_session = pdb_obj

    # Create a local function.
    kernel.shell.pdb_session.default(
        'def fun_a(): return [i for i in range(1)]')
    kernel.shell.pdb_session.default('zz = fun_a()')
    assert kernel.get_value('zz') == [0]

    kernel.shell.pdb_session.default('a = 1')
    kernel.shell.pdb_session.default('def fun_a(): return a')
    kernel.shell.pdb_session.default('zz = fun_a()')
    assert kernel.get_value('zz') == 1

    pdb_obj.curframe = None
    pdb_obj.curframe_locals = None
def test_locals_globals_in_pdb(kernel):
    """
    Test thal locals and globals work properly in Pdb.
    """
    a = 1
    pdb_obj = SpyderPdb()
    pdb_obj.curframe = inspect.currentframe()
    pdb_obj.curframe_locals = pdb_obj.curframe.f_locals
    kernel.shell.pdb_session = pdb_obj

    assert kernel.get_value('a') == 1

    kernel.shell.pdb_session.default('test = "a" in globals()')
    assert kernel.get_value('test') == False

    kernel.shell.pdb_session.default('test = "a" in locals()')
    assert kernel.get_value('test') == True

    kernel.shell.pdb_session.default('def f(): return a')
    kernel.shell.pdb_session.default('test = f()')
    assert kernel.get_value('test') == 1

    kernel.shell.pdb_session.default('a = 2')
    assert kernel.get_value('a') == 2

    kernel.shell.pdb_session.default('test = "a" in globals()')
    assert kernel.get_value('test') == False

    kernel.shell.pdb_session.default('test = "a" in locals()')
    assert kernel.get_value('test') == True

    pdb_obj.curframe = None
    pdb_obj.curframe_locals = None
def test_functions_with_locals_in_pdb_2(kernel):
    """
    Test that functions with locals work in Pdb.

    This is another regression test for spyder-ide/spyder-kernels#345
    """
    baba = 1
    pdb_obj = SpyderPdb()
    pdb_obj.curframe = inspect.currentframe()
    pdb_obj.curframe_locals = pdb_obj.curframe.f_locals
    kernel.shell.pdb_session = pdb_obj

    # Create a local function.
    kernel.shell.pdb_session.default(
        'def fun_a(): return [i for i in range(1)]')
    kernel.shell.pdb_session.default('zz = fun_a()')
    assert kernel.get_value('zz') == [0]

    kernel.shell.pdb_session.default('a = 1')
    kernel.shell.pdb_session.default('def fun_a(): return a')
    kernel.shell.pdb_session.default('zz = fun_a()')
    assert kernel.get_value('zz') == 1

    # Check baba is in locals and not globals
    kernel.shell.pdb_session.default('ll = locals().keys()')
    assert "baba" in kernel.get_value('ll')
    kernel.shell.pdb_session.default('gg = globals().keys()')
    assert "baba" not in kernel.get_value('gg')

    pdb_obj.curframe = None
    pdb_obj.curframe_locals = None
Exemplo n.º 7
0
def test_comprehensions_with_locals_in_pdb_2(kernel):
    """
    Test that evaluating comprehensions with locals works in Pdb.

    This is a regression test for spyder-ide/spyder#16790.
    """
    pdb_obj = SpyderPdb()
    pdb_obj.curframe = inspect.currentframe()
    pdb_obj.prompt_waiting = True
    pdb_obj.curframe_locals = pdb_obj.curframe.f_locals
    kernel.shell.pdb_session = pdb_obj

    # Create a local variable.
    kernel.shell.pdb_session.default('aa = [1, 2]')
    kernel.shell.pdb_session.default('bb = [3, 4]')
    kernel.shell.pdb_session.default('res = []')

    # Run a list comprehension with this variable.
    kernel.shell.pdb_session.default(
        "for c0 in aa: res.append([(c0, c1) for c1 in bb])")
    assert kernel.get_value('res') == [[(1, 3), (1, 4)], [(2, 3), (2, 4)]]

    pdb_obj.curframe = None
    pdb_obj.curframe_locals = None
def test_namespaces_in_pdb(kernel):
    """
    Test namespaces in pdb
    """
    # Define get_ipython for timeit
    get_ipython = lambda: kernel.shell
    kernel.shell.user_ns["test"] = 0
    pdb_obj = SpyderPdb()
    pdb_obj.curframe = inspect.currentframe()
    pdb_obj.curframe_locals = pdb_obj.curframe.f_locals
    kernel.shell.pdb_session = pdb_obj

    # Check adding something to globals works
    pdb_obj.default("globals()['test2'] = 0")
    assert pdb_obj.curframe.f_globals["test2"] == 0

    if PY2:
        # no error method in py2
        pdb_obj.curframe = None
        pdb_obj.curframe_locals = None
        return

    # Create wrapper to check for errors
    old_error = pdb_obj.error
    pdb_obj._error_occured = False

    def error_wrapper(*args, **kwargs):
        print(args, kwargs)
        pdb_obj._error_occured = True
        return old_error(*args, **kwargs)

    pdb_obj.error = error_wrapper

    # Test globals are visible
    pdb_obj.curframe.f_globals["test3"] = 0
    pdb_obj.default("%timeit test3")
    assert not pdb_obj._error_occured

    # Test locals are visible
    pdb_obj.curframe_locals["test4"] = 0
    pdb_obj.default("%timeit test4")
    assert not pdb_obj._error_occured

    # Test user namespace is not visible
    pdb_obj.default("%timeit test")
    assert pdb_obj._error_occured

    pdb_obj.curframe = None
    pdb_obj.curframe_locals = None