public DerivedInterpreterFactory(
            PythonInterpreterFactoryWithDatabase baseFactory,
            InterpreterFactoryCreationOptions options
        ) : base(
                options.Id,
                options.Description,
                new InterpreterConfiguration(
                    options.PrefixPath,
                    options.InterpreterPath,
                    options.WindowInterpreterPath,
                    options.LibraryPath,
                    options.PathEnvironmentVariableName,
                    options.Architecture,
                    options.LanguageVersion,
                    InterpreterUIMode.CannotBeDefault | InterpreterUIMode.CannotBeConfigured
                ),
                options.WatchLibraryForNewModules
        ) {
            if (baseFactory.Configuration.Version != options.LanguageVersion) {
                throw new ArgumentException("Language versions do not match", "options");
            }

            _base = baseFactory;
            _base.IsCurrentChanged += Base_IsCurrentChanged;
            _base.NewDatabaseAvailable += Base_NewDatabaseAvailable;

            _description = options.Description;

            if (Volatile.Read(ref _deferRefreshIsCurrent)) {
                // This rare race condition is due to a design flaw that is in
                // shipped public API and cannot be fixed without breaking
                // compatibility with 3rd parties.
                RefreshIsCurrent();
            }
        }
Beispiel #2
0
        public PythonTypeDatabase(
            PythonInterpreterFactoryWithDatabase factory,
            IEnumerable <string> databaseDirectories = null,
            PythonTypeDatabase innerDatabase         = null
            )
        {
            if (innerDatabase != null && factory.Configuration.Version != innerDatabase.LanguageVersion)
            {
                throw new InvalidOperationException("Language versions do not match");
            }

            _factory = factory;
            if (innerDatabase != null)
            {
                _sharedState = new SharedDatabaseState(innerDatabase._sharedState);
            }
            else
            {
                _sharedState = new SharedDatabaseState(_factory.Configuration.Version);
            }

            if (databaseDirectories != null)
            {
                foreach (var d in databaseDirectories)
                {
                    LoadDatabase(d);
                }
            }

            _sharedState.ListenForCorruptDatabase(this);
        }
Beispiel #3
0
        public DerivedInterpreterFactory(PythonInterpreterFactoryWithDatabase baseFactory,
                                         InterpreterFactoryCreationOptions options)
            : base(options.Id,
                   options.Description,
                   new InterpreterConfiguration(options.PrefixPath,
                                                options.InterpreterPath,
                                                options.WindowInterpreterPath,
                                                options.LibraryPath,
                                                options.PathEnvironmentVariableName,
                                                options.Architecture,
                                                options.LanguageVersion),
                   options.WatchLibraryForNewModules)
        {
            if (baseFactory.Configuration.Version != options.LanguageVersion)
            {
                throw new ArgumentException("Language versions do not match", "options");
            }

            _base = baseFactory;
            _base.IsCurrentChanged     += Base_IsCurrentChanged;
            _base.NewDatabaseAvailable += Base_NewDatabaseAvailable;

            _description = options.Description;

            if (Volatile.Read(ref _deferRefreshIsCurrent))
            {
                // This rare race condition is due to a design flaw that is in
                // shipped public API and cannot be fixed without breaking
                // compatibility with 3rd parties.
                RefreshIsCurrent();
            }
        }
Beispiel #4
0
        public IronPythonInterpreter(PythonInterpreterFactoryWithDatabase factory) {
#if DEBUG
            _id = Interlocked.Increment(ref _interpreterCount);
            Debug.WriteLine(String.Format("IronPython Interpreter Created {0}", _id));
            Debug.WriteLine(new StackTrace(true).ToString());
#endif

            AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.Instance.CurrentDomain_AssemblyResolve;

            InitializeRemoteDomain();

            try {
                LoadAssemblies();
            } catch {
                // IronPython not installed in the GAC...
            }

            var mod = Remote.ImportBuiltinModule("__builtin__");
            var newMod = new IronPythonBuiltinModule(this, mod, "__builtin__");
            _modules[newMod.Name] = newMod;

            _factory = factory;
            _typeDb = _factory.GetCurrentDatabase().CloneWithNewBuiltins(newMod);
            _factory.NewDatabaseAvailable += OnNewDatabaseAvailable;

            LoadModules();
        }
        public bool IsConfigurable(IPythonInterpreterFactory factory)
        {
            PythonInterpreterFactoryWithDatabase fact = factory as PythonInterpreterFactoryWithDatabase;

            if (fact == null)
            {
                return(false);
            }
            return(_interpreters.ContainsValue(fact));
        }
Beispiel #6
0
 private PythonTypeDatabase(
     PythonInterpreterFactoryWithDatabase factory,
     string databaseDirectory,
     bool isDefaultDatabase
 ) {
     _factory = factory;
     _sharedState = new SharedDatabaseState(
         factory.Configuration.Version,
         databaseDirectory,
         defaultDatabase: isDefaultDatabase
     );
 }
Beispiel #7
0
 private PythonTypeDatabase(
     PythonInterpreterFactoryWithDatabase factory,
     string databaseDirectory,
     bool isDefaultDatabase
     )
 {
     _factory     = factory;
     _sharedState = new SharedDatabaseState(
         factory.Configuration.Version,
         databaseDirectory,
         defaultDatabase: isDefaultDatabase
         );
 }
Beispiel #8
0
        public DerivedInterpreterFactory(
            PythonInterpreterFactoryWithDatabase baseFactory,
            InterpreterConfiguration config,
            InterpreterFactoryCreationOptions options
            ) : base(config, options.WatchLibraryForNewModules)
        {
            _base = baseFactory;
            _base.IsCurrentChanged     += Base_IsCurrentChanged;
            _base.NewDatabaseAvailable += Base_NewDatabaseAvailable;

            if (Volatile.Read(ref _deferRefreshIsCurrent))
            {
                // This rare race condition is due to a design flaw that is in
                // shipped public API and cannot be fixed without breaking
                // compatibility with 3rd parties.
                RefreshIsCurrent();
            }
        }
Beispiel #9
0
        public DerivedInterpreterFactory(
            PythonInterpreterFactoryWithDatabase baseFactory,
            InterpreterConfiguration config,
            InterpreterFactoryCreationOptions options
            )
            : base(config, options.WatchLibraryForNewModules)
        {
            _base = baseFactory;
            _base.IsCurrentChanged += Base_IsCurrentChanged;
            _base.NewDatabaseAvailable += Base_NewDatabaseAvailable;

            if (Volatile.Read(ref _deferRefreshIsCurrent)) {
                // This rare race condition is due to a design flaw that is in
                // shipped public API and cannot be fixed without breaking
                // compatibility with 3rd parties.
                RefreshIsCurrent();
            }
        }
Beispiel #10
0
        public PythonTypeDatabase(
            PythonInterpreterFactoryWithDatabase factory,
            IEnumerable<string> databaseDirectories = null,
            PythonTypeDatabase innerDatabase = null
        ) {
            if (innerDatabase != null && factory.Configuration.Version != innerDatabase.LanguageVersion) {
                throw new InvalidOperationException("Language versions do not match");
            }

            _factory = factory;
            if (innerDatabase != null) {
                _sharedState = new SharedDatabaseState(innerDatabase._sharedState);
            } else {
                _sharedState = new SharedDatabaseState(_factory.Configuration.Version);
            }

            if (databaseDirectories != null) {
                foreach (var d in databaseDirectories) {
                    LoadDatabase(d);
                }
            }

            _sharedState.ListenForCorruptDatabase(this);
        }
 public override IPythonInterpreter MakeInterpreter(PythonInterpreterFactoryWithDatabase factory) {
     return new PythonUwpInterpreter(factory);
 }
Beispiel #12
0
 public override IPythonInterpreter MakeInterpreter(PythonInterpreterFactoryWithDatabase factory)
 {
     return(_base.MakeInterpreter(factory));
 }
Beispiel #13
0
 public static PythonTypeDatabase CreateDefaultTypeDatabase(PythonInterpreterFactoryWithDatabase factory) {
     return new PythonTypeDatabase(factory, BaselineDatabasePath, isDefaultDatabase: true);
 }
Beispiel #14
0
 public PythonTypeDatabase CloneWithNewFactory(PythonInterpreterFactoryWithDatabase newFactory)
 {
     return(new PythonTypeDatabase(newFactory, null, this));
 }
 /// <summary>
 /// Returns a new interpreter created with the specified factory.
 /// </summary>
 /// <remarks>
 /// This is intended for use by derived classes only. To get an
 /// interpreter instance, use <see cref="CreateInterpreter"/>.
 /// </remarks>
 public virtual IPythonInterpreter MakeInterpreter(PythonInterpreterFactoryWithDatabase factory)
 {
     return(new CPythonInterpreter(factory));
 }
Beispiel #16
0
 public MockCompletionDB(string path, PythonLanguageVersion version) {
     DBPath = path;
     LanguageVersion = version;
     _factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion(), null, DBPath);
     Directory.CreateDirectory(DBPath);
 }
Beispiel #17
0
        /// <summary>
        /// Call to find interpreters in the associated project. Separated from
        /// the constructor to allow exceptions to be handled without causing
        /// the project node to be invalid.
        /// </summary>
        /// <exception cref="InvalidDataException">
        /// One or more interpreters failed to load. The error message should be
        /// presented to the user, but can otherwise be ignored.
        /// </exception>
        public void DiscoverInterpreters()
        {
            // <Interpreter Include="InterpreterDirectory">
            //   <Id>guid</Id>
            //   <BaseInterpreter>guid</BaseInterpreter>
            //   <Version>...</Version>
            //   <InterpreterPath>...</InterpreterPath>
            //   <WindowsInterpreterPath>...</WindowsInterpreterPath>
            //   <LibraryPath>...</LibraryPath>
            //   <PathEnvironmentVariable>...</PathEnvironmentVariable>
            //   <Description>...</Description>
            // </Interpreter>

            var errors = new StringBuilder();

            errors.AppendLine("Some project interpreters failed to load:");
            bool anyChange = false, anyError = false;

            var projectHome = CommonUtils.GetAbsoluteDirectoryPath(_project.DirectoryPath, _project.GetPropertyValue("ProjectHome"));
            var factories   = new Dictionary <IPythonInterpreterFactory, FactoryInfo>();

            foreach (var item in _project.GetItems(InterpreterItem))
            {
                IPythonInterpreterFactory fact;
                Guid id, baseId;

                // Errors in these options are fatal, so we set anyError and
                // continue with the next entry.
                var dir = item.EvaluatedInclude;
                if (!CommonUtils.IsValidPath(dir))
                {
                    errors.AppendLine(string.Format("Interpreter has invalid path: {0}", dir ?? "(null)"));
                    anyError = true;
                    continue;
                }
                dir = CommonUtils.GetAbsoluteDirectoryPath(projectHome, dir);

                var value = item.GetMetadataValue(IdKey);
                if (string.IsNullOrEmpty(value) || !Guid.TryParse(value, out id))
                {
                    errors.AppendLine(string.Format("Interpreter {0} has invalid value for '{1}': {2}", dir, IdKey, value));
                    anyError = true;
                    continue;
                }
                if (factories.Keys.Any(f => f.Id == id))
                {
                    errors.AppendLine(string.Format("Interpreter {0} has a non-unique id: {1}", dir, id));
                    continue;
                }

                var     verStr = item.GetMetadataValue(VersionKey);
                Version ver;
                if (string.IsNullOrEmpty(verStr) || !Version.TryParse(verStr, out ver))
                {
                    errors.AppendLine(string.Format("Interpreter {0} has invalid value for '{1}': {2}", dir, VersionKey, verStr));
                    anyError = true;
                    continue;
                }

                // The rest of the options are non-fatal. We create an instance
                // of NotFoundError with an amended description, which will
                // allow the user to remove the entry from the project file
                // later.
                bool hasError = false;

                var description = item.GetMetadataValue(DescriptionKey);
                if (string.IsNullOrEmpty(description))
                {
                    description = CommonUtils.CreateFriendlyDirectoryPath(projectHome, dir);
                }

                value = item.GetMetadataValue(BaseInterpreterKey);
                PythonInterpreterFactoryWithDatabase baseInterp = null;
                if (!string.IsNullOrEmpty(value) && Guid.TryParse(value, out baseId))
                {
                    // It's a valid GUID, so find a suitable base. If we
                    // don't find one now, we'll try and figure it out from
                    // the pyvenv.cfg/orig-prefix.txt files later.
                    // Using an empty GUID will always go straight to the
                    // later lookup.
                    if (baseId != Guid.Empty)
                    {
                        baseInterp = _service.FindInterpreter(baseId, ver) as PythonInterpreterFactoryWithDatabase;
                    }
                }

                var path = item.GetMetadataValue(InterpreterPathKey);
                if (!CommonUtils.IsValidPath(path))
                {
                    errors.AppendLine(string.Format("Interpreter {0} has invalid value for '{1}': {2}", dir, InterpreterPathKey, path));
                    hasError = true;
                }
                else if (!hasError)
                {
                    path = CommonUtils.GetAbsoluteFilePath(dir, path);
                }

                var winPath = item.GetMetadataValue(WindowsPathKey);
                if (!CommonUtils.IsValidPath(winPath))
                {
                    errors.AppendLine(string.Format("Interpreter {0} has invalid value for '{1}': {2}", dir, WindowsPathKey, winPath));
                    hasError = true;
                }
                else if (!hasError)
                {
                    winPath = CommonUtils.GetAbsoluteFilePath(dir, winPath);
                }

                var libPath = item.GetMetadataValue(LibraryPathKey);
                if (string.IsNullOrEmpty(libPath))
                {
                    libPath = "lib";
                }
                if (!CommonUtils.IsValidPath(libPath))
                {
                    errors.AppendLine(string.Format("Interpreter {0} has invalid value for '{1}': {2}", dir, LibraryPathKey, libPath));
                    hasError = true;
                }
                else if (!hasError)
                {
                    libPath = CommonUtils.GetAbsoluteDirectoryPath(dir, libPath);
                }

                var pathVar = item.GetMetadataValue(PathEnvVarKey);
                if (string.IsNullOrEmpty(pathVar))
                {
                    if (baseInterp != null)
                    {
                        pathVar = baseInterp.Configuration.PathEnvironmentVariable;
                    }
                    else
                    {
                        pathVar = "PYTHONPATH";
                    }
                }

                string arch = null;
                if (baseInterp == null)
                {
                    arch = item.GetMetadataValue(ArchitectureKey);
                    if (string.IsNullOrEmpty(arch))
                    {
                        arch = "x86";
                    }
                }

                if (baseInterp == null && !hasError)
                {
                    // Only thing missing is the base interpreter, so let's try
                    // to find it using paths
                    baseInterp = DerivedInterpreterFactory.FindBaseInterpreterFromVirtualEnv(dir, libPath, _service) as
                                 PythonInterpreterFactoryWithDatabase;

                    if (baseInterp == null)
                    {
                        errors.AppendLine(string.Format("Interpreter {0} has invalid value for '{1}': {2}", dir, BaseInterpreterKey, value ?? "(null)"));
                        hasError = true;
                    }
                }

                if (hasError)
                {
                    fact = new NotFoundInterpreterFactory(
                        id,
                        ver,
                        string.Format("{0} (unavailable)", description),
                        Directory.Exists(dir) ? dir : null
                        );
                }
                else if (baseInterp != null)
                {
                    MigrateOldDerivedInterpreterFactoryDatabase(id, baseInterp.Configuration.Version, dir);
                    fact = new DerivedInterpreterFactory(
                        baseInterp,
                        new InterpreterFactoryCreationOptions {
                        LanguageVersion = baseInterp.Configuration.Version,
                        Id                          = id,
                        Description                 = description,
                        InterpreterPath             = path,
                        WindowInterpreterPath       = winPath,
                        LibraryPath                 = libPath,
                        PrefixPath                  = dir,
                        PathEnvironmentVariableName = pathVar,
                        Architecture                = baseInterp.Configuration.Architecture,
                        WatchLibraryForNewModules   = true
                    }
                        );
                }
                else
                {
                    fact = InterpreterFactoryCreator.CreateInterpreterFactory(new InterpreterFactoryCreationOptions {
                        LanguageVersion = ver,
                        Id                          = id,
                        Description                 = description,
                        InterpreterPath             = path,
                        WindowInterpreterPath       = winPath,
                        LibraryPath                 = libPath,
                        PrefixPath                  = dir,
                        PathEnvironmentVariableName = pathVar,
                        ArchitectureString          = arch,
                        WatchLibraryForNewModules   = true
                    });
                }
                var existing = FindInterpreter(id, ver);
                if (existing != null && existing.IsEqual(fact))
                {
                    factories[existing] = new FactoryInfo(item, factories[existing].Owned);
                    var disposable = fact as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
                else
                {
                    _rootPaths[id]  = dir;
                    factories[fact] = new FactoryInfo(item, true);
                    anyChange       = true;
                }
            }

            // <InterpreterReference Include="{guid};{version}" />
            foreach (var item in _project.GetItems(InterpreterReferenceItem))
            {
                var match = InterpreterReferencePath.Match(item.EvaluatedInclude);
                if (match == null || !match.Success || !match.Groups.Cast <Group>().All(g => g.Success))
                {
                    errors.AppendLine(string.Format("Interpreter reference has invalid path: {0}", item.EvaluatedInclude));
                    anyError = true;
                    continue;
                }
                Guid id;
                var  value = match.Groups["id"];
                if (string.IsNullOrEmpty(value.Value) || !Guid.TryParse(value.Value, out id))
                {
                    errors.AppendLine(string.Format("Interpreter reference has invalid id: {0}", value.Value ?? "(null)"));
                    anyError = true;
                    continue;
                }
                Version ver;
                value = match.Groups["version"];
                if (string.IsNullOrEmpty(value.Value) || !Version.TryParse(value.Value, out ver))
                {
                    errors.AppendLine(string.Format("Interpreter reference has invalid version: {0}", value.Value ?? "(null)"));
                    anyError = true;
                    continue;
                }

                bool owned = false;
                var  fact  = _service.FindInterpreter(id, ver);
                if (fact == null)
                {
                    owned = true;
                    fact  = new NotFoundInterpreterFactory(id, ver);
                }

                var existing = FindInterpreter(id, ver);
                if (existing != null)
                {
                    factories[existing] = new FactoryInfo(item, factories[existing].Owned);
                    if (owned)
                    {
                        ((PythonInterpreterFactoryWithDatabase)fact).Dispose();
                    }
                }
                else
                {
                    factories[fact] = new FactoryInfo(item, owned);
                    anyChange       = true;
                }
            }

            if (anyChange || _factories == null || factories.Count != _factories.Count)
            {
                // Lock here mainly to ensure that any searches complete before
                // we trigger the changed event.
                lock (_factoriesLock) {
                    _factories = factories;
                }
                OnInterpreterFactoriesChanged();
                UpdateActiveInterpreter();
            }

            if (anyError)
            {
                throw new InvalidDataException(errors.ToString());
            }
        }
Beispiel #18
0
 public static PythonTypeDatabase CreateDefaultTypeDatabase(PythonInterpreterFactoryWithDatabase factory)
 {
     return(new PythonTypeDatabase(factory, BaselineDatabasePath, isDefaultDatabase: true));
 }
 /// <summary>
 /// Returns a new interpreter created with the specified factory.
 /// </summary>
 /// <remarks>
 /// This is intended for use by derived classes only. To get an
 /// interpreter instance, use <see cref="CreateInterpreter"/>.
 /// </remarks>
 public virtual IPythonInterpreter MakeInterpreter(PythonInterpreterFactoryWithDatabase factory) {
     return new CPythonInterpreter(factory);
 }
        private CanopyInterpreterFactory(
            Guid id,
            string description,
            PythonInterpreterFactoryWithDatabase baseFactory,
            InterpreterConfiguration config
        )
            : base(id, description, config, true) {
            if (baseFactory == null) {
                throw new ArgumentNullException("baseFactory");
            }

            _base = baseFactory;
            _base.IsCurrentChanged += OnIsCurrentChanged;
            _base.NewDatabaseAvailable += OnNewDatabaseAvailable;
        }
Beispiel #21
0
        public void Dispose() {
            _typeDb = null;

            var factory = _factory;
            _factory = null;
            if (factory != null) {
                factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
            }
        }
Beispiel #22
0
 public PythonUwpInterpreter(PythonInterpreterFactoryWithDatabase factory) {
     _langVersion = factory.Configuration.Version;
     _factory = factory;
     _typeDb = _factory.GetCurrentDatabase();
     _factory.NewDatabaseAvailable += OnNewDatabaseAvailable;
 }
Beispiel #23
0
 public PythonTypeDatabase CloneWithNewFactory(PythonInterpreterFactoryWithDatabase newFactory) {
     return new PythonTypeDatabase(newFactory, null, this);
 }
        /// <summary>
        /// Creates the Canopy User interpreter. This handles layering of the
        /// User database on top of the App database, and ensures that refreshes
        /// work correctly.
        /// 
        /// Because it is exposed as its own factory type, it can also be used
        /// as a base interpreter for DerivedInterpreterFactory (virtual
        /// environments).
        /// </summary>
        public static CanopyInterpreterFactory Create(
            PythonInterpreterFactoryWithDatabase baseFactory,
            string userPath,
            string canopyVersion
        ) {
            var interpPath = FindFile(userPath, CanopyInterpreterFactoryConstants.ConsoleExecutable);
            var winInterpPath = FindFile(userPath, CanopyInterpreterFactoryConstants.WindowsExecutable);
            var libPath = Path.Combine(userPath, CanopyInterpreterFactoryConstants.LibrarySubPath);

            if (!File.Exists(interpPath)) {
                throw new FileNotFoundException(interpPath);
            }
            if (!File.Exists(winInterpPath)) {
                throw new FileNotFoundException(winInterpPath);
            }
            if (!Directory.Exists(libPath)) {
                throw new DirectoryNotFoundException(libPath);
            }

            var id = (baseFactory.Configuration.Architecture == ProcessorArchitecture.Amd64) ?
                CanopyInterpreterFactoryConstants.UserGuid64 :
                CanopyInterpreterFactoryConstants.UserGuid32;

            // Make the description string look like "Canopy 1.1.0.46 (2.7 32-bit)"
            var description = "Canopy ";
            if (!string.IsNullOrEmpty(canopyVersion)) {
                description += " " + canopyVersion;
            }
            description += string.Format(" ({0} ", baseFactory.Configuration.Version);
            if (baseFactory.Configuration.Architecture == ProcessorArchitecture.Amd64) {
                description += " 64-bit)";
            } else {
                description += " 32-bit)";
            }

            var config = new InterpreterConfiguration(
                userPath,
                interpPath,
                winInterpPath,
                libPath,
                CanopyInterpreterFactoryConstants.PathEnvironmentVariableName,
                baseFactory.Configuration.Architecture,
                baseFactory.Configuration.Version
            );

            return new CanopyInterpreterFactory(id, description, baseFactory, config);
        }