diff --git a/Source/DafnyCore/Compilers/Python/PythonBackend.cs b/Source/DafnyCore/Compilers/Python/PythonBackend.cs index 41189692bb..247f858e9a 100644 --- a/Source/DafnyCore/Compilers/Python/PythonBackend.cs +++ b/Source/DafnyCore/Compilers/Python/PythonBackend.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text.RegularExpressions; +using System.Runtime.InteropServices; namespace Microsoft.Dafny.Compilers; @@ -46,6 +47,17 @@ private static string FindModuleName(string externFilename) { return Path.GetExtension(externFilename) == ".py" ? Path.GetFileNameWithoutExtension(externFilename) : null; } + private static readonly Dictionary PlatformDefaults = new() { + {OSPlatform.Linux, "python3"}, + {OSPlatform.Windows, "python"}, + {OSPlatform.FreeBSD, "python3"}, + {OSPlatform.OSX, "python3"}, + }; + private static string DefaultPythonCommand => PlatformDefaults.SingleOrDefault( + kv => RuntimeInformation.IsOSPlatform(kv.Key), + new(OSPlatform.Linux, "python3") + ).Value; + bool CopyExternLibraryIntoPlace(string externFilename, string mainProgram, TextWriter outputWriter) { // Grossly, we need to look in the file to figure out where to put it var moduleName = FindModuleName(externFilename); @@ -88,7 +100,7 @@ public override bool CompileTargetProgram(string dafnyProgramName, string target } } if (!runAfterCompile) { - var psi = PrepareProcessStartInfo("python3"); + var psi = PrepareProcessStartInfo(DefaultPythonCommand); psi.Arguments = $"-m compileall -q {Path.GetDirectoryName(targetFilename)}"; return 0 == RunProcess(psi, outputWriter, outputWriter, "Error while compiling Python files."); } @@ -99,7 +111,7 @@ public override bool RunTargetProgram(string dafnyProgramName, string targetProg string targetFilename, ReadOnlyCollection otherFileNames, object compilationResult, TextWriter outputWriter, TextWriter errorWriter) { Contract.Requires(targetFilename != null || otherFileNames.Count == 0); - var psi = PrepareProcessStartInfo("python3", Options.MainArgs.Prepend(targetFilename)); + var psi = PrepareProcessStartInfo(DefaultPythonCommand, Options.MainArgs.Prepend(targetFilename)); psi.EnvironmentVariables["PYTHONIOENCODING"] = "utf8"; return 0 == RunProcess(psi, outputWriter, errorWriter); }