Validate that the preferred output encoding is valid if it's set. We do this separately from set_preferred_output_encoding because that may be called from error handlers while handling another exception, which we still want to print successfully.
()
| 136 | |
| 137 | |
| 138 | def validate_preferred_output_encoding(): |
| 139 | """ |
| 140 | Validate that the preferred output encoding is valid if it's set. |
| 141 | We do this separately from set_preferred_output_encoding because that |
| 142 | may be called from error handlers while handling another exception, which |
| 143 | we still want to print successfully. |
| 144 | """ |
| 145 | if OUTPUT_ENCODING_ENV_VAR in os.environ: |
| 146 | try: |
| 147 | ''.encode(os.environ[OUTPUT_ENCODING_ENV_VAR]) |
| 148 | except LookupError: |
| 149 | raise ValueError( |
| 150 | f'Unknown codec `' |
| 151 | f'{os.environ[OUTPUT_ENCODING_ENV_VAR]}` ' |
| 152 | f'specified for {OUTPUT_ENCODING_ENV_VAR}.' |
| 153 | ) |
| 154 | |
| 155 | |
| 156 | def set_preferred_output_encoding(stream): |