Show More
@@ -0,0 +1,37 b'' | |||||
|
1 | """Tests for the key interactiveshell module. | |||
|
2 | ||||
|
3 | Historically the main classes in interactiveshell have been under-tested. This | |||
|
4 | module should grow as many single-method tests as possible to trap many of the | |||
|
5 | recurring bugs we seem to encounter with high-level interaction. | |||
|
6 | ||||
|
7 | Authors | |||
|
8 | ------- | |||
|
9 | * Fernando Perez | |||
|
10 | """ | |||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Copyright (C) 2011 The IPython Development Team | |||
|
13 | # | |||
|
14 | # Distributed under the terms of the BSD License. The full license is in | |||
|
15 | # the file COPYING, distributed as part of this software. | |||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | ||||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | # Imports | |||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # stdlib | |||
|
22 | import unittest | |||
|
23 | ||||
|
24 | #----------------------------------------------------------------------------- | |||
|
25 | # Tests | |||
|
26 | #----------------------------------------------------------------------------- | |||
|
27 | ||||
|
28 | class InteractiveShellTestCase(unittest.TestCase): | |||
|
29 | def test_naked_string_cells(self): | |||
|
30 | """Test that cells with only naked strings are fully executed""" | |||
|
31 | ip = get_ipython() | |||
|
32 | # First, single-line inputs | |||
|
33 | ip.run_cell('"a"\n') | |||
|
34 | self.assertEquals(ip.user_ns['_'], 'a') | |||
|
35 | # And also multi-line cells | |||
|
36 | ip.run_cell('"""a\nb"""\n') | |||
|
37 | self.assertEquals(ip.user_ns['_'], 'a\nb') |
@@ -260,11 +260,14 b' class DisplayHook(Configurable):' | |||||
260 | self.flush() |
|
260 | self.flush() | |
261 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
261 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise | |
262 | # we cause buggy behavior for things like gettext). |
|
262 | # we cause buggy behavior for things like gettext). | |
|
263 | ||||
263 | if '_' not in __builtin__.__dict__: |
|
264 | if '_' not in __builtin__.__dict__: | |
264 | self.___ = self.__ |
|
265 | self.___ = self.__ | |
265 | self.__ = self._ |
|
266 | self.__ = self._ | |
266 | self._ = result |
|
267 | self._ = result | |
267 |
self.shell.user_ns.update({'_':self._, |
|
268 | self.shell.user_ns.update({'_':self._, | |
|
269 | '__':self.__, | |||
|
270 | '___':self.___}) | |||
268 |
|
271 | |||
269 | # hackish access to top-level namespace to create _1,_2... dynamically |
|
272 | # hackish access to top-level namespace to create _1,_2... dynamically | |
270 | to_main = {} |
|
273 | to_main = {} |
@@ -4,7 +4,7 b'' | |||||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 |
# Copyright (C) 2008-201 |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
@@ -2113,19 +2113,6 b' class InteractiveShell(Configurable, Magic):' | |||||
2113 | self.history_manager.store_inputs(ipy_cell, cell) |
|
2113 | self.history_manager.store_inputs(ipy_cell, cell) | |
2114 |
|
2114 | |||
2115 | self.logger.log(ipy_cell, cell) |
|
2115 | self.logger.log(ipy_cell, cell) | |
2116 | # dbg code!!! |
|
|||
2117 | if 0: |
|
|||
2118 | def myapp(self, val): # dbg |
|
|||
2119 | import traceback as tb |
|
|||
2120 | stack = ''.join(tb.format_stack()) |
|
|||
2121 | print 'Value:', val |
|
|||
2122 | print 'Stack:\n', stack |
|
|||
2123 | list.append(self, val) |
|
|||
2124 |
|
||||
2125 | import new |
|
|||
2126 | self.history_manager.input_hist_parsed.append = types.MethodType(myapp, |
|
|||
2127 | self.history_manager.input_hist_parsed) |
|
|||
2128 | # End dbg |
|
|||
2129 |
|
2116 | |||
2130 | # All user code execution must happen with our context managers active |
|
2117 | # All user code execution must happen with our context managers active | |
2131 | with nested(self.builtin_trap, self.display_trap): |
|
2118 | with nested(self.builtin_trap, self.display_trap): | |
@@ -2167,15 +2154,31 b' class InteractiveShell(Configurable, Magic):' | |||||
2167 | self.execution_count += 1 |
|
2154 | self.execution_count += 1 | |
2168 |
|
2155 | |||
2169 | def run_one_block(self, block): |
|
2156 | def run_one_block(self, block): | |
2170 | """Run a single interactive block. |
|
2157 | """Run a single interactive block of source code. | |
2171 |
|
2158 | |||
2172 | If the block is single-line, dynamic transformations are applied to it |
|
2159 | If the block is single-line, dynamic transformations are applied to it | |
2173 | (like automagics, autocall and alias recognition). |
|
2160 | (like automagics, autocall and alias recognition). | |
|
2161 | ||||
|
2162 | If the block is multi-line, it must consist of valid Python code only. | |||
|
2163 | ||||
|
2164 | Parameters | |||
|
2165 | ---------- | |||
|
2166 | block : string | |||
|
2167 | A (possibly multiline) string of code to be executed. | |||
|
2168 | ||||
|
2169 | Returns | |||
|
2170 | ------- | |||
|
2171 | The output of the underlying execution method used, be it | |||
|
2172 | :meth:`run_source` or :meth:`run_single_line`. | |||
2174 | """ |
|
2173 | """ | |
2175 | if len(block.splitlines()) <= 1: |
|
2174 | if len(block.splitlines()) <= 1: | |
2176 | out = self.run_single_line(block) |
|
2175 | out = self.run_single_line(block) | |
2177 | else: |
|
2176 | else: | |
2178 | out = self.run_code(block) |
|
2177 | # Call run_source, which correctly compiles the input cell. | |
|
2178 | # run_code must only be called when we know we have a code object, | |||
|
2179 | # as it does a naked exec and the compilation mode may not be what | |||
|
2180 | # we wanted. | |||
|
2181 | out = self.run_source(block) | |||
2179 | return out |
|
2182 | return out | |
2180 |
|
2183 | |||
2181 | def run_single_line(self, line): |
|
2184 | def run_single_line(self, line): | |
@@ -2329,7 +2332,7 b' class InteractiveShell(Configurable, Magic):' | |||||
2329 | try: |
|
2332 | try: | |
2330 | try: |
|
2333 | try: | |
2331 | self.hooks.pre_run_code_hook() |
|
2334 | self.hooks.pre_run_code_hook() | |
2332 | #rprint('Running code') # dbg |
|
2335 | #rprint('Running code', repr(code_obj)) # dbg | |
2333 | exec code_obj in self.user_global_ns, self.user_ns |
|
2336 | exec code_obj in self.user_global_ns, self.user_ns | |
2334 | finally: |
|
2337 | finally: | |
2335 | # Reset our crash handler in place |
|
2338 | # Reset our crash handler in place |
@@ -1,5 +1,10 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Tests for the inputsplitter module. |
|
2 | """Tests for the inputsplitter module. | |
|
3 | ||||
|
4 | Authors | |||
|
5 | ------- | |||
|
6 | * Fernando Perez | |||
|
7 | * Robert Kern | |||
3 | """ |
|
8 | """ | |
4 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2010 The IPython Development Team |
|
10 | # Copyright (C) 2010 The IPython Development Team |
@@ -68,11 +68,19 b' class ipnsdict(dict):' | |||||
68 | This subclass adds a simple checkpointing capability so that when testing |
|
68 | This subclass adds a simple checkpointing capability so that when testing | |
69 | machinery clears it (we use it as the test execution context), it doesn't |
|
69 | machinery clears it (we use it as the test execution context), it doesn't | |
70 | get completely destroyed. |
|
70 | get completely destroyed. | |
|
71 | ||||
|
72 | In addition, it can handle the presence of the '_' key in a special manner, | |||
|
73 | which is needed because of how Python's doctest machinery operates with | |||
|
74 | '_'. See constructor and :meth:`update` for details. | |||
71 | """ |
|
75 | """ | |
72 |
|
76 | |||
73 | def __init__(self,*a): |
|
77 | def __init__(self,*a): | |
74 | dict.__init__(self,*a) |
|
78 | dict.__init__(self,*a) | |
75 | self._savedict = {} |
|
79 | self._savedict = {} | |
|
80 | # If this flag is True, the .update() method will unconditionally | |||
|
81 | # remove a key named '_'. This is so that such a dict can be used as a | |||
|
82 | # namespace in doctests that call '_'. | |||
|
83 | self.protect_underscore = False | |||
76 |
|
84 | |||
77 | def clear(self): |
|
85 | def clear(self): | |
78 | dict.clear(self) |
|
86 | dict.clear(self) | |
@@ -86,10 +94,15 b' class ipnsdict(dict):' | |||||
86 | self._checkpoint() |
|
94 | self._checkpoint() | |
87 | dict.update(self,other) |
|
95 | dict.update(self,other) | |
88 |
|
96 | |||
89 | # If '_' is in the namespace, python won't set it when executing code, |
|
97 | if self.protect_underscore: | |
90 | # and we have examples that test it. So we ensure that the namespace |
|
98 | # If '_' is in the namespace, python won't set it when executing | |
91 | # is always 'clean' of it before it's used for test code execution. |
|
99 | # code *in doctests*, and we have multiple doctests that use '_'. | |
92 | self.pop('_',None) |
|
100 | # So we ensure that the namespace is always 'clean' of it before | |
|
101 | # it's used for test code execution. | |||
|
102 | # This flag is only turned on by the doctest machinery, so that | |||
|
103 | # normal test code can assume the _ key is updated like any other | |||
|
104 | # key and can test for its presence after cell executions. | |||
|
105 | self.pop('_', None) | |||
93 |
|
106 | |||
94 | # The builtins namespace must *always* be the real __builtin__ module, |
|
107 | # The builtins namespace must *always* be the real __builtin__ module, | |
95 | # else weird stuff happens. The main ipython code does have provisions |
|
108 | # else weird stuff happens. The main ipython code does have provisions |
@@ -39,11 +39,7 b' from __future__ import absolute_import' | |||||
39 | import re |
|
39 | import re | |
40 | import sys |
|
40 | import sys | |
41 | import unittest |
|
41 | import unittest | |
42 | from doctest import DocTestFinder, DocTestRunner |
|
42 | from doctest import DocTestFinder, DocTestRunner, TestResults | |
43 | try: |
|
|||
44 | from doctest import TestResults |
|
|||
45 | except: |
|
|||
46 | from ._doctest26 import TestResults |
|
|||
47 |
|
43 | |||
48 | # We already have python3-compliant code for parametric tests |
|
44 | # We already have python3-compliant code for parametric tests | |
49 | if sys.version[0]=='2': |
|
45 | if sys.version[0]=='2': |
@@ -273,6 +273,10 b' class DocTestCase(doctests.DocTestCase):' | |||||
273 | # fills with the necessary info from the module being tested). |
|
273 | # fills with the necessary info from the module being tested). | |
274 | _ip.user_ns.update(self._dt_test.globs) |
|
274 | _ip.user_ns.update(self._dt_test.globs) | |
275 | self._dt_test.globs = _ip.user_ns |
|
275 | self._dt_test.globs = _ip.user_ns | |
|
276 | # IPython must protect the _ key in the namespace (it can't exist) | |||
|
277 | # so that Python's doctest code sets it naturally, so we enable | |||
|
278 | # this feature of our testing namespace. | |||
|
279 | _ip.user_ns.protect_underscore = True | |||
276 |
|
280 | |||
277 | super(DocTestCase, self).setUp() |
|
281 | super(DocTestCase, self).setUp() | |
278 |
|
282 | |||
@@ -282,6 +286,9 b' class DocTestCase(doctests.DocTestCase):' | |||||
282 | # teardown doesn't destroy the ipython namespace |
|
286 | # teardown doesn't destroy the ipython namespace | |
283 | if isinstance(self._dt_test.examples[0],IPExample): |
|
287 | if isinstance(self._dt_test.examples[0],IPExample): | |
284 | self._dt_test.globs = self._dt_test_globs_ori |
|
288 | self._dt_test.globs = self._dt_test_globs_ori | |
|
289 | # Restore the behavior of the '_' key in the user namespace to | |||
|
290 | # normal after each doctest, so that unittests behave normally | |||
|
291 | _ip.user_ns.protect_underscore = False | |||
285 |
|
292 | |||
286 | # XXX - fperez: I am not sure if this is truly a bug in nose 0.11, but |
|
293 | # XXX - fperez: I am not sure if this is truly a bug in nose 0.11, but | |
287 | # it does look like one to me: its tearDown method tries to run |
|
294 | # it does look like one to me: its tearDown method tries to run |
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