Return the given stream's encoding or a default. There are cases where ``sys.std*`` might not actually be a stream, so check for the encoding attribute prior to returning it, and return a default if it doesn't exist or evaluates as False. ``default`` is None if not provided.
(stream: Any, default: str | None = None)
| 23 | |
| 24 | # to deal with the possibility of sys.std* not being a stream at all |
| 25 | def get_stream_enc(stream: Any, default: str | None = None) -> str | None: |
| 26 | """Return the given stream's encoding or a default. |
| 27 | |
| 28 | There are cases where ``sys.std*`` might not actually be a stream, so |
| 29 | check for the encoding attribute prior to returning it, and return |
| 30 | a default if it doesn't exist or evaluates as False. ``default`` |
| 31 | is None if not provided. |
| 32 | """ |
| 33 | if not hasattr(stream, "encoding") or not stream.encoding: |
| 34 | return default |
| 35 | else: |
| 36 | return stream.encoding |
| 37 | |
| 38 | |
| 39 | _sentinel: object = object() |
no outgoing calls
no test coverage detected
searching dependent graphs…