Пример #1
0
def test_sys_exitfunc():
    import clr

    inputScript = testpath.test_inputs_dir + "\\exitFuncRuns.py"
    ipi = IronPythonInstance(executable, exec_prefix,
                             extraArgs + " \"" + inputScript + "\"")
    (result, output, output2, exitCode) = ipi.StartAndRunToCompletion()
    AreEqual(exitCode, 0)
    AreEqual(output.find('hello world') > -1, True)
    ipi.End()

    args = extraArgs

    if clr.GetCurrentRuntime().Configuration.DebugMode:
        args = "-D " + args

    inputScript = testpath.test_inputs_dir + "\\exitFuncRaises.py"
    ipi = IronPythonInstance(executable, exec_prefix,
                             args + " \"" + inputScript + "\"")
    (result, output, output2, exitCode) = ipi.StartAndRunToCompletion()
    AreEqual(exitCode, 0)
    AreEqual(output2.find('Error in sys.exitfunc:') > -1, True)

    AreEqual(output2.find('exitFuncRaises.py", line 19, in foo') > -1, True)

    ipi.End()

    # verify sys.exit(True) and sys.exit(False) return 1 and 0

    ipi = IronPythonInstance(executable, exec_prefix,
                             '-c "import sys; sys.exit(False)"')
    res = ipi.StartAndRunToCompletion()
    AreEqual(res[0], True)  # should have started
    AreEqual(res[1], '')  # no std out
    AreEqual(res[2], '')  # no std err
    AreEqual(res[3], 0)  # should return 0

    ipi = IronPythonInstance(executable, exec_prefix,
                             '-c "import sys; sys.exit(True)"')
    res = ipi.StartAndRunToCompletion()
    AreEqual(res[0], True)  # should have started
    AreEqual(res[1], '')  # no std out
    AreEqual(res[2], '')  # no std err
    AreEqual(res[3], 1)  # should return 0

    # and verify it works at the interactive console as well
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs)
    AreEqual(ipi.Start(), True)

    # parameterless exception
    ipi.ExecuteLine("import sys")
    AreEqual(ipi.ExecuteAndExit("sys.exit(False)"), 0)

    # and verify it works at the interactive console as well
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs)
    AreEqual(ipi.Start(), True)

    # parameterless exception
    ipi.ExecuteLine("import sys")
    AreEqual(ipi.ExecuteAndExit("sys.exit(True)"), 1)
Пример #2
0
def test_ipy_dash_m_pkgs():
    # Python packages work
    import nt
    Assert("testpkg1" in [x.lower() for x in nt.listdir(nt.getcwd())],
           nt.getcwd())

    old_ipy_path = get_environ_variable("IRONPYTHONPATH")
    try:
        nt.environ["IRONPYTHONPATH"] = nt.getcwd()
        ipi = IronPythonInstance(executable, exec_prefix,
                                 extraArgs + " -m testpkg1")
        res, output, err, exit = ipi.StartAndRunToCompletion()
        AreEqual(res, True)  # run should have worked
        AreEqual(exit, 0)  # should have returned 0
        AreEqual(output, "")

        # Bad module names should not work
        ipi = IronPythonInstance(executable, exec_prefix,
                                 extraArgs + " -m libxyz")
        res, output, err, exit = ipi.StartAndRunToCompletion()
        AreEqual(res, True)  # run should have worked
        AreEqual(exit, 1)  # should have returned 0
        Assert("ImportError: No module named libxyz" in err,
               "stderr is:" + str(err))
    finally:
        nt.environ["IRONPYTHONPATH"] = old_ipy_path
Пример #3
0
def test_ipy_dash_m():
    import sys
    for path in sys.path:
        if path.find('Lib') != -1:
            filename = System.IO.Path.Combine(path, 'somemodule.py')
            break

    try:
        f = file(filename, 'w')
        f.write('print "hello"\n')
        f.write('import sys\n')
        f.write('print sys.argv')
        f.close()

        # need to run these tests where we have access to runpy.py
        path = System.IO.FileInfo(__file__).DirectoryName

        # simple case works
        ipi = IronPythonInstance(executable, path,
                                 extraArgs + " -m somemodule")
        res, output, err, exit = ipi.StartAndRunToCompletion()
        AreEqual(res, True)  # run should have worked
        AreEqual(exit, 0)  # should have returned 0
        output = output.replace('\r\n', '\n')
        lines = output.split('\n')
        AreEqual(lines[0], 'hello')
        Assert(samefile(eval(lines[1])[0], filename))

        # we receive any arguments in sys.argv
        ipi = IronPythonInstance(executable, path,
                                 extraArgs + " -m somemodule foo bar")
        res, output, err, exit = ipi.StartAndRunToCompletion()
        AreEqual(res, True)  # run should have worked
        AreEqual(exit, 0)  # should have returned 0
        output = output.replace('\r\n', '\n')
        lines = output.split('\n')
        AreEqual(lines[0], 'hello')
        AreEqual(eval(lines[1]), [filename, 'foo', 'bar'])

        f = file(filename, 'w')
        f.write('print "hello"\n')
        f.write('import sys\n')
        f.write('sys.exit(1)')
        f.close()

        # sys.exit works
        ipi = IronPythonInstance(executable, path,
                                 extraArgs + " -m somemodule")
        res, output, err, exit = ipi.StartAndRunToCompletion()
        AreEqual(res, True)  # run should have worked
        AreEqual(exit, 1)  # should have returned 0
        output = output.replace('\r\n', '\n')
        lines = output.split('\n')
        AreEqual(lines[0], 'hello')
    finally:
        nt.unlink(filename)
Пример #4
0
def test_ipy_dash_m_negative():
    # builtin modules should not work
    for modname in ["sys", "datetime"]:
        ipi = IronPythonInstance(executable, exec_prefix,
                                 extraArgs + " -m " + modname)
        res, output, err, exit = ipi.StartAndRunToCompletion()
        AreEqual(exit, -1)

    # Modules within packages should not work
    ipi = IronPythonInstance(executable, exec_prefix,
                             extraArgs + " -m testpkg1.mod1")
    res, output, err, exit = ipi.StartAndRunToCompletion()
    AreEqual(res, True)  # run should have worked
    AreEqual(exit, 1)  # should have returned 0
    Assert("SyntaxError: invalid syntax" in err, "stderr is:" + str(err))
Пример #5
0
def test_interactive_mode():
    inputScript = testpath.test_inputs_dir + "\\simpleCommand.py"
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs + " -i \"" + inputScript + "\"")
    AreEqual(ipi.Start(), True)
    ipi.EnsureInteractive()
    AreEqual("1", ipi.ExecuteLine("x"))
    ipi.End()
    
    inputScript = testpath.test_inputs_dir + "\\raise.py"
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs + " -i \"" + inputScript + "\"")
    AreEqual(ipi.Start(), True)
    ipi.ReadError()
    ipi.EnsureInteractive()
    AreEqual("1", ipi.ExecuteLine("x"))
    ipi.End()
    
    inputScript = testpath.test_inputs_dir + "\\syntaxError.py"
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs + " -i \"" + inputScript + "\"")
    AreEqual(ipi.Start(), True)
    # ipi.EnsureInteractive()
    AssertContains(ipi.ExecuteLine("x", True), "NameError")
    ipi.End()
    
    inputScript = testpath.test_inputs_dir + "\\exit.py"
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs + " -i \"" + inputScript + "\"")
    (result, output, output2, exitCode) = ipi.StartAndRunToCompletion()
    AreEqual(exitCode, 0)
    ipi.End()
    
    # interactive + -c
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs + " -i -c x=2")
    AreEqual(ipi.Start(), True)
    ipi.EnsureInteractive()
    Assert(ipi.ExecuteLine("x", True).find("2") != -1)
    ipi.End()
Пример #6
0
def test_ipy_dash_c():
    """verify ipy -c cmd doesn't print expression statements"""
    ipi = IronPythonInstance(executable, exec_prefix, "-c True;False")
    res = ipi.StartAndRunToCompletion()
    AreEqual(res[0], True)  # should have started
    AreEqual(res[1], '')  # no std out
    AreEqual(res[2], '')  # no std err
    AreEqual(res[3], 0)  # should return 0
Пример #7
0
def test_sta_sleep_Warning():
    ipi = IronPythonInstance(
        executable, exec_prefix,
        '-c "from System.Threading import Thread;Thread.Sleep(100)"')
    retval, stdouttext, stderrtext, exitcode = ipi.StartAndRunToCompletion()
    Assert(
        stderrtext.endswith(
            "RuntimeWarning: Calling Thread.Sleep on an STA thread doesn't pump messages.  Use Thread.CurrentThread.Join instead.\r\n"
        ))
Пример #8
0
def test_exception_slicing_warning():
    ipi = IronPythonInstance(executable, exec_prefix, '-c "print Exception(*range(2))[1]"')
    res = ipi.StartAndRunToCompletion()
    AreEqual(res[0], True)  # should have started
    AreEqual(res[1], '1\r\n')   # some std out
    AreEqual(res[2], '')    # no std err
    AreEqual(res[3], 0)     # should return 0
    
    ipi = IronPythonInstance(executable, exec_prefix,
        '-3 -c "import warnings;'
        'warnings.filters.reverse();'
        'warnings.filters.pop();'
        'print Exception(*range(2))[1]"')
    res = ipi.StartAndRunToCompletion()
    AreEqual(res[0], True)  # should have started
    AreEqual(res[1], '1\r\n')   # std out
    Assert(res[2].endswith('DeprecationWarning: __getitem__ not supported for exception classes in 3.x; use args attribute\r\n')) #std err
    AreEqual(res[3], 0)     # should return 0
Пример #9
0
def test_thrown_from_thread():
    inputScript = path_combine(testpath.temporary_dir, "throwingfromthread.py")
    write_to_file(inputScript, '''
def f(): raise AssertionError, 'hello'
import thread, time
thread.start_new_thread(f, tuple())
time.sleep(2)
''')
    
    ipi = IronPythonInstance(executable, exec_prefix, extraArgs + " " + inputScript)
    (result, output, output2, exitCode) = ipi.StartAndRunToCompletion()
    AreEqual(exitCode, 0)
    Assert("AssertionError: hello" in output2)
    Assert("IronPython." not in output2)     # '.' is necessary here
    ipi.End()