diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-10-04 11:58:43 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-10-04 11:58:43 (GMT) |
commit | 3deeeb0c39ee4fcc1949d127f95702edbed56943 (patch) | |
tree | 3e9963cadcb3320b1658f3187b937470c47acd71 /Lib/posixpath.py | |
parent | 385328bf761969a761d48727bfec88dccf4fe641 (diff) | |
download | cpython-3deeeb0c39ee4fcc1949d127f95702edbed56943.zip cpython-3deeeb0c39ee4fcc1949d127f95702edbed56943.tar.gz cpython-3deeeb0c39ee4fcc1949d127f95702edbed56943.tar.bz2 |
Issue #21883: os.path.join() and os.path.relpath() now raise a TypeError with
more helpful error message for unsupported or mismatched types of arguments.
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r-- | Lib/posixpath.py | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py index f08c931..ce5f792 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -82,13 +82,9 @@ def join(a, *p): path += b else: path += sep + b - except (TypeError, AttributeError): - for s in (a,) + p: - if not isinstance(s, (str, bytes)): - raise TypeError('join() argument must be str or bytes, not %r' % - s.__class__.__name__) from None - # Must have a mixture of text and binary data - raise TypeError("Can't mix strings and bytes in path components") from None + except (TypeError, AttributeError, BytesWarning): + genericpath._check_arg_types('join', a, *p) + raise return path @@ -446,13 +442,16 @@ def relpath(path, start=None): if start is None: start = curdir - start_list = [x for x in abspath(start).split(sep) if x] - path_list = [x for x in abspath(path).split(sep) if x] - - # Work out how much of the filepath is shared by start and path. - i = len(commonprefix([start_list, path_list])) - - rel_list = [pardir] * (len(start_list)-i) + path_list[i:] - if not rel_list: - return curdir - return join(*rel_list) + try: + start_list = [x for x in abspath(start).split(sep) if x] + path_list = [x for x in abspath(path).split(sep) if x] + # Work out how much of the filepath is shared by start and path. + i = len(commonprefix([start_list, path_list])) + + rel_list = [pardir] * (len(start_list)-i) + path_list[i:] + if not rel_list: + return curdir + return join(*rel_list) + except (TypeError, AttributeError, BytesWarning): + genericpath._check_arg_types('relpath', path, start) + raise |