1
0
mirror of https://github.com/django/django.git synced 2025-10-29 08:36:09 +00:00

Fixed #19357 -- Allow non-ASCII chars in filesystem paths

Thanks kujiu for the report and Aymeric Augustin for the review.
This commit is contained in:
Claude Paroz
2012-12-08 11:13:52 +01:00
parent 53b879f045
commit c91667338a
56 changed files with 228 additions and 147 deletions

View File

@@ -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)