Skip to content

Commit 958e8eb

Browse files
use existing APIs to call Python
1 parent f163a58 commit 958e8eb

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,14 @@ public struct Driver {
865865
if case .subcommand = try Self.invocationRunMode(forArgs: args).mode {
866866
throw Error.subcommandPassedToDriver
867867
}
868+
869+
#if os(Windows)
870+
if args[1] == "-repl" { // a `-` gets automatically added to `swift repl`.
871+
try Driver.checkIfMatchingPythonArch(
872+
executor: executor, env: env, diagnosticsEngine: diagnosticsEngine)
873+
}
874+
#endif
875+
868876
var args = args
869877
if let additional = env["ADDITIONAL_SWIFT_DRIVER_FLAGS"] {
870878
args.append(contentsOf: additional.components(separatedBy: " "))
@@ -1426,9 +1434,6 @@ extension Driver {
14261434
if firstArg == "repl" {
14271435
updatedArgs.remove(at: 1)
14281436
updatedArgs.append("-repl")
1429-
#if os(Windows)
1430-
checkIfMatchingPythonArch()
1431-
#endif
14321437
return (.normal(isRepl: true), updatedArgs)
14331438
}
14341439

@@ -1446,40 +1451,33 @@ extension Driver {
14461451
/// install an x86 version of Python, they will get acryptic error message
14471452
/// when running lldb (`0xC000007B`). Calling this function before invoking
14481453
/// lldb gives them a warning to help troublshoot the issue.
1449-
private static func checkIfMatchingPythonArch() {
1454+
public static func checkIfMatchingPythonArch(
1455+
executor: DriverExecutor, env: [String: String], diagnosticsEngine: DiagnosticsEngine
1456+
) throws {
14501457
#if arch(arm64)
1451-
let arch = "arm64"
1458+
let toolchainArchitecture = "arm64"
14521459
#elseif arch(x86_64)
1453-
let arch = "amd64"
1460+
let toolchainArchitecture = "amd64"
14541461
#elseif arch(x86)
1455-
let arch = "x86"
1462+
let toolchainArchitecture = "x86"
14561463
#else
1457-
return;
1464+
return;
14581465
#endif
1459-
let process = Process()
1460-
process.executableURL = URL(fileURLWithPath: "C:\\Windows\\System32\\cmd.exe")
1461-
process.arguments = ["/c", "python.exe", "-c", "import platform; print(platform.machine())"]
1462-
1463-
let pipe = Pipe()
1464-
process.standardOutput = pipe
1465-
do {
1466-
try process.run()
1467-
process.waitUntilExit()
1468-
1469-
let data = pipe.fileHandleForReading.readDataToEndOfFile()
1470-
guard let output = String(data: data, encoding: .utf8) else {
1471-
return;
1472-
}
1473-
if !output.lowercased().contains(arch) {
1474-
stderrStream.send("There is an architecture mismatch between the installed toolchain and the resolved Python's architecture:\n")
1475-
stderrStream.send("Toolchain: \(arch)\n")
1476-
stderrStream.send("Python: \(output)\n")
1477-
}
1478-
} catch {
1479-
stderrStream.flush()
1480-
return
1466+
let pythonArchitecture = try executor.checkNonZeroExit(
1467+
args: "python.exe", "-c", "import platform; print(platform.machine())",
1468+
environment: env
1469+
).trimmingCharacters(in: .whitespacesAndNewlines)
1470+
.lowercased()
1471+
1472+
if !pythonArchitecture.contains(toolchainArchitecture) {
1473+
diagnosticsEngine.emit(
1474+
.warning(
1475+
"""
1476+
There is an architecture mismatch between the installed toolchain and the resolved Python's architecture:
1477+
Toolchain: \(toolchainArchitecture)
1478+
Python: \(pythonArchitecture)
1479+
"""))
14811480
}
1482-
stderrStream.flush()
14831481
}
14841482
}
14851483

0 commit comments

Comments
 (0)