##// END OF EJS Templates
Merge pull request #1732 from fperez/cellmagics...
Merge pull request #1732 from fperez/cellmagics Refactoring of the magics system and implementation of cell magics. This PR completely refactors the magic system, finally moving the magic objects to standalone, independent objects instead of being the mixin class we'd had since the beginning of IPython. Now, a separate base class is provided in IPython.core.magic.Magics that users can subclass to create their own magics. Decorators are also provided to create magics from simple functions without the need for object orientation. All builtin magics now exist in a few subclasses that group together related functionality, and the new IPython.core.magics package has been created to organize this into smaller files. This cleanup was the last major piece of deep refactoring needed from the original 2001 codebase. Secondly, this PR introduces a new type of magic function, prefixed with `%%` instead of `%`, which operates at the cell level. A cell magic receives two arguments: the line it is called on (like a line magic) and the body of the cell below it. Cell magics are most natural in the notebook, but they also work in the terminal and qt console, with the usual approach of using a blank line to signal cell termination. This PR closes #1611, or IPEP 1, where the design had been discussed.

File last commit:

r6303:9c22fc1e
r7011:61eb2ffe merge
Show More
test_run.py
250 lines | 7.6 KiB | text/x-python | PythonLexer
Thomas Kluyver
Add encoding cookie to test_run.
r6303 # encoding: utf-8
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 """Tests for code execution (%run and related), which is particularly tricky.
Because of how %run manages namespaces, and the fact that we are trying here to
verify subtle object deletion and reference counting issues, the %run tests
will be kept in this separate file. This makes it easier to aggregate in one
place the tricks needed to handle it; most other magics are much easier to test
and we do so in a common test_magic file.
"""
from __future__ import absolute_import
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import sys
import tempfile
import nose.tools as nt
Min RK
fix various tests on Windows...
r4105 from nose import SkipTest
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
from IPython.testing import decorators as dec
from IPython.testing import tools as tt
Thomas Kluyver
Fix almost all IPython.core tests for Python 3.
r4896 from IPython.utils import py3compat
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
#-----------------------------------------------------------------------------
# Test functions begin
#-----------------------------------------------------------------------------
def doctest_refbug():
"""Very nasty problem with references held by multiple runs of a script.
Thomas Kluyver
Replace links to launchpad bugs in comments/docstrings with equivalent github links.
r3917 See: https://github.com/ipython/ipython/issues/141
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
In [1]: _ip.clear_main_mod_cache()
# random
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 In [2]: %run refbug
In [3]: call_f()
lowercased: hello
In [4]: %run refbug
In [5]: call_f()
lowercased: hello
lowercased: hello
"""
def doctest_run_builtins():
r"""Check that %run doesn't damage __builtins__.
In [1]: import tempfile
In [2]: bid1 = id(__builtins__)
In [3]: fname = tempfile.mkstemp('.py')[1]
In [3]: f = open(fname,'w')
Thomas Kluyver
Fix various tests in IPython.core for Python 3.
r4895 In [4]: dummy= f.write('pass\n')
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
In [5]: f.flush()
In [6]: t1 = type(__builtins__)
Fernando Perez
Do not call %run with quotes, since on Windows quotes aren't stripped....
r2484 In [7]: %run $fname
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
In [7]: f.close()
In [8]: bid2 = id(__builtins__)
In [9]: t2 = type(__builtins__)
In [10]: t1 == t2
Out[10]: True
In [10]: bid1 == bid2
Out[10]: True
In [12]: try:
....: os.unlink(fname)
....: except:
....: pass
Bernardo B. Marques
remove all trailling spaces
r4872 ....:
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 """
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107
Thomas Kluyver
Fix almost all IPython.core tests for Python 3.
r4896 @py3compat.doctest_refactor_print
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107 def doctest_reset_del():
"""Test that resetting doesn't cause errors in __del__ methods.
In [2]: class A(object):
...: def __del__(self):
Thomas Kluyver
Make test work (i.e. it now fails, as expected).
r3156 ...: print str("Hi")
Bernardo B. Marques
remove all trailling spaces
r4872 ...:
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107
In [3]: a = A()
In [4]: get_ipython().reset()
Thomas Kluyver
Make test work (i.e. it now fails, as expected).
r3156 Hi
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107
In [5]: 1+1
Out[5]: 2
"""
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
# For some tests, it will be handy to organize them in a class with a common
# setup that makes a temp file
Fernando Perez
Fix extensions test suite (small, but now it runs and passes!)
r2415 class TestMagicRunPass(tt.TempFileMixin):
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def setup(self):
"""Make a valid python temp file."""
self.mktmp('pass\n')
Alcides
- Test case correctly fails....
r5238
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 def run_tmpfile(self):
_ip = get_ipython()
# This fails on Windows if self.tmpfile.name has spaces or "~" in it.
# See below and ticket https://bugs.launchpad.net/bugs/366353
Fernando Perez
Do not call %run with quotes, since on Windows quotes aren't stripped....
r2484 _ip.magic('run %s' % self.fname)
Alcides
- Test case correctly fails....
r5238
def run_tmpfile_p(self):
_ip = get_ipython()
# This fails on Windows if self.tmpfile.name has spaces or "~" in it.
# See below and ticket https://bugs.launchpad.net/bugs/366353
_ip.magic('run -p %s' % self.fname)
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def test_builtins_id(self):
"""Check that %run doesn't damage __builtins__ """
_ip = get_ipython()
# Test that the id of __builtins__ is not modified by %run
bid1 = id(_ip.user_ns['__builtins__'])
self.run_tmpfile()
bid2 = id(_ip.user_ns['__builtins__'])
tt.assert_equals(bid1, bid2)
def test_builtins_type(self):
"""Check that the type of __builtins__ doesn't change with %run.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 However, the above could pass if __builtins__ was already modified to
be a dict (it should be a module) by a previous use of %run. So we
also check explicitly that it really is a module:
"""
_ip = get_ipython()
self.run_tmpfile()
tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
def test_prompts(self):
"""Test that prompts correctly generate after %run"""
self.run_tmpfile()
_ip = get_ipython()
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 p2 = _ip.prompt_manager.render('in2').strip()
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 nt.assert_equals(p2[:3], '...')
Alcides
- Test case correctly fails....
r5238
def test_run_profile( self ):
"""Test that the option -p, which invokes the profiler, do not
crash by invoking execfile"""
_ip = get_ipython()
self.run_tmpfile_p()
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Fernando Perez
Fix extensions test suite (small, but now it runs and passes!)
r2415 class TestMagicRunSimple(tt.TempFileMixin):
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def test_simpledef(self):
"""Test that simple class definitions work."""
src = ("class foo: pass\n"
"def f(): return foo()")
self.mktmp(src)
Fernando Perez
More win32 test fixes and a new test....
r2451 _ip.magic('run %s' % self.fname)
Thomas Kluyver
Remove runlines method and calls to it.
r3752 _ip.run_cell('t = isinstance(f(), foo)')
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 nt.assert_true(_ip.user_ns['t'])
Fernando Perez
Various fixes for IPython.core tests to pass under win32.
r2446
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 def test_obj_del(self):
"""Test that object's __del__ methods are called on exit."""
Min RK
fix various tests on Windows...
r4105 if sys.platform == 'win32':
try:
import win32api
except ImportError:
raise SkipTest("Test requires pywin32")
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 src = ("class A(object):\n"
" def __del__(self):\n"
" print 'object A deleted'\n"
"a = A()\n")
Thomas Kluyver
Fix almost all IPython.core tests for Python 3.
r4896 self.mktmp(py3compat.doctest_refactor_print(src))
MinRK
Allow IPython to run without sqlite3...
r5147 if dec.module_not_available('sqlite3'):
err = 'WARNING: IPython History requires SQLite, your history will not be saved\n'
else:
err = None
tt.ipexec_validate(self.fname, 'object A deleted', err)
@dec.skip_known_failure
Paul Ivanov
test for GH-238 %run's aggressive name cleaning
r3502 def test_aggressive_namespace_cleanup(self):
"""Test that namespace cleanup is not too aggressive GH-238
Paul Ivanov
added the skip_known decorator
r3503
Returning from another run magic deletes the namespace"""
Paul Ivanov
test for GH-238 %run's aggressive name cleaning
r3502 # see ticket https://github.com/ipython/ipython/issues/238
class secondtmp(tt.TempFileMixin): pass
empty = secondtmp()
empty.mktmp('')
src = ("ip = get_ipython()\n"
"for i in range(5):\n"
" try:\n"
" ip.magic('run %s')\n"
" except NameError, e:\n"
" print i;break\n" % empty.fname)
Thomas Kluyver
Fix almost all IPython.core tests for Python 3.
r4896 self.mktmp(py3compat.doctest_refactor_print(src))
Paul Ivanov
test for GH-238 %run's aggressive name cleaning
r3502 _ip.magic('run %s' % self.fname)
Thomas Kluyver
Remove runlines method and calls to it.
r3752 _ip.run_cell('ip == get_ipython()')
Paul Ivanov
test for GH-238 %run's aggressive name cleaning
r3502 tt.assert_equals(_ip.user_ns['i'], 5)
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Fernando Perez
Various fixes for IPython.core tests to pass under win32.
r2446 @dec.skip_win32
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 def test_tclass(self):
mydir = os.path.dirname(__file__)
tc = os.path.join(mydir, 'tclass')
src = ("%%run '%s' C-first\n"
Thomas Kluyver
Make test of %run more rigorous.
r3763 "%%run '%s' C-second\n"
"%%run '%s' C-third\n") % (tc, tc, tc)
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 self.mktmp(src, '.ipy')
out = """\
MinRK
fix tests that depended on unicode sys.argv
r5126 ARGV 1-: ['C-first']
ARGV 1-: ['C-second']
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 tclass.py: deleting object: C-first
MinRK
fix tests that depended on unicode sys.argv
r5126 ARGV 1-: ['C-third']
Thomas Kluyver
Shell's reset method clears namespace from last %run command.
r3762 tclass.py: deleting object: C-second
Thomas Kluyver
Make test of %run more rigorous.
r3763 tclass.py: deleting object: C-third
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 """
MinRK
Allow IPython to run without sqlite3...
r5147 if dec.module_not_available('sqlite3'):
err = 'WARNING: IPython History requires SQLite, your history will not be saved\n'
else:
err = None
tt.ipexec_validate(self.fname, out, err)
Thomas Kluyver
Add test for gh-693 (%run -i after %reset).
r5466
def test_run_i_after_reset(self):
"""Check that %run -i still works after %reset (gh-693)"""
src = "yy = zz\n"
self.mktmp(src)
_ip.run_cell("zz = 23")
_ip.magic('run -i %s' % self.fname)
tt.assert_equals(_ip.user_ns['yy'], 23)
_ip.magic('reset -f')
_ip.run_cell("zz = 23")
_ip.magic('run -i %s' % self.fname)
tt.assert_equals(_ip.user_ns['yy'], 23)
Thomas Kluyver
Fix for %run on a Python file using non-default encoding.
r6300
def test_unicode(self):
"""Check that files in odd encodings are accepted."""
mydir = os.path.dirname(__file__)
na = os.path.join(mydir, 'nonascii.py')
_ip.magic('run %s' % na)
tt.assert_equals(_ip.user_ns['u'], u'Ўт№Ф')