diff --git a/src/core/IronPython/Hosting/Python.cs b/src/core/IronPython/Hosting/Python.cs index e91f3d622..8367450dd 100644 --- a/src/core/IronPython/Hosting/Python.cs +++ b/src/core/IronPython/Hosting/Python.cs @@ -115,7 +115,7 @@ public static class Python { /// Given a ScriptRuntime gets the ScriptEngine for IronPython. /// public static ScriptEngine/*!*/ GetEngine(ScriptRuntime/*!*/ runtime) { - return runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName); + return runtime.GetEngineByTypeName(GetPythonAssemblyName()); } /// @@ -302,7 +302,7 @@ value is bool && /// public static LanguageSetup/*!*/ CreateLanguageSetup(IDictionary? options) { var setup = new LanguageSetup( - typeof(PythonContext).AssemblyQualifiedName, + GetPythonAssemblyName(), PythonContext.IronPythonDisplayName, PythonContext.IronPythonNames.Split(';'), PythonContext.IronPythonFileExtensions.Split(';') @@ -358,13 +358,18 @@ public static string[] GetModuleFilenames(this ScriptEngine engine) { #region Private helpers private static PythonService/*!*/ GetPythonService(ScriptEngine/*!*/ engine) { - return engine.GetService(engine); + return engine.GetService(engine)!; // not null since PythonContext.GetService override will create/return the service if it doesn't exist } private static PythonContext/*!*/ GetPythonContext(ScriptEngine/*!*/ engine) { return (PythonContext)HostingHelpers.GetLanguageContext(engine); } + private static string GetPythonAssemblyName() { + return typeof(PythonContext).AssemblyQualifiedName + ?? throw new InvalidOperationException($"Could not get assembly qualified name for {nameof(PythonContext)}."); + } + #endregion } } diff --git a/src/core/IronPython/Hosting/PythonCommandLine.cs b/src/core/IronPython/Hosting/PythonCommandLine.cs index 1c4b4c453..bc9bb0483 100644 --- a/src/core/IronPython/Hosting/PythonCommandLine.cs +++ b/src/core/IronPython/Hosting/PythonCommandLine.cs @@ -433,9 +433,8 @@ protected override int RunInteractive() { protected override string Prompt { get { - object value; - if (Engine.GetSysModule().TryGetVariable("ps1", out value)) { - var context = ((PythonScopeExtension)Scope.GetExtension(Language.ContextId)).ModuleContext.GlobalContext; + if (Engine.GetSysModule().TryGetVariable("ps1", out object? value)) { + var context = ((PythonScopeExtension)Scope.GetExtension(Language.ContextId)!).ModuleContext.GlobalContext; return PythonOps.ToString(context, value); } @@ -446,9 +445,8 @@ protected override string Prompt { public override string PromptContinuation { get { - object value; - if (Engine.GetSysModule().TryGetVariable("ps2", out value)) { - var context = ((PythonScopeExtension)Scope.GetExtension(Language.ContextId)).ModuleContext.GlobalContext; + if (Engine.GetSysModule().TryGetVariable("ps2", out object? value)) { + var context = ((PythonScopeExtension)Scope.GetExtension(Language.ContextId)!).ModuleContext.GlobalContext; return PythonOps.ToString(context, value); } @@ -544,7 +542,8 @@ private void RunStartup() { Action action = delegate () { try { - su.Compile(pco, ErrorSink).Run(Scope); + var sc = su.Compile(pco, ErrorSink) ?? throw new InvalidOperationException("Compilation failed without reporting errors"); + sc.Run(Scope); } catch (Exception e) { if (e is SystemExitException) { throw; @@ -659,7 +658,7 @@ private int RunFileWorker(string/*!*/ fileName) { PythonModule module = PythonContext.CompileModule( fileName, "__main__", - PythonContext.CreateFileUnit(String.IsNullOrEmpty(fileName) ? null : fileName, PythonContext.DefaultEncoding), + PythonContext.CreateFileUnit(fileName, PythonContext.DefaultEncoding), modOpt, out compiledCode); PythonContext.PublishModule("__main__", module); diff --git a/src/core/IronPython/Hosting/PythonService.cs b/src/core/IronPython/Hosting/PythonService.cs index 4a0d39ccc..260b58107 100644 --- a/src/core/IronPython/Hosting/PythonService.cs +++ b/src/core/IronPython/Hosting/PythonService.cs @@ -94,7 +94,7 @@ public PythonService(PythonContext/*!*/ context, ScriptEngine/*!*/ engine) { public string[] GetModuleFilenames() { List res = new List(); - if ((object)_engine.GetSysModule().GetVariable("modules") is PythonDictionary dict) { + if ((object?)_engine.GetSysModule().GetVariable("modules") is PythonDictionary dict) { foreach (var kvp in dict) { if (kvp.Key is string key && kvp.Value is PythonModule module) { var modDict = module.Get__dict__(); @@ -130,7 +130,7 @@ public ObjectHandle GetLocalCommandDispatcher() { return new ObjectHandle((Action)(action => _context.DispatchCommand(action))); } - public override object InitializeLifetimeService() { + public override object? InitializeLifetimeService() { return _engine.InitializeLifetimeService(); } #endif diff --git a/src/core/IronPython/Runtime/CompiledLoader.cs b/src/core/IronPython/Runtime/CompiledLoader.cs index cf802eff5..d7acaf2cd 100644 --- a/src/core/IronPython/Runtime/CompiledLoader.cs +++ b/src/core/IronPython/Runtime/CompiledLoader.cs @@ -21,7 +21,7 @@ internal void AddScriptCode(ScriptCode code) { if (onDiskCode.ModuleName == "__main__") { _codes["__main__"] = onDiskCode; } else { - string name = code.SourceUnit.Path; + string name = code.SourceUnit.Path ?? throw new InvalidOperationException("OnDiskScriptCode must have a path"); name = name.Replace(Path.DirectorySeparatorChar, '.'); if (name.EndsWith("__init__.py", StringComparison.Ordinal)) { name = name.Substring(0, name.Length - ".__init__.py".Length); diff --git a/src/core/IronPython/Runtime/Operations/PythonOps.cs b/src/core/IronPython/Runtime/Operations/PythonOps.cs index 9a88b06e7..e1e6f9a7b 100644 --- a/src/core/IronPython/Runtime/Operations/PythonOps.cs +++ b/src/core/IronPython/Runtime/Operations/PythonOps.cs @@ -3390,7 +3390,7 @@ public static bool IsPythonType(PythonType type) { public static object? PublishModule(CodeContext/*!*/ context, string name) { context.LanguageContext.SystemStateModules.TryGetValue(name, out object? original); - var module = ((PythonScopeExtension)context.GlobalScope.GetExtension(context.LanguageContext.ContextId)).Module; + var module = ((PythonScopeExtension)context.GlobalScope.GetExtension(context.LanguageContext.ContextId)!).Module; context.LanguageContext.SystemStateModules[name] = module; return original; } @@ -3442,7 +3442,7 @@ public static void Warn(CodeContext/*!*/ context, PythonType category, string me } } - public static void ShowWarning(CodeContext/*!*/ context, PythonType category, string message, string filename, int lineNo) { + public static void ShowWarning(CodeContext/*!*/ context, PythonType category, string message, string? filename, int lineNo) { PythonContext pc = context.LanguageContext; object? warnings = pc.GetWarningsModule(); object? warn = null; @@ -4335,8 +4335,8 @@ internal static void ScopeSetMember(CodeContext/*!*/ context, Scope scope, strin PythonOps.SetAttr(context, scope, name, value); } - internal static object ScopeGetMember(CodeContext/*!*/ context, Scope scope, string name) { - if (((object)scope.Storage) is ScopeStorage scopeStorage) { + internal static object? ScopeGetMember(CodeContext/*!*/ context, Scope scope, string name) { + if (scope.Storage is ScopeStorage scopeStorage) { return scopeStorage.GetValue(name, false); } @@ -4344,7 +4344,7 @@ internal static object ScopeGetMember(CodeContext/*!*/ context, Scope scope, str } internal static bool ScopeTryGetMember(CodeContext/*!*/ context, Scope scope, string name, out object? value) { - if (((object)scope.Storage) is ScopeStorage scopeStorage) { + if (scope.Storage is ScopeStorage scopeStorage) { return scopeStorage.TryGetValue(name, false, out value); } @@ -4352,7 +4352,7 @@ internal static bool ScopeTryGetMember(CodeContext/*!*/ context, Scope scope, st } internal static bool ScopeContainsMember(CodeContext/*!*/ context, Scope scope, string name) { - if (((object)scope.Storage) is ScopeStorage scopeStorage) { + if (scope.Storage is ScopeStorage scopeStorage) { return scopeStorage.HasValue(name, false); } @@ -4360,7 +4360,7 @@ internal static bool ScopeContainsMember(CodeContext/*!*/ context, Scope scope, } internal static bool ScopeDeleteMember(CodeContext/*!*/ context, Scope scope, string name) { - if (((object)scope.Storage) is ScopeStorage scopeStorage) { + if (scope.Storage is ScopeStorage scopeStorage) { return scopeStorage.DeleteValue(name, false); } @@ -4370,7 +4370,7 @@ internal static bool ScopeDeleteMember(CodeContext/*!*/ context, Scope scope, st } internal static IList ScopeGetMemberNames(CodeContext/*!*/ context, Scope scope) { - if (((object)scope.Storage) is ScopeStorage scopeStorage) { + if (scope.Storage is ScopeStorage scopeStorage) { List res = new List(); foreach (string name in scopeStorage.GetMemberNames()) { diff --git a/src/dlr b/src/dlr index a646d7e43..3b7c63816 160000 --- a/src/dlr +++ b/src/dlr @@ -1 +1 @@ -Subproject commit a646d7e43ac94a8ee3de2f8fcfa86a1cc7168563 +Subproject commit 3b7c63816005f842ae5e1156cf522cf42079b1e9