Ticket #18790: 18790-1.diff

File 18790-1.diff, 3.4 KB (added by claudep, 3 years ago)

Encode db password on Python 2

  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index cec3b04..4043014 100644
    a b from django.db.backends.mysql.client import DatabaseClient 
    3737from django.db.backends.mysql.creation import DatabaseCreation
    3838from django.db.backends.mysql.introspection import DatabaseIntrospection
    3939from django.db.backends.mysql.validation import DatabaseValidation
     40from django.utils.encoding import force_str
    4041from django.utils.functional import cached_property
    4142from django.utils.safestring import SafeBytes, SafeText
    4243from django.utils import six
    class DatabaseWrapper(BaseDatabaseWrapper): 
    390391            if settings_dict['NAME']:
    391392                kwargs['db'] = settings_dict['NAME']
    392393            if settings_dict['PASSWORD']:
    393                 kwargs['passwd'] = settings_dict['PASSWORD']
     394                kwargs['passwd'] = force_str(settings_dict['PASSWORD'])
    394395            if settings_dict['HOST'].startswith('/'):
    395396                kwargs['unix_socket'] = settings_dict['HOST']
    396397            elif settings_dict['HOST']:
  • django/db/backends/postgresql_psycopg2/base.py

    diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
    index f6f534d..c8b88d5 100644
    a b from django.db.backends.postgresql_psycopg2.client import DatabaseClient 
    1313from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
    1414from django.db.backends.postgresql_psycopg2.version import get_version
    1515from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection
     16from django.utils.encoding import force_str
    1617from django.utils.log import getLogger
    1718from django.utils.safestring import SafeText, SafeBytes
    1819from django.utils import six
    class DatabaseWrapper(BaseDatabaseWrapper): 
    172173            if settings_dict['USER']:
    173174                conn_params['user'] = settings_dict['USER']
    174175            if settings_dict['PASSWORD']:
    175                 conn_params['password'] = settings_dict['PASSWORD']
     176                conn_params['password'] = force_str(settings_dict['PASSWORD'])
    176177            if settings_dict['HOST']:
    177178                conn_params['host'] = settings_dict['HOST']
    178179            if settings_dict['PORT']:
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index dc92189..70cf63c 100644
    a b class BackendTestCase(TestCase): 
    401401        self.assertEqual(list(cursor.fetchmany(2)), [('Jane', 'Doe'), ('John', 'Doe')])
    402402        self.assertEqual(list(cursor.fetchall()), [('Mary', 'Agnelline'), ('Peter', 'Parker')])
    403403
     404    def test_unicode_password(self):
     405        old_password = connection.settings_dict['PASSWORD']
     406        connection.settings_dict['PASSWORD'] = "françois"
     407        try:
     408            cursor = connection.cursor()
     409        except backend.Database.DatabaseError:
     410            # As password is probably wrong, an exception is expected
     411            pass
     412        except Exception as e:
     413            self.fail("Unexpected error raised with unicode password: %s" % e)
     414        finally:
     415            connection.settings_dict['PASSWORD'] = old_password
     416
    404417    def test_database_operations_helper_class(self):
    405418        # Ticket #13630
    406419        self.assertTrue(hasattr(connection, 'ops'))
Back to Top