mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
issue27186: add C version of os.fspath(); patch by Jelle Zijlstra
This commit is contained in:
parent
c55014f371
commit
410ef8e230
6 changed files with 125 additions and 18 deletions
35
Lib/os.py
35
Lib/os.py
|
@ -1104,23 +1104,24 @@ def fdopen(fd, *args, **kwargs):
|
|||
import io
|
||||
return io.open(fd, *args, **kwargs)
|
||||
|
||||
# Supply os.fspath()
|
||||
def fspath(path):
|
||||
"""Return the string representation of the path.
|
||||
# Supply os.fspath() if not defined in C
|
||||
if not _exists('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
|
||||
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
|
||||
# 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__)
|
||||
raise TypeError("expected str, bytes or os.PathLike object, not "
|
||||
+ path_type.__name__)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue