Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9579 -- Properly handle apps running with (and specifically, l…

…oading templates from) a current working directory path that contains non-ASCII characters. Thanks for the report to gonzalodelgado and for advice on how to fix it to Daniel Pope.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9411 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dfa90aec1bed28f581b0f0471dc95860bb166cc9 1 parent 5c4fcbb
Karen Tracey authored November 13, 2008
4  django/template/loaders/app_directories.py
@@ -4,6 +4,7 @@
4 4
 """
5 5
 
6 6
 import os
  7
+import sys
7 8
 
8 9
 from django.conf import settings
9 10
 from django.core.exceptions import ImproperlyConfigured
@@ -11,6 +12,7 @@
11 12
 from django.utils._os import safe_join
12 13
 
13 14
 # At compile time, cache the directories to search.
  15
+fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
14 16
 app_template_dirs = []
15 17
 for app in settings.INSTALLED_APPS:
16 18
     i = app.rfind('.')
@@ -27,7 +29,7 @@
27 29
         raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0])
28 30
     template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')
29 31
     if os.path.isdir(template_dir):
30  
-        app_template_dirs.append(template_dir)
  32
+        app_template_dirs.append(template_dir.decode(fs_encoding))
31 33
 
32 34
 # It won't change, so convert it to a tuple to save memory.
33 35
 app_template_dirs = tuple(app_template_dirs)
26  django/utils/_os.py
... ...
@@ -1,6 +1,26 @@
1  
-from os.path import join, normcase, abspath, sep
  1
+import os
  2
+from os.path import join, normcase, normpath, abspath, isabs, sep
2 3
 from django.utils.encoding import force_unicode
3 4
 
  5
+# Define our own abspath function that can handle joining 
  6
+# unicode paths to a current working directory that has non-ASCII
  7
+# characters in it.  This isn't necessary on Windows since the 
  8
+# Windows version of abspath handles this correctly.  The Windows
  9
+# abspath also handles drive letters differently than the pure 
  10
+# Python implementation, so it's best not to replace it.
  11
+if os.name == 'nt':
  12
+    abspathu = abspath
  13
+else:
  14
+    def abspathu(path):
  15
+        """
  16
+        Version of os.path.abspath that uses the unicode representation
  17
+        of the current working directory, thus avoiding a UnicodeDecodeError
  18
+        in join when the cwd has non-ASCII characters.
  19
+        """
  20
+        if not isabs(path):
  21
+            path = join(os.getcwdu(), path)
  22
+        return normpath(path)
  23
+
4 24
 def safe_join(base, *paths):
5 25
     """
6 26
     Joins one or more path components to the base path component intelligently.
@@ -13,8 +33,8 @@ def safe_join(base, *paths):
13 33
     # insensitive operating systems (like Windows).
14 34
     base = force_unicode(base)
15 35
     paths = [force_unicode(p) for p in paths]
16  
-    final_path = normcase(abspath(join(base, *paths)))
17  
-    base_path = normcase(abspath(base))
  36
+    final_path = normcase(abspathu(join(base, *paths)))
  37
+    base_path = normcase(abspathu(base))
18 38
     base_path_len = len(base_path)
19 39
     # Ensure final_path starts with base_path and that the next character after
20 40
     # the final path is os.sep (or nothing, in which case final_path must be

0 notes on commit dfa90ae

Please sign in to comment.
Something went wrong with that request. Please try again.