File size: 1,078 Bytes
c19d9d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Force la sortie console en UTF-8 (Windows cp1252 plante sinon sur ✓ ─ ⏱ …).

À importer en TOUT premier dans chaque entrypoint d'app.
"""
from __future__ import annotations

import io
import sys


def setup_console() -> None:
    for stream_name in ("stdout", "stderr"):
        stream = getattr(sys, stream_name, None)
        if stream is None:
            continue
        # Python 3.7+: TextIOWrapper.reconfigure(encoding=...)
        reconf = getattr(stream, "reconfigure", None)
        if reconf is not None:
            try:
                reconf(encoding="utf-8", errors="replace")
                continue
            except Exception:
                pass
        # Fallback : enveloppe le buffer brut.
        buffer = getattr(stream, "buffer", None)
        if buffer is not None:
            try:
                setattr(sys, stream_name, io.TextIOWrapper(buffer, encoding="utf-8",
                                                            errors="replace", line_buffering=True))
            except Exception:
                pass


setup_console()