Pillow/src/PIL/_util.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

32 lines
813 B
Python
Raw Normal View History

from __future__ import annotations
2018-06-13 12:44:36 +03:00
import os
2023-12-27 02:17:57 +03:00
from typing import Any, NoReturn
2024-01-22 22:37:37 +03:00
from ._typing import StrOrBytesPath, TypeGuard
2018-04-20 02:19:13 +03:00
2023-12-27 02:17:57 +03:00
2024-01-22 22:37:37 +03:00
def is_path(f: Any) -> TypeGuard[StrOrBytesPath]:
2024-01-26 20:11:18 +03:00
return isinstance(f, (bytes, str, os.PathLike))
2013-06-30 22:50:38 +04:00
2014-07-06 02:50:24 +04:00
def is_directory(f: Any) -> TypeGuard[StrOrBytesPath]:
"""Checks if an object is a string, and that it points to a directory."""
return is_path(f) and os.path.isdir(f)
2014-04-03 07:09:04 +04:00
2014-07-06 02:50:24 +04:00
class DeferredError:
2023-12-27 02:17:57 +03:00
def __init__(self, ex: BaseException):
self.ex = ex
2014-07-06 02:50:24 +04:00
2023-12-27 02:17:57 +03:00
def __getattr__(self, elt: str) -> NoReturn:
raise self.ex
2023-12-27 02:17:57 +03:00
@staticmethod
def new(ex: BaseException) -> Any:
"""
Creates an object that raises the wrapped exception ``ex`` when used,
and casts it to :py:obj:`~typing.Any` type.
"""
return DeferredError(ex)