Download SkillOpt-official/skillopt/utils/console.py from SitongGong/gst-code-collection: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/datasets/SitongGong/gst-code-collection/resolve/main/SkillOpt-official/skillopt/utils/console.py
- Command line
-
hf download hf://datasets/SitongGong/gst-code-collection/SkillOpt-official/skillopt/utils/console.py
-
curl -L -o console.py https://huggingface.co/datasets/SitongGong/gst-code-collection/resolve/main/SkillOpt-official/skillopt/utils/console.py
1.1 kB
| """Console encoding helpers shared by the entry-point scripts.""" | |
| from __future__ import annotations | |
| import sys | |
| from typing import Any | |
| def force_utf8_stdout_stderr() -> None: | |
| """Best-effort switch of ``stdout``/``stderr`` to UTF-8. | |
| Progress output contains box-drawing and arrow characters. On a Windows | |
| console defaulting to cp1252 those raise ``UnicodeEncodeError`` mid-run, | |
| which kills a loop after real work has already been done; a cp1252 file | |
| redirection crashes identically, so this is deliberately not gated on | |
| ``isatty()``. It is a no-op for streams that are already UTF-8 or that | |
| expose an incompatible ``reconfigure()``. | |
| """ | |
| for stream in (sys.stdout, sys.stderr): | |
| reconfigure: Any = getattr(stream, "reconfigure", None) | |
| if reconfigure is None: | |
| continue | |
| if (getattr(stream, "encoding", "") or "").lower().replace("-", "").replace("_", "") == "utf8": | |
| continue | |
| try: | |
| reconfigure(encoding="utf-8", errors="replace") | |
| except (ValueError, OSError, TypeError): | |
| pass | |