Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[py3] Always convert values from sqlite3 to unicode strings

Thanks Aymeric Augustin for the review.
  • Loading branch information...
commit 5513480fe1e616c236a7629072b4317e334b9b4b 1 parent dce34dc
Claude Paroz claudep authored
Showing with 15 additions and 7 deletions.
  1. +15 −7 django/db/backends/sqlite3/base.py
22 django/db/backends/sqlite3/base.py
View
@@ -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):
Please sign in to comment.
Something went wrong with that request. Please try again.