Inheritance: IExpressionSerializable
Ejemplo n.º 1
0
        /// <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();
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 4
0
        /// <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();
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 9
0
        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();
        }
Ejemplo n.º 10
0
        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();
        }
Ejemplo n.º 11
0
        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");
        }
Ejemplo n.º 12
0
        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();
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
 internal virtual void RewriteBody(PythonAst.LookupVisitor visitor) {
     _funcCode = null;
 }
Ejemplo n.º 15
0
 protected void PushFrame(CodeContext context, FunctionCode code) {
     if (((PythonContext)SourceUnit.LanguageContext).PythonOptions.Frames) {
         PythonOps.PushFrame(context, code);
     }
 }
Ejemplo n.º 16
0
 public PythonDebuggingPayload(FunctionCode code, Dictionary<int, Dictionary<int, bool>> loopLocations, Dictionary<int, bool> handlerLocations) {
     Code = code;
     LoopAndFinallyLocations = loopLocations;
     HandlerLocations = handlerLocations;
 }
Ejemplo n.º 17
0
 public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name, PythonTuple defaults)
     : this(context, code, globals, name, defaults, null) {
 }
Ejemplo n.º 18
0
 internal virtual void RewriteBody(MSAst.ExpressionVisitor visitor) {
     _funcCode = null;
 }
Ejemplo n.º 19
0
 public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name)
     : this(context, code, globals, name, null, null)
 {
 }
Ejemplo n.º 20
0
        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));
        }
Ejemplo n.º 21
0
        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;
            }
        }
Ejemplo n.º 23
0
 public PythonDebuggingPayload(FunctionCode code) {
     Code = code;
 }
Ejemplo n.º 24
0
 public TargetUpdaterForCompilation(PythonContext context, FunctionCode code) {
     _code = code;
     _context = context;
 }
Ejemplo n.º 25
0
        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();
        }
Ejemplo n.º 26
0
 private object OptimizedEvalWrapper(FunctionCode funcCode) {
     try {
         return _optimizedTarget(funcCode);
     } catch (Exception e) {
         PythonOps.UpdateStackTrace(e, _optimizedContext, Code, 0);
         throw;
     }
 }
Ejemplo n.º 27
0
        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();
        }
Ejemplo n.º 28
0
        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)
        {
        }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
 public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name, PythonTuple defaults)
     : this(context, code, globals, name, defaults, null)
 {
 }
Ejemplo n.º 31
0
        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) {
        }
Ejemplo n.º 32
0
 public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name)
     : this(context, code, globals, name, null, null) {
 }
Ejemplo n.º 33
0
 /// <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;
 }