Created
April 13, 2016 17:08
-
-
Save brettcannon/b3719f54715787d54a206bc011869aa1 to your computer and use it in GitHub Desktop.
Possibly implementations of os.fspath()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# str-only. | |
def fspath(path): | |
try: | |
path = path.__fspath__() | |
except AttributeError: | |
pass | |
if isinstance(path, str): | |
return path | |
else: | |
raise TypeError | |
# Default to str, bytes allowed only when an argument. | |
def fspath(path, *, allow_bytes=False): | |
if allow_bytes and isinstance(path, bytes): | |
return path | |
try: | |
path = path.__fspath__() | |
except AttributeError: | |
pass | |
if isinstance(path, str): | |
return path | |
else: | |
raise TypeError | |
# Default to str, bytes allowed from anywhere. | |
def fspath(path, *, allow_bytes=False): | |
try: | |
path = path.__fspath__() | |
except AttributeError: | |
pass | |
if isinstance(path, str): | |
return path | |
elif allow_bytes and isinstance(path, bytes): | |
return path | |
else: | |
raise TypeError | |
# str or bytes allowed. | |
def fspath(path): | |
try: | |
path = path.__fspath__() | |
except AttributeError: | |
pass | |
if isinstance(path, (str, bytes)): | |
return path | |
else: | |
raise TypeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment