Exemplo n.º 1
0
        /// <summary>
        /// Loads a new instance from the specified registry key.
        /// </summary>
        /// <param name="key">
        /// The key to load settings from. Each setting is a DWORD value. If
        /// null, all settings are assumed to be unspecified and the default
        /// values are used.
        /// </param>
        /// <param name="defaultToStdLib">
        /// If True, unspecified settings are taken from the defaults for
        /// standard library analysis. Otherwise, they are taken from the
        /// usual defaults.
        /// </param>
        public static AnalysisLimits LoadFromStorage(RegistryKey key, bool defaultToStdLib = false)
        {
            var limits = defaultToStdLib ? AnalysisLimits.GetStandardLibraryLimits() : new AnalysisLimits();

            if (key != null)
            {
                limits.CrossModule                    = (key.GetValue(CrossModuleId) as int?) ?? limits.CrossModule;
                limits.CallDepth                      = (key.GetValue(CallDepthId) as int?) ?? limits.CallDepth;
                limits.DecreaseCallDepth              = (key.GetValue(DecreaseCallDepthId) as int?) ?? limits.DecreaseCallDepth;
                limits.NormalArgumentTypes            = (key.GetValue(NormalArgumentTypesId) as int?) ?? limits.NormalArgumentTypes;
                limits.ListArgumentTypes              = (key.GetValue(ListArgumentTypesId) as int?) ?? limits.ListArgumentTypes;
                limits.DictArgumentTypes              = (key.GetValue(DictArgumentTypesId) as int?) ?? limits.DictArgumentTypes;
                limits.ReturnTypes                    = (key.GetValue(ReturnTypesId) as int?) ?? limits.ReturnTypes;
                limits.YieldTypes                     = (key.GetValue(YieldTypesId) as int?) ?? limits.YieldTypes;
                limits.InstanceMembers                = (key.GetValue(InstanceMembersId) as int?) ?? limits.InstanceMembers;
                limits.DictKeyTypes                   = (key.GetValue(DictKeyTypesId) as int?) ?? limits.DictKeyTypes;
                limits.DictValueTypes                 = (key.GetValue(DictValueTypesId) as int?) ?? limits.DictValueTypes;
                limits.IndexTypes                     = (key.GetValue(IndexTypesId) as int?) ?? limits.IndexTypes;
                limits.AssignedTypes                  = (key.GetValue(AssignedTypesId) as int?) ?? limits.AssignedTypes;
                limits.UnifyCallsToNew                = ((key.GetValue(UnifyCallsToNewId) as int?) ?? (limits.UnifyCallsToNew ? 1 : 0)) != 0;
                limits.ProcessCustomDecorators        = ((key.GetValue(ProcessCustomDecoratorsId) as int?) ?? (limits.ProcessCustomDecorators ? 1 : 0)) != 0;
                limits.UseTypeStubPackages            = ((key.GetValue(UseTypeStubPackagesId) as int?) ?? (limits.UseTypeStubPackages ? 1 : 0)) != 0;
                limits.UseTypeStubPackagesExclusively = ((key.GetValue(UseTypeStubPackagesExclusivelyId) as int?) ?? (limits.UseTypeStubPackagesExclusively ? 1 : 0)) != 0;
            }

            return(limits);
        }
Exemplo n.º 2
0
        public TestAnalyzer(
            IPythonInterpreterFactory factory,
            string containerFilePath,
            string codeFileBasePath,
            Uri executorUri
            )
        {
            _analyzer        = PythonAnalyzer.CreateAsync(factory).WaitAndUnwrapExceptions();
            _analyzer.Limits = AnalysisLimits.GetStandardLibraryLimits();

            _containerFilePath = containerFilePath;
            _codeFileBasePath  = codeFileBasePath;
            _executorUri       = executorUri;

            _entries = new List <IPythonProjectEntry>();
        }
Exemplo n.º 3
0
        internal PythonAnalyzer AnalyzeDir(string dir, PythonLanguageVersion version = PythonLanguageVersion.V27, IEnumerable <string> excludeDirectories = null, CancellationToken?cancel = null)
        {
            List <string> files = new List <string>();

            try {
                ISet <string> excluded = null;
                if (excludeDirectories != null)
                {
                    excluded = new HashSet <string>(excludeDirectories, StringComparer.InvariantCultureIgnoreCase);
                }
                CollectFiles(dir, files, excluded);
            } catch (DirectoryNotFoundException) {
                return(null);
            }

            List <FileStreamReader> sourceUnits = new List <FileStreamReader>();

            foreach (string file in files)
            {
                sourceUnits.Add(
                    new FileStreamReader(file)
                    );
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();
            long start0 = sw.ElapsedMilliseconds;
            // Explicitly specify the builtins name because we are using a 2.7
            // interpreter for all versions.
            var fact         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var projectState = new PythonAnalyzer(fact, fact.CreateInterpreter(), "__builtin__");

            projectState.Limits = AnalysisLimits.GetStandardLibraryLimits();

            var modules = new List <IPythonProjectEntry>();

            foreach (var sourceUnit in sourceUnits)
            {
                try {
                    modules.Add(projectState.AddModule(
                                    ModulePath.FromFullPath(sourceUnit.Path).ModuleName,
                                    sourceUnit.Path,
                                    null
                                    ));
                } catch (ArgumentException) {
                    // Invalid module name, so skip the module
                }
            }
            long start1 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("AddSourceUnit: {0} ms", start1 - start0);

            var nodes = new List <Microsoft.PythonTools.Parsing.Ast.PythonAst>();

            for (int i = 0; i < modules.Count; i++)
            {
                PythonAst ast = null;
                try {
                    var sourceUnit = sourceUnits[i];

                    ast = Parser.CreateParser(sourceUnit, version).ParseFile();
                } catch (Exception) {
                }
                nodes.Add(ast);
            }
            long start2 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("Parse: {0} ms", start2 - start1);

            for (int i = 0; i < modules.Count; i++)
            {
                var ast = nodes[i];

                if (ast != null)
                {
                    modules[i].UpdateTree(ast, null);
                }
            }

            long start3 = sw.ElapsedMilliseconds;

            for (int i = 0; i < modules.Count; i++)
            {
                Trace.TraceInformation("Analyzing {1}: {0} ms", sw.ElapsedMilliseconds - start3, sourceUnits[i].Path);
                var ast = nodes[i];
                if (ast != null)
                {
                    modules[i].Analyze(cancel ?? CancellationToken.None, true);
                }
            }
            if (modules.Count > 0)
            {
                Trace.TraceInformation("Analyzing queue");
                modules[0].AnalysisGroup.AnalyzeQueuedEntries(cancel ?? CancellationToken.None);
            }

            long start4 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("Analyze: {0} ms", start4 - start3);
            return(projectState);
        }