Example #1
0
        public IPythonModule ImportModule(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }

            // Handle builtins explicitly
            if (name == BuiltinModuleName)
            {
                if (_builtinModule == null)
                {
                    _modules[BuiltinModuleName] = _builtinModule = new AstBuiltinsPythonModule(_factory.LanguageVersion);
                    _builtinModule.Imported(this);
                    var bmn = ((AstBuiltinsPythonModule)_builtinModule).GetAnyMember("__builtin_module_names") as AstPythonStringLiteral;
                    _builtinModuleNames = bmn?.Value?.Split(',');
                }
                return(_builtinModule);
            }

            IPythonModule mod;

            // Return any existing module
            if (_modules.TryGetValue(name, out mod) && mod != null)
            {
                if (mod is EmptyModule)
                {
                    Trace.TraceWarning($"Recursively importing {name}");
                }
                return(mod);
            }

            // Set up a sentinel so we can detect recursive imports
            var sentinalValue = new EmptyModule();

            if (!_modules.TryAdd(name, sentinalValue))
            {
                return(_modules[name]);
            }

            // Do normal searches
            mod = ImportFromBuiltins(name) ?? ImportFromSearchPaths(name);

            // Replace our sentinel, or if we raced, get the current
            // value and abandon the one we just created.
            if (!_modules.TryUpdate(name, mod, sentinalValue))
            {
                mod = _modules[name];
            }

            return(mod);
        }
Example #2
0
        private IPythonModule ImportModuleOrRetry(string name, out bool retry)
        {
            retry = false;
            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }

            Debug.Assert(!name.EndsWith("."), $"{name} should not end with '.'");

            // Handle builtins explicitly
            if (name == BuiltinModuleName)
            {
                if (_builtinModule == null)
                {
                    _modules[BuiltinModuleName] = _builtinModule = new AstBuiltinsPythonModule(_factory.LanguageVersion);
                    _builtinModuleNames         = null;
                    _builtinModule.Imported(this);
                    var bmn = ((AstBuiltinsPythonModule)_builtinModule).GetAnyMember("__builtin_module_names") as AstPythonStringLiteral;
                    _builtinModuleNames = bmn?.Value?.Split(',') ?? Array.Empty <string>();
                }
                return(_builtinModule);
            }

            IPythonModule mod;

            // Return any existing module
            if (_modules.TryGetValue(name, out mod) && mod != null)
            {
                if (mod is SentinelModule smod)
                {
                    // If we are importing this module on another thread, allow
                    // time for it to complete. This does not block if we are
                    // importing on the current thread or the module is not
                    // really being imported.
                    var newMod = smod.WaitForImport(5000);
                    if (newMod is SentinelModule)
                    {
                        _log?.Log(TraceLevel.Warning, "RecursiveImport", name);
                        mod = newMod;
                    }
                    else if (newMod == null)
                    {
                        _log?.Log(TraceLevel.Warning, "ImportTimeout", name);
                    }
                    else
                    {
                        mod = newMod;
                    }
                }
                return(mod);
            }

            // Set up a sentinel so we can detect recursive imports
            var sentinalValue = new SentinelModule(name, true);

            if (!_modules.TryAdd(name, sentinalValue))
            {
                // Try to get the new module, in case we raced with a .Clear()
                if (_modules.TryGetValue(name, out mod) && !(mod is SentinelModule))
                {
                    return(mod);
                }
                // If we reach here, the race is too complicated to recover
                // from. Signal the caller to try importing again.
                _log?.Log(TraceLevel.Warning, "RetryImport", name);
                retry = true;
                return(null);
            }

            // Do normal searches
            mod = ImportFromBuiltins(name) ?? ImportFromSearchPaths(name);

            // Replace our sentinel, or if we raced, get the current
            // value and abandon the one we just created.
            if (!_modules.TryUpdate(name, mod, sentinalValue))
            {
                // Try to get the new module, in case we raced
                if (_modules.TryGetValue(name, out mod) && !(mod is SentinelModule))
                {
                    return(mod);
                }
                // If we reach here, the race is too complicated to recover
                // from. Signal the caller to try importing again.
                _log?.Log(TraceLevel.Warning, "RetryImport", name);
                retry = true;
                return(null);
            }

            sentinalValue.Complete(mod);
            sentinalValue.Dispose();
            return(mod);
        }