Ejemplo n.º 1
0
def test_interactivehooks_ast_modes():
    """
    Test that ast nodes can be triggered with different modes
    """
    saved_mode = ip.ast_node_interactivity
    ip.ast_node_interactivity = "last_expr_or_assign"

    try:
        with AssertPrints("2"):
            ip.run_cell("a = 1+1", store_history=True)

        with AssertPrints("9"):
            ip.run_cell("b = 1+8 # comment with a semicolon;",
                        store_history=False)

        with AssertPrints("7"):
            ip.run_cell("c = 1+6\n#commented_out_function();",
                        store_history=True)

        ip.run_cell("d = 11", store_history=True)
        with AssertPrints("12"):
            ip.run_cell("d += 1", store_history=True)

        with AssertNotPrints("42"):
            ip.run_cell("(u,v) = (41+1, 43-1)")

    finally:
        ip.ast_node_interactivity = saved_mode
Ejemplo n.º 2
0
def test_interactivehooks_ast_modes():
    """
    Test that ast nodes can be triggered with different modes
    """
    saved_mode = ip.ast_node_interactivity
    ip.ast_node_interactivity = 'last_expr_or_assign'

    try:
        with AssertPrints('2'):
            ip.run_cell('a = 1+1', store_history=True)

        with AssertPrints('9'):
            ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False)

        with AssertPrints('7'):
            ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True)

        ip.run_cell('d = 11', store_history=True)
        with AssertPrints('12'):
            ip.run_cell('d += 1', store_history=True)

        with AssertNotPrints('42'):
            ip.run_cell('(u,v) = (41+1, 43-1)')

    finally:
        ip.ast_node_interactivity = saved_mode
Ejemplo n.º 3
0
def test_output_displayed():
    """Checking to make sure that output is displayed"""

    with AssertPrints('2'):
        ip.run_cell('1+1', store_history=True)

    with AssertPrints('2'):
        ip.run_cell('1+1 # comment with a semicolon;', store_history=True)
Ejemplo n.º 4
0
def test_underscore_no_overrite_user():
    ip.run_cell('_ = 42', store_history=True)
    ip.run_cell('1+1', store_history=True)

    with AssertPrints('42'):
        ip.run_cell('print(_)', store_history=True)

    ip.run_cell('del _', store_history=True)
    ip.run_cell('6+6', store_history=True)
    with AssertPrints('12'):
        ip.run_cell('_', store_history=True)
Ejemplo n.º 5
0
def test_output_displayed():
    """Checking to make sure that output is displayed"""

    with AssertPrints("2"):
        ip.run_cell("1+1", store_history=True)

    with AssertPrints("2"):
        ip.run_cell("1+1 # comment with a semicolon;", store_history=True)

    with AssertPrints("2"):
        ip.run_cell("1+1\n#commented_out_function();", store_history=True)
Ejemplo n.º 6
0
def test_underscore_no_overrite_user():
    ip.run_cell("_ = 42", store_history=True)
    ip.run_cell("1+1", store_history=True)

    with AssertPrints("42"):
        ip.run_cell("print(_)", store_history=True)

    ip.run_cell("del _", store_history=True)
    ip.run_cell("6+6", store_history=True)
    with AssertPrints("12"):
        ip.run_cell("_", store_history=True)
Ejemplo n.º 7
0
def test_underscore_no_overrite_builtins():
    ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
    ip.run_cell('3+3', store_history=True)

    with AssertPrints('gettext'):
        ip.run_cell('print(_)', store_history=True)

    ip.run_cell('_ = "userset"', store_history=True)

    with AssertPrints('userset'):
        ip.run_cell('print(_)', store_history=True)
    ip.run_cell('import builtins; del builtins._')
Ejemplo n.º 8
0
def test_underscore_no_overrite_builtins():
    ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
    ip.run_cell("3+3", store_history=True)

    with AssertPrints("gettext"):
        ip.run_cell("print(_)", store_history=True)

    ip.run_cell('_ = "userset"', store_history=True)

    with AssertPrints("userset"):
        ip.run_cell("print(_)", store_history=True)
    ip.run_cell("import builtins; del builtins._")
Ejemplo n.º 9
0
def test_pinfo_no_docstring_if_source():
    """Docstring should not be included with detail_level=1 if source is found"""
    def foo():
        """foo has a docstring"""

    ip.user_ns['foo'] = foo

    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'foo', detail_level=0)
    with AssertPrints('Source:'):
        ip._inspect('pinfo', 'foo', detail_level=1)
    with AssertNotPrints('Docstring:'):
        ip._inspect('pinfo', 'foo', detail_level=1)
Ejemplo n.º 10
0
def test_pinfo_no_docstring_if_source():
    """Docstring should not be included with detail_level=1 if source is found"""
    def foo():
        """foo has a docstring"""

    ip.user_ns["foo"] = foo

    with AssertPrints("Docstring:"):
        ip._inspect("pinfo", "foo", detail_level=0)
    with AssertPrints("Source:"):
        ip._inspect("pinfo", "foo", detail_level=1)
    with AssertNotPrints("Docstring:"):
        ip._inspect("pinfo", "foo", detail_level=1)
Ejemplo n.º 11
0
def test_not_writable_ipdir():
    tmpdir = tempfile.mkdtemp()
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env.pop('XDG_CONFIG_HOME', None)
    env['HOME'] = tmpdir
    ipdir = os.path.join(tmpdir, '.ipython')
    os.mkdir(ipdir)
    os.chmod(ipdir, 600)
    with AssertPrints('is not a writable location', channel='stderr'):
        ipdir = path.get_ipython_dir()
    env.pop('IPYTHON_DIR', None)
Ejemplo n.º 12
0
def test_pinfo_docstring_if_detail_and_no_source():
    """ Docstring should be displayed if source info not available """
    obj_def = '''class Foo(object):
                  """ This is a docstring for Foo """
                  def bar(self):
                      """ This is a docstring for Foo.bar """
                      pass
              '''

    ip.run_cell(obj_def)
    ip.run_cell('foo = Foo()')

    with AssertNotPrints("Source:"):
        with AssertPrints('Docstring:'):
            ip._inspect('pinfo', 'foo', detail_level=0)
        with AssertPrints('Docstring:'):
            ip._inspect('pinfo', 'foo', detail_level=1)
        with AssertPrints('Docstring:'):
            ip._inspect('pinfo', 'foo.bar', detail_level=0)

    with AssertNotPrints('Docstring:'):
        with AssertPrints('Source:'):
            ip._inspect('pinfo', 'foo.bar', detail_level=1)
Ejemplo n.º 13
0
def test_not_writable_ipdir():
    tmpdir = tempfile.mkdtemp()
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env.pop('XDG_CONFIG_HOME', None)
    env['HOME'] = tmpdir
    ipdir = os.path.join(tmpdir, '.ipython')
    os.mkdir(ipdir, 0o555)
    try:
        open(os.path.join(ipdir, "_foo_"), 'w').close()
    except IOError:
        pass
    else:
        # I can still write to an unwritable dir,
        # assume I'm root and skip the test
        raise SkipTest("I can't create directories that I can't write to")
    with AssertPrints('is not a writable location', channel='stderr'):
        ipdir = paths.get_ipython_dir()
    env.pop('IPYTHON_DIR', None)
Ejemplo n.º 14
0
def test_not_writable_ipdir():
    tmpdir = tempfile.mkdtemp()
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env.pop('XDG_CONFIG_HOME', None)
    env['HOME'] = tmpdir
    ipdir = os.path.join(tmpdir, '.ipython')
    os.mkdir(ipdir)
    os.chmod(ipdir, 600)
    try:
        os.listdir(ipdir)
    except OSError:
        pass
    else:
        # I can still read an unreadable dir,
        # assume I'm root and skip the test
        raise SkipTest("I can't create directories that I can't list")
    with AssertPrints('is not a writable location', channel='stderr'):
        ipdir = path.get_ipython_dir()
    env.pop('IPYTHON_DIR', None)
Ejemplo n.º 15
0
def test_pinfo_magic():
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'lsmagic', detail_level=0)

    with AssertPrints('Source:'):
        ip._inspect('pinfo', 'lsmagic', detail_level=1)
Ejemplo n.º 16
0
def test_pinfo_docstring_no_source():
    """Docstring should be included with detail_level=1 if there is no source"""
    with AssertPrints("Docstring:"):
        ip._inspect("pinfo", "str.format", detail_level=0)
    with AssertPrints("Docstring:"):
        ip._inspect("pinfo", "str.format", detail_level=1)
Ejemplo n.º 17
0
def test_pinfo_magic():
    with AssertPrints("Docstring:"):
        ip._inspect("pinfo", "lsmagic", detail_level=0)

    with AssertPrints("Source:"):
        ip._inspect("pinfo", "lsmagic", detail_level=1)
Ejemplo n.º 18
0
def test_pinfo_docstring_no_source():
    """Docstring should be included with detail_level=1 if there is no source"""
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'str.format', detail_level=0)
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'str.format', detail_level=1)