MCPcopy Index your code
hub / github.com/RustPython/RustPython / _fspath

Function _fspath

Lib/os.py:1081–1114  ·  view source on GitHub ↗

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 provided path is not

(path)

Source from the content-addressed store, hash-verified

1079# For testing purposes, make sure the function is available when the C
1080# implementation exists.
1081def _fspath(path):
1082 """Return the path representation of a path-like object.
1083
1084 If str or bytes is passed in, it is returned unchanged. Otherwise the
1085 os.PathLike interface is used to get the path representation. If the
1086 path representation is not str or bytes, TypeError is raised. If the
1087 provided path is not str, bytes, or os.PathLike, TypeError is raised.
1088 """
1089 if isinstance(path, (str, bytes)):
1090 return path
1091
1092 # Work from the object's type to match method resolution of other magic
1093 # methods.
1094 path_type = type(path)
1095 try:
1096 path_repr = path_type.__fspath__(path)
1097 except AttributeError:
1098 if hasattr(path_type, '__fspath__'):
1099 raise
1100 else:
1101 raise TypeError("expected str, bytes or os.PathLike object, "
1102 "not " + path_type.__name__)
1103 except TypeError:
1104 if path_type.__fspath__ is None:
1105 raise TypeError("expected str, bytes or os.PathLike object, "
1106 "not " + path_type.__name__) from None
1107 else:
1108 raise
1109 if isinstance(path_repr, (str, bytes)):
1110 return path_repr
1111 else:
1112 raise TypeError("expected {}.__fspath__() to return str or bytes, "
1113 "not {}".format(path_type.__name__,
1114 type(path_repr).__name__))
1115
1116# If there is no C implementation, make the pure Python version the
1117# implementation as transparently as possible.

Callers

nothing calls this directly

Calls 4

isinstanceFunction · 0.85
hasattrFunction · 0.85
__fspath__Method · 0.45
formatMethod · 0.45

Tested by

no test coverage detected