Ejemplo n.º 1
0
        internal PythonAnalyzer(IPythonInterpreterFactory factory, IPythonInterpreter pythonInterpreter)
        {
            _interpreterFactory           = factory;
            _langVersion                  = factory.GetLanguageVersion();
            _disposeInterpreter           = pythonInterpreter == null;
            _interpreter                  = pythonInterpreter ?? factory.CreateInterpreter();
            _builtinName                  = _langVersion.Is3x() ? SharedDatabaseState.BuiltinName3x : SharedDatabaseState.BuiltinName2x;
            _modules                      = new ModuleTable(this, _interpreter);
            _modulesByFilename            = new ConcurrentDictionary <string, ModuleInfo>(StringComparer.OrdinalIgnoreCase);
            _modulesWithUnresolvedImports = new HashSet <ModuleInfo>();
            _itemCache                    = new Dictionary <object, AnalysisValue>();

            Limits = AnalysisLimits.GetDefaultLimits();

            _queue = new Deque <AnalysisUnit>();

            _defaultContext = _interpreter.CreateModuleContext();

            _evalUnit = new AnalysisUnit(null, null, new ModuleInfo("$global", new ProjectEntry(this, "$global", String.Empty, null), _defaultContext).Scope, true);
            AnalysisLog.NewUnit(_evalUnit);

            try {
                LoadInitialKnownTypes();
            } catch (Exception ex) {
                if (ex.IsCriticalException())
                {
                    throw;
                }
                // This will be rethrown in LoadKnownTypesAsync
                _loadKnownTypesException = ExceptionDispatchInfo.Capture(ex);
            }
        }
Ejemplo n.º 2
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Uri documentUri,
     Dictionary <string, IMember> members,
     bool includeLocationInfo,
     bool warnAboutUndefinedValues,
     AnalysisLogWriter log = null
     )
 {
     _log     = log ?? (interpreter as AstPythonInterpreter)?._log;
     _module  = module ?? throw new ArgumentNullException(nameof(module));
     _members = members ?? throw new ArgumentNullException(nameof(members));
     _scope   = new NameLookupContext(
         interpreter ?? throw new ArgumentNullException(nameof(interpreter)),
         interpreter.CreateModuleContext(),
         ast ?? throw new ArgumentNullException(nameof(ast)),
         _module,
         filePath,
         documentUri,
         includeLocationInfo,
         log: warnAboutUndefinedValues ? _log : null
         );
     _postWalkers             = new List <AstAnalysisFunctionWalker>();
     WarnAboutUndefinedValues = warnAboutUndefinedValues;
 }
Ejemplo n.º 3
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PathResolverSnapshot pathResolver,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Uri documentUri,
     Dictionary <string, IMember> members,
     bool includeLocationInfo,
     bool warnAboutUndefinedValues,
     bool suppressBuiltinLookup,
     AnalysisLogWriter log = null
     )
 {
     _log     = log ?? (interpreter as AstPythonInterpreter)?.Log;
     _module  = module ?? throw new ArgumentNullException(nameof(module));
     _members = members ?? throw new ArgumentNullException(nameof(members));
     _scope   = new NameLookupContext(
         interpreter ?? throw new ArgumentNullException(nameof(interpreter)),
         interpreter.CreateModuleContext(),
         ast ?? throw new ArgumentNullException(nameof(ast)),
         _module,
         filePath,
         documentUri,
         includeLocationInfo,
         _functionWalkers,
         log: warnAboutUndefinedValues ? _log : null
         );
     _ast          = ast;
     _interpreter  = interpreter;
     _pathResolver = pathResolver;
     _scope.SuppressBuiltinLookup = suppressBuiltinLookup;
     _scope.PushScope(_typingScope);
     WarnAboutUndefinedValues = warnAboutUndefinedValues;
 }
Ejemplo n.º 4
0
        internal PythonAnalyzer(IPythonInterpreterFactory factory, IPythonInterpreter pythonInterpreter, string builtinName)
        {
            _interpreterFactory           = factory;
            _langVersion                  = factory.GetLanguageVersion();
            _disposeInterpreter           = pythonInterpreter == null;
            _interpreter                  = pythonInterpreter ?? factory.CreateInterpreter();
            _builtinName                  = builtinName ?? (_langVersion.Is3x() ? SharedDatabaseState.BuiltinName3x : SharedDatabaseState.BuiltinName2x);
            _modules                      = new ModuleTable(this, _interpreter);
            _modulesByFilename            = new ConcurrentDictionary <string, ModuleInfo>(StringComparer.OrdinalIgnoreCase);
            _modulesWithUnresolvedImports = new HashSet <ModuleInfo>();
            _itemCache                    = new Dictionary <object, AnalysisValue>();

            try {
                using (var key = Registry.CurrentUser.OpenSubKey(AnalysisLimitsKey)) {
                    Limits = AnalysisLimits.LoadFromStorage(key);
                }
            } catch (SecurityException) {
                Limits = new AnalysisLimits();
            } catch (UnauthorizedAccessException) {
                Limits = new AnalysisLimits();
            } catch (IOException) {
                Limits = new AnalysisLimits();
            }

            _queue = new Deque <AnalysisUnit>();

            _defaultContext = _interpreter.CreateModuleContext();

            _evalUnit = new AnalysisUnit(null, null, new ModuleInfo("$global", new ProjectEntry(this, "$global", String.Empty, null), _defaultContext).Scope, true);
            AnalysisLog.NewUnit(_evalUnit);

            LoadInitialKnownTypes();
        }
Ejemplo n.º 5
0
        public override bool Walk(FromImportStatement node)
        {
            var m       = _scope.Peek();
            var modName = node.Root.MakeString();

            if (modName == "__future__")
            {
                return(false);
            }

            if (m != null && node.Names != null)
            {
                bool onlyImportModules = modName.EndsWith(".");

                var mod = new AstNestedPythonModule(
                    _interpreter,
                    modName,
                    PythonAnalyzer.ResolvePotentialModuleNames(_module.Name, _filePath, modName, true).ToArray()
                    );
                var ctxt = _interpreter.CreateModuleContext();
                mod.Imported(ctxt);
                // Ensure child modules have been loaded
                mod.GetChildrenModules();

                try {
                    for (int i = 0; i < node.Names.Count; ++i)
                    {
                        if (!onlyImportModules)
                        {
                            if (node.Names[i].Name == "*")
                            {
                                foreach (var member in mod.GetMemberNames(ctxt))
                                {
                                    IMember mem;
                                    m[member] = mem = mod.GetMember(ctxt, member) ?? new AstPythonConstant(
                                        _interpreter.GetBuiltinType(BuiltinTypeId.Unknown),
                                        mod.Locations.ToArray()
                                        );
                                    (mem as IPythonModule)?.Imported(ctxt);
                                }
                                continue;
                            }
                            var n = node.AsNames?[i] ?? node.Names[i];
                            if (n != null)
                            {
                                IMember mem;
                                m[n.Name] = mem = mod.GetMember(ctxt, node.Names[i].Name) ?? new AstPythonConstant(
                                    _interpreter.GetBuiltinType(BuiltinTypeId.Unknown),
                                    GetLoc(n)
                                    );
                                (mem as IPythonModule)?.Imported(ctxt);
                            }
                        }
                    }
                } catch (IndexOutOfRangeException) {
                }
            }
            return(false);
        }
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,
     bool includeLocationInfo
     )
 {
     _interpreter         = interpreter;
     _context             = _interpreter.CreateModuleContext();
     _ast                 = ast;
     _module              = module;
     _filePath            = filePath;
     _members             = members;
     _scope               = new Stack <Dictionary <string, IMember> >();
     _noneInst            = new AstPythonConstant(_interpreter.GetBuiltinType(BuiltinTypeId.NoneType));
     _includeLocationInfo = includeLocationInfo;
 }
Ejemplo n.º 8
0
        internal PythonAnalyzer(IPythonInterpreterFactory factory, IPythonInterpreter pythonInterpreter)
        {
            _interpreterFactory           = factory;
            _langVersion                  = factory.GetLanguageVersion();
            _disposeInterpreter           = pythonInterpreter == null;
            _interpreter                  = pythonInterpreter ?? factory.CreateInterpreter();
            _builtinName                  = BuiltinTypeId.Unknown.GetModuleName(_langVersion);
            _modules                      = new ModuleTable(this, _interpreter);
            _modulesByFilename            = new ConcurrentDictionary <string, ModuleInfo>(StringComparer.OrdinalIgnoreCase);
            _modulesWithUnresolvedImports = new HashSet <ModuleInfo>();
            _itemCache                    = new Dictionary <object, AnalysisValue>();

            Limits = AnalysisLimits.GetDefaultLimits();

            Queue = new Deque <AnalysisUnit>();

            _defaultContext = _interpreter.CreateModuleContext();

            _evalUnit = new AnalysisUnit(null, null, new ModuleInfo("$global", new ProjectEntry(this, "$global", String.Empty, null, null), _defaultContext).Scope, true);
            AnalysisLog.NewUnit(_evalUnit);
        }
Ejemplo n.º 9
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Dictionary <string, IMember> members,
     bool includeLocationInfo
     )
 {
     _scope = new NameLookupContext(
         interpreter ?? throw new ArgumentNullException(nameof(interpreter)),
         interpreter.CreateModuleContext(),
         ast ?? throw new ArgumentNullException(nameof(ast)),
         filePath,
         includeLocationInfo
         );
     _module      = module ?? throw new ArgumentNullException(nameof(module));
     _members     = members ?? throw new ArgumentNullException(nameof(members));
     _noneInst    = new AstPythonConstant(_interpreter.GetBuiltinType(BuiltinTypeId.NoneType));
     _postWalkers = new List <AstAnalysisFunctionWalker>();
 }
Ejemplo n.º 10
0
        public AnalysisView(
            string dbDir,
            Version version     = null,
            bool withContention = false,
            bool withRecursion  = false
            )
        {
            var paths = new List <string>();

            paths.Add(dbDir);
            while (!File.Exists(IOPath.Combine(paths[0], "__builtin__.idb")) &&
                   !File.Exists(IOPath.Combine(paths[0], "builtins.idb")))
            {
                var upOne = IOPath.GetDirectoryName(paths[0]);
                if (string.IsNullOrEmpty(upOne) || upOne == paths[0])
                {
                    break;
                }
                paths.Insert(0, upOne);
            }

            if (withRecursion)
            {
                paths.AddRange(Directory.EnumerateDirectories(dbDir, "*", SearchOption.AllDirectories));
            }

            if (version == null)
            {
                if (File.Exists(IOPath.Combine(paths[0], "builtins.idb")))
                {
                    version = new Version(3, 3);
                }
                else
                {
                    version = new Version(2, 7);
                }
            }

            _factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                version,
                dbDir,
                paths.ToArray()
                );
            Path         = dbDir;
            _interpreter = _factory.CreateInterpreter();
            _context     = _interpreter.CreateModuleContext();

            var modNames = _interpreter.GetModuleNames();
            IEnumerable <Tuple <string, string> > modItems;

            if (!withRecursion)
            {
                modItems = modNames
                           .Select(n => Tuple.Create(n, IOPath.Combine(dbDir, n + ".idb")))
                           .Where(t => File.Exists(t.Item2));
            }
            else
            {
                modItems = modNames
                           .Select(n => Tuple.Create(
                                       n,
                                       Directory.EnumerateFiles(dbDir, n + ".idb", SearchOption.AllDirectories).FirstOrDefault()
                                       ))
                           .Where(t => File.Exists(t.Item2));
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            if (withContention)
            {
                modItems = modItems
                           .AsParallel()
                           .WithExecutionMode(ParallelExecutionMode.ForceParallelism);
            }
            _modules = modItems
                       .Select(t => new ModuleView(_interpreter, _context, t.Item1, t.Item2))
                       .OrderBy(m => m.SortKey)
                       .ThenBy(m => m.Name)
                       .ToList <IAnalysisItemView>();
            stopwatch.Stop();

            _modules.Insert(0, new KnownTypesView(_interpreter, version));

            LoadMilliseconds    = stopwatch.ElapsedMilliseconds;
            TopLevelModuleCount = _modules.Count - 1;
        }
Ejemplo n.º 11
0
        public AnalysisView(
            string dbDir,
            Version version = null,
            bool withContention = false,
            bool withRecursion = false
        ) {
            var paths = new List<string>();
            paths.Add(dbDir);
            while (!File.Exists(IOPath.Combine(paths[0], "__builtin__.idb")) &&
                !File.Exists(IOPath.Combine(paths[0], "builtins.idb"))) {
                var upOne = IOPath.GetDirectoryName(paths[0]);
                if (string.IsNullOrEmpty(upOne) || upOne == paths[0]) {
                    break;
                }
                paths.Insert(0, upOne);
            }

            if (withRecursion) {
                paths.AddRange(Directory.EnumerateDirectories(dbDir, "*", SearchOption.AllDirectories));
            }

            if (version == null) {
                if (File.Exists(IOPath.Combine(paths[0], "builtins.idb"))) {
                    version = new Version(3, 3);
                } else {
                    version = new Version(2, 7);
                }
            }

            _factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                version,
                dbDir,
                paths.ToArray()
            );
            Path = dbDir;
            _interpreter = _factory.CreateInterpreter();
            _context = _interpreter.CreateModuleContext();

            var modNames = _interpreter.GetModuleNames();
            IEnumerable<Tuple<string, string>> modItems;

            if (!withRecursion) {
                modItems = modNames
                    .Select(n => Tuple.Create(n, IOPath.Combine(dbDir, n + ".idb")))
                    .Where(t => File.Exists(t.Item2));
            } else {
                modItems = modNames
                    .Select(n => Tuple.Create(
                        n,
                        Directory.EnumerateFiles(dbDir, n + ".idb", SearchOption.AllDirectories).FirstOrDefault()
                    ))
                    .Where(t => File.Exists(t.Item2));
            }

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            if (withContention) {
                modItems = modItems
                    .AsParallel()
                    .WithExecutionMode(ParallelExecutionMode.ForceParallelism);
            }
            _modules = modItems
                .Select(t => new ModuleView(_interpreter, _context, t.Item1, t.Item2))
                .OrderBy(m => m.SortKey)
                .ThenBy(m => m.Name)
                .ToList<IAnalysisItemView>();
            stopwatch.Stop();

            _modules.Insert(0, new KnownTypesView(_interpreter, version));

            LoadMilliseconds = stopwatch.ElapsedMilliseconds;
            TopLevelModuleCount = _modules.Count - 1;
        }