-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Local usage uses unbuffered. CI usage uses buffered. Local usage of PYTHONUNBUFFERED=1 uses buffered Detection is not working
- Loading branch information
Showing
5 changed files
with
115 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""IO related functions and classes.""" | ||
import os | ||
import sys | ||
|
||
# Any use of is_stdout_buffered is temporary, because -u is not reflected | ||
# anywhere in the python sys module, such as sys.flags. | ||
# It is undesirable as written, as it has side-effects, especially bad | ||
# side-effects as using a stream usually causes parts of the stream | ||
# state to get baked-in, preventing reconfiguration on Python 3.7. | ||
# Also temporary because of incompatible licensing. | ||
|
||
|
||
def is_stdout_buffered(): | ||
"""Check if stdout is buffered. | ||
Copied from https://stackoverflow.com/a/49736559 | ||
Licensed CC-BY-SA 4.0 | ||
Author https://stackoverflow.com/users/528711/sparrowt | ||
with a fix of the print for Python 3 compatibility | ||
""" | ||
# Print a single space + carriage return but no new-line | ||
# (should have no visible effect) | ||
print(" \r") | ||
# If the file position is a positive integer then stdout is buffered | ||
try: | ||
pos = sys.stdout.tell() | ||
if pos > 0: | ||
return True | ||
except IOError: # In some terminals tell() throws IOError if stdout is unbuffered | ||
pass | ||
return False | ||
|
||
|
||
def is_stdio_unbufferedio(): | ||
"""Detect if PYTHONUNBUFFERED was set or CI was set as proxy for -u.""" | ||
# It should be possible to detect unbuffered by the *initial* state of | ||
# sys.stdout and its buffers. | ||
# Also need to take into account PYTHONLEGACYWINDOWSSTDIO | ||
# And all this is quite unhelpful because the state of sys.stdio | ||
# may be very different by the time that a StdioManager is instantiated | ||
if os.environ.get("PYTHONUNBUFFERED"): | ||
return True | ||
|
||
try: | ||
sys.stdout.buffer.raw | ||
except AttributeError: | ||
return True | ||
|
||
return False |
This file contains 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
This file contains 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
This file contains 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