Ejemplo n.º 1
0
        public BaseAnalysisTest(IPythonInterpreterFactory factory, IPythonInterpreter interpreter) {
            InterpreterFactory = factory;
            Interpreter = interpreter;
            var objectType = Interpreter.GetBuiltinType(BuiltinTypeId.Object);
            Assert.IsNotNull(objectType);
            var intType = Interpreter.GetBuiltinType(BuiltinTypeId.Int);
            var bytesType = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);
            var listType = Interpreter.GetBuiltinType(BuiltinTypeId.List);
            var functionType = Interpreter.GetBuiltinType(BuiltinTypeId.Function);

            _objectMembers = objectType.GetMemberNames(DefaultContext).ToArray();
            _strMembers = bytesType.GetMemberNames(DefaultContext).ToArray();
            _listMembers = listType.GetMemberNames(DefaultContext).ToArray();
            _intMembers = intType.GetMemberNames(DefaultContext).ToArray();
            _functionMembers = functionType.GetMemberNames(DefaultContext).ToArray();
        }
Ejemplo n.º 2
0
        public KnownTypesView(IPythonInterpreter interpreter, Version version) {
            int count = (int)BuiltinTypeIdExtensions.LastTypeId;
            _children = new IAnalysisItemView[count];
            for (int value = 1; value <= count; ++value) {
                var expectedName = SharedDatabaseState.GetBuiltinTypeName((BuiltinTypeId)value, version);
                string name = string.Format("{0} ({1})",
                    expectedName,
                    Enum.GetName(typeof(BuiltinTypeId), value)
                );

                IPythonType type;
                try {
                    type = interpreter.GetBuiltinType((BuiltinTypeId)value);
                    if (expectedName != type.Name) {
                        name = string.Format("{2} ({1}/{0})",
                            expectedName,
                            Enum.GetName(typeof(BuiltinTypeId), value),
                            type.Name
                        );
                    }
                } catch {
                    type = null;
                }

                if (type != null) {
                    _children[value - 1] = new ClassView(
                        null,
                        name,
                        type
                    );
                } else {
                    _children[value - 1] = new NullMember(name);
                }
            }
        }
Ejemplo n.º 3
0
        public BaseAnalysisTest(IPythonInterpreterFactory factory, IPythonInterpreter interpreter)
        {
            InterpreterFactory = factory;
            Interpreter        = interpreter;
            var objectType = Interpreter.GetBuiltinType(BuiltinTypeId.Object);

            Assert.IsNotNull(objectType);
            var intType      = Interpreter.GetBuiltinType(BuiltinTypeId.Int);
            var bytesType    = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);
            var listType     = Interpreter.GetBuiltinType(BuiltinTypeId.List);
            var functionType = Interpreter.GetBuiltinType(BuiltinTypeId.Function);

            _objectMembers   = objectType.GetMemberNames(DefaultContext).ToArray();
            _strMembers      = bytesType.GetMemberNames(DefaultContext).ToArray();
            _listMembers     = listType.GetMemberNames(DefaultContext).ToArray();
            _intMembers      = intType.GetMemberNames(DefaultContext).ToArray();
            _functionMembers = functionType.GetMemberNames(DefaultContext).ToArray();
        }
Ejemplo n.º 4
0
 internal void SetBases(IPythonInterpreter interpreter, IEnumerable <IPythonType> bases)
 {
     if (Bases != null)
     {
         throw new InvalidOperationException("cannot set Bases multiple times");
     }
     Bases = bases.MaybeEnumerate().ToArray();
     lock (_members) {
         if (Bases.Count > 0)
         {
             _members["__base__"] = Bases[0];
         }
         _members["__bases__"] = new AstPythonSequence(
             interpreter?.GetBuiltinType(BuiltinTypeId.Tuple),
             DeclaringModule,
             Bases,
             interpreter?.GetBuiltinType(BuiltinTypeId.TupleIterator)
             );
     }
 }
        internal void SetBases(IPythonInterpreter interpreter, IEnumerable <IPythonType> bases)
        {
            lock (_members) {
                if (Bases != null)
                {
                    return; // Already set
                }

                Bases = bases.MaybeEnumerate().ToArray();
                if (Bases.Count > 0)
                {
                    _members["__base__"] = Bases[0];
                }

                _members["__bases__"] = new AstPythonSequence(
                    interpreter?.GetBuiltinType(BuiltinTypeId.Tuple),
                    DeclaringModule,
                    Bases,
                    interpreter?.GetBuiltinType(BuiltinTypeId.TupleIterator)
                    );
            }
        }
Ejemplo n.º 6
0
        public PythonAnalyzer(IPythonInterpreter pythonInterpreter, PythonLanguageVersion langVersion)
        {
            _langVersion       = langVersion;
            _interpreter       = pythonInterpreter;
            _modules           = new Dictionary <string, ModuleReference>();
            _modulesByFilename = new Dictionary <string, ModuleInfo>(StringComparer.OrdinalIgnoreCase);
            _itemCache         = new Dictionary <object, object>();

            InitializeBuiltinModules();
            pythonInterpreter.ModuleNamesChanged += new EventHandler(ModuleNamesChanged);

            _types           = new KnownTypes(this);
            _builtinModule   = (BuiltinModule)Modules["__builtin__"].Module;
            _propertyObj     = GetBuiltin("property");
            _classmethodObj  = GetBuiltin("classmethod");
            _staticmethodObj = GetBuiltin("staticmethod");
            _typeObj         = GetBuiltin("type");
            _intType         = GetBuiltin("int");
            _stringType      = (BuiltinClassInfo)GetBuiltin("str");

            _objectSet = new HashSet <Namespace>(new[] { GetBuiltin("object") });

            _setType       = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Set));
            _rangeFunc     = GetBuiltin("range");
            _frozensetType = GetBuiltin("frozenset");
            _functionType  = GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Function));
            _generatorType = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Generator));
            _dictType      = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Dict));
            _boolType      = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Bool));
            _noneInst      = (ConstantInfo)GetNamespaceFromObjects(null);
            _listType      = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.List));
            _tupleType     = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Tuple));

            _queue = new Deque <AnalysisUnit>();

            SpecializeFunction("__builtin__", "range", (n, unit, args) => unit.DeclaringModule.GetOrMakeNodeVariable(n, (nn) => new RangeInfo(_types.List, unit.ProjectState).SelfSet));
            SpecializeFunction("__builtin__", "min", ReturnUnionOfInputs);
            SpecializeFunction("__builtin__", "max", ReturnUnionOfInputs);

            pythonInterpreter.Initialize(this);

            _defaultContext = pythonInterpreter.CreateModuleContext();

            // cached for quick checks to see if we're a call to clr.AddReference

            try {
                SpecializeFunction("wpf", "LoadComponent", LoadComponent);
            } catch (KeyNotFoundException) {
                // IronPython.Wpf.dll isn't available...
            }
        }
Ejemplo n.º 7
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Dictionary<string, IMember> members
 ) {
     _interpreter = interpreter;
     _ast = ast;
     _module = module;
     _filePath = filePath;
     _members = members;
     _scope = new Stack<Dictionary<string, IMember>>();
     _noneInst = new AstPythonConstant(_interpreter.GetBuiltinType(BuiltinTypeId.NoneType));
 }
Ejemplo n.º 8
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Dictionary <string, IMember> members
     )
 {
     _interpreter = interpreter;
     _ast         = ast;
     _module      = module;
     _filePath    = filePath;
     _members     = members;
     _scope       = new Stack <Dictionary <string, IMember> >();
     _noneInst    = new AstPythonConstant(_interpreter.GetBuiltinType(BuiltinTypeId.NoneType));
 }
Ejemplo n.º 9
0
        public KnownTypesView(IPythonInterpreter interpreter, Version version)
        {
            int count = (int)BuiltinTypeIdExtensions.LastTypeId;

            _children = new IAnalysisItemView[count];
            for (int value = 1; value <= count; ++value)
            {
                var    expectedName = SharedDatabaseState.GetBuiltinTypeName((BuiltinTypeId)value, version);
                string name         = string.Format("{0} ({1})",
                                                    expectedName,
                                                    Enum.GetName(typeof(BuiltinTypeId), value)
                                                    );

                IPythonType type;
                try {
                    type = interpreter.GetBuiltinType((BuiltinTypeId)value);
                    if (expectedName != type.Name)
                    {
                        name = string.Format("{2} ({1}/{0})",
                                             expectedName,
                                             Enum.GetName(typeof(BuiltinTypeId), value),
                                             type.Name
                                             );
                    }
                } catch {
                    type = null;
                }

                if (type != null)
                {
                    _children[value - 1] = new ClassView(
                        null,
                        name,
                        type
                        );
                }
                else
                {
                    _children[value - 1] = new NullMember(name);
                }
            }
        }
Ejemplo n.º 10
0
 public PythonFString(string unparsedFString, IPythonInterpreter interpreter, LocationInfo location = null)
     : base(interpreter.GetBuiltinType(interpreter.GetUnicodeTypeId()), location)
 {
     UnparsedFString = unparsedFString;
 }
Ejemplo n.º 11
0
        private IPythonType GetTypeFromExpression(Expression expr)
        {
            var ce = expr as ConstantExpression;

            if (ce != null)
            {
                if (ce.Value == null)
                {
                    return(_interpreter.GetBuiltinType(BuiltinTypeId.NoneType));
                }
                switch (Type.GetTypeCode(ce.Value.GetType()))
                {
                case TypeCode.Boolean: return(_interpreter.GetBuiltinType(BuiltinTypeId.Bool));

                case TypeCode.Double: return(_interpreter.GetBuiltinType(BuiltinTypeId.Float));

                case TypeCode.Int32: return(_interpreter.GetBuiltinType(BuiltinTypeId.Int));

                case TypeCode.String: return(_interpreter.GetBuiltinType(BuiltinTypeId.Unicode));

                case TypeCode.Object:
                    if (ce.Value.GetType() == typeof(Complex))
                    {
                        return(_interpreter.GetBuiltinType(BuiltinTypeId.Complex));
                    }
                    else if (ce.Value.GetType() == typeof(AsciiString))
                    {
                        return(_interpreter.GetBuiltinType(BuiltinTypeId.Bytes));
                    }
                    else if (ce.Value.GetType() == typeof(BigInteger))
                    {
                        return(_interpreter.GetBuiltinType(BuiltinTypeId.Long));
                    }
                    else if (ce.Value.GetType() == typeof(Ellipsis))
                    {
                        return(_interpreter.GetBuiltinType(BuiltinTypeId.Ellipsis));
                    }
                    break;
                }
                return(null);
            }

            if (expr is ListExpression || expr is ListComprehension)
            {
                return(_interpreter.GetBuiltinType(BuiltinTypeId.List));
            }
            if (expr is DictionaryExpression || expr is DictionaryComprehension)
            {
                return(_interpreter.GetBuiltinType(BuiltinTypeId.Dict));
            }
            if (expr is TupleExpression)
            {
                return(_interpreter.GetBuiltinType(BuiltinTypeId.Tuple));
            }
            if (expr is SetExpression || expr is SetComprehension)
            {
                return(_interpreter.GetBuiltinType(BuiltinTypeId.Set));
            }
            if (expr is LambdaExpression)
            {
                return(_interpreter.GetBuiltinType(BuiltinTypeId.Function));
            }

            return(null);
        }
Ejemplo n.º 12
0
 public PythonAsciiString(AsciiString s, IPythonInterpreter interpreter)
     : base(s, interpreter.GetBuiltinType(interpreter.GetAsciiTypeId()))
 {
 }
Ejemplo n.º 13
0
 public override IPythonIterator GetIterator()
 => new PythonRepeatingIterator(_interpreter.GetBuiltinType(BuiltinTypeId.SetIterator), _yieldValue);
Ejemplo n.º 14
0
 public PythonUnicodeString(string s, IPythonInterpreter interpreter)
     : base(s, interpreter.GetBuiltinType(interpreter.GetUnicodeTypeId()))
 {
 }
Ejemplo n.º 15
0
 internal IPythonType GetPythonType(BuiltinTypeId id)
 {
     return(_interpreter.GetBuiltinType(id));
 }
Ejemplo n.º 16
0
 public PythonGenerator(IPythonInterpreter interpreter, IMember yieldValue)
     : base(interpreter.GetBuiltinType(BuiltinTypeId.Generator), yieldValue)
 {
     _interpreter = interpreter;
     _yieldValue  = yieldValue;
 }
Ejemplo n.º 17
0
 public PythonFString(string unparsedFString, IPythonInterpreter interpreter)
     : base(interpreter.GetBuiltinType(interpreter.GetUnicodeTypeId()))
 {
     UnparsedFString = unparsedFString;
 }
Ejemplo n.º 18
0
        public BaseAnalysisTest(IPythonInterpreter interpreter)
        {
            Interpreter   = interpreter;
            PyObjectType  = Interpreter.GetBuiltinType(BuiltinTypeId.Object);
            IntType       = Interpreter.GetBuiltinType(BuiltinTypeId.Int);
            ComplexType   = Interpreter.GetBuiltinType(BuiltinTypeId.Complex);
            StringType    = Interpreter.GetBuiltinType(BuiltinTypeId.Str);
            FloatType     = Interpreter.GetBuiltinType(BuiltinTypeId.Float);
            TypeType      = Interpreter.GetBuiltinType(BuiltinTypeId.Type);
            ListType      = Interpreter.GetBuiltinType(BuiltinTypeId.List);
            TupleType     = Interpreter.GetBuiltinType(BuiltinTypeId.Tuple);
            BoolType      = Interpreter.GetBuiltinType(BuiltinTypeId.Bool);
            FunctionType  = Interpreter.GetBuiltinType(BuiltinTypeId.Function);
            GeneratorType = Interpreter.GetBuiltinType(BuiltinTypeId.Generator);

            _objectMembers   = PyObjectType.GetMemberNames(IronPythonModuleContext.DontShowClrInstance).ToArray();
            _strMembers      = StringType.GetMemberNames(IronPythonModuleContext.DontShowClrInstance).ToArray();
            _listMembers     = ListType.GetMemberNames(IronPythonModuleContext.DontShowClrInstance).ToArray();
            _intMembers      = IntType.GetMemberNames(IronPythonModuleContext.DontShowClrInstance).ToArray();
            _functionMembers = FunctionType.GetMemberNames(IronPythonModuleContext.DontShowClrInstance).ToArray();
        }
Ejemplo n.º 19
0
 public PythonAsciiString(AsciiString s, IPythonInterpreter interpreter, LocationInfo location = null)
     : base(s, interpreter.GetBuiltinType(interpreter.GetAsciiTypeId()), location)
 {
 }
Ejemplo n.º 20
0
        private IMember GetValueFromExpression(Expression expr, Dictionary <string, IMember> scope)
        {
            var ne = expr as NameExpression;

            if (ne != null)
            {
                IMember existing = null;
                if (scope != null && scope.TryGetValue(ne.Name, out existing) && existing != null)
                {
                    return(existing);
                }
                if (_members != null && _members.TryGetValue(ne.Name, out existing) && existing != null)
                {
                    return(existing);
                }
            }

            IPythonType type;

            var me = expr as MemberExpression;

            if (me != null && me.Target != null && !string.IsNullOrEmpty(me.Name))
            {
                var mc = GetValueFromExpression(me.Target, scope) as IMemberContainer;
                if (mc != null)
                {
                    return(mc.GetMember(_context, me.Name));
                }
                return(null);
            }

            if (expr is CallExpression)
            {
                var cae = (CallExpression)expr;
                var m   = GetValueFromExpression(cae.Target, scope);
                type = m as IPythonType;
                if (type != null)
                {
                    return(new AstPythonConstant(type, GetLoc(expr)));
                }
                var fn = m as IPythonFunction;
                if (fn != null)
                {
                    return(new AstPythonConstant(_interpreter.GetBuiltinType(BuiltinTypeId.NoneType), GetLoc(expr)));
                }
            }

            type = GetTypeFromExpression(expr);
            if (type != null)
            {
                return(new AstPythonConstant(type, GetLoc(expr)));
            }

            return(null);
        }
Ejemplo n.º 21
0
 public PythonUnicodeString(string s, IPythonInterpreter interpreter, LocationInfo location = null)
     : base(s, interpreter.GetBuiltinType(interpreter.GetUnicodeTypeId()), location)
 {
 }