Skip to content

Commit

Permalink
Simplified os.fspath and manual check to os.fsdecode
Browse files Browse the repository at this point in the history
Also changed to checking isinstance instad of trying and excepting. Although this is taking more of a LBYL than an EAFP approach, catching the TypeError was actually swallowing a potential bug. We caught TypeError before for when the obj was not a str, bytes, or os.PathLike, but this actually hid the case when the obj implemented a faulty __fspath__.

os.fsdecode(x) calls x.__fspath__() and expects that to return a str or bytes, which it then decodes into a str. A type error is raised here if that __fspath__() call returned a bad type. We actually want this to be raised, since that implies a serious error in that object's __fspath__ function.
  • Loading branch information
jaden-young committed Feb 10, 2018
1 parent 30a8bd4 commit 7dd5721
Showing 1 changed file with 2 additions and 16 deletions.
18 changes: 2 additions & 16 deletions pydub/audio_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,23 +448,9 @@ def is_format(f):
if isinstance(orig_file, basestring):
return orig_file.lower().endswith(".{0}".format(f))
if sys.version_info >= (3, 6):
try:
path = os.fspath(orig_file)

if isinstance(path, bytes):
try:
path = path.decode(sys.getdefaultencoding())
except Exception:
pass

if isinstance(orig_file, os.PathLike):
path = os.fsdecode(orig_file)
return path.lower().endswith(".{0}".format(f))
except TypeError:
# Either orig_file was not a str, bytes, or PathLike
# object, or os.fspath() returned a byte string and and we
# failed to decode it, then endswith() tried to compare
# that with a regular str. In either case, we can't discern
# the file extension.
pass

return False

Expand Down

0 comments on commit 7dd5721

Please sign in to comment.