Show More
@@ -1,136 +1,137 | |||||
1 | """Test embedding of IPython""" |
|
1 | """Test embedding of IPython""" | |
2 |
|
2 | |||
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (C) 2013 The IPython Development Team |
|
4 | # Copyright (C) 2013 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING, distributed as part of this software. |
|
7 | # the file COPYING, distributed as part of this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | import os |
|
14 | import os | |
15 | import subprocess |
|
15 | import subprocess | |
16 | import sys |
|
16 | import sys | |
17 | import nose.tools as nt |
|
17 | ||
18 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
18 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory | |
19 | from IPython.testing.decorators import skip_win32 |
|
19 | from IPython.testing.decorators import skip_win32 | |
20 | from IPython.testing import IPYTHON_TESTING_TIMEOUT_SCALE |
|
20 | from IPython.testing import IPYTHON_TESTING_TIMEOUT_SCALE | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Tests |
|
23 | # Tests | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | _sample_embed = b""" |
|
27 | _sample_embed = b""" | |
28 | import IPython |
|
28 | import IPython | |
29 |
|
29 | |||
30 | a = 3 |
|
30 | a = 3 | |
31 | b = 14 |
|
31 | b = 14 | |
32 | print(a, '.', b) |
|
32 | print(a, '.', b) | |
33 |
|
33 | |||
34 | IPython.embed() |
|
34 | IPython.embed() | |
35 |
|
35 | |||
36 | print('bye!') |
|
36 | print('bye!') | |
37 | """ |
|
37 | """ | |
38 |
|
38 | |||
39 | _exit = b"exit\r" |
|
39 | _exit = b"exit\r" | |
40 |
|
40 | |||
41 | def test_ipython_embed(): |
|
41 | def test_ipython_embed(): | |
42 | """test that `IPython.embed()` works""" |
|
42 | """test that `IPython.embed()` works""" | |
43 | with NamedFileInTemporaryDirectory('file_with_embed.py') as f: |
|
43 | with NamedFileInTemporaryDirectory('file_with_embed.py') as f: | |
44 | f.write(_sample_embed) |
|
44 | f.write(_sample_embed) | |
45 | f.flush() |
|
45 | f.flush() | |
46 | f.close() # otherwise msft won't be able to read the file |
|
46 | f.close() # otherwise msft won't be able to read the file | |
47 |
|
47 | |||
48 | # run `python file_with_embed.py` |
|
48 | # run `python file_with_embed.py` | |
49 | cmd = [sys.executable, f.name] |
|
49 | cmd = [sys.executable, f.name] | |
50 | env = os.environ.copy() |
|
50 | env = os.environ.copy() | |
51 | env['IPY_TEST_SIMPLE_PROMPT'] = '1' |
|
51 | env['IPY_TEST_SIMPLE_PROMPT'] = '1' | |
52 |
|
52 | |||
53 | p = subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE, |
|
53 | p = subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE, | |
54 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
54 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
55 | out, err = p.communicate(_exit) |
|
55 | out, err = p.communicate(_exit) | |
56 | std = out.decode('UTF-8') |
|
56 | std = out.decode('UTF-8') | |
57 |
|
57 | |||
58 |
|
|
58 | assert p.returncode == 0 | |
59 |
|
|
59 | assert "3 . 14" in std | |
60 |
if os.name != |
|
60 | if os.name != "nt": | |
61 | # TODO: Fix up our different stdout references, see issue gh-14 |
|
61 | # TODO: Fix up our different stdout references, see issue gh-14 | |
62 |
|
|
62 | assert "IPython" in std | |
63 |
|
|
63 | assert "bye!" in std | |
|
64 | ||||
64 |
|
65 | |||
65 | @skip_win32 |
|
66 | @skip_win32 | |
66 | def test_nest_embed(): |
|
67 | def test_nest_embed(): | |
67 | """test that `IPython.embed()` is nestable""" |
|
68 | """test that `IPython.embed()` is nestable""" | |
68 | import pexpect |
|
69 | import pexpect | |
69 | ipy_prompt = r']:' #ansi color codes give problems matching beyond this |
|
70 | ipy_prompt = r']:' #ansi color codes give problems matching beyond this | |
70 | env = os.environ.copy() |
|
71 | env = os.environ.copy() | |
71 | env['IPY_TEST_SIMPLE_PROMPT'] = '1' |
|
72 | env['IPY_TEST_SIMPLE_PROMPT'] = '1' | |
72 |
|
73 | |||
73 |
|
74 | |||
74 | child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'], |
|
75 | child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'], | |
75 | env=env) |
|
76 | env=env) | |
76 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
77 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE | |
77 | child.expect(ipy_prompt) |
|
78 | child.expect(ipy_prompt) | |
78 | child.sendline("import IPython") |
|
79 | child.sendline("import IPython") | |
79 | child.expect(ipy_prompt) |
|
80 | child.expect(ipy_prompt) | |
80 | child.sendline("ip0 = get_ipython()") |
|
81 | child.sendline("ip0 = get_ipython()") | |
81 | #enter first nested embed |
|
82 | #enter first nested embed | |
82 | child.sendline("IPython.embed()") |
|
83 | child.sendline("IPython.embed()") | |
83 | #skip the banner until we get to a prompt |
|
84 | #skip the banner until we get to a prompt | |
84 | try: |
|
85 | try: | |
85 | prompted = -1 |
|
86 | prompted = -1 | |
86 | while prompted != 0: |
|
87 | while prompted != 0: | |
87 | prompted = child.expect([ipy_prompt, '\r\n']) |
|
88 | prompted = child.expect([ipy_prompt, '\r\n']) | |
88 | except pexpect.TIMEOUT as e: |
|
89 | except pexpect.TIMEOUT as e: | |
89 | print(e) |
|
90 | print(e) | |
90 | #child.interact() |
|
91 | #child.interact() | |
91 | child.sendline("embed1 = get_ipython()") |
|
92 | child.sendline("embed1 = get_ipython()") | |
92 | child.expect(ipy_prompt) |
|
93 | child.expect(ipy_prompt) | |
93 | child.sendline("print('true' if embed1 is not ip0 else 'false')") |
|
94 | child.sendline("print('true' if embed1 is not ip0 else 'false')") | |
94 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
95 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
95 | child.expect(ipy_prompt) |
|
96 | child.expect(ipy_prompt) | |
96 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") |
|
97 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") | |
97 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
98 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
98 | child.expect(ipy_prompt) |
|
99 | child.expect(ipy_prompt) | |
99 | #enter second nested embed |
|
100 | #enter second nested embed | |
100 | child.sendline("IPython.embed()") |
|
101 | child.sendline("IPython.embed()") | |
101 | #skip the banner until we get to a prompt |
|
102 | #skip the banner until we get to a prompt | |
102 | try: |
|
103 | try: | |
103 | prompted = -1 |
|
104 | prompted = -1 | |
104 | while prompted != 0: |
|
105 | while prompted != 0: | |
105 | prompted = child.expect([ipy_prompt, '\r\n']) |
|
106 | prompted = child.expect([ipy_prompt, '\r\n']) | |
106 | except pexpect.TIMEOUT as e: |
|
107 | except pexpect.TIMEOUT as e: | |
107 | print(e) |
|
108 | print(e) | |
108 | #child.interact() |
|
109 | #child.interact() | |
109 | child.sendline("embed2 = get_ipython()") |
|
110 | child.sendline("embed2 = get_ipython()") | |
110 | child.expect(ipy_prompt) |
|
111 | child.expect(ipy_prompt) | |
111 | child.sendline("print('true' if embed2 is not embed1 else 'false')") |
|
112 | child.sendline("print('true' if embed2 is not embed1 else 'false')") | |
112 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
113 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
113 | child.expect(ipy_prompt) |
|
114 | child.expect(ipy_prompt) | |
114 | child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')") |
|
115 | child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')") | |
115 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
116 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
116 | child.expect(ipy_prompt) |
|
117 | child.expect(ipy_prompt) | |
117 | child.sendline('exit') |
|
118 | child.sendline('exit') | |
118 | #back at first embed |
|
119 | #back at first embed | |
119 | child.expect(ipy_prompt) |
|
120 | child.expect(ipy_prompt) | |
120 | child.sendline("print('true' if get_ipython() is embed1 else 'false')") |
|
121 | child.sendline("print('true' if get_ipython() is embed1 else 'false')") | |
121 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
122 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
122 | child.expect(ipy_prompt) |
|
123 | child.expect(ipy_prompt) | |
123 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") |
|
124 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") | |
124 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
125 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
125 | child.expect(ipy_prompt) |
|
126 | child.expect(ipy_prompt) | |
126 | child.sendline('exit') |
|
127 | child.sendline('exit') | |
127 | #back at launching scope |
|
128 | #back at launching scope | |
128 | child.expect(ipy_prompt) |
|
129 | child.expect(ipy_prompt) | |
129 | child.sendline("print('true' if get_ipython() is ip0 else 'false')") |
|
130 | child.sendline("print('true' if get_ipython() is ip0 else 'false')") | |
130 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
131 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
131 | child.expect(ipy_prompt) |
|
132 | child.expect(ipy_prompt) | |
132 | child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')") |
|
133 | child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')") | |
133 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
134 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |
134 | child.expect(ipy_prompt) |
|
135 | child.expect(ipy_prompt) | |
135 | child.sendline('exit') |
|
136 | child.sendline('exit') | |
136 | child.close() |
|
137 | child.close() |
General Comments 0
You need to be logged in to leave comments.
Login now