##// END OF EJS Templates
Fix extensions test suite (small, but now it runs and passes!)
Fernando Perez -
r2415:21955d0e
parent child
Show More
@@ -2334,7 +2334,7 class InteractiveShell(Component, Magic):
2334 with prepended_to_syspath(self.ipython_extension_dir):
2334 with prepended_to_syspath(self.ipython_extension_dir):
2335 __import__(module_str)
2335 __import__(module_str)
2336 mod = sys.modules[module_str]
2336 mod = sys.modules[module_str]
2337 self._call_load_ipython_extension(mod)
2337 return self._call_load_ipython_extension(mod)
2338
2338
2339 def unload_extension(self, module_str):
2339 def unload_extension(self, module_str):
2340 """Unload an IPython extension by its module name.
2340 """Unload an IPython extension by its module name.
@@ -2366,11 +2366,11 class InteractiveShell(Component, Magic):
2366
2366
2367 def _call_load_ipython_extension(self, mod):
2367 def _call_load_ipython_extension(self, mod):
2368 if hasattr(mod, 'load_ipython_extension'):
2368 if hasattr(mod, 'load_ipython_extension'):
2369 mod.load_ipython_extension(self)
2369 return mod.load_ipython_extension(self)
2370
2370
2371 def _call_unload_ipython_extension(self, mod):
2371 def _call_unload_ipython_extension(self, mod):
2372 if hasattr(mod, 'unload_ipython_extension'):
2372 if hasattr(mod, 'unload_ipython_extension'):
2373 mod.unload_ipython_extension(self)
2373 return mod.unload_ipython_extension(self)
2374
2374
2375 #-------------------------------------------------------------------------
2375 #-------------------------------------------------------------------------
2376 # Things related to the prefilter
2376 # Things related to the prefilter
@@ -3496,7 +3496,7 Defaulting color scheme to 'NoColor'"""
3496
3496
3497 def magic_load_ext(self, module_str):
3497 def magic_load_ext(self, module_str):
3498 """Load an IPython extension by its module name."""
3498 """Load an IPython extension by its module name."""
3499 self.load_extension(module_str)
3499 return self.load_extension(module_str)
3500
3500
3501 def magic_unload_ext(self, module_str):
3501 def magic_unload_ext(self, module_str):
3502 """Unload an IPython extension by its module name."""
3502 """Unload an IPython extension by its module name."""
@@ -24,6 +24,7 else:
24 c = C(name)
24 c = C(name)
25
25
26 #print >> sys.stderr, "ARGV:", sys.argv # dbg
26 #print >> sys.stderr, "ARGV:", sys.argv # dbg
27 # This print statement is NOT debugging, we're making the check on a completely
27
28 # separate process so we verify by capturing stdout.
28 # This next print statement is NOT debugging, we're making the check on a
29 # completely separate process so we verify by capturing stdout:
29 print 'ARGV 1-:', sys.argv[1:]
30 print 'ARGV 1-:', sys.argv[1:]
@@ -91,24 +91,7 def doctest_run_builtins():
91 # For some tests, it will be handy to organize them in a class with a common
91 # For some tests, it will be handy to organize them in a class with a common
92 # setup that makes a temp file
92 # setup that makes a temp file
93
93
94 class TempFileMixin(object):
94 class TestMagicRunPass(tt.TempFileMixin):
95 def mktmp(self, src, ext='.py'):
96 """Make a valid python temp file."""
97 fname, f = tt.temp_pyfile(src, ext)
98 self.tmpfile = f
99 self.fname = fname
100
101 def teardown(self):
102 self.tmpfile.close()
103 try:
104 os.unlink(self.fname)
105 except:
106 # On Windows, even though we close the file, we still can't delete
107 # it. I have no clue why
108 pass
109
110
111 class TestMagicRunPass(TempFileMixin):
112
95
113 def setup(self):
96 def setup(self):
114 """Make a valid python temp file."""
97 """Make a valid python temp file."""
@@ -148,7 +131,7 class TestMagicRunPass(TempFileMixin):
148 nt.assert_equals(p2[:3], '...')
131 nt.assert_equals(p2[:3], '...')
149
132
150
133
151 class TestMagicRunSimple(TempFileMixin):
134 class TestMagicRunSimple(tt.TempFileMixin):
152
135
153 def test_simpledef(self):
136 def test_simpledef(self):
154 """Test that simple class definitions work."""
137 """Test that simple class definitions work."""
@@ -19,6 +19,7 import new
19 from IPython.core.component import Component
19 from IPython.core.component import Component
20 from IPython.utils.traitlets import Bool, Any
20 from IPython.utils.traitlets import Bool, Any
21 from IPython.utils.autoattr import auto_attr
21 from IPython.utils.autoattr import auto_attr
22 from IPython.testing import decorators as testdec
22
23
23 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
24 # Definitions of magic functions for use with IPython
25 # Definitions of magic functions for use with IPython
@@ -58,6 +59,7 class ParalleMagicComponent(Component):
58 self.shell.define_magic('px', self.magic_px)
59 self.shell.define_magic('px', self.magic_px)
59 self.shell.define_magic('autopx', self.magic_autopx)
60 self.shell.define_magic('autopx', self.magic_autopx)
60
61
62 @testdec.skip_doctest
61 def magic_result(self, ipself, parameter_s=''):
63 def magic_result(self, ipself, parameter_s=''):
62 """Print the result of command i on all engines..
64 """Print the result of command i on all engines..
63
65
@@ -89,6 +91,7 class ParalleMagicComponent(Component):
89 result = self.active_multiengine_client.get_result(index)
91 result = self.active_multiengine_client.get_result(index)
90 return result
92 return result
91
93
94 @testdec.skip_doctest
92 def magic_px(self, ipself, parameter_s=''):
95 def magic_px(self, ipself, parameter_s=''):
93 """Executes the given python command in parallel.
96 """Executes the given python command in parallel.
94
97
@@ -112,6 +115,7 class ParalleMagicComponent(Component):
112 result = self.active_multiengine_client.execute(parameter_s)
115 result = self.active_multiengine_client.execute(parameter_s)
113 return result
116 return result
114
117
118 @testdec.skip_doctest
115 def magic_autopx(self, ipself, parameter_s=''):
119 def magic_autopx(self, ipself, parameter_s=''):
116 """Toggles auto parallel mode.
120 """Toggles auto parallel mode.
117
121
@@ -128,13 +128,15 class PrettyResultDisplay(Component):
128 #-----------------------------------------------------------------------------
128 #-----------------------------------------------------------------------------
129
129
130
130
131 def load_ipython_extension(ip):
131 def load_ipython_extension(ip=None):
132 """Load the extension in IPython as a hook."""
132 """Load the extension in IPython as a hook."""
133 if ip is None: ip = get_ipython()
133 global _loaded
134 global _loaded
134 if not _loaded:
135 if not _loaded:
135 prd = PrettyResultDisplay(ip, name='pretty_result_display')
136 prd = PrettyResultDisplay(ip, name='pretty_result_display')
136 ip.set_hook('result_display', prd, priority=99)
137 ip.set_hook('result_display', prd, priority=99)
137 _loaded = True
138 _loaded = True
139 return prd
138
140
139 def unload_ipython_extension(ip):
141 def unload_ipython_extension(ip):
140 """Unload the extension."""
142 """Unload the extension."""
@@ -163,60 +165,3 def dtype_pprinter(obj, p, cycle):
163 p.breakable()
165 p.breakable()
164 p.pretty(field)
166 p.pretty(field)
165 p.end_group(7, '])')
167 p.end_group(7, '])')
166
167
168 #-----------------------------------------------------------------------------
169 # Tests
170 #-----------------------------------------------------------------------------
171
172
173 def test_pretty():
174 """
175 In [1]: from IPython.extensions import ipy_pretty
176
177 In [2]: ipy_pretty.activate()
178
179 In [3]: class A(object):
180 ...: def __repr__(self):
181 ...: return 'A()'
182 ...:
183 ...:
184
185 In [4]: a = A()
186
187 In [5]: a
188 Out[5]: A()
189
190 In [6]: def a_pretty_printer(obj, p, cycle):
191 ...: p.text('<A>')
192 ...:
193 ...:
194
195 In [7]: ipy_pretty.for_type(A, a_pretty_printer)
196
197 In [8]: a
198 Out[8]: <A>
199
200 In [9]: class B(object):
201 ...: def __repr__(self):
202 ...: return 'B()'
203 ...:
204 ...:
205
206 In [10]: B.__module__, B.__name__
207 Out[10]: ('__main__', 'B')
208
209 In [11]: def b_pretty_printer(obj, p, cycle):
210 ....: p.text('<B>')
211 ....:
212 ....:
213
214 In [12]: ipy_pretty.for_type_by_name('__main__', 'B', b_pretty_printer)
215
216 In [13]: b = B()
217
218 In [14]: b
219 Out[14]: <B>
220 """
221 assert False, "This should only be doctested, not run."
222
@@ -15,14 +15,13 Simple tests for :mod:`IPython.extensions.pretty`.
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 import sys
19 from unittest import TestCase
18 from unittest import TestCase
20
19
21 from IPython.core.component import Component, masquerade_as
20 from IPython.core.component import Component, masquerade_as
22 from IPython.core.iplib import InteractiveShell
21 from IPython.core.iplib import InteractiveShell
23 from IPython.extensions import pretty as pretty_ext
22 from IPython.extensions import pretty as pretty_ext
24 from IPython.external import pretty
23 from IPython.external import pretty
25
24 from IPython.testing import tools as tt
26 from IPython.utils.traitlets import Bool
25 from IPython.utils.traitlets import Bool
27
26
28 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
@@ -43,9 +42,11 class TestPrettyResultDisplay(TestCase):
43
42
44 def setUp(self):
43 def setUp(self):
45 self.ip = InteractiveShellStub(None)
44 self.ip = InteractiveShellStub(None)
46 # This allows our stub to be retrieved instead of the real InteractiveShell
45 # This allows our stub to be retrieved instead of the real
46 # InteractiveShell
47 masquerade_as(self.ip, InteractiveShell)
47 masquerade_as(self.ip, InteractiveShell)
48 self.prd = pretty_ext.PrettyResultDisplay(self.ip, name='pretty_result_display')
48 self.prd = pretty_ext.PrettyResultDisplay(self.ip,
49 name='pretty_result_display')
49
50
50 def test_for_type(self):
51 def test_for_type(self):
51 self.prd.for_type(A, a_pprinter)
52 self.prd.for_type(A, a_pprinter)
@@ -53,4 +54,44 class TestPrettyResultDisplay(TestCase):
53 result = pretty.pretty(a)
54 result = pretty.pretty(a)
54 self.assertEquals(result, "<A>")
55 self.assertEquals(result, "<A>")
55
56
57 ipy_src = """
58 class A(object):
59 def __repr__(self):
60 return 'A()'
61
62 class B(object):
63 def __repr__(self):
64 return 'B()'
65
66 a = A()
67 b = B()
68
69 def a_pretty_printer(obj, p, cycle):
70 p.text('<A>')
71
72 def b_pretty_printer(obj, p, cycle):
73 p.text('<B>')
74
75
76 a
77 b
78
79 ip = get_ipython()
80 prd = ip.load_extension('pretty')
81 prd.for_type(A, a_pretty_printer)
82 prd.for_type_by_name(B.__module__, B.__name__, b_pretty_printer)
83
84 a
85 b
86 """
87 ipy_out = """
88 A()
89 B()
90 <A>
91 <B>
92 """
56
93
94 class TestPrettyInteractively(tt.TempFileMixin):
95 def test_printers(self):
96 self.mktmp(ipy_src, '.ipy')
97 tt.ipexec_validate(self.fname, ipy_out)
@@ -165,12 +165,12 def default_argv():
165 from IPython.config import default
165 from IPython.config import default
166 ipcdir = os.path.dirname(default.__file__)
166 ipcdir = os.path.dirname(default.__file__)
167 ipconf = os.path.join(ipcdir,'ipython_config.py')
167 ipconf = os.path.join(ipcdir,'ipython_config.py')
168 #print 'conf:',ipconf # dbg
169 return ['--colors=NoColor', '--no-term-title','--no-banner',
168 return ['--colors=NoColor', '--no-term-title','--no-banner',
170 '--config-file=%s' % ipconf, '--autocall=0', '--quick']
169 '--config-file=%s' % ipconf, '--autocall=0',
170 '--prompt-out=""']
171
171
172
172
173 def ipexec(fname):
173 def ipexec(fname, options=None):
174 """Utility to call 'ipython filename'.
174 """Utility to call 'ipython filename'.
175
175
176 Starts IPython witha minimal and safe configuration to make startup as fast
176 Starts IPython witha minimal and safe configuration to make startup as fast
@@ -183,19 +183,26 def ipexec(fname):
183 fname : str
183 fname : str
184 Name of file to be executed (should have .py or .ipy extension).
184 Name of file to be executed (should have .py or .ipy extension).
185
185
186 options : optional, list
187 Extra command-line flags to be passed to IPython.
188
186 Returns
189 Returns
187 -------
190 -------
188 (stdout, stderr) of ipython subprocess.
191 (stdout, stderr) of ipython subprocess.
189 """
192 """
193 if options is None: options = []
194 cmdargs = ' '.join(default_argv() + options)
195
190 _ip = get_ipython()
196 _ip = get_ipython()
191 test_dir = os.path.dirname(__file__)
197 test_dir = os.path.dirname(__file__)
192 full_fname = os.path.join(test_dir, fname)
198 full_fname = os.path.join(test_dir, fname)
193 ipython_cmd = platutils.find_cmd('ipython')
199 ipython_cmd = platutils.find_cmd('ipython')
194 cmdargs = ' '.join(default_argv())
200 full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
195 return genutils.getoutputerror('%s %s' % (ipython_cmd, full_fname))
201 return genutils.getoutputerror(full_cmd)
196
202
197
203
198 def ipexec_validate(fname, expected_out, expected_err=None):
204 def ipexec_validate(fname, expected_out, expected_err=None,
205 options=None):
199 """Utility to call 'ipython filename' and validate output/error.
206 """Utility to call 'ipython filename' and validate output/error.
200
207
201 This function raises an AssertionError if the validation fails.
208 This function raises an AssertionError if the validation fails.
@@ -210,6 +217,12 def ipexec_validate(fname, expected_out, expected_err=None):
210 expected_out : str
217 expected_out : str
211 Expected stdout of the process.
218 Expected stdout of the process.
212
219
220 expected_err : optional, str
221 Expected stderr of the process.
222
223 options : optional, list
224 Extra command-line flags to be passed to IPython.
225
213 Returns
226 Returns
214 -------
227 -------
215 None
228 None
@@ -219,3 +232,25 def ipexec_validate(fname, expected_out, expected_err=None):
219 nt.assert_equals(out.strip(), expected_out.strip())
232 nt.assert_equals(out.strip(), expected_out.strip())
220 if expected_err:
233 if expected_err:
221 nt.assert_equals(err.strip(), expected_err.strip())
234 nt.assert_equals(err.strip(), expected_err.strip())
235
236
237 class TempFileMixin(object):
238 """Utility class to create temporary Python/IPython files.
239
240 Meant as a mixin class for test cases."""
241
242 def mktmp(self, src, ext='.py'):
243 """Make a valid python temp file."""
244 fname, f = temp_pyfile(src, ext)
245 self.tmpfile = f
246 self.fname = fname
247
248 def teardown(self):
249 self.tmpfile.close()
250 try:
251 os.unlink(self.fname)
252 except:
253 # On Windows, even though we close the file, we still can't delete
254 # it. I have no clue why
255 pass
256
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now