From b649f11016b59f0e31761d10fab877828dd53af7 Mon Sep 17 00:00:00 2001 From: Thomas Sibley Date: Wed, 14 Aug 2024 17:25:40 -0700 Subject: [PATCH] Always line-buffer stderr for consistency across Python versions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The change in default from block-buffering in ≤3.8 to line-buffering in ≥3.9¹ made a Cram test output vary between Python versions (and thus fail). I could fix the Cram test various ways, but always line-buffering stderr makes sense because we're exclusively using it for messaging/logging. ¹ --- CHANGES.md | 1 + augur/__main__.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 8ae00afea..bb1bb8d5d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ ### Bug Fixes * Embedded newlines in quoted field values of metadata files read/written by many commands, annotation files read by `augur curate apply-record-annotations`, and index files written by `augur index` are now properly handled. [#1561][] [#1564][] (@tsibley) +* Output written to stderr (e.g. informational messages, warnings, errors, etc.) is now always line-buffered regardless of the Python version in use. This helps with interleaved stderr and stdout. Previously, stderr was block-buffered on Python 3.8 and line-buffered on 3.9 and higher. [#1563][] (@tsibley) [#1561]: https://github.com/nextstrain/augur/pull/1561 [#1562]: https://github.com/nextstrain/augur/pull/1562 diff --git a/augur/__main__.py b/augur/__main__.py index d69eae231..59f89dd8f 100644 --- a/augur/__main__.py +++ b/augur/__main__.py @@ -14,11 +14,21 @@ def main(): # Explicitly enable universal newlines mode so we do the right thing. newline=None, + + # By default, stdout is line-buffered when interactive (e.g. for + # consistent stdio interleaving) but block-buffered when not for (I + # assume) better performance. ) # Apply the above to stderr as well. sys.stderr.reconfigure( errors="backslashreplace", newline=None, + + # Always line-buffer stderr since we only use it for messaging, not + # data output. This is the Python default from 3.9 onwards, but we + # also run on 3.8 where it's not. Be consistent regardless of Python + # version. + line_buffering=True, ) return augur.run( argv[1:] )