aboutsummaryrefslogtreecommitdiffstats
path: root/slack/python_compatibility.py
blob: 8aa8a84e16d9b6747d6b303c7e1e55912309c56c (plain) (blame)
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
import traceback
from typing import List


# Copied from https://peps.python.org/pep-0616/ for support for Python < 3.9
def removeprefix(self: str, prefix: str) -> str:
    if self.startswith(prefix):
        return self[len(prefix) :]
    else:
        return self[:]


# Copied from https://peps.python.org/pep-0616/ for support for Python < 3.9
def removesuffix(self: str, suffix: str) -> str:
    if suffix and self.endswith(suffix):
        return self[: -len(suffix)]
    else:
        return self[:]


def format_exception_only(exc: BaseException) -> List[str]:
    return traceback.format_exception_only(type(exc), exc)


def format_exception(exc: BaseException) -> List[str]:
    return traceback.format_exception(type(exc), exc, exc.__traceback__)