Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #10335: handle system locals unknown to Python in timez…

…one name handling. Thanks, mitsuhiko. Backport of [10703] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10704 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a109a223b83fa7fc4e46795219a545a651e8f8a9 1 parent e93b3a7
Jacob Kaplan-Moss authored May 08, 2009
11  django/utils/encoding.py
... ...
@@ -1,6 +1,8 @@
1 1
 import types
2 2
 import urllib
  3
+import locale
3 4
 import datetime
  5
+import codecs
4 6
 
5 7
 from django.utils.functional import Promise
6 8
 
@@ -136,3 +138,12 @@ def iri_to_uri(iri):
136 138
         return iri
137 139
     return urllib.quote(smart_str(iri), safe='/#%[]=:;$&()+,!?*')
138 140
 
  141
+
  142
+# The encoding of the default system locale but falls back to the
  143
+# given fallback encoding if the encoding is unsupported by python or could
  144
+# not be determined.  See tickets #10335 and #5846
  145
+try:
  146
+    DEFAULT_LOCALE_ENCODING = locale.getdefaultlocale()[1] or 'ascii'
  147
+    codecs.lookup(DEFAULT_LOCALE_ENCODING)
  148
+except:
  149
+    DEFAULT_LOCALE_ENCODING = 'ascii'
14  django/utils/tzinfo.py
... ...
@@ -1,15 +1,8 @@
1 1
 "Implementation of tzinfo classes for use with datetime.datetime."
2 2
 
3  
-import locale
4 3
 import time
5 4
 from datetime import timedelta, tzinfo
6  
-from django.utils.encoding import smart_unicode
7  
-
8  
-try:
9  
-    DEFAULT_ENCODING = locale.getdefaultlocale()[1] or 'ascii'
10  
-except:
11  
-    # Any problems at all determining the locale and we fallback. See #5846.
12  
-    DEFAULT_ENCODING = 'ascii'
  5
+from django.utils.encoding import smart_unicode, smart_str, DEFAULT_LOCALE_ENCODING
13 6
 
14 7
 class FixedOffset(tzinfo):
15 8
     "Fixed offset in minutes east from UTC."
@@ -41,7 +34,7 @@ def __init__(self, dt):
41 34
         self._tzname = self.tzname(dt)
42 35
 
43 36
     def __repr__(self):
44  
-        return self._tzname
  37
+        return smart_str(self._tzname)
45 38
 
46 39
     def utcoffset(self, dt):
47 40
         if self._isdst(dt):
@@ -57,7 +50,8 @@ def dst(self, dt):
57 50
 
58 51
     def tzname(self, dt):
59 52
         try:
60  
-            return smart_unicode(time.tzname[self._isdst(dt)], DEFAULT_ENCODING)
  53
+            return smart_unicode(time.tzname[self._isdst(dt)],
  54
+                                 DEFAULT_LOCALE_ENCODING)
61 55
         except UnicodeDecodeError:
62 56
             return None
63 57
 

0 notes on commit a109a22

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