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, default=None)
| 19 | |
| 20 | # to deal with the possibility of sys.std* not being a stream at all |
| 21 | def get_stream_enc(stream, default=None): |
| 22 | """Return the given stream's encoding or a default. |
| 23 | |
| 24 | There are cases where ``sys.std*`` might not actually be a stream, so |
| 25 | check for the encoding attribute prior to returning it, and return |
| 26 | a default if it doesn't exist or evaluates as False. ``default`` |
| 27 | is None if not provided. |
| 28 | """ |
| 29 | if not hasattr(stream, 'encoding') or not stream.encoding: |
| 30 | return default |
| 31 | else: |
| 32 | return stream.encoding |
| 33 | |
| 34 | # Less conservative replacement for sys.getdefaultencoding, that will try |
| 35 | # to match the environment. |
no outgoing calls
no test coverage detected