"""Clipshelf core package."""
import os
import sys

__version__ = "0.1.8"
API_VERSION = 1


def _make_output_safe():
    """Make stdout/stderr able to carry the characters this app prints.

    A Windows console defaults to the legacy ANSI code page, which cannot
    encode 📌, ✂, ✓ or →. Printing one raises UnicodeEncodeError — and
    because clip marks log a 📌, that took down the REQUEST, turning a
    cosmetic log line into an HTTP 500 on the app's most-used feature.

    This lives here, in the package, rather than in run.py where it started.
    run.py is one entry point among several: uvicorn can be pointed at
    clipshelf.app directly, which is how the tests run and how anyone
    deploying it would do it. Protection that only covers the front door is
    not protection.
    """
    if os.name == "nt":
        try:
            import ctypes
            ctypes.windll.kernel32.SetConsoleOutputCP(65001)
        except Exception:
            pass
    for stream in (sys.stdout, sys.stderr):
        try:
            stream.reconfigure(encoding="utf-8", errors="replace")
        except Exception:
            pass          # None under pythonw, or already a plain file
    os.environ.setdefault("PYTHONIOENCODING", "utf-8")


_make_output_safe()
