Skip to content

Commit

Permalink
Use _run_module_as_main instead of run_module (IronLanguages#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
slozier authored Aug 8, 2023
1 parent 10d30d5 commit a1f06f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ dotnet_diagnostic.CA1845.severity = none # CA1845: Use span-based 'string
dotnet_diagnostic.CA1846.severity = none # CA1846: Prefer 'AsSpan' over 'Substring'
dotnet_diagnostic.CA1847.severity = none # CA1847: Use char literal for a single character lookup
dotnet_diagnostic.CA1852.severity = suggestion # CA1852: Seal internal types
dotnet_diagnostic.CA1859.severity = suggestion # CA1859: Use concrete types when possible for improved performance
dotnet_diagnostic.CA2101.severity = suggestion # CA2101: Specify marshaling for P/Invoke string arguments
dotnet_diagnostic.CA2201.severity = none # CA2201: Do not raise reserved exception types
dotnet_diagnostic.CA2208.severity = suggestion # CA2208: Instantiate argument exceptions correctly
Expand Down
20 changes: 14 additions & 6 deletions Src/IronPython/Hosting/PythonCommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,29 @@ protected override int Run() {
return -1;
}

// get the run_module method
// get the _run_module_as_main method
try {
runMod = PythonOps.GetBoundAttr(PythonContext.SharedContext, runpy, "run_module");
runMod = PythonOps.GetBoundAttr(PythonContext.SharedContext, runpy, "_run_module_as_main");
} catch (Exception) {
Console.WriteLine("Could not access runpy.run_module", Style.Error);
Console.WriteLine("Could not access runpy._run_module_as_main", Style.Error);
return -1;
}

if (Scope == null) {
Scope = CreateScope();
}

var argv = PythonContext.GetSystemStateValue("argv") as PythonList;
if (argv is not null) {
argv[0] = "-m";
}

// call it with the name of the module to run
try {
PythonCalls.CallWithKeywordArgs(
PythonCalls.Call(
PythonContext.SharedContext,
runMod,
new object[] { Options.ModuleToRun, "__main__", ScriptingRuntimeHelpers.True },
new string[] { "run_name", "alter_sys" }
Options.ModuleToRun
);
} catch (SystemExitException e) {
return GetEffectiveExitCode(e);
Expand Down

0 comments on commit a1f06f9

Please sign in to comment.