[py3] Always convert values from sqlite3 to unicode strings

Thanks Aymeric Augustin for the review.
This commit is contained in:
Claude Paroz 2012-08-12 20:43:01 +02:00
parent dce34dc969
commit 5513480fe1
1 changed files with 15 additions and 7 deletions

View File

@ -56,13 +56,21 @@ def adapt_datetime_with_timezone_support(value):
value = value.astimezone(timezone.utc).replace(tzinfo=None)
return value.isoformat(str(" "))
Database.register_converter(str("bool"), lambda s: str(s) == '1')
Database.register_converter(str("time"), parse_time)
Database.register_converter(str("date"), parse_date)
Database.register_converter(str("datetime"), parse_datetime_with_timezone_support)
Database.register_converter(str("timestamp"), parse_datetime_with_timezone_support)
Database.register_converter(str("TIMESTAMP"), parse_datetime_with_timezone_support)
Database.register_converter(str("decimal"), util.typecast_decimal)
def decoder(conv_func):
""" The Python sqlite3 interface returns always byte strings.
This function converts the received value to a regular string before
passing it to the receiver function.
"""
return lambda s: conv_func(s.decode('utf-8'))
Database.register_converter(str("bool"), decoder(lambda s: s == '1'))
Database.register_converter(str("time"), decoder(parse_time))
Database.register_converter(str("date"), decoder(parse_date))
Database.register_converter(str("datetime"), decoder(parse_datetime_with_timezone_support))
Database.register_converter(str("timestamp"), decoder(parse_datetime_with_timezone_support))
Database.register_converter(str("TIMESTAMP"), decoder(parse_datetime_with_timezone_support))
Database.register_converter(str("decimal"), decoder(util.typecast_decimal))
Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support)
Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
if Database.version_info >= (2, 4, 1):