diff options
Diffstat (limited to 'Lib/posixpath.py')
| -rw-r--r-- | Lib/posixpath.py | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 64fe9df..2e3625b 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -49,6 +49,9 @@ def _get_sep(path): def normcase(s): """Normalize case of pathname. Has no effect under Posix""" # TODO: on Mac OS X, this should really return s.lower(). + if not isinstance(s, (bytes, str)): + raise TypeError("normcase() argument must be str or bytes, " + "not '{}'".format(s.__class__.__name__)) return s @@ -68,16 +71,25 @@ def isabs(s): def join(a, *p): """Join two or more pathname components, inserting '/' as needed. If any component is an absolute path, all previous path components - will be discarded.""" + will be discarded. An empty last part will result in a path that + ends with a separator.""" sep = _get_sep(a) path = a - for b in p: - if b.startswith(sep): - path = b - elif not path or path.endswith(sep): - path += b - else: - path += sep + b + try: + for b in p: + if b.startswith(sep): + path = b + elif not path or path.endswith(sep): + path += b + else: + path += sep + b + except TypeError: + valid_types = all(isinstance(s, (str, bytes, bytearray)) + for s in (a, ) + p) + if valid_types: + # Must have a mixture of text and binary data + raise TypeError("Can't mix strings and bytes in path components.") + raise return path @@ -158,7 +170,7 @@ def islink(path): def lexists(path): """Test whether a path exists. Returns True for broken symbolic links""" try: - st = os.lstat(path) + os.lstat(path) except os.error: return False return True @@ -259,12 +271,12 @@ def expanduser(path): return path userhome = pwent.pw_dir if isinstance(path, bytes): - userhome = userhome.encode(sys.getfilesystemencoding()) + userhome = os.fsencode(userhome) root = b'/' else: root = '/' - userhome = userhome.rstrip(root) or userhome - return userhome + path[i:] + userhome = userhome.rstrip(root) + return (userhome + path[i:]) or root # Expand paths containing shell variable substitutions. @@ -424,7 +436,7 @@ def _resolve_link(path): path = normpath(resolved) return path -supports_unicode_filenames = False +supports_unicode_filenames = (sys.platform == 'darwin') def relpath(path, start=None): """Return a relative version of a path""" |
