Skip to content

Commit 225b1d4

Browse files
committed
Improve Python stdio handling and logging on Darwin
Clarifies in the README that Python stdout/stderr is forwarded via NSLog for better visibility. In the Swift plugin, ensures Python stdio is line-buffered for prompt log output and uses NSLog in addition to os_log for maximum log visibility in Xcode and log stream.
1 parent 2bdc7be commit 225b1d4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/serious_python/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ dart run serious_python:main package app/src -p Darwin --verbose
183183

184184
### iOS/macOS Python output
185185

186-
On Darwin (iOS/macOS), Python `stdout`/`stderr` is forwarded into Apple’s unified logging, so it’s visible in `flutter run` and Xcode logs.
186+
On Darwin (iOS/macOS), Python `stdout`/`stderr` is forwarded into Apple’s unified logging (via `NSLog`), so it’s visible in `flutter run` and Xcode logs.
187187

188188
To disable forwarding, set `SERIOUS_PYTHON_FORWARD_STDIO=0` in `environmentVariables`.
189189

src/serious_python_darwin/darwin/Classes/SeriousPythonPlugin.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public class SeriousPythonPlugin: NSObject, FlutterPlugin {
116116
@objc func runPythonFile(appPath: String) {
117117
SeriousPythonLogForwarder.shared.beginCapturing()
118118
Py_Initialize()
119+
_configurePythonStdioLineBuffering()
119120

120121
// run app
121122
let file = fopen(appPath, "r")
@@ -131,6 +132,7 @@ public class SeriousPythonPlugin: NSObject, FlutterPlugin {
131132
@objc func runPythonScript(script: String) {
132133
SeriousPythonLogForwarder.shared.beginCapturing()
133134
Py_Initialize()
135+
_configurePythonStdioLineBuffering()
134136

135137
// run app
136138
let result = PyRun_SimpleString(script)
@@ -141,6 +143,23 @@ public class SeriousPythonPlugin: NSObject, FlutterPlugin {
141143
Py_Finalize()
142144
SeriousPythonLogForwarder.shared.endCapturing()
143145
}
146+
147+
private func _configurePythonStdioLineBuffering() {
148+
// Best-effort: make print()/logging visible promptly when stdout/stderr is a pipe.
149+
// Safe to ignore failures across Python versions/configs.
150+
let code = """
151+
import sys
152+
try:
153+
sys.stdout.reconfigure(line_buffering=True)
154+
except Exception:
155+
pass
156+
try:
157+
sys.stderr.reconfigure(line_buffering=True)
158+
except Exception:
159+
pass
160+
"""
161+
_ = PyRun_SimpleString(code)
162+
}
144163
}
145164

146165
private final class SeriousPythonLogForwarder {
@@ -277,6 +296,8 @@ private final class SeriousPythonLogForwarder {
277296
private func _emit(_ message: String) {
278297
let trimmed = message.trimmingCharacters(in: .newlines)
279298
guard !trimmed.isEmpty else { return }
299+
// Use NSLog for maximum visibility in Xcode + `log stream` by process.
300+
NSLog("%@", trimmed)
280301
os_log("%{public}@", log: log, type: .default, trimmed)
281302
}
282303
}

0 commit comments

Comments
 (0)