From 859aa2a6c4496a0feafebef7c7ea8fc57d9913f4 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 12 Sep 2012 10:16:49 +0200 Subject: [PATCH] Fixed #18790 -- Encoded database password on Python 2 Thanks thcourbon@gmail.com for the report. --- django/db/backends/mysql/base.py | 3 ++- django/db/backends/postgresql_psycopg2/base.py | 3 ++- tests/regressiontests/backends/tests.py | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index cec3b04f7e..4043014b8e 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -37,6 +37,7 @@ from django.db.backends.mysql.client import DatabaseClient from django.db.backends.mysql.creation import DatabaseCreation from django.db.backends.mysql.introspection import DatabaseIntrospection from django.db.backends.mysql.validation import DatabaseValidation +from django.utils.encoding import force_str from django.utils.functional import cached_property from django.utils.safestring import SafeBytes, SafeText from django.utils import six @@ -390,7 +391,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): if settings_dict['NAME']: kwargs['db'] = settings_dict['NAME'] if settings_dict['PASSWORD']: - kwargs['passwd'] = settings_dict['PASSWORD'] + kwargs['passwd'] = force_str(settings_dict['PASSWORD']) if settings_dict['HOST'].startswith('/'): kwargs['unix_socket'] = settings_dict['HOST'] elif settings_dict['HOST']: diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index f6f534da8c..c8b88d5619 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -13,6 +13,7 @@ from django.db.backends.postgresql_psycopg2.client import DatabaseClient from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation from django.db.backends.postgresql_psycopg2.version import get_version from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection +from django.utils.encoding import force_str from django.utils.log import getLogger from django.utils.safestring import SafeText, SafeBytes from django.utils import six @@ -172,7 +173,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): if settings_dict['USER']: conn_params['user'] = settings_dict['USER'] if settings_dict['PASSWORD']: - conn_params['password'] = settings_dict['PASSWORD'] + conn_params['password'] = force_str(settings_dict['PASSWORD']) if settings_dict['HOST']: conn_params['host'] = settings_dict['HOST'] if settings_dict['PORT']: diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py index e53b02032e..cfa298253c 100644 --- a/tests/regressiontests/backends/tests.py +++ b/tests/regressiontests/backends/tests.py @@ -401,6 +401,19 @@ class BackendTestCase(TestCase): self.assertEqual(list(cursor.fetchmany(2)), [('Jane', 'Doe'), ('John', 'Doe')]) self.assertEqual(list(cursor.fetchall()), [('Mary', 'Agnelline'), ('Peter', 'Parker')]) + def test_unicode_password(self): + old_password = connection.settings_dict['PASSWORD'] + connection.settings_dict['PASSWORD'] = "françois" + try: + cursor = connection.cursor() + except backend.Database.DatabaseError: + # As password is probably wrong, a database exception is expected + pass + except Exception as e: + self.fail("Unexpected error raised with unicode password: %s" % e) + finally: + connection.settings_dict['PASSWORD'] = old_password + def test_database_operations_helper_class(self): # Ticket #13630 self.assertTrue(hasattr(connection, 'ops'))