def test_runfile(tmpdir): """ Test that runfile uses the proper name space for execution. """ # Command to start the kernel cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: # Remove all variables client.execute("%reset -f") client.get_shell_msg(block=True, timeout=TIMEOUT) # Write defined variable code to a file code = u"result = 'hello world'" d = tmpdir.join("defined-test.py") d.write(code) # Write undefined variable code to a file code = dedent(u""" try: result3 = result except NameError: result2 = 'hello world' """) u = tmpdir.join("undefined-test.py") u.write(code) # Run code file `d` to define `result` client.execute("runfile(r'{}', current_namespace=False)" .format(to_text_string(d))) client.get_shell_msg(block=True, timeout=TIMEOUT) # Verify that `result` is defined in the current namespace client.inspect('result') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found'] # Run code file `u` without current namespace client.execute("runfile(r'{}', current_namespace=False)" .format(to_text_string(u))) client.get_shell_msg(block=True, timeout=TIMEOUT) # Verify that the variable `result2` is defined client.inspect('result2') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found'] # Run code file `u` with current namespace client.execute("runfile(r'{}', current_namespace=True)" .format(to_text_string(u))) msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] # Verify that the variable `result3` is defined client.inspect('result3') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found']
def test_multiprocessing(tmpdir): """ Test that multiprocessing works on Windows and Python 3. """ # Command to start the kernel cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: # Remove all variables client.execute("%reset -f") client.get_shell_msg(block=True, timeout=TIMEOUT) # Write multiprocessing code to a file code = """ from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': with Pool(5) as p: result = p.map(f, [1, 2, 3]) """ p = tmpdir.join("mp-test.py") p.write(code) # Run code client.execute("runfile(r'{}')".format(to_text_string(p))) client.get_shell_msg(block=True, timeout=TIMEOUT) # Verify that the `result` variable is defined client.inspect('result') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found']
def test_turtle_launch(tmpdir): """Test turtle scripts running in the same kernel.""" # Command to start the kernel cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: # Remove all variables client.execute("%reset -f") client.get_shell_msg(block=True, timeout=TIMEOUT) # Write turtle code to a file code = """ import turtle wn=turtle.Screen() wn.bgcolor("lightgreen") tess = turtle.Turtle() # Create tess and set some attributes tess.color("hotpink") tess.pensize(5) tess.forward(80) # Make tess draw equilateral triangle tess.left(120) tess.forward(80) tess.left(120) tess.forward(80) tess.left(120) # Complete the triangle turtle.bye() """ p = tmpdir.join("turtle-test.py") p.write(code) # Run code client.execute("runfile(r'{}')".format(to_text_string(p))) client.get_shell_msg(block=True, timeout=TIMEOUT) # Verify that the `tess` variable is defined client.inspect('tess') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found'] # Write turtle code to a file code = code + "a = 10" p = tmpdir.join("turtle-test1.py") p.write(code) # Run code again client.execute("runfile(r'{}')".format(to_text_string(p))) client.get_shell_msg(block=True, timeout=TIMEOUT) # Verify that the `a` variable is defined client.inspect('a') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found']
def test_np_threshold(kernel): """Test that setting Numpy threshold doesn't make the Variable Explorer slow.""" cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: # Set Numpy threshold, suppress and formatter client.execute(""" import numpy as np; np.set_printoptions( threshold=np.inf, suppress=True, formatter={'float_kind':'{:0.2f}'.format}) """) client.get_shell_msg(block=True, timeout=TIMEOUT) # Create a big Numpy array and an array to check decimal format client.execute(""" x = np.random.rand(75000,5); a = np.array([123412341234.123412341234]) """) client.get_shell_msg(block=True, timeout=TIMEOUT) # Assert that NumPy threshold, suppress and formatter # are the same as the ones set by the user client.execute(""" t = np.get_printoptions()['threshold']; s = np.get_printoptions()['suppress']; f = np.get_printoptions()['formatter'] """) client.get_shell_msg(block=True, timeout=TIMEOUT) # Check correct decimal format client.inspect('a') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content']['data']['text/plain'] assert "123412341234.12" in content # Check threshold value client.inspect('t') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content']['data']['text/plain'] assert "inf" in content # Check suppress value client.inspect('s') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content']['data']['text/plain'] assert "True" in content # Check formatter client.inspect('f') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content']['data']['text/plain'] assert "{'float_kind': <built-in method format of str object" in content
def test_matplotlib_inline(kernel): """Test that the default backend for our kernels is 'inline'.""" # Command to start the kernel cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: # Get current backend code = "import matplotlib; backend = matplotlib.get_backend()" client.execute(code, user_expressions={'output': 'backend'}) reply = client.get_shell_msg(block=True, timeout=TIMEOUT) # Transform value obtained through user_expressions user_expressions = reply['content']['user_expressions'] str_value = user_expressions['output']['data']['text/plain'] value = ast.literal_eval(str_value) # Assert backend is inline assert 'inline' in value
def test_cwd_in_sys_path(): """ Test that cwd stays as the first element in sys.path after the kernel has started. """ # Command to start the kernel cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: msg_id = client.execute("import sys; sys_path = sys.path", user_expressions={'output':'sys_path'}) reply = client.get_shell_msg(block=True, timeout=TIMEOUT) # Transform value obtained through user_expressions user_expressions = reply['content']['user_expressions'] str_value = user_expressions['output']['data']['text/plain'] value = ast.literal_eval(str_value) # Assert the first value of sys_path is an empty string assert value[0] == ''
def test_runcell(tmpdir): """Test the runcell command.""" # Command to start the kernel cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: # Write code with a cell to a file code = u"result = 10; fname = __file__" p = tmpdir.join("cell-test.py") p.write(code) # Attach cell_code to the IPython shell instance to simulate # that the code was sent from Spyder's Editor client.execute(u"get_ipython().cell_code = '{}'".format(code)) client.get_shell_msg(block=True, timeout=TIMEOUT) # Execute runcell client.execute(u"runcell('', r'{}')".format(to_text_string(p))) client.get_shell_msg(block=True, timeout=TIMEOUT) # Verify that the `result` variable is defined client.inspect('result') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found'] # Verify that the `fname` variable is `cell-test.py` client.inspect('fname') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert "cell-test.py" in content['data']['text/plain'] # Verify that the `__file__` variable is undefined client.inspect('__file__') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert not content['found']
def test_runcell(tmpdir, debug): """Test the runcell command.""" # Command to start the kernel cmd = "from spyder_kernels.console import start; start.main()" with setup_kernel(cmd) as client: # Write code with a cell to a file code = u"result = 10; fname = __file__" p = tmpdir.join("cell-test.py") p.write(code) class Signal(): def connect(self, function): self.function = function # Fake Qt signal iopub_recieved = Signal() client.iopub_channel.message_received = iopub_recieved # Open comm comm_manager = CommManager(client) kernel_comm = KernelComm() kernel_comm._register_comm( comm_manager.new_comm('spyder_api', data={'pickle_protocol': 2})) def process_msg(call_name): msg = {'msg_type': None} while (msg['msg_type'] != 'comm_msg' or msg['content']['data']['content']['call_name'] != call_name): msg = client.get_iopub_msg(block=True, timeout=TIMEOUT) iopub_recieved.function(msg) def runcell(cellname, filename): return code def set_debug_state(state): set_debug_state.state = state set_debug_state.state = None kernel_comm.register_call_handler('run_cell', runcell) kernel_comm.register_call_handler('get_breakpoints', lambda: {}) kernel_comm.register_call_handler('pdb_state', lambda state: None) kernel_comm.register_call_handler('set_debug_state', set_debug_state) if debug: function = 'debugcell' else: function = 'runcell' # Execute runcell client.execute(function + u"('', r'{}')".format(to_text_string(p))) # Get the runcell call process_msg('run_cell') if debug: # Continue process_msg('set_debug_state') process_msg('get_breakpoints') assert set_debug_state.state time.sleep(.5) client.input('c') process_msg('set_debug_state') assert not set_debug_state.state msg = client.get_shell_msg(block=True, timeout=TIMEOUT) assert msg['msg_type'] == 'execute_reply' # Verify that the `result` variable is defined client.inspect('result') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert content['found'] # Verify that the `fname` variable is `cell-test.py` client.inspect('fname') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert "cell-test.py" in content['data']['text/plain'] # Verify that the `__file__` variable is undefined client.inspect('__file__') msg = client.get_shell_msg(block=True, timeout=TIMEOUT) content = msg['content'] assert not content['found']