public void LayeredDatabase() { using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os")) using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) { Assert.IsNotNull(db1.Database.GetModule("os")); Assert.IsNull(db1.Database.GetModule("posixpath")); var ptd1 = db1.Database; var ptd2 = ptd1.Clone(); ptd2.LoadDatabase(db2.DBPath); Assert.IsNull(ptd1.GetModule("posixpath")); Assert.IsNotNull(ptd2.GetModule("os")); Assert.IsNotNull(ptd2.GetModule("posixpath")); Assert.AreSame(ptd1.GetModule("os"), db1.Database.GetModule("os")); Assert.AreSame(ptd2.GetModule("os"), db1.Database.GetModule("os")); } var factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7)); using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os", "posixpath")) using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) { var ptd1 = new PythonTypeDatabase(factory, new[] { db1.DBPath, db2.DBPath }); Assert.IsNotNull(ptd1.GetModule("posixpath")); Assert.AreNotSame(ptd1.GetModule("posixpath"), db1.Database.GetModule("posixpath")); Assert.AreNotSame(ptd1.GetModule("posixpath"), db2.Database.GetModule("posixpath")); var ptd2 = new PythonTypeDatabase(factory, new[] { db2.DBPath, db1.DBPath }); Assert.IsNotNull(ptd2.GetModule("posixpath")); Assert.AreNotSame(ptd2.GetModule("posixpath"), db1.Database.GetModule("posixpath")); Assert.AreNotSame(ptd2.GetModule("posixpath"), db2.Database.GetModule("posixpath")); } using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os", "posixpath")) using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) using (var db3 = MockCompletionDB.Create(PythonLanguageVersion.V27, "ntpath")) { var ptd = db1.Database; Assert.AreSame(ptd.GetModule("posixpath"), db1.Database.GetModule("posixpath")); Assert.AreNotSame(ptd.GetModule("posixpath"), db2.Database.GetModule("posixpath")); var ptd2 = ptd.Clone(); ptd2.LoadDatabase(db2.DBPath); Assert.AreNotSame(ptd2.GetModule("posixpath"), ptd.GetModule("posixpath")); var ptd3 = ptd2.Clone(); ptd3.LoadDatabase(db3.DBPath); Assert.IsNotNull(ptd3.GetModule("ntpath")); Assert.IsNull(ptd2.GetModule("ntpath")); } }
public void TestPthFiles() { var outputPath = TestData.GetTempPath(); Console.WriteLine("Writing to: " + outputPath); // run the analyzer using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe", "/lib", TestData.GetPath("TestData", "PathStdLib"), "/version", "2.7", "/outdir", outputPath, "/indir", CompletionDB, "/unittest", "/log", "AnalysisLog.txt")) { output.Wait(); Console.WriteLine("* Stdout *"); foreach (var line in output.StandardOutputLines) { Console.WriteLine(line); } Console.WriteLine("* Stderr *"); foreach (var line in output.StandardErrorLines) { Console.WriteLine(line); } Assert.AreEqual(0, output.ExitCode); } File.Copy(Path.Combine(CompletionDB, "__builtin__.idb"), Path.Combine(outputPath, "__builtin__.idb")); var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7)); var paths = new List <string> { outputPath }; paths.AddRange(Directory.EnumerateDirectories(outputPath)); var typeDb = new PythonTypeDatabase(fact, paths); var module = typeDb.GetModule("SomeLib"); Assert.IsNotNull(module, "Could not import SomeLib"); var fobMod = typeDb.GetModule("SomeLib.fob"); Assert.IsNotNull(fobMod, "Could not import SomeLib.fob"); var cClass = ((IPythonModule)fobMod).GetMember(null, "C"); Assert.IsNotNull(cClass, "Could not get SomeLib.fob.C"); Assert.AreEqual(PythonMemberType.Class, cClass.MemberType); }
public IPythonModule ImportModule(string name) { var mod = _typeDb?.GetModule(name); if (mod == null) { mod = _searchPathDb?.GetModule(name); if (mod == null) { foreach (var searchPath in _searchPaths.MaybeEnumerate()) { try { if (File.Exists(searchPath)) { mod = LoadModuleFromZipFile(searchPath, name); } else if (Directory.Exists(searchPath)) { mod = LoadModuleFromDirectory(searchPath, name); } } catch (ArgumentException) { return(null); } if (mod != null) { break; } } } } return(mod); }
public IPythonModule ImportModule(string name) { if (_typeDb == null) { return(null); } return(_typeDb.GetModule(name)); }
public IPythonModule ImportModule(string name) { if (string.IsNullOrWhiteSpace(name)) { return(null); } // clr module needs to be an IronPythonModule, not a CPythonModule, so we can track when it's imported // and make clr completions available. if (_typeDb != null && name != "clr") { var res = _typeDb.GetModule(name); if (res != null) { return(res); } } IronPythonModule mod; if (_modules.TryGetValue(name, out mod)) { return(mod); } var handle = Remote.LookupNamespace(name); if (!handle.IsNull) { return(MakeObject(handle) as IPythonModule); } var nameParts = name.Split('.'); IPythonModule pythonMod; if (nameParts.Length > 1 && (pythonMod = ImportModule(nameParts[0])) != null) { for (int i = 1; i < nameParts.Length && pythonMod != null; ++i) { pythonMod = pythonMod.GetMember(IronPythonModuleContext.ShowClrInstance, nameParts[i]) as IPythonModule; } if (pythonMod != null) { return(pythonMod); } } return(null); }
public IPythonModule ImportModule(string name) { var mod = _typeDb?.GetModule(name); if (mod == null) { PythonTypeDatabase db; lock (_searchPathDbLock) { db = _searchPathDb; } mod = db?.GetModule(name); if (mod == null) { foreach (var searchPath in _searchPaths.MaybeEnumerate()) { try { if (File.Exists(searchPath)) { mod = LoadModuleFromZipFile(searchPath, name); } else if (Directory.Exists(searchPath)) { mod = LoadModuleFromDirectory(searchPath, name); } } catch (ArgumentException ex) { Debug.Fail(ex.ToUnhandledExceptionMessage(GetType())); return(null); } if (mod != null) { break; } } } } return(mod); }
public void PydInPackage() { PythonPaths.Python27.AssertInstalled(); var outputPath = TestData.GetTempPath(); Console.WriteLine("Writing to: " + outputPath); // run the analyzer using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe", "/python", PythonPaths.Python27.InterpreterPath, "/lib", TestData.GetPath(@"TestData\PydStdLib"), "/version", "2.7", "/outdir", outputPath, "/indir", CompletionDB, "/log", "AnalysisLog.txt")) { output.Wait(); Console.WriteLine("* Stdout *"); foreach (var line in output.StandardOutputLines) { Console.WriteLine(line); } Console.WriteLine("* Stderr *"); foreach (var line in output.StandardErrorLines) { Console.WriteLine(line); } Assert.AreEqual(0, output.ExitCode); } var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7)); var paths = new List <string> { outputPath }; paths.AddRange(Directory.EnumerateDirectories(outputPath)); var typeDb = new PythonTypeDatabase(fact, paths); var module = typeDb.GetModule("Package.winsound"); Assert.IsNotNull(module, "Package.winsound was not analyzed"); var package = typeDb.GetModule("Package"); Assert.IsNotNull(package, "Could not import Package"); var member = package.GetMember(null, "winsound"); Assert.IsNotNull(member, "Could not get member Package.winsound"); Assert.AreSame(module, member); module = typeDb.GetModule("Package._testcapi"); Assert.IsNotNull(module, "Package._testcapi was not analyzed"); package = typeDb.GetModule("Package"); Assert.IsNotNull(package, "Could not import Package"); member = package.GetMember(null, "_testcapi"); Assert.IsNotNull(member, "Could not get member Package._testcapi"); Assert.IsNotInstanceOfType(member, typeof(CPythonMultipleMembers)); Assert.AreSame(module, member); module = typeDb.GetModule("Package.select"); Assert.IsNotNull(module, "Package.select was not analyzed"); package = typeDb.GetModule("Package"); Assert.IsNotNull(package, "Could not import Package"); member = package.GetMember(null, "select"); Assert.IsNotNull(member, "Could not get member Package.select"); Assert.IsInstanceOfType(member, typeof(CPythonMultipleMembers)); var mm = (CPythonMultipleMembers)member; AssertUtil.ContainsExactly(mm.Members.Select(m => m.MemberType), PythonMemberType.Module, PythonMemberType.Constant, PythonMemberType.Class ); Assert.IsNotNull(mm.Members.Contains(module)); try { // Only clean up if the test passed Directory.Delete(outputPath, true); } catch { } }
public IPythonModule ImportModule(string name) { if (string.IsNullOrWhiteSpace(name)) { return(null); } if (name == _builtinModule?.Name) { return(_builtinModule); } IPythonModule mod; if (_modules.TryGetValue(name, out mod)) { return(mod); } var handle = Remote.LookupNamespace(name); if (!handle.IsNull) { mod = MakeObject(handle) as IPythonModule; if (mod != null) { return(_modules.GetOrAdd(name, mod)); } } if (_typeDb != null) { var res = _typeDb.GetModule(name); if (res != null) { return(res); } } else if (_factory is AstPythonInterpreterFactory apf) { var ctxt = new AstPythonInterpreterFactory.TryImportModuleContext { Interpreter = this, BuiltinModule = _builtinModule, ModuleCache = _modules }; for (int retries = 5; retries > 0; --retries) { // The call should be cancelled by the cancellation token, but since we // are blocking here we wait for slightly longer. Timeouts are handled // gracefully by TryImportModuleAsync(), so we want those to trigger if // possible, but if all else fails then we'll abort and treat it as an // error. // (And if we've got a debugger attached, don't time out at all.) Task <AstPythonInterpreterFactory.TryImportModuleResult> impTask; #if DEBUG if (Debugger.IsAttached) { impTask = apf.TryImportModuleAsync(name, ctxt, CancellationToken.None); } else { #endif var cts = new CancellationTokenSource(5000); impTask = apf.TryImportModuleAsync(name, ctxt, cts.Token); try { if (!impTask.Wait(10000)) { throw new OperationCanceledException(); } } catch (OperationCanceledException) { Debug.Fail("Import timeout"); return(null); } #if DEBUG } #endif var result = impTask.Result; mod = result.Module; switch (result.Status) { case AstPythonInterpreterFactory.TryImportModuleResultCode.Success: if (mod == null) { _modules.TryRemove(name, out _); break; } return(mod); case AstPythonInterpreterFactory.TryImportModuleResultCode.ModuleNotFound: retries = 0; _modules.TryRemove(name, out _); break; case AstPythonInterpreterFactory.TryImportModuleResultCode.NotSupported: retries = 0; break; case AstPythonInterpreterFactory.TryImportModuleResultCode.NeedRetry: case AstPythonInterpreterFactory.TryImportModuleResultCode.Timeout: break; } } } var nameParts = name.Split('.'); mod = null; if (nameParts.Length > 1 && (mod = ImportModule(nameParts[0])) != null) { for (int i = 1; i < nameParts.Length && mod != null; ++i) { mod = mod.GetMember(IronPythonModuleContext.ShowClrInstance, nameParts[i]) as IPythonModule; } } return(_modules.GetOrAdd(name, mod)); }
public IPythonModule ImportModule(string name) { if (string.IsNullOrWhiteSpace(name)) { return(null); } if (name == _builtinModule?.Name) { return(_builtinModule); } IPythonModule mod; if (_modules.TryGetValue(name, out mod)) { return(mod); } var handle = Remote.LookupNamespace(name); if (!handle.IsNull) { mod = MakeObject(handle) as IPythonModule; if (mod != null) { return(_modules.GetOrAdd(name, mod)); } } if (_typeDb != null) { var res = _typeDb.GetModule(name); if (res != null) { return(res); } } else if (_factory is AstPythonInterpreterFactory apf) { var ctxt = new AstPythonInterpreterFactory.TryImportModuleContext { Interpreter = this, BuiltinModule = _builtinModule, ModuleCache = _modules }; for (int retries = 5; retries > 0; --retries) { switch (apf.TryImportModule(name, out mod, ctxt)) { case AstPythonInterpreterFactory.TryImportModuleResult.Success: if (mod == null) { _modules.TryRemove(name, out _); break; } return(mod); case AstPythonInterpreterFactory.TryImportModuleResult.ModuleNotFound: retries = 0; _modules.TryRemove(name, out _); break; case AstPythonInterpreterFactory.TryImportModuleResult.NotSupported: retries = 0; break; case AstPythonInterpreterFactory.TryImportModuleResult.NeedRetry: case AstPythonInterpreterFactory.TryImportModuleResult.Timeout: break; } } } var nameParts = name.Split('.'); mod = null; if (nameParts.Length > 1 && (mod = ImportModule(nameParts[0])) != null) { for (int i = 1; i < nameParts.Length && mod != null; ++i) { mod = mod.GetMember(IronPythonModuleContext.ShowClrInstance, nameParts[i]) as IPythonModule; } } return(_modules.GetOrAdd(name, mod)); }