Skip to content

mode.utils.compat

Compatibility utilities.

isatty(fh)

Return True if fh has a controlling terminal.

Notes

Use with e.g. sys.stdin.

Source code in mode/utils/compat.py
22
23
24
25
26
27
28
29
30
31
def isatty(fh: IO) -> bool:
    """Return True if fh has a controlling terminal.

    Notes:
        Use with e.g. `sys.stdin`.
    """
    try:
        return fh.isatty()
    except AttributeError:
        return False

want_bytes(s)

Convert string to bytes.

Source code in mode/utils/compat.py
 8
 9
10
11
12
def want_bytes(s: AnyStr) -> bytes:
    """Convert string to bytes."""
    if isinstance(s, str):
        return s.encode()
    return s

want_str(s)

Convert bytes to string.

Source code in mode/utils/compat.py
15
16
17
18
19
def want_str(s: AnyStr) -> str:
    """Convert bytes to string."""
    if isinstance(s, bytes):
        return s.decode()
    return s