public KnownTypes(PythonAnalyzer state)
        {
            ITypeDatabaseReader  fallbackDb = null;
            IBuiltinPythonModule fallback   = null;

            int count = (int)BuiltinTypeIdExtensions.LastTypeId + 1;

            _types      = new IPythonType[count];
            _classInfos = new BuiltinClassInfo[count];

            var interpreter = state.Interpreter;

            for (int value = 0; value < count; ++value)
            {
                try {
                    _types[value] = interpreter.GetBuiltinType((BuiltinTypeId)value);
                } catch (KeyNotFoundException) {
                    if (fallback == null)
                    {
                        var tempDb = PythonTypeDatabase.CreateDefaultTypeDatabase(state.LanguageVersion.ToVersion());
                        fallbackDb = (ITypeDatabaseReader)tempDb;
                        fallback   = tempDb.BuiltinModule;
                    }

                    _types[value] = fallback.GetAnyMember(fallbackDb.GetBuiltinTypeName((BuiltinTypeId)value)) as IPythonType;
                }
                if (_types[value] != null)
                {
                    _classInfos[value] = state.GetBuiltinType(_types[value]);
                }
            }
        }
Ejemplo n.º 2
0
        private void FinishLoadKnownTypes(PythonTypeDatabase db)
        {
            _itemCache.Clear();

            if (db == null)
            {
                db    = PythonTypeDatabase.CreateDefaultTypeDatabase(LanguageVersion.ToVersion());
                Types = KnownTypes.Create(this, db);
            }
            else
            {
                Types = KnownTypes.CreateDefault(this, db);
            }

            ClassInfos = (IKnownClasses)Types;
            _noneInst  = (ConstantInfo)GetCached(
                _nullKey,
                () => new ConstantInfo(ClassInfos[BuiltinTypeId.NoneType], null, PythonMemberType.Constant)
                );

            DoNotUnionInMro = AnalysisSet.Create(new AnalysisValue[] {
                ClassInfos[BuiltinTypeId.Object],
                ClassInfos[BuiltinTypeId.Type]
            });

            AddBuiltInSpecializations();
        }
Ejemplo n.º 3
0
 public override PythonTypeDatabase MakeTypeDatabase(string databasePath, bool includeSitePackages = true)
 {
     if (_actualDatabasePaths != null)
     {
         return(new PythonTypeDatabase(this, _actualDatabasePaths));
     }
     else
     {
         return(PythonTypeDatabase.CreateDefaultTypeDatabase(this));
     }
 }
Ejemplo n.º 4
0
        private void LoadInitialKnownTypes()
        {
            if (_builtinModule != null)
            {
                Debug.Fail("LoadInitialKnownTypes should only be called once");
                return;
            }

            var fallbackDb = PythonTypeDatabase.CreateDefaultTypeDatabase(LanguageVersion.ToVersion());

            _builtinModule = _modules.GetBuiltinModule(fallbackDb.GetModule(SharedDatabaseState.BuiltinName2x));

            FinishLoadKnownTypes(fallbackDb);
        }
        private void LoadKnownTypes()
        {
            _itemCache.Clear();

            ModuleReference moduleRef;

            if (Modules.TryImport(_builtinName, out moduleRef))
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                var fallbackDb = PythonTypeDatabase.CreateDefaultTypeDatabase(LanguageVersion.ToVersion());
                _builtinModule        = _modules.GetBuiltinModule(fallbackDb.GetModule(SharedDatabaseState.BuiltinName2x));
                Modules[_builtinName] = new ModuleReference(_builtinModule);
            }

            Types      = new KnownTypes(this);
            ClassInfos = (IKnownClasses)Types;

            _noneInst = (ConstantInfo)GetCached(_nullKey, () => new ConstantInfo(ClassInfos[BuiltinTypeId.NoneType], (object)null));

            DoNotUnionInMro = AnalysisSet.Create(new AnalysisValue[] {
                ClassInfos[BuiltinTypeId.Object],
                ClassInfos[BuiltinTypeId.Type]
            });

            AddBuiltInSpecializations();

            ModuleReference sysModule;

            if (_modules.TryImport("sys", out sysModule))
            {
                var bm = sysModule.AnalysisModule as BuiltinModule;
                if (bm != null)
                {
                    sysModule.Module = new SysModuleInfo(bm);
                }
            }
        }
Ejemplo n.º 6
0
        public void VersionedSharedDatabase()
        {
            var twoFive  = PythonTypeDatabase.CreateDefaultTypeDatabase(new Version(2, 5));
            var twoSix   = PythonTypeDatabase.CreateDefaultTypeDatabase(new Version(2, 6));
            var twoSeven = PythonTypeDatabase.CreateDefaultTypeDatabase(new Version(2, 7));
            var threeOh  = PythonTypeDatabase.CreateDefaultTypeDatabase(new Version(3, 0));
            var threeOne = PythonTypeDatabase.CreateDefaultTypeDatabase(new Version(3, 1));
            var threeTwo = PythonTypeDatabase.CreateDefaultTypeDatabase(new Version(3, 2));

            // new in 2.6
            Assert.AreEqual(null, twoFive.BuiltinModule.GetAnyMember("bytearray"));
            foreach (var version in new[] { twoSix, twoSeven, threeOh, threeOne, threeTwo })
            {
                Assert.AreNotEqual(version, version.BuiltinModule.GetAnyMember("bytearray"));
            }

            // new in 2.7
            Assert.AreEqual(null, twoSix.BuiltinModule.GetAnyMember("memoryview"));
            foreach (var version in new[] { twoSeven, threeOh, threeOne, threeTwo })
            {
                Assert.AreNotEqual(version, version.BuiltinModule.GetAnyMember("memoryview"));
            }

            // not in 3.0
            foreach (var version in new[] { twoFive, twoSix, twoSeven })
            {
                Assert.AreNotEqual(null, version.BuiltinModule.GetAnyMember("StandardError"));
            }

            foreach (var version in new[] { threeOh, threeOne, threeTwo })
            {
                Assert.AreEqual(null, version.BuiltinModule.GetAnyMember("StandardError"));
            }

            // new in 3.0
            foreach (var version in new[] { twoFive, twoSix, twoSeven })
            {
                Assert.AreEqual(null, version.BuiltinModule.GetAnyMember("exec"));
                Assert.AreEqual(null, version.BuiltinModule.GetAnyMember("print"));
            }

            foreach (var version in new[] { threeOh, threeOne, threeTwo })
            {
                Assert.AreNotEqual(null, version.BuiltinModule.GetAnyMember("exec"));
                Assert.AreNotEqual(null, version.BuiltinModule.GetAnyMember("print"));
            }


            // new in 3.1
            foreach (var version in new[] { twoFive, twoSix, twoSeven, threeOh })
            {
                Assert.AreEqual(null, version.GetModule("sys").GetMember(null, "int_info"));
            }

            foreach (var version in new[] { threeOne, threeTwo })
            {
                Assert.AreNotEqual(null, version.GetModule("sys").GetMember(null, "int_info"));
            }

            // new in 3.2
            foreach (var version in new[] { twoFive, twoSix, twoSeven, threeOh, threeOne })
            {
                Assert.AreEqual(null, version.GetModule("sys").GetMember(null, "setswitchinterval"));
            }

            foreach (var version in new[] { threeTwo })
            {
                Assert.AreNotEqual(null, version.GetModule("sys").GetMember(null, "setswitchinterval"));
            }
        }