Skip to content

Commit

Permalink
Added OS defaults for Python executable (#4934)
Browse files Browse the repository at this point in the history
### Description
Fixes #4922 
Adds default python executable name by detected OS.

### How has this been tested?
Not tested

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
  • Loading branch information
alexporter8013 authored Jan 4, 2024
1 parent 1acddf7 commit 880ba3f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Source/DafnyCore/Compilers/Python/PythonBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace Microsoft.Dafny.Compilers;

Expand Down Expand Up @@ -46,6 +47,17 @@ private static string FindModuleName(string externFilename) {
return Path.GetExtension(externFilename) == ".py" ? Path.GetFileNameWithoutExtension(externFilename) : null;
}

private static readonly Dictionary<OSPlatform, string> 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);
Expand Down Expand Up @@ -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.");
}
Expand All @@ -99,7 +111,7 @@ public override bool RunTargetProgram(string dafnyProgramName, string targetProg
string targetFilename, ReadOnlyCollection<string> 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);
}
Expand Down

0 comments on commit 880ba3f

Please sign in to comment.