Django

Code

Changeset 9467

Show
Ignore:
Timestamp:
11/16/08 02:50:06 (2 months ago)
Author:
mtredinnick
Message:

Fixed #6052 -- Worked around a bug in MySQLdb with regards to handling
SafeUnicode? (handle SafeString? similarly, just to be safe). Based on a patch
from sfllaw.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/backends/mysql/base.py

    r8802 r9467  
    3030from django.db.backends.mysql.introspection import DatabaseIntrospection 
    3131from django.db.backends.mysql.validation import DatabaseValidation 
     32from django.utils.safestring import SafeString, SafeUnicode 
    3233 
    3334# Raise exceptions for database warnings if DEBUG is on 
     
    4041IntegrityError = Database.IntegrityError 
    4142 
    42 # MySQLdb-1.2.1 supports the Python boolean type, and only uses datetime 
    43 # module for time-related columns; older versions could have used mx.DateTime 
    44 # or strings if there were no datetime module. However, MySQLdb still returns 
    45 # TIME columns as timedelta -- they are more like timedelta in terms of actual 
    46 # behavior as they are signed and include days -- and Django expects time, so 
    47 # we still need to override that. 
     43# MySQLdb-1.2.1 returns TIME columns as timedelta -- they are more like 
     44# timedelta in terms of actual behavior as they are signed and include days -- 
     45# and Django expects time, so we still need to override that. We also need to 
     46# add special handling for SafeUnicode and SafeString as MySQLdb's type 
     47# checking is too tight to catch those (see Django ticket #6052). 
    4848django_conversions = conversions.copy() 
    4949django_conversions.update({ 
     
    175175        if value is None: 
    176176            return None 
    177          
     177 
    178178        # MySQL doesn't support tz-aware datetimes 
    179179        if value.tzinfo is not None: 
     
    186186        if value is None: 
    187187            return None 
    188              
     188 
    189189        # MySQL doesn't support tz-aware datetimes 
    190190        if value.tzinfo is not None: 
    191191            raise ValueError("MySQL backend does not support timezone-aware datetimes.") 
    192          
     192 
    193193        # MySQL doesn't support microseconds 
    194194        return unicode(value.replace(microsecond=0)) 
     
    261261            kwargs.update(self.options) 
    262262            self.connection = Database.connect(**kwargs) 
     263            self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode] 
     264            self.connection.encoders[SafeString] = self.connection.encoders[str] 
    263265        cursor = CursorWrapper(self.connection.cursor()) 
    264266        return cursor 
  • django/trunk/tests/modeltests/field_defaults/models.py

    r8532 r9467  
     1# coding: utf-8 
    12""" 
    2332. Callable defaults 
     
    2021        return self.headline 
    2122 
    22 __test__ = {'API_TESTS':""" 
     23__test__ = {'API_TESTS': u""" 
    2324>>> from datetime import datetime 
    2425 
     
    5253# make sure that SafeString/SafeUnicode fields work 
    5354>>> from django.utils.safestring import SafeUnicode, SafeString 
    54 >>> a.headline = SafeUnicode(u'SafeUnicode Headline') 
     55>>> a.headline = SafeUnicode(u'Iñtërnâtiônàlizætiøn1') 
    5556>>> a.save() 
    56 >>> a.headline = SafeString(u'SafeString Headline'
     57>>> a.headline = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8')
    5758>>> a.save() 
    5859"""}