test_start_kernel.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from .test_embed_kernel import setup_kernel
  2. TIMEOUT = 15
  3. def test_ipython_start_kernel_userns():
  4. cmd = ('from IPython import start_kernel\n'
  5. 'ns = {"tre": 123}\n'
  6. 'start_kernel(user_ns=ns)')
  7. with setup_kernel(cmd) as client:
  8. msg_id = client.inspect('tre')
  9. msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
  10. content = msg['content']
  11. assert content['found']
  12. text = content['data']['text/plain']
  13. assert u'123' in text
  14. # user_module should be an instance of DummyMod
  15. msg_id = client.execute("usermod = get_ipython().user_module")
  16. msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
  17. content = msg['content']
  18. assert content['status'] == u'ok'
  19. msg_id = client.inspect('usermod')
  20. msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
  21. content = msg['content']
  22. assert content['found']
  23. text = content['data']['text/plain']
  24. assert u'DummyMod' in text
  25. def test_ipython_start_kernel_no_userns():
  26. # Issue #4188 - user_ns should be passed to shell as None, not {}
  27. cmd = ('from IPython import start_kernel\n'
  28. 'start_kernel()')
  29. with setup_kernel(cmd) as client:
  30. # user_module should not be an instance of DummyMod
  31. msg_id = client.execute("usermod = get_ipython().user_module")
  32. msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
  33. content = msg['content']
  34. assert content['status'] == u'ok'
  35. msg_id = client.inspect('usermod')
  36. msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
  37. content = msg['content']
  38. assert content['found']
  39. text = content['data']['text/plain']
  40. assert u'DummyMod' not in text