public bool DoOneInteractive(Frame topFrame) { bool continueInteraction; Stmt s = ReadStatement(out continueInteraction); if (continueInteraction == false) { return(false); } // 's' is null when we parse a line composed only of a NEWLINE (interactive_input grammar); // we don't generate anything when 's' is null if (s != null) { FrameCode code = OutputGenerator.GenerateSnippet(context, s, true); if (ExecWrapper != null) { CallTarget0 t = delegate() { try { code.Run(topFrame); } catch (Exception e) { DumpException(e); } return(null); }; object callable = new Function0(topFrame.__module__, "wrapper", t, new string[0], new object[0]); Ops.Call(ExecWrapper, callable); } else { code.Run(topFrame); } } return(true); }
public void ExecuteFile(string fileName) { Parser p = Parser.FromFile(engineContext.SystemState, context.CopyWithNewSourceFile(fileName)); Stmt s = p.ParseFileInput(); Frame topFrame = new Frame(engineContext.Module); FrameCode code = OutputGenerator.GenerateSnippet(p.CompilerContext, s, false); code.Run(topFrame); }
public int Execute(string text) { Parser p = Parser.FromString(engineContext.SystemState, context, text); Stmt s = p.ParseFileInput(); FrameCode code = OutputGenerator.GenerateSnippet(context, s, true); try { code.Run(new Frame(engineContext.Module)); } catch (PythonSystemExit e) { return(e.GetExitCode(engineContext)); } return(0); }
public void Execute(object code) { FrameCode fc = code as FrameCode; if (fc != null) { fc.Run(new Frame(engineContext.Module)); } else if (code is string) { Execute((string)code); } else { throw new ArgumentException("code object must be string or have been generated via PythonEngine.Compile"); } }
public static object ExecFile(ICallerContext context, object filename, object globals, object locals) { PythonModule mod = context.Module; if (globals == null) { globals = mod.__dict__; } if (locals == null) { locals = globals; } string fname = Converter.ConvertToString(filename); IDictionary <object, object> g = globals as IDictionary <object, object>; if (g == null) { throw Ops.TypeError("execfile: arg 2 must be dictionary"); } CompilerContext cc = context.CreateCompilerContext().CopyWithNewSourceFile(fname); Parser p; try { p = Parser.FromFile(context.SystemState, cc); } catch (UnauthorizedAccessException x) { throw Ops.IOError(x.Message); } Stmt s = p.ParseFileInput(); IDictionary <object, object> l = locals as IDictionary <object, object>; if (l == null) { throw Ops.TypeError("execfile: arg 3 must be dictionary"); } Frame topFrame = new Frame(mod, g, l); FrameCode code = OutputGenerator.GenerateSnippet(cc, s, false); code.Run(topFrame); return(null); }
public static object Eval(ICallerContext context, string expression, IDictionary <object, object> globals, object locals) { if (locals != null && PythonOperator.IsMappingType(context, locals) == Ops.FALSE) { throw Ops.TypeError("locals must be mapping"); } CompilerContext cc = context.CreateCompilerContext(); Parser p = Parser.FromString(context.SystemState, cc, expression.TrimStart(' ', '\t')); Expr e = p.ParseTestListAsExpression(); PythonModule mod = new PythonModule("<eval>", globals, context.SystemState, null, context.ContextFlags); if (Options.FastEval) //!!! experimenting with a HUGE (>100x) performance boost to simple evals { return(e.Evaluate(new NameEnv(mod, locals))); } else { Stmt s = new ReturnStmt(e); FrameCode fc = OutputGenerator.GenerateSnippet(cc, s, false); return(fc.Run(new Frame(mod, globals, locals))); } }