Return the path representation of a path-like object. If str or bytes is passed in, it is returned unchanged. Otherwise the os.PathLike interface is used to get the path representation. If the path representation is not str or bytes, TypeError is raised. If the provi
(path)
| 32 | |
| 33 | |
| 34 | def fspath(path): |
| 35 | """Return the path representation of a path-like object. |
| 36 | |
| 37 | If str or bytes is passed in, it is returned unchanged. Otherwise the |
| 38 | os.PathLike interface is used to get the path representation. If the |
| 39 | path representation is not str or bytes, TypeError is raised. If the |
| 40 | provided path is not str, bytes, or os.PathLike, TypeError is raised. |
| 41 | """ |
| 42 | if isinstance(path, (str, bytes)): |
| 43 | return path |
| 44 | |
| 45 | # Work from the object's type to match method resolution of other magic |
| 46 | # methods. |
| 47 | path_type = type(path) |
| 48 | try: |
| 49 | path_repr = path_type.__fspath__(path) |
| 50 | except AttributeError: |
| 51 | if hasattr(path_type, '__fspath__'): |
| 52 | raise |
| 53 | else: |
| 54 | raise TypeError("expected str, bytes or os.PathLike object, " |
| 55 | "not " + path_type.__name__) |
| 56 | if isinstance(path_repr, (str, bytes)): |
| 57 | return path_repr |
| 58 | else: |
| 59 | raise TypeError("expected {}.__fspath__() to return str or bytes, " |
| 60 | "not {}".format(path_type.__name__, |
| 61 | type(path_repr).__name__)) |
| 62 | |
| 63 | class PathLike(abc.ABC): |
| 64 |
no test coverage detected