diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2016-06-02 22:06:09 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2016-06-02 22:06:09 (GMT) |
commit | cdc0879d3a550ee7f339d149c451e2ebee1906f9 (patch) | |
tree | d2ee559bac76d8fae019310c2d5c96d0260fc24c /Lib/os.py | |
parent | 1f56e5f6afa137300d655a40e00675195e0248fe (diff) | |
download | cpython-cdc0879d3a550ee7f339d149c451e2ebee1906f9.zip cpython-cdc0879d3a550ee7f339d149c451e2ebee1906f9.tar.gz cpython-cdc0879d3a550ee7f339d149c451e2ebee1906f9.tar.bz2 |
issue27186 -- initial docs, tests, and python version of os.fspath
Diffstat (limited to 'Lib/os.py')
-rw-r--r-- | Lib/os.py | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -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__) |