/// <summary> /// Python ctor - maps to function.__new__ /// /// y = func(x.__code__, globals(), 'foo', None, (a, )) /// </summary> public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name, PythonTuple defaults, PythonTuple closure) { if (closure != null && closure.__len__() != 0) { throw new NotImplementedException("non empty closure argument is not supported"); } if (globals == context.GlobalDict) { _module = context.Module.GetName(); _context = context; } else { _module = null; _context = new CodeContext(new PythonDictionary(), new ModuleContext(globals, DefaultContext.DefaultPythonContext)); } _defaults = defaults == null ? ArrayUtils.EmptyObjects : defaults.ToArray(); _code = code; _name = name; _doc = code._initialDoc; Closure = null; var scopeStatement = _code.PythonCode; if (scopeStatement.IsClosure) { throw new NotImplementedException("code containing closures is not supported"); } scopeStatement.RewriteBody(FunctionDefinition.ArbitraryGlobalsVisitorInstance); _compat = CalculatedCachedCompat(); }
public PythonDynamicStackFrame(CodeContext/*!*/ context, FunctionCode/*!*/ funcCode, MethodBase method, string funcName, string filename, int line) : base(method, funcName, filename, line) { Assert.NotNull(context, funcCode); _context = context; _code = funcCode; }
public PythonDynamicStackFrame(CodeContext/*!*/ context, FunctionCode/*!*/ funcCode, int line) : base(GetMethod(context, funcCode), funcCode.co_name, funcCode.co_filename, line) { Assert.NotNull(context, funcCode); _context = context; _code = funcCode; }
public PythonDynamicStackFrame(CodeContext /*!*/ context, FunctionCode /*!*/ funcCode, MethodBase method, string funcName, string filename, int line) : base(method, funcName, filename, line) { Assert.NotNull(context, funcCode); _context = context; _code = funcCode; }
public PythonDynamicStackFrame(CodeContext /*!*/ context, FunctionCode /*!*/ funcCode, int line) : base(GetMethod(context, funcCode), funcCode.co_name, funcCode.co_filename, line) { Assert.NotNull(context, funcCode); _context = context; _code = funcCode; }
private static MethodBase GetMethod(CodeContext context, FunctionCode funcCode) { MethodBase method; Debug.Assert(funcCode._normalDelegate != null || funcCode._tracingDelegate != null); if (!context.LanguageContext.EnableTracing || funcCode._tracingDelegate == null) { method = funcCode._normalDelegate.GetMethodInfo(); } else { method = funcCode._tracingDelegate.GetMethodInfo(); } return method; }
private static MethodBase GetMethod(CodeContext context, FunctionCode funcCode) { MethodBase method; Debug.Assert(funcCode._normalDelegate != null || funcCode._tracingDelegate != null); if (!context.LanguageContext.EnableTracing || funcCode._tracingDelegate == null) { method = funcCode._normalDelegate.GetMethodInfo(); } else { method = funcCode._tracingDelegate.GetMethodInfo(); } return(method); }
internal PythonFunction(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, MutableTuple closure) { Assert.NotNull(context, funcInfo); _context = context; _defaults = defaults ?? ArrayUtils.EmptyObjects; _code = funcInfo; _doc = funcInfo._initialDoc; _name = funcInfo.co_name; Debug.Assert(_defaults.Length <= _code.co_argcount); if (modName != Uninitialized.Instance) { _module = modName; } Closure = closure; _compat = CalculatedCachedCompat(); }
internal PythonFunction(CodeContext /*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, MutableTuple closure) { Assert.NotNull(context, funcInfo); _context = context; _defaults = defaults ?? ArrayUtils.EmptyObjects; _code = funcInfo; _doc = funcInfo._initialDoc; _name = funcInfo.co_name; Debug.Assert(_defaults.Length <= _code.co_argcount); if (modName != Uninitialized.Instance) { _module = modName; } Closure = closure; _compat = CalculatedCachedCompat(); }
public override bool Equals(object obj) { FunctionCode other = obj as FunctionCode; if (other == null) { return(false); } if (fc != null) { return(fc == other.fc); } else if (func != null) { return(func == other.func); } throw Ops.TypeError("bad code"); }
internal PythonFunction(CodeContext /*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, PythonDictionary kwdefaults, PythonDictionary annotations, MutableTuple closure) { Assert.NotNull(context, funcInfo); _context = context; _defaults = defaults ?? ArrayUtils.EmptyObjects; __kwdefaults__ = kwdefaults; _code = funcInfo; _doc = funcInfo._initialDoc; _name = funcInfo.co_name; _annotations = annotations ?? new PythonDictionary(); Debug.Assert(_defaults.Length <= _code.co_argcount); Debug.Assert((__kwdefaults__?.Count ?? 0) <= _code.co_kwonlyargcount); if (modName != Uninitialized.Instance) { _module = modName; } Closure = closure; FunctionCompatibility = CalculatedCachedCompat(); }
internal PythonFunction(CodeContext/*!*/ context, string/*!*/ name, Delegate target, string[] argNames, object[] defaults, FunctionAttributes flags) { Assert.NotNull(context, name); Assert.NotNull(context.Scope); _name = name; _context = context; _argNames = argNames ?? ArrayUtils.EmptyStrings; _defaults = defaults ?? ArrayUtils.EmptyObjects; _flags = flags; _nparams = _argNames.Length; Target = target; _name = name; if ((flags & FunctionAttributes.KeywordDictionary) != 0) { _nparams--; } if ((flags & FunctionAttributes.ArgumentList) != 0) { _nparams--; } Debug.Assert(_defaults.Length <= _nparams); object modName; if (context.GlobalScope.Dict.TryGetValue(Symbols.Name, out modName)) { _module = modName; } _compat = CalculatedCachedCompat(); _code = new FunctionCode(this); }
internal virtual void RewriteBody(PythonAst.LookupVisitor visitor) { _funcCode = null; }
protected void PushFrame(CodeContext context, FunctionCode code) { if (((PythonContext)SourceUnit.LanguageContext).PythonOptions.Frames) { PythonOps.PushFrame(context, code); } }
public PythonDebuggingPayload(FunctionCode code, Dictionary<int, Dictionary<int, bool>> loopLocations, Dictionary<int, bool> handlerLocations) { Code = code; LoopAndFinallyLocations = loopLocations; HandlerLocations = handlerLocations; }
public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name, PythonTuple defaults) : this(context, code, globals, name, defaults, null) { }
internal virtual void RewriteBody(MSAst.ExpressionVisitor visitor) { _funcCode = null; }
public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name) : this(context, code, globals, name, null, null) { }
internal override MSAst.Expression Transform(AstGenerator ag) { string className = _name; AstGenerator classGen = new AstGenerator(ag, className, false, "class " + className); classGen.Parameter(_parentContextParam); // we always need to create a nested context for class defs classGen.CreateNestedContext(); List<MSAst.Expression> init = new List<MSAst.Expression>(); classGen.AddHiddenVariable(ArrayGlobalAllocator._globalContext); init.Add(Ast.Assign(ArrayGlobalAllocator._globalContext, Ast.Call(typeof(PythonOps).GetMethod("GetGlobalContext"), _parentContextParam))); init.AddRange(classGen.Globals.PrepareScope(classGen)); CreateVariables(classGen, _parentContextParam, init, true, false); List<MSAst.Expression> statements = new List<MSAst.Expression>(); // Create the body MSAst.Expression bodyStmt = classGen.Transform(_body); // __module__ = __name__ MSAst.Expression modStmt = GlobalAllocator.Assign( classGen.Globals.GetVariable(classGen, _modVariable), classGen.Globals.GetVariable(classGen, _modNameVariable)); string doc = classGen.GetDocumentation(_body); if (doc != null) { statements.Add( GlobalAllocator.Assign( classGen.Globals.GetVariable(classGen, _docVariable), AstUtils.Constant(doc) ) ); } FunctionCode funcCodeObj = new FunctionCode( ag.PyContext, null, null, Name, ag.GetDocumentation(_body), ArrayUtils.EmptyStrings, FunctionAttributes.None, Span, ag.Context.SourceUnit.Path, ag.EmitDebugSymbols, ag.ShouldInterpret, FreeVariables, GlobalVariables, CellVariables, AppendVariables(new List<string>()), Variables == null ? 0 : Variables.Count, classGen.LoopLocationsNoCreate, classGen.HandlerLocationsNoCreate ); MSAst.Expression funcCode = classGen.Globals.GetConstant(funcCodeObj); classGen.FuncCodeExpr = funcCode; if (_body.CanThrow && ag.PyContext.PythonOptions.Frames) { bodyStmt = AstGenerator.AddFrame(classGen.LocalContext, funcCode, bodyStmt); classGen.AddHiddenVariable(AstGenerator._functionStack); } bodyStmt = classGen.WrapScopeStatements( Ast.Block( statements.Count == 0 ? AstGenerator.EmptyBlock : Ast.Block(new ReadOnlyCollection<MSAst.Expression>(statements)), modStmt, bodyStmt, classGen.LocalContext // return value ) ); var lambda = Ast.Lambda<Func<CodeContext, CodeContext>>( classGen.MakeBody(_parentContextParam, init.ToArray(), bodyStmt), classGen.Name + "$" + _classId++, classGen.Parameters ); funcCodeObj.Code = lambda; MSAst.Expression classDef = Ast.Call( AstGenerator.GetHelperMethod("MakeClass"), ag.EmitDebugSymbols ? (MSAst.Expression)lambda : Ast.Convert(funcCode, typeof(object)), ag.LocalContext, AstUtils.Constant(_name), Ast.NewArrayInit( typeof(object), ag.TransformAndConvert(_bases, typeof(object)) ), AstUtils.Constant(FindSelfNames()) ); classDef = ag.AddDecorators(classDef, _decorators); return ag.AddDebugInfoAndVoid(GlobalAllocator.Assign(ag.Globals.GetVariable(ag, _variable), classDef), new SourceSpan(Start, Header)); }
private static int _CurrentId = 1; // The current ID for functions which are called in complex ways. /// <summary> /// Python ctor - maps to function.__new__ /// /// y = func(x.__code__, globals(), 'foo', None, (a, )) /// </summary> public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name, PythonTuple defaults, PythonTuple closure) { throw new NotImplementedException(); }
private void OnTraceback(TraceBackFrame frame, string result, object payload) { var code = (FunctionCode)frame.f_code; if (_curCode == null || _curCode.co_filename != code.co_filename) { _source.Inlines.Clear(); foreach (var line in System.IO.File.ReadAllLines(code.co_filename)) { _source.Inlines.Add(new Run(line + "\r\n")); } } _curFrame = frame; _curCode = code; _curResult = result; _curPayload = payload; switch (result) { case "call": TracebackCall(); break; case "line": TracebackLine(); break; case "return": TracebackReturn(); break; default: MessageBox.Show(string.Format("{0} not supported!", result)); break; } }
public PythonDebuggingPayload(FunctionCode code) { Code = code; }
public TargetUpdaterForCompilation(PythonContext context, FunctionCode code) { _code = code; _context = context; }
private static int _CurrentId = 1; // The current ID for functions which are called in complex ways. /// <summary> /// Python ctor - maps to function.__new__ /// </summary> public PythonFunction(CodeContext context, FunctionCode code, IAttributesCollection globals) { throw new NotImplementedException(); }
private object OptimizedEvalWrapper(FunctionCode funcCode) { try { return _optimizedTarget(funcCode); } catch (Exception e) { PythonOps.UpdateStackTrace(e, _optimizedContext, Code, 0); throw; } }
private static int _CurrentId = 1; // The current ID for functions which are called in complex ways. /// <summary> /// Python ctor - maps to function.__new__ /// /// y = func(x.__code__, globals(), 'foo', None, (a, )) /// </summary> public PythonFunction(CodeContext context, FunctionCode code, IAttributesCollection globals, string name, PythonTuple defaults, PythonTuple closure) { throw new NotImplementedException(); }
private static int _CurrentId = 1; // The current ID for functions which are called in complex ways. public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals) : this(context, code, globals, code.PythonCode.Name, null, null) { }
internal PythonFunction(CodeContext/*!*/ context, Delegate target, FunctionInfo funcInfo, object modName, object[] defaults, Tuple closure) { Assert.NotNull(context, funcInfo); Assert.NotNull(context.Scope); _funcInfo = funcInfo; _context = context; _defaults = defaults ?? ArrayUtils.EmptyObjects; Target = target; Debug.Assert(_defaults.Length <= _funcInfo.ParameterCount); if (modName != Uninitialized.Instance) { _module = modName; } Closure = closure; _compat = CalculatedCachedCompat(); _code = new FunctionCode(this, funcInfo); }
/// <summary> /// Gets or creates the FunctionCode object for this FunctionDefinition. /// </summary> internal FunctionCode GetOrMakeFunctionCode() { if (_funcCode == null) { _funcCode = new FunctionCode(GlobalParent.PyContext, OriginalDelegate, this, ScopeDocumentation); } return _funcCode; }