Streams
π€ AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
provide.foundation.utils.streams
¶
Functions¶
ensure_utf8_stream
¶
Ensure a text stream uses UTF-8 encoding with error replacement.
On Windows, console streams default to legacy encodings (e.g., cp1252) which cannot encode Unicode characters like emoji. This function reconfigures or wraps such streams to use UTF-8 with 'replace' error handling, preventing UnicodeEncodeError in logging paths.
On non-Windows platforms or for streams that already use UTF-8 or have no encoding attribute (e.g., StringIO), the stream is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
TextIO
|
A text stream to ensure UTF-8 encoding on. |
required |
Returns:
| Type | Description |
|---|---|
TextIO
|
The stream, possibly reconfigured or wrapped for UTF-8 safety. |
Source code in provide/foundation/utils/streams.py
get_foundation_log_stream
¶
Get the appropriate stream for Foundation internal logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_setting
|
str
|
One of "stderr", "stdout", or "main" |
required |
Returns:
| Type | Description |
|---|---|
TextIO
|
A writable text stream based on the output setting |
Notes
- "stderr": Returns sys.stderr (default, RPC-safe)
- "stdout": Returns sys.stdout
- "main": Returns the main logger stream from _PROVIDE_LOG_STREAM
- Invalid values default to sys.stderr with warning
Source code in provide/foundation/utils/streams.py
get_safe_stderr
¶
Get a safe stderr stream, falling back to StringIO if stderr is not available.
This is used during initialization when sys.stderr might not be available (e.g., in some embedded Python environments or during testing).
Returns:
| Type | Description |
|---|---|
TextIO
|
A writable text stream, either sys.stderr or io.StringIO() |