diff options
| author | Claude Paroz <claude@2xlibre.net> | 2012-12-08 11:13:52 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2012-12-08 11:13:52 +0100 |
| commit | c91667338a4e774e2819ccf4da852dc7b759bc19 (patch) | |
| tree | 42a700d237c85ac2b647999d02d3dcd7d8047068 /django/utils/_os.py | |
| parent | 53b879f045f0e55cc8f4bedff67b5a14f3057561 (diff) | |
Fixed #19357 -- Allow non-ASCII chars in filesystem paths
Thanks kujiu for the report and Aymeric Augustin for the review.
Diffstat (limited to 'django/utils/_os.py')
| -rw-r--r-- | django/utils/_os.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/django/utils/_os.py b/django/utils/_os.py index 1ea12aed8a..6c1cd17a83 100644 --- a/django/utils/_os.py +++ b/django/utils/_os.py @@ -1,6 +1,8 @@ import os import stat +import sys from os.path import join, normcase, normpath, abspath, isabs, sep, dirname + from django.utils.encoding import force_text from django.utils import six @@ -10,6 +12,9 @@ except NameError: class WindowsError(Exception): pass +if not six.PY3: + fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() + # Under Python 2, define our own abspath function that can handle joining # unicode paths to a current working directory that has non-ASCII characters @@ -29,6 +34,23 @@ else: path = join(os.getcwdu(), path) return normpath(path) +def upath(path): + """ + Always return a unicode path. + """ + if not six.PY3: + return path.decode(fs_encoding) + return path + +def npath(path): + """ + Always return a native path, that is unicode on Python 3 and bytestring on + Python 2. + """ + if not six.PY3 and not isinstance(path, bytes): + return path.encode(fs_encoding) + return path + def safe_join(base, *paths): """ Joins one or more path components to the base path component intelligently. @@ -74,4 +96,3 @@ def rmtree_errorhandler(func, path, exc_info): os.chmod(path, stat.S_IWRITE) # use the original function to repeat the operation func(path) - |
