docs: document sys.stdout flush on exit#6081
Closed
SMC17 wants to merge 1 commit into
Closed
Conversation
Add a section to the "Calling Python from Rust" chapter covering the behaviour reported in PyO3#1490: when PyO3 embeds a Python interpreter, the buffered streams are not flushed at process exit because PyO3 does not call Py_Finalize automatically (see PyO3#1355). The new section enumerates three workarounds (PYTHONUNBUFFERED, explicit sys.stdout.flush, and unsafe Py_Finalize) matching the maintainer's guidance in PyO3#1490. Closes PyO3#1490. Signed-off-by: Sean Collins <sean@sunlitmoon.online> Assisted-by: claude-opus-4-7
fbc9316 to
2fdbf1f
Compare
Author
|
Sorry — been testing some things and this leaked out. Closing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
docs: document sys.stdout flush on exit
Closes #1490.
Summary
Issue #1490 reports that
sys.stdoutandsys.stderrare not flushedwhen a Rust program embedding PyO3 exits, so Python writes without a
trailing newline can be silently dropped.
In 2021 the maintainer redirected the issue from "fix the runtime" to
"document the workaround":
The issue carries the
documentationandneeds-implementerlabelsand has had no follow-up PR.
Change
Adds one section to
guide/src/python-from-rust/calling-existing-code.mdtitled "
sys.stdoutandsys.stderrare not flushed when the Rustprogram exits", placed after "Handling system signals/interrupts
(Ctrl-C)" and before the link-reference block.
The section:
PyO3 does not run
Py_Finalizeautomatically.PYTHONUNBUFFERED=1, explicitsys.stdout.flush()/sys.stderr.flush()from Python, and an
unsafe { pyo3::ffi::Py_Finalize() }call inmain, with aSAFETY:note on the lifetime constraint.Initialization, Finalization, and Threads.
The explicit-flush code block uses the current
Python::attachAPI andfollows the
# fn main() -> PyResult<()> {hidden-line pattern alreadyused elsewhere in the file. The
Py_Finalizeblock is markedrust,no_runbecause finalising the interpreter inside a doctest wouldinteract badly with the rest of the test suite.
Verification
cargo doc --no-deps -p pyo3completes cleanly on the branch(
pyo3 v0.28.3docs build,Finished dev profile).Contributing.md: "Docs-only PRs do notneed news items; start your PR title with
docs:to skip the check."Out of scope
This PR does not change the runtime behaviour. PR #1355 already
documented the decision to remove automatic finalisation; the present
PR only fills the doc gap that decision left behind.