예제 #1
0
        public PythonAnalyzer CreateAnalyzer(PythonLanguageVersion version = PythonLanguageVersion.V27, string[] analysisDirs = null)
        {
            // Explicitly provide the builtins name, since we aren't recreating
            // the interpreter for each version like we should be.
            var fact   = InterpreterFactory;
            var interp = Interpreter;

            if (version != fact.GetLanguageVersion())
            {
                fact   = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                interp = fact.CreateInterpreter();
            }
            var state = PythonAnalyzer.CreateSynchronously(fact, interp, "__builtin__");

            if (version.Is3x() || this is IronPythonAnalysisTest)
            {
                var types = (KnownTypes)state.Types;
                types._types[(int)BuiltinTypeId.Str]              = state.Types[BuiltinTypeId.Unicode];
                types._types[(int)BuiltinTypeId.StrIterator]      = state.Types[BuiltinTypeId.UnicodeIterator];
                types._classInfos[(int)BuiltinTypeId.Str]         = state.ClassInfos[BuiltinTypeId.Unicode];
                types._classInfos[(int)BuiltinTypeId.StrIterator] = state.ClassInfos[BuiltinTypeId.UnicodeIterator];
            }

            state.Limits = GetLimits();
            if (analysisDirs != null)
            {
                foreach (var dir in analysisDirs)
                {
                    state.AddAnalysisDirectory(dir);
                }
            }

            return(state);
        }
예제 #2
0
        private static void AnalyzeCode(
            out PythonAnalyzer analyzer,
            out IPythonProjectEntry entry,
            string code,
            Version preferredVersion = null,
            InterpreterArchitecture preferredArch = null,
            string module = "test-module"
            )
        {
            var provider = InterpFactory;
            var factory  = provider.GetInterpreterFactories().OrderByDescending(f => f.Configuration.Version)
                           .Where(f => preferredVersion == null || f.Configuration.Version == preferredVersion)
                           .Where(f => preferredArch == null || f.Configuration.Architecture == preferredArch)
                           .FirstOrDefault();

            Assert.IsNotNull(factory, "no factory found");

            analyzer = PythonAnalyzer.CreateSynchronously(factory);
            var path = Path.Combine(TestData.GetTempPath(randomSubPath: true), module.Replace('.', '\\'));

            Directory.CreateDirectory(PathUtils.GetParent(path));
            File.WriteAllText(path, code);

            entry = analyzer.AddModule(module, path);
            PythonAst ast;

            using (var p = Parser.CreateParser(new StringReader(code), factory.GetLanguageVersion())) {
                ast = p.ParseFile();
                entry.UpdateTree(ast, null);
            }

            entry.Analyze(CancellationToken.None, true);
        }
예제 #3
0
        private SaveLoadResult SaveLoad(PythonLanguageVersion version, params AnalysisModule[] modules)
        {
            IPythonProjectEntry[] entries = new IPythonProjectEntry[modules.Length];

            var fact   = InterpreterFactory;
            var interp = Interpreter;

            if (version != fact.GetLanguageVersion())
            {
                fact   = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                interp = fact.CreateInterpreter();
            }

            var codeFolder = TestData.GetTempPath(randomSubPath: true);
            var dbFolder   = Path.Combine(codeFolder, "DB");

            Directory.CreateDirectory(codeFolder);
            Directory.CreateDirectory(dbFolder);

            var state = PythonAnalyzer.CreateSynchronously(fact, interp, SharedDatabaseState.BuiltinName2x);

            for (int i = 0; i < modules.Length; i++)
            {
                var fullname = Path.Combine(codeFolder, modules[i].Filename);
                File.WriteAllText(fullname, modules[i].Code);
                entries[i] = state.AddModule(modules[i].ModuleName, fullname);
                Prepare(entries[i], new StringReader(modules[i].Code), version);
            }

            for (int i = 0; i < modules.Length; i++)
            {
                entries[i].Analyze(CancellationToken.None, false);
            }

            new SaveAnalysis().Save(state, dbFolder);

            File.Copy(
                Path.Combine(PythonTypeDatabase.BaselineDatabasePath, "__builtin__.idb"),
                Path.Combine(dbFolder, "__builtin__.idb"),
                true
                );

            var loadFactory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                version.ToVersion(),
                null,
                dbFolder
                );

            return(new SaveLoadResult(PythonAnalyzer.CreateSynchronously(loadFactory), codeFolder));
        }
예제 #4
0
 public PythonAnalysis(
     IPythonInterpreterFactory factory,
     IPythonInterpreter interpreter = null
     )
 {
     if (factory == null)
     {
         Assert.Inconclusive("Expected interpreter is not installed");
     }
     _factory       = factory;
     _analyzer      = PythonAnalyzer.CreateSynchronously(factory, interpreter);
     _entries       = new Dictionary <string, IPythonProjectEntry>(StringComparer.OrdinalIgnoreCase);
     _tasks         = new ConcurrentDictionary <IPythonProjectEntry, TaskCompletionSource <CollectingErrorSink> >();
     _cachedMembers = new Dictionary <BuiltinTypeId, string[]>();
     _root          = TestData.GetTempPath();
 }
예제 #5
0
        public void InvalidIronPythonDatabase()
        {
            using (var db = MockCompletionDB.Create(PythonLanguageVersion.V27,
                                                    // __bad_builtin__ is missing str
                                                    Tuple.Create("__bad_builtin__", "__builtin__")
                                                    )) {
                var ptd = db.Database;

                Assert.IsNotNull(ptd.GetModule("__builtin__"));

                var factory = new IronPythonInterpreterFactory();
                // Explicitly create an IronPythonInterpreter from factory that
                // will use the database in db.Factory.
                using (var analyzer = PythonAnalyzer.CreateSynchronously(factory, factory.MakeInterpreter(db.Factory))) {
                    // String type should have been loaded anyway
                    Assert.IsNotNull(analyzer.ClassInfos[BuiltinTypeId.Str]);
                }
            }
        }
예제 #6
0
        private SaveLoadResult SaveLoad(PythonLanguageVersion version, params AnalysisModule[] modules)
        {
            IPythonProjectEntry[] entries = new IPythonProjectEntry[modules.Length];

            var fact   = InterpreterFactory;
            var interp = Interpreter;

            if (version != fact.GetLanguageVersion())
            {
                fact   = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                interp = fact.CreateInterpreter();
            }

            var state = new PythonAnalyzer(fact, interp, SharedDatabaseState.BuiltinName2x);

            state.ReloadModulesAsync().WaitAndUnwrapExceptions();
            for (int i = 0; i < modules.Length; i++)
            {
                entries[i] = state.AddModule(modules[i].ModuleName, modules[i].Filename);
                Prepare(entries[i], new StringReader(modules[i].Code), version);
            }

            for (int i = 0; i < modules.Length; i++)
            {
                entries[i].Analyze(CancellationToken.None);
            }

            string tmpFolder = TestData.GetTempPath("6666d700-a6d8-4e11-8b73-3ba99a61e27b");

            Directory.CreateDirectory(tmpFolder);

            new SaveAnalysis().Save(state, tmpFolder);

            File.Copy(Path.Combine(PythonTypeDatabase.BaselineDatabasePath, "__builtin__.idb"), Path.Combine(tmpFolder, "__builtin__.idb"), true);

            return(new SaveLoadResult(
                       PythonAnalyzer.CreateSynchronously(InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion(), null, tmpFolder)),
                       tmpFolder
                       ));
        }
예제 #7
0
        public void XamlEmptyXName()
        {
            // [Python Tools] Adding attribute through XAML in IronPython application crashes VS.
            // http://pytools.codeplex.com/workitem/743
            using (var analyzer = PythonAnalyzer.CreateSynchronously(InterpreterFactory, Interpreter)) {
                string xamlPath  = TestData.GetPath(@"TestData\Xaml\EmptyXName.xaml");
                string pyPath    = TestData.GetPath(@"TestData\Xaml\EmptyXName.py");
                var    xamlEntry = analyzer.AddXamlFile(xamlPath);
                var    pyEntry   = analyzer.AddModule("EmptyXName", pyPath);

                xamlEntry.ParseContent(new FileStreamReader(xamlPath), null);

                using (var parser = Parser.CreateParser(new FileStreamReader(pyPath), PythonLanguageVersion.V27, new ParserOptions()
                {
                    BindReferences = true
                })) {
                    pyEntry.UpdateTree(parser.ParseFile(), null);
                }

                pyEntry.Analyze(CancellationToken.None);
            }
        }
예제 #8
0
        public void RemovedModule()
        {
            var id       = Guid.NewGuid().ToString();
            var config   = new InterpreterConfiguration(id, id, version: new Version(3, 5));
            var fact     = new MockPythonInterpreterFactory(config);
            var interp   = new MockPythonInterpreter(fact);
            var analyzer = PythonAnalyzer.CreateSynchronously(fact, interp);
            var modules  = new ModuleTable(analyzer, interp);

            var orig = modules.Select(kv => kv.Key).ToSet();

            interp.AddModule("test", new MockPythonModule("test"));

            ModuleReference modref;

            Assert.IsTrue(modules.TryImport("test", out modref));

            interp.RemoveModule("test", retainName: true);

            modules.ReInit();
            Assert.IsFalse(modules.TryImport("test", out modref));
        }
예제 #9
0
        public void TestParsePerf_Decimal()
        {
            string merlin = Environment.GetEnvironmentVariable("DLR_ROOT") ?? @"C:\Product\0\dlr";
            var    text   = File.ReadAllText(Path.Combine(merlin + @"\External.LCA_RESTRICTED\Languages\IronPython\27\Lib\decimal.py"));

            var       sourceUnit   = GetSourceUnit(text);
            var       projectState = PythonAnalyzer.CreateSynchronously(InterpreterFactory, Interpreter);
            Stopwatch sw           = new Stopwatch();
            var       entry        = projectState.AddModule("decimal", "decimal", null);

            Prepare(entry, sourceUnit);
            entry.Analyze(CancellationToken.None);

            sw.Start();
            for (int i = 0; i < 5; i++)
            {
                Prepare(entry, sourceUnit);
                entry.Analyze(CancellationToken.None);
            }

            sw.Stop();
            Console.WriteLine("{0}", sw.ElapsedMilliseconds);
        }
예제 #10
0
        private IEnumerable <Task> StartCrossThreadAnalysisCalls(
            CancellationToken cancel,
            PythonLanguageVersion version
            )
        {
            var fact  = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var state = PythonAnalyzer.CreateSynchronously(fact);

            const string testCode = @"from mod{0:000} import test_func as other_test_func, MyClass as other_mc

c = None
def test_func(a, b={1}()):
    '''My test function'''
    globals c
    a = b
    a = {1}(a)
    b = other_test_func(a)
    c = other_mc.fn(b)
    return b

class MyClass:
    fn = test_func

my_test_func = test_func
my_test_func = other_test_func
my_test_func('abc')

mc = MyClass()
mc.fn([])
";

            var entries = Enumerable.Range(0, 100)
                          .Select(i => {
                var entry  = state.AddModule(string.Format("mod{0:000}", i), string.Format("mod{0:000}.py", i));
                var parser = Parser.CreateParser(new StringReader(testCode.FormatInvariant(i + 1, PythonTypes[i % PythonTypes.Count])), PythonLanguageVersion.V34);
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                return(entry);
            })
                          .ToList();

            // One analysis before we start
            foreach (var e in entries)
            {
                e.Analyze(cancel, true);
            }
            state.AnalyzeQueuedEntries(cancel);

            // Repeatedly re-analyse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select(e => Tuple.Create(rnd.Next(), e))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 2)
                                      .Select(t => t.Item2)
                                      .ToList();
                    foreach (var e in shufEntries)
                    {
                        e.Analyze(cancel, true);
                    }

                    state.AnalyzeQueuedEntries(cancel);
                    Console.WriteLine("Analysis complete");
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly re-parse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select((e, i) => Tuple.Create(rnd.Next(), e, i))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 4)
                                      .ToList();
                    foreach (var t in shufEntries)
                    {
                        var i = t.Item3;
                        var parser = Parser.CreateParser(new StringReader(testCode.FormatInvariant(i + 1, PythonTypes[i % PythonTypes.Count])), PythonLanguageVersion.V34);
                        using (var p = t.Item2.BeginParse()) {
                            p.Tree = parser.ParseFile();
                            p.Complete();
                        }
                    }
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly request signatures
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var sigs = entry.Analysis.GetSignaturesByIndex("my_test_func", 0).ToList();

                    if (sigs.Any())
                    {
                        AssertUtil.ContainsExactly(
                            sigs.Select(s => s.Name),
                            "test_func"
                            );

                        foreach (var s in sigs)
                        {
                            AssertUtil.ContainsExactly(s.Parameters.Select(p => p.Name), "a", "b");
                        }
                    }
                }
            }, cancel));

            // Repeated request variables and descriptions
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetDescriptionsByIndex("my_test_func", 0).ToList();
                    descriptions = entry.Analysis.GetDescriptionsByIndex("c", 0).ToList();
                }
            }, cancel));

            // Repeated request members and documentation
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetCompletionDocumentationByIndex("mc", "fn", 0).ToList();
                }
            }, cancel));
        }
예제 #11
0
 /// <summary>
 /// Returns an analyzer using the mock DB. If not provided,
 /// <see cref="Factory"/> is used in place of <paramref name="factory"/>
 /// </summary>
 /// <param name="factory">The factory to use for the analyzer. If
 /// omitted, <see cref="Factory"/> will be used.</param>
 public PythonAnalyzer MakeAnalyzer(IPythonInterpreterFactory factory = null)
 {
     return(PythonAnalyzer.CreateSynchronously(factory ?? Factory));
 }
예제 #12
0
 private PythonAnalyzer MakeTestAnalyzer()
 {
     return(PythonAnalyzer.CreateSynchronously(InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7))));
 }