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)
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()
def test_remote_console_processes(): # First check that a simple local console uses a single process ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) consoleProcessId, remoteRuntimeProcessId = get_process_ids(ipi) AreEqual(consoleProcessId, remoteRuntimeProcessId) ipi.End() # Now use the remote console ipi = start_remote_console() consoleProcessId, remoteRuntimeProcessId = get_process_ids(ipi) AreNotEqual(consoleProcessId, remoteRuntimeProcessId) ipi.End()
def test_dump_exception(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs + " -X:ExceptionDetail") AreEqual(ipi.Start(), True) response = ipi.ExecuteLine("raise 'goodbye'", True) AreEqual(response.count("IronPython.Hosting") >= 1, True) ipi.End()
def test_from_cmdline(): ''' ''' from iptest.console_util import IronPythonInstance from sys import executable, exec_prefix from System import Environment extraArgs = "" if "-X:PreferComInteropAssembly" in Environment.GetCommandLineArgs(): extraArgs += "-X:PreferComInteropAssembly" #Does it work from the commandline with positive cases? ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) try: ipi.ExecuteLine("from System import Type, Activator") ipi.ExecuteLine( "com_obj = Activator.CreateInstance(Type.GetTypeFromProgID('DlrComLibrary.DlrUniversalObj'))" ) #Dev10 409941 Assert("System.__ComObject" in ipi.ExecuteLine("print com_obj")) AreEqual(ipi.ExecuteLine("print com_obj.m0()"), "None") finally: ipi.End()
def test_strings(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) # String exception response = ipi.ExecuteLine("raise 'foo'", True) AreEqual( response.replace("\r\r\n", "\n").replace("\r", ""), """Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: exceptions must be classes or instances, not str""") # Multi-line string literal ipi.ExecutePartialLine("\"\"\"Hello") ipi.ExecutePartialLine("") ipi.ExecutePartialLine("") AreEqual("'Hello\\n\\n\\nWorld'", ipi.ExecuteLine("World\"\"\"")) ipi.ExecutePartialLine("if False: print 3") ipi.ExecutePartialLine("else: print 'hello'") AreEqual(r'hello', ipi.ExecuteLine("")) # Empty line AreEqual("", ipi.ExecuteLine("")) ipi.End()
def test_future_division(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecuteLine("from __future__ import division") response = ipi.ExecuteLine("11/4") AreEqual(response, "2.75") ipi.End()
def test_ipy_dash(): #Verify that typing a - in the arguments starts an interactive session ipi = IronPythonInstance(executable, exec_prefix, "-") AreEqual(ipi.Start(), True) response = ipi.ExecuteLine("42") AreEqual(response, "42") ipi.End()
def test_from_cmdline_neg(): ''' ''' from iptest.console_util import IronPythonInstance from sys import executable, exec_prefix from System import Environment extraArgs = "" if "-X:PreferComInteropAssembly" in Environment.GetCommandLineArgs(): extraArgs += "-X:PreferComInteropAssembly" #Does it work from the commandline with negative cases? ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) try: ipi.ExecuteLine("from System import Type, Activator") ipi.ExecuteLine("import sys") ipi.ExecuteLine( "com_obj = Activator.CreateInstance(Type.GetTypeFromProgID('DlrComLibrary.DlrUniversalObj'))" ) ipi.ExecuteLine("sys.stderr = sys.stdout" ) #Limitation of ipi. Cannot get sys.stderr... response = ipi.ExecuteLine("com_obj.m0(3)") Assert("Does not support a collection." in response) #Merlin 324233 Assert("EnvironmentError:" in response) #Merlin 324233 finally: ipi.End()
def test_incomplate_syntax(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("class K:") response = ipi.ExecuteLine("", True) Assert("IndentationError:" in response) ipi.End()
def test_missing_test(): for x in ['if', 'while', 'for', 'try']: ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) response = ipi.ExecuteLine(x, True) Assert("SyntaxError:" in response) ipi.End()
def test_indentation(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("if False:") ipi.ExecutePartialLine(" print 'hello'") response = ipi.ExecuteLine(" print 'goodbye'", True) AreEqual(response.find('IndentationError') > 1, True) ipi.End()
def test_logo(self): from iptest.console_util import IronPythonInstance i = IronPythonInstance(sys.executable, sys.exec_prefix, "") self.assertEqual(i.proc.Start(), True) i.reader = i.proc.StandardOutput x = i.EatToPrompt() self.assertTrue(x.find('\r\r\n') == -1) i.End()
def TestInteractive(args, expected_exitcode=0): ipi = IronPythonInstance(sys.executable, sys.exec_prefix, args) AreEqual(ipi.Start(), True) #Verify basic behavior AreEqual("4", ipi.ExecuteLine("2+2")) ipi.End()
def TestInteractive(self, args, expected_exitcode = 0): from iptest.console_util import IronPythonInstance ipi = IronPythonInstance(sys.executable, sys.exec_prefix, args, '-X:BasicConsole') self.assertEqual(ipi.Start(), True) #Verify basic behavior self.assertEqual("4", ipi.ExecuteLine("2+2")) ipi.End()
def test_indentation_levels(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("class K:") ipi.ExecutePartialLine(" def M(self):") ipi.ExecutePartialLine(" if 1:") ipi.ExecutePartialLine(" pass") response = ipi.ExecuteLine("") ipi.End()
def test_mta(): ipi = IronPythonInstance(executable, exec_prefix, '-X:MTA') AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("class C:pass") response = ipi.ExecuteLine("") AreEqual(response, "") ipi.ExecutePartialLine("class D(C):") response = ipi.ExecuteLine("", True) Assert("IndentationError:" in response) ipi.End()
def test_whitespace(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecuteLine(" ") response = ipi.ExecuteLine("") ipi.End() ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecuteLine(" ") response = ipi.ExecuteLine("2") Assert("2" in response) ipi.End() ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecuteLine(" ") response = ipi.ExecuteLine(" 2", True) Assert("SyntaxError:" in response) ipi.End()
def test_try_except(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("try:") ipi.ExecutePartialLine(" raise Exception('foo')") ipi.ExecutePartialLine("except Exception, e:") ipi.ExecutePartialLine(" if e.message=='foo':") ipi.ExecutePartialLine(" print 'okay'") response = ipi.ExecuteLine("") Assert(response.find('okay') > -1) ipi.End()
def test_future_with(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("class K(object):") ipi.ExecutePartialLine(" def __enter__(self): return 3.14") ipi.ExecutePartialLine(" def __exit__(self, type, value, tb): return False") ipi.ExecuteLine("") ipi.ExecutePartialLine("with K() as d:") ipi.ExecutePartialLine(" print d") response = ipi.ExecuteLine("") AreEqual(response, "3.14") ipi.End()
def test_exceptions(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) # parameterless exception response = ipi.ExecuteLine("raise Exception", True) AreEqual( response, '''Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception'''.replace("\n", "\r\r\n") + "\r") ipi.End()
def test_mta(): ipi = IronPythonInstance(executable, exec_prefix, '-X:MTA') AreEqual(ipi.Start(), True) ipi.ExecuteLine("import System") response = ipi.ExecuteLine("str(System.Threading.Thread.CurrentThread.ApartmentState)") AreEqual(response, "'MTA'") ipi.ExecutePartialLine("class C:pass") response = ipi.ExecuteLine("") AreEqual(response, "") response = ipi.ExecuteLine("str(System.Threading.Thread.CurrentThread.ApartmentState)") AreEqual(response, "'MTA'") ipi.End()
def test_incomplate_syntax_backslash(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) for i in xrange(4): for j in xrange(i): ipi.ExecutePartialLine("\\") ipi.ExecutePartialLine("1 + \\") for j in xrange(i): ipi.ExecutePartialLine("\\") response = ipi.ExecuteLine("2", True) Assert("3" in response) ipi.End()
def test_partial_tuples(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("(2") ipi.ExecutePartialLine(" ,") ipi.ExecutePartialLine(" 3") response = ipi.ExecuteLine(")") Assert("(2, 3)" in response) ipi.ExecutePartialLine("(") response = ipi.ExecuteLine(")") Assert("()" in response) ipi.ExecutePartialLine("'abc %s %s %s %s %s' % (") ipi.ExecutePartialLine(" 'def'") ipi.ExecutePartialLine(" ,'qrt',") ipi.ExecutePartialLine(" 'jkl'") ipi.ExecutePartialLine(",'jkl'") ipi.ExecutePartialLine("") ipi.ExecutePartialLine(",") ipi.ExecutePartialLine("") ipi.ExecutePartialLine("") ipi.ExecutePartialLine("'123'") response = ipi.ExecuteLine(")") Assert("'abc def qrt jkl jkl 123'" in response) ipi.ExecutePartialLine("a = (") ipi.ExecutePartialLine(" 1") ipi.ExecutePartialLine(" , ") ipi.ExecuteLine(")") response = ipi.ExecuteLine("a") Assert("(1,)" in response) ipi.ExecutePartialLine("(") ipi.ExecutePartialLine("'joe'") ipi.ExecutePartialLine(" ") ipi.ExecutePartialLine(" #") ipi.ExecutePartialLine(",") ipi.ExecutePartialLine("2") response = ipi.ExecuteLine(")") Assert("('joe', 2)" in response) ipi.ExecutePartialLine("(") ipi.ExecutePartialLine("") ipi.ExecutePartialLine("") response = ipi.ExecuteLine(")") Assert("()" in response) ipi.End()
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()
def test_partial_lists(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("[1") ipi.ExecutePartialLine(" ,") ipi.ExecutePartialLine(" 2") response = ipi.ExecuteLine("]") Assert("[1, 2]" in response) ipi.ExecutePartialLine("[") ipi.ExecutePartialLine("") ipi.ExecutePartialLine("") response = ipi.ExecuteLine("]") Assert("[]" in response) ipi.End()
def test_exceptions_nested(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) ipi.ExecutePartialLine("def a(): return b()") ipi.ExecuteLine("") ipi.ExecutePartialLine("def b(): return 1/0") ipi.ExecuteLine("") response = ipi.ExecuteLine("a()", True) response = response.replace("\r\r\n", "\n").strip() Assert(response.startswith('''Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in a File "<stdin>", line 1, in b ZeroDivisionError:'''), response) ipi.End()
def test_comments(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) response = ipi.ExecuteLine("# this is some comment line") AreEqual(response, "") response = ipi.ExecuteLine(" # this is some comment line") AreEqual(response, "") response = ipi.ExecuteLine("# this is some more comment line") AreEqual(response, "") ipi.ExecutePartialLine("if 100:") ipi.ExecutePartialLine(" print 100") ipi.ExecutePartialLine("# this is some more comment line inside if") ipi.ExecutePartialLine("# this is some indented comment line inside if") ipi.ExecutePartialLine(" print 200") response = ipi.ExecuteLine("") AreEqual(response, "100" + newline + "200") ipi.End()
def test_console_input_output(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) input_output = [ ("x=100", ""), ("x=200\n", ""), ("\nx=300", ""), ("\nx=400\n", ""), ("500", "500"), ("600\n\n\n\n\n\n\n\n\n\n\n", "600"), ("valid=3;more_valid=4;valid", "3"), ("valid=5;more_valid=6;more_valid\n\n\n\n\n", "6"), ("valid=7;more_valid=8;#valid", ""), ("valid=9;valid;# more_valid\n", "9"), ("valid=11;more_valid=12;more_valid# should be valid input\n\n\n\n", "12"), ] for x in input_output: AreEqual(ipi.Start(), True) AreEqual(ipi.ExecuteLine(x[0]), x[1]) ipi.End()
def test_partial_lists_cp3530(): ipi = IronPythonInstance(executable, exec_prefix, extraArgs) AreEqual(ipi.Start(), True) try: ipi.ExecutePartialLine("[{'a':None},") response = ipi.ExecuteLine("]") Assert("[{'a': None}]" in response, response) ipi.ExecutePartialLine("[{'a'") response = ipi.ExecutePartialLine(":None},") response = ipi.ExecuteLine("]") Assert("[{'a': None}]" in response, response) ipi.ExecutePartialLine("[{'a':None},") ipi.ExecutePartialLine("1,") response = ipi.ExecuteLine("2]") Assert("[{'a': None}, 1, 2]" in response, response) finally: ipi.End()