Skip to content

Commit a1f06f9

Browse files
authored
Use _run_module_as_main instead of run_module (#1720)
1 parent 10d30d5 commit a1f06f9

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ dotnet_diagnostic.CA1845.severity = none # CA1845: Use span-based 'string
8181
dotnet_diagnostic.CA1846.severity = none # CA1846: Prefer 'AsSpan' over 'Substring'
8282
dotnet_diagnostic.CA1847.severity = none # CA1847: Use char literal for a single character lookup
8383
dotnet_diagnostic.CA1852.severity = suggestion # CA1852: Seal internal types
84+
dotnet_diagnostic.CA1859.severity = suggestion # CA1859: Use concrete types when possible for improved performance
8485
dotnet_diagnostic.CA2101.severity = suggestion # CA2101: Specify marshaling for P/Invoke string arguments
8586
dotnet_diagnostic.CA2201.severity = none # CA2201: Do not raise reserved exception types
8687
dotnet_diagnostic.CA2208.severity = suggestion # CA2208: Instantiate argument exceptions correctly

Src/IronPython/Hosting/PythonCommandLine.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,29 @@ protected override int Run() {
8787
return -1;
8888
}
8989

90-
// get the run_module method
90+
// get the _run_module_as_main method
9191
try {
92-
runMod = PythonOps.GetBoundAttr(PythonContext.SharedContext, runpy, "run_module");
92+
runMod = PythonOps.GetBoundAttr(PythonContext.SharedContext, runpy, "_run_module_as_main");
9393
} catch (Exception) {
94-
Console.WriteLine("Could not access runpy.run_module", Style.Error);
94+
Console.WriteLine("Could not access runpy._run_module_as_main", Style.Error);
9595
return -1;
9696
}
9797

98+
if (Scope == null) {
99+
Scope = CreateScope();
100+
}
101+
102+
var argv = PythonContext.GetSystemStateValue("argv") as PythonList;
103+
if (argv is not null) {
104+
argv[0] = "-m";
105+
}
106+
98107
// call it with the name of the module to run
99108
try {
100-
PythonCalls.CallWithKeywordArgs(
109+
PythonCalls.Call(
101110
PythonContext.SharedContext,
102111
runMod,
103-
new object[] { Options.ModuleToRun, "__main__", ScriptingRuntimeHelpers.True },
104-
new string[] { "run_name", "alter_sys" }
112+
Options.ModuleToRun
105113
);
106114
} catch (SystemExitException e) {
107115
return GetEffectiveExitCode(e);

0 commit comments

Comments
 (0)