Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/core/IronPython/Hosting/Python.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static class Python {
/// Given a ScriptRuntime gets the ScriptEngine for IronPython.
/// </summary>
public static ScriptEngine/*!*/ GetEngine(ScriptRuntime/*!*/ runtime) {
return runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);
return runtime.GetEngineByTypeName(GetPythonAssemblyName());
}

/// <summary>
Expand Down Expand Up @@ -302,7 +302,7 @@ value is bool &&
/// </summary>
public static LanguageSetup/*!*/ CreateLanguageSetup(IDictionary<string, object>? options) {
var setup = new LanguageSetup(
typeof(PythonContext).AssemblyQualifiedName,
GetPythonAssemblyName(),
PythonContext.IronPythonDisplayName,
PythonContext.IronPythonNames.Split(';'),
PythonContext.IronPythonFileExtensions.Split(';')
Expand Down Expand Up @@ -358,13 +358,18 @@ public static string[] GetModuleFilenames(this ScriptEngine engine) {
#region Private helpers

private static PythonService/*!*/ GetPythonService(ScriptEngine/*!*/ engine) {
return engine.GetService<PythonService>(engine);
return engine.GetService<PythonService>(engine)!; // not null since PythonContext.GetService<PythonService> 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
}
}
15 changes: 7 additions & 8 deletions src/core/IronPython/Hosting/PythonCommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/core/IronPython/Hosting/PythonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public PythonService(PythonContext/*!*/ context, ScriptEngine/*!*/ engine) {

public string[] GetModuleFilenames() {
List<string> res = new List<string>();
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__();
Expand Down Expand Up @@ -130,7 +130,7 @@ public ObjectHandle GetLocalCommandDispatcher() {
return new ObjectHandle((Action<Action>)(action => _context.DispatchCommand(action)));
}

public override object InitializeLifetimeService() {
public override object? InitializeLifetimeService() {
return _engine.InitializeLifetimeService();
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Runtime/CompiledLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/core/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -4335,32 +4335,32 @@ 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);
}

return PythonOps.GetBoundAttr(context, scope, name);
}

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);
}

return PythonOps.TryGetBoundAttr(context, scope, name, out value);
}

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);
}

return PythonOps.HasAttr(context, scope, name);
}

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);
}

Expand All @@ -4370,7 +4370,7 @@ internal static bool ScopeDeleteMember(CodeContext/*!*/ context, Scope scope, st
}

internal static IList<object?> ScopeGetMemberNames(CodeContext/*!*/ context, Scope scope) {
if (((object)scope.Storage) is ScopeStorage scopeStorage) {
if (scope.Storage is ScopeStorage scopeStorage) {
List<object?> res = new List<object?>();

foreach (string name in scopeStorage.GetMemberNames()) {
Expand Down
2 changes: 1 addition & 1 deletion src/dlr
Submodule dlr updated 254 files