summaryrefslogtreecommitdiff
path: root/Lib/os.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 90646a0553..edd61ab8f8 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -1097,3 +1097,24 @@ def fdopen(fd, *args, **kwargs):
raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
import io
return io.open(fd, *args, **kwargs)
+
+# Supply os.fspath()
+def fspath(path):
+ """Return the string representation of the path.
+
+ If str or bytes is passed in, it is returned unchanged.
+ """
+ if isinstance(path, (str, bytes)):
+ return path
+
+ # Work from the object's type to match method resolution of other magic
+ # methods.
+ path_type = type(path)
+ try:
+ return path_type.__fspath__(path)
+ except AttributeError:
+ if hasattr(path_type, '__fspath__'):
+ raise
+
+ raise TypeError("expected str, bytes or os.PathLike object, not "
+ + path_type.__name__)