Changeset 3310

Show
Ignore:
Timestamp:
01/30/08 23:16:51 (16 years ago)
Author:
jerome
Message:

Added a workaround for buggy versions of python-mysqldb (e.g
1.2.1-p2-4 on my Debian Etch box) which have the double
encoding bug (#1521274 on sourceforge.net).

Location:
pykota/trunk/pykota
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/storages/mysqlstorage.py

    r3308 r3310  
    5858        self.cursor.execute("SET NAMES 'utf8';") 
    5959        self.cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;") # Same as PostgreSQL and Oracle's default 
    60         self.closed = 0 
     60        self.closed = False 
    6161        self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) 
     62        try : 
     63            # Here we try to select a string (an é) which is 
     64            # already encoded in UTF-8. If python-mysqldb suffers from  
     65            # the double encoding problem, we will catch the exception 
     66            # and activate a workaround. 
     67            self.cursor.execute("SELECT '%s';" % (chr(0xc3) + chr(0xa9))) # é in UTF-8 
     68            self.cursor.fetchall() 
     69        except UnicodeDecodeError :     
     70            self.needsworkaround = True 
     71            self.tool.logdebug("Database needs encoding workaround.") 
     72        else : 
     73            self.needsworkaround = False 
     74            self.tool.logdebug("Database doesn't need encoding workaround.") 
    6275             
    6376    def close(self) :     
     
    6679            self.cursor.close() 
    6780            self.database.close() 
    68             self.closed = 1 
     81            self.closed = True 
    6982            self.tool.logdebug("Database closed.") 
    7083         
     
    89102        if not query.endswith(';') :     
    90103            query += ';' 
     104        self.querydebug("QUERY : %s" % query) 
     105        if self.needsworkaround :     
     106            query = query.decode("UTF-8") 
    91107        try : 
    92             self.querydebug("QUERY : %s" % query) 
    93108            self.cursor.execute(query) 
    94109        except self.database.Error, msg :     
     
    96111        else :     
    97112            # This returns a list of lists. Integers are returned as longs. 
    98             result = self.cursor.fetchall() 
    99             return result 
     113            return self.cursor.fetchall() 
    100114             
    101115    def doSearch(self, query) :         
     
    110124                rowdict = {} 
    111125                for field in fields.keys() : 
    112                     value = row[field] 
    113                     try : 
    114                         value = value.encode("UTF-8") 
    115                     except: 
    116                         pass 
    117                     rowdict[fields[field]] = value 
     126                    rowdict[fields[field]] = row[field] 
    118127                rows.append(rowdict) 
    119128            # returns a list of dicts 
     
    125134        if not query.endswith(';') :     
    126135            query += ';' 
     136        self.querydebug("QUERY : %s" % query) 
     137        if self.needsworkaround :     
     138            query = query.decode("UTF-8") 
    127139        try : 
    128             self.querydebug("QUERY : %s" % query) 
    129140            self.cursor.execute(query) 
    130141        except self.database.Error, msg :     
     
    141152            return field 
    142153        elif field is not None : 
    143             newfield = self.database.string_literal(field) 
    144             try : 
    145                 return newfield.encode("UTF-8") 
    146             except :     
    147                 return newfield 
     154            return self.database.string_literal(field) 
    148155        else : 
    149             self.tool.logdebug("WARNING: field has no type, returning NULL") 
    150156            return "NULL" 
    151157 
     
    153159        """Prepares a raw result by including the headers.""" 
    154160        if result : 
    155             entries = [tuple([f[0] for f in self.cursor.description])] 
    156             for entry in result : 
    157                 row = [] 
    158                 for value in entry : 
    159                     try : 
    160                         value = value.encode("UTF-8") 
    161                     except : 
    162                         pass 
    163                     row.append(value) 
    164                 entries.append(tuple(row)) 
    165             return entries 
     161            return [tuple([f[0] for f in self.cursor.description])] \ 
     162                 + list(result) 
  • pykota/trunk/pykota/utils.py

    r3298 r3310  
    2626import locale 
    2727import gettext 
     28from types import UnicodeType 
    2829 
    2930def initlocale(lang="", cset=None) : 
     
    9495    """ 
    9596    if text is not None : 
    96         return text.decode("UTF-8", "replace") 
     97        if not isinstance(text, UnicodeType) : 
     98            return text.decode("UTF-8", "replace") 
     99        else :     
     100            # MySQL already returns unicode objects 
     101            return text 
    97102    else :  
    98103        return None