Debian Codegraph @

DisplayCAL/encoding.py

raw
1 """Utility functions for retrieving system and filesystem encodings."""
2
3 from __future__ import annotations
4
5 import sys
6 from typing import BinaryIO, TextIO
7
8
9 def get_encoding(stream: BinaryIO | TextIO) -> str:
10 """Return stream encoding.
11
12 Args:
13 stream (BinaryIO | TextIO): A binary or text stream (e.g., sys.stdout,
14 sys.stdin).
15
16 Returns:
17 str: The encoding of the stream, typically "utf-8" for Python 3.6 and
18 later.
19 """
20 return sys.getdefaultencoding() # which is "utf-8" for all OSes after Python 3.6
21
22
23 def get_encodings() -> tuple[str, str]:
24 """Return console encoding, filesystem encoding.
25
26 Returns:
27 tuple[str, str]: A tuple containing the console encoding and the
28 filesystem encoding. Both are typically "utf-8" for Python 3.6 and
29 later.
30 """
31 enc = get_encoding(sys.stdout) # this is "utf-8" for all OSes
32 fs_enc = sys.getfilesystemencoding() or enc # this is "utf-8" for all OSes
33 return enc, fs_enc