From f6018c1e63a04e0c12e2ca759e76e05ccf5e09de Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 29 Mar 2021 10:10:19 +0200 Subject: [PATCH] Fixed #32595 -- Fixed SchemaEditor.quote_value() crash with bytes. --- django/db/backends/mysql/base.py | 9 ++++++++- tests/backends/mysql/test_schema.py | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 470271c3766..2c62182d31c 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -230,7 +230,14 @@ class DatabaseWrapper(BaseDatabaseWrapper): @async_unsafe def get_new_connection(self, conn_params): - return Database.connect(**conn_params) + connection = Database.connect(**conn_params) + # bytes encoder in mysqlclient doesn't work and was added only to + # prevent KeyErrors in Django < 2.0. We can remove this workaround when + # mysqlclient 2.1 becomes the minimal mysqlclient supported by Django. + # See https://github.com/PyMySQL/mysqlclient/issues/489 + if connection.encoders.get(bytes) is bytes: + connection.encoders.pop(bytes) + return connection def init_connection_state(self): assignments = [] diff --git a/tests/backends/mysql/test_schema.py b/tests/backends/mysql/test_schema.py index e4b25538ddd..44f4a07b186 100644 --- a/tests/backends/mysql/test_schema.py +++ b/tests/backends/mysql/test_schema.py @@ -12,6 +12,7 @@ class SchemaEditorTests(TestCase): tested_values = [ ('string', "'string'"), ('¿Tú hablas inglés?', "'¿Tú hablas inglés?'"), + (b'bytes', b"'bytes'"), (42, '42'), (1.754, '1.754e0' if MySQLdb.version_info >= (1, 3, 14) else '1.754'), (False, b'0' if MySQLdb.version_info >= (1, 4, 0) else '0'),