コード例 #1
0
        /// <summary>
        /// Adds a new module of code to the list of available modules and returns a ProjectEntry object.
        /// </summary>
        /// <param name="moduleName">The name of the module; used to associate with imports</param>
        /// <param name="filePath">The path to the file on disk</param>
        /// <param name="cookie">An application-specific identifier for the module</param>
        /// <returns></returns>
        public IPythonProjectEntry AddModule(string moduleName, string filePath, IAnalysisCookie cookie = null)
        {
            var entry = new ProjectEntry(this, moduleName, filePath, cookie);

            if (moduleName != null)
            {
                Modules[moduleName] = new ModuleReference(entry.MyScope);
            }
            if (filePath != null)
            {
                _modulesByFilename[filePath] = entry.MyScope;
            }
            return(entry);
        }
コード例 #2
0
ファイル: ModuleTable.cs プロジェクト: tdoffice/pytools
        /// <summary>
        /// Gets a reference to a module.
        /// </summary>
        /// <param name="name">The full import name of the module.</param>
        /// <param name="res">The module reference object.</param>
        /// <returns>
        /// True if the module is available. This means that <c>res.Module</c>
        /// is not null. If this function returns false, <paramref name="res"/>
        /// may be valid and should not be replaced, but it is an unresolved
        /// reference.
        /// </returns>
        public bool TryImport(string name, out ModuleReference res)
        {
            bool firstImport = false;

            if (!_modules.TryGetValue(name, out res) || res == null)
            {
                _modules[name] = res = new ModuleReference(GetBuiltinModule(_interpreter.ImportModule(name)));
                firstImport    = true;
            }
            if (res != null && res.Module == null)
            {
                res.Module = GetBuiltinModule(_interpreter.ImportModule(name));
            }
            if (firstImport && res != null && res.Module != null)
            {
                _analyzer.DoDelayedSpecialization(name);
            }
            return(res != null && res.Module != null);
        }
コード例 #3
0
        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);
                }
            }
        }
コード例 #4
0
        private async Task LoadKnownTypesAsync()
        {
            if (_loadKnownTypesException != null)
            {
                _loadKnownTypesException.Throw();
            }

            _itemCache.Clear();

            Debug.Assert(_builtinModule != null, "LoadInitialKnownTypes was not called");
            if (_builtinModule == null)
            {
                LoadInitialKnownTypes();
            }

            var moduleRef = await Modules.TryImportAsync(_builtinName).ConfigureAwait(false);

            if (moduleRef != null)
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                Modules[_builtinName] = new ModuleReference(_builtinModule);
            }

            FinishLoadKnownTypes(null);

            var sysModule = await _modules.TryImportAsync("sys").ConfigureAwait(false);

            if (sysModule != null)
            {
                var bm = sysModule.AnalysisModule as BuiltinModule;
                if (bm != null)
                {
                    sysModule.Module = new SysModuleInfo(bm);
                }
            }
        }
コード例 #5
0
ファイル: ModuleTable.cs プロジェクト: tdoffice/pytools
        internal void ImportChildren(IPythonModule interpreterModule)
        {
            BuiltinModule module = null;

            foreach (var child in interpreterModule.GetChildrenModules())
            {
                module = module ?? GetBuiltinModule(interpreterModule);
                ModuleReference modRef;
                var             fullname = module.Name + "." + child;
                if (!_modules.TryGetValue(fullname, out modRef) || modRef == null || modRef.Module == null)
                {
                    IAnalysisSet value;
                    if (module.TryGetMember(child, out value))
                    {
                        var mod = value as IModule;
                        if (mod != null)
                        {
                            _modules[fullname] = new ModuleReference(mod);
                        }
                    }
                }
            }
        }
コード例 #6
0
        private void InitializeBuiltinModules()
        {
            var names = _interpreter.GetModuleNames();

            foreach (string modName in names)
            {
                var mod = _interpreter.ImportModule(modName);
                if (mod != null)
                {
                    ModuleReference modRef;
                    if (Modules.TryGetValue(modName, out modRef))
                    {
                        var existingBuiltin = modRef.Module as BuiltinModule;
                        if (existingBuiltin != null && existingBuiltin._type == mod)
                        {
                            // don't replace existing module which is the same
                            continue;
                        }
                    }
                    Modules[modName] = new ModuleReference(new BuiltinModule(mod, this));
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Gets a reference to a module.
        /// </summary>
        /// <param name="name">The full import name of the module.</param>
        /// <param name="moduleReference">The module reference object.</param>
        /// <returns>
        /// True if the module is available. This means that <c>moduleReference.Module</c>
        /// is not null. If this function returns false, <paramref name="moduleReference"/>
        /// may be valid and should not be replaced, but it is an unresolved
        /// reference.
        /// </returns>
        public bool TryImport(string name, out ModuleReference moduleReference)
        {
            var firstImport = false;

            if (!_modules.TryGetValue(name, out moduleReference) || moduleReference == null)
            {
                var pythonModule = _interpreter.ImportModule(name);
                moduleReference = SetModule(name, GetBuiltinModule(pythonModule));
                firstImport     = true;
            }

            if (moduleReference.Module == null)
            {
                moduleReference.Module = GetBuiltinModule(_interpreter.ImportModule(name));
            }

            if (firstImport && moduleReference.Module != null)
            {
                _analyzer.DoDelayedSpecialization(name);
            }

            return(moduleReference.Module != null);
        }
コード例 #8
0
ファイル: PythonAnalyzer.cs プロジェクト: sadgood/PTVS
        private async Task LoadKnownTypesAsync()
        {
            _itemCache.Clear();

            var fallback = new FallbackBuiltinModule(_langVersion);

            var moduleRef = await Modules.TryImportAsync(_builtinName).ConfigureAwait(false);

            if (moduleRef != null)
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                _builtinModule        = new BuiltinModule(fallback, this);
                Modules[_builtinName] = new ModuleReference(_builtinModule, _builtinName);
            }

            Modules.AddBuiltinModuleWrapper("sys", SysModuleInfo.Wrap);
            Modules.AddBuiltinModuleWrapper("typing", TypingModuleInfo.Wrap);

            Types = KnownTypes.Create(this, fallback);

            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();
        }
コード例 #9
0
ファイル: ModuleTable.cs プロジェクト: tdoffice/pytools
 /// <summary>
 /// Gets a reference to a module that has already been imported. You
 /// probably want to use <see cref="TryImport"/>.
 /// </summary>
 /// <returns>
 /// True if an attempt to import the module was made during the analysis
 /// that used this module table. The reference may be null, or the
 /// module within the reference may be null, even if this function
 /// returns true.
 /// </returns>
 /// <remarks>
 /// This exists for inspecting the results of an analysis (for example,
 /// <see cref="SaveAnalysis"/>). To get access to a module while
 /// analyzing code, even (especially!) if the module may not exist,
 /// you should call <see cref="TryImport"/>.
 /// </remarks>
 internal bool TryGetImportedModule(string name, out ModuleReference res)
 {
     return(_modules.TryGetValue(name, out res));
 }
コード例 #10
0
ファイル: ModuleTable.cs プロジェクト: tdoffice/pytools
 public InitializedModuleLoadState(ModuleReference reference)
 {
     _reference = reference;
 }
コード例 #11
0
ファイル: ModuleTable.cs プロジェクト: tdoffice/pytools
 public bool TryRemove(string name, out ModuleReference res)
 {
     return(_modules.TryRemove(name, out res));
 }
コード例 #12
0
 public bool TryRemove(string name, out ModuleReference res) => _modules.TryRemove(name, out res);