Ejemplo n.º 1
0
 def test_cb_error(self):
     cb = Mock(side_effect=ValueError)
     self.em.register('ping_received', cb)
     with tt.AssertPrints("Error in callback"):
         self.em.trigger('ping_received')
Ejemplo n.º 2
0
 def test_lines_non_integer(self, ipython):
     '''Ensure that invalid input is gracefully handled.'''
     with tt.AssertPrints(self.error_integer_expected):
         ipython.run_line_magic('rerun', '-l one')
Ejemplo n.º 3
0
 def func():
     with tt.AssertPrints("abc"):
         print("acd")
         print("def")
         print(b"ghi")
Ejemplo n.º 4
0
def test_bookmark():
    ip = get_ipython()
    ip.run_line_magic('bookmark', 'bmname')
    with tt.AssertPrints('bmname'):
        ip.run_line_magic('bookmark', '-l')
    ip.run_line_magic('bookmark', '-d bmname')
Ejemplo n.º 5
0
def test_psearch():
    with tt.AssertPrints("dict.fromkeys"):
        _ip.run_cell("dict.fr*?")
Ejemplo n.º 6
0
 def test_exception_during_handling_error(self):
     with tt.AssertPrints(["KeyError", "NameError", "During handling"]):
         ip.run_cell(self.EXCEPTION_DURING_HANDLING_CODE)
Ejemplo n.º 7
0
 def test_recursion_one_frame(self):
     with tt.AssertPrints("1 frames repeated"):
         ip.run_cell("r1()")
Ejemplo n.º 8
0
 def test_memoryerror(self):
     memoryerror_code = "(" * 200 + ")" * 200
     with tt.AssertPrints("MemoryError"):
         ip.run_cell(memoryerror_code)
Ejemplo n.º 9
0
 def test_plain_direct_cause_error(self):
     with tt.AssertPrints(["KeyError", "NameError", "direct cause"]):
         ip.run_cell("%xmode Plain")
         ip.run_cell(self.DIRECT_CAUSE_ERROR_CODE)
         ip.run_cell("%xmode Verbose")
Ejemplo n.º 10
0
 def test_passing(self):
     with tt.AssertPrints("abc"):
         print "abcd"
         print "def"
         print b"ghi"
Ejemplo n.º 11
0
 def func():
     with tt.AssertPrints("abc"):
         print "acd"
         print "def"
         print b"ghi"
Ejemplo n.º 12
0
 def test_recursion_three_frames(self):
     with tt.AssertPrints("[... skipping similar frames: "), \
             tt.AssertPrints(re.compile(r"r3a at line 8 \(\d{2} times\)"), suppress=False), \
             tt.AssertPrints(re.compile(r"r3b at line 11 \(\d{2} times\)"), suppress=False), \
             tt.AssertPrints(re.compile(r"r3c at line 14 \(\d{2} times\)"), suppress=False):
         ip.run_cell("r3o2()")
Ejemplo n.º 13
0
 def test_recursion_one_frame(self):
     with tt.AssertPrints(
             re.compile(
                 r"\[\.\.\. skipping similar frames: r1 at line 5 \(\d{2,3} times\)\]"
             )):
         ip.run_cell("r1()")
Ejemplo n.º 14
0
 def test_invalid_input(self, ipython):
     with tt.AssertPrints('Valid modes: (0->Off, 1->Smart, 2->Full'):
         ipython.run_line_magic('autocall', 'random')
Ejemplo n.º 15
0
def test_bookmark():
    ip = get_ipython()
    ip.run_line_magic("bookmark", "bmname")
    with tt.AssertPrints("bmname"):
        ip.run_line_magic("bookmark", "-l")
    ip.run_line_magic("bookmark", "-d bmname")
 def test_non_int_const(self):
     with tt.AssertPrints("hello"):
         ip.run_cell('print("hello")')
Ejemplo n.º 17
0
 def test_direct_cause_error(self):
     with tt.AssertPrints(["KeyError", "NameError", "direct cause"]):
         ip.run_cell(self.DIRECT_CAUSE_ERROR_CODE)
Ejemplo n.º 18
0
 def test_cb_keyboard_interrupt(self):
     cb = Mock(side_effect=KeyboardInterrupt)
     self.em.register('ping_received', cb)
     with tt.AssertPrints("Error in callback"):
         self.em.trigger('ping_received')
Ejemplo n.º 19
0
 def test_suppress_exception_chaining(self):
     with tt.AssertNotPrints("ZeroDivisionError"), \
          tt.AssertPrints("ValueError", suppress=False):
         ip.run_cell(self.SUPPRESS_CHAINING_CODE)
Ejemplo n.º 20
0
def test_psearch():
    with tt.AssertPrints("dict.fromkeys"):
        _ip.run_cell("dict.fr*?")
    with tt.AssertPrints("π.is_integer"):
        _ip.run_cell("π = 3.14;\nπ.is_integ*?")
Ejemplo n.º 21
0
 def test_recursion_three_frames(self):
     with tt.AssertPrints("3 frames repeated"):
         ip.run_cell("r3o2()")
Ejemplo n.º 22
0
    def _check_smoketest(self, use_aimport=True):
        """
        Functional test for the automatic reloader using either
        '%autoreload 1' or '%autoreload 2'
        """

        mod_name, mod_fn = self.new_module("""
x = 9

z = 123  # this item will be deleted

def foo(y):
    return y + 3

class Baz(object):
    def __init__(self, x):
        self.x = x
    def bar(self, y):
        return self.x + y
    @property
    def quux(self):
        return 42
    def zzz(self):
        '''This method will be deleted below'''
        return 99

class Bar:    # old-style class: weakref doesn't work for it on Python < 2.7
    def foo(self):
        return 1
""")

        #
        # Import module, and mark for reloading
        #
        if use_aimport:
            self.shell.magic_autoreload("1")
            self.shell.magic_aimport(mod_name)
            stream = StringIO()
            self.shell.magic_aimport("", stream=stream)
            nt.assert_true(("Modules to reload:\n%s" % mod_name) in
                           stream.getvalue())

            nt.assert_raises(
                ImportError,
                self.shell.magic_aimport, "tmpmod_as318989e89ds")
        else:
            self.shell.magic_autoreload("2")
            self.shell.run_code("import %s" % mod_name)
            stream = StringIO()
            self.shell.magic_aimport("", stream=stream)
            nt.assert_true("Modules to reload:\nall-except-skipped" in
                           stream.getvalue())
        nt.assert_in(mod_name, self.shell.ns)

        mod = sys.modules[mod_name]

        #
        # Test module contents
        #
        old_foo = mod.foo
        old_obj = mod.Baz(9)
        old_obj2 = mod.Bar()

        def check_module_contents():
            nt.assert_equal(mod.x, 9)
            nt.assert_equal(mod.z, 123)

            nt.assert_equal(old_foo(0), 3)
            nt.assert_equal(mod.foo(0), 3)

            obj = mod.Baz(9)
            nt.assert_equal(old_obj.bar(1), 10)
            nt.assert_equal(obj.bar(1), 10)
            nt.assert_equal(obj.quux, 42)
            nt.assert_equal(obj.zzz(), 99)

            obj2 = mod.Bar()
            nt.assert_equal(old_obj2.foo(), 1)
            nt.assert_equal(obj2.foo(), 1)

        check_module_contents()

        #
        # Simulate a failed reload: no reload should occur and exactly
        # one error message should be printed
        #
        self.write_file(mod_fn, """
a syntax error
""")

        with tt.AssertPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
            self.shell.run_code("pass") # trigger reload
        with tt.AssertNotPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
            self.shell.run_code("pass") # trigger another reload
        check_module_contents()

        #
        # Rewrite module (this time reload should succeed)
        #
        self.write_file(mod_fn, """
x = 10

def foo(y):
    return y + 4

class Baz(object):
    def __init__(self, x):
        self.x = x
    def bar(self, y):
        return self.x + y + 1
    @property
    def quux(self):
        return 43

class Bar:    # old-style class
    def foo(self):
        return 2
""")

        def check_module_contents():
            nt.assert_equal(mod.x, 10)
            nt.assert_false(hasattr(mod, 'z'))

            nt.assert_equal(old_foo(0), 4) # superreload magic!
            nt.assert_equal(mod.foo(0), 4)

            obj = mod.Baz(9)
            nt.assert_equal(old_obj.bar(1), 11) # superreload magic!
            nt.assert_equal(obj.bar(1), 11)

            nt.assert_equal(old_obj.quux, 43)
            nt.assert_equal(obj.quux, 43)

            nt.assert_false(hasattr(old_obj, 'zzz'))
            nt.assert_false(hasattr(obj, 'zzz'))

            obj2 = mod.Bar()
            nt.assert_equal(old_obj2.foo(), 2)
            nt.assert_equal(obj2.foo(), 2)

        self.shell.run_code("pass") # trigger reload
        check_module_contents()

        #
        # Another failure case: deleted file (shouldn't reload)
        #
        os.unlink(mod_fn)

        self.shell.run_code("pass") # trigger reload
        check_module_contents()

        #
        # Disable autoreload and rewrite module: no reload should occur
        #
        if use_aimport:
            self.shell.magic_aimport("-" + mod_name)
            stream = StringIO()
            self.shell.magic_aimport("", stream=stream)
            nt.assert_true(("Modules to skip:\n%s" % mod_name) in
                           stream.getvalue())

            # This should succeed, although no such module exists
            self.shell.magic_aimport("-tmpmod_as318989e89ds")
        else:
            self.shell.magic_autoreload("0")

        self.write_file(mod_fn, """
x = -99
""")

        self.shell.run_code("pass") # trigger reload
        self.shell.run_code("pass")
        check_module_contents()

        #
        # Re-enable autoreload: reload should now occur
        #
        if use_aimport:
            self.shell.magic_aimport(mod_name)
        else:
            self.shell.magic_autoreload("")

        self.shell.run_code("pass") # trigger reload
        nt.assert_equal(mod.x, -99)
Ejemplo n.º 23
0
def test_time2():
    ip = get_ipython()

    with tt.AssertPrints("CPU times: user "):
        ip.run_cell("%time None")
Ejemplo n.º 24
0
 def test_passing(self):
     with tt.AssertPrints("abc"):
         print("abcd")
         print("def")
         print(b"ghi")
Ejemplo n.º 25
0
 def test_syntaxerror_without_lineno(self):
     with tt.AssertNotPrints("TypeError"):
         with tt.AssertPrints("line unknown"):
             ip.run_cell("raise SyntaxError()")
Ejemplo n.º 26
0
 def test_search_failed(self, ipython, history):
     with tt.AssertPrints("Couldn't evaluate or find in history"):
         ipython.run_line_magic('recall', 'not_in_ns_or_history')