Ticket #17760: 17760-2.diff

File 17760-2.diff, 7.0 KB (added by Claude Paroz, 12 years ago)

Using the cached_property decorator

  • django/contrib/gis/db/backends/spatialite/creation.py

    diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py
    index 33b6f95..5c97113 100644
    a b class SpatiaLiteCreation(DatabaseCreation):  
    3131        self.connection.close()
    3232        self.connection.settings_dict["NAME"] = test_database_name
    3333
    34         # Confirm the feature set of the test database
    35         self.connection.features.confirm()
    36 
    3734        # Need to load the SpatiaLite initialization SQL before running `syncdb`.
    3835        self.load_spatialite_sql()
    3936
  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 7674f5c..4a15e40 100644
    a b from django.conf import settings  
    1010from django.db import DEFAULT_DB_ALIAS
    1111from django.db.backends import util
    1212from django.db.transaction import TransactionManagementError
     13from django.utils.functional import cached_property
    1314from django.utils.importlib import import_module
    1415from django.utils.timezone import is_aware
    1516
    class BaseDatabaseFeatures(object):  
    399400    # in the SQL standard.
    400401    supports_tablespaces = False
    401402
    402     # Features that need to be confirmed at runtime
    403     # Cache whether the confirmation has been performed.
    404     _confirmed = False
    405     supports_transactions = None
    406     supports_stddev = None
    407     can_introspect_foreign_keys = None
     403    # Confirm support for introspected foreign keys
     404    # Every database can do this reliably, except MySQL,
     405    # which can't do it for MyISAM tables
     406    can_introspect_foreign_keys = True
    408407
    409408    # Support for the DISTINCT ON clause
    410409    can_distinct_on_fields = False
    class BaseDatabaseFeatures(object):  
    412411    def __init__(self, connection):
    413412        self.connection = connection
    414413
    415     def confirm(self):
    416         "Perform manual checks of any database features that might vary between installs"
    417         self._confirmed = True
    418         self.supports_transactions = self._supports_transactions()
    419         self.supports_stddev = self._supports_stddev()
    420         self.can_introspect_foreign_keys = self._can_introspect_foreign_keys()
    421 
    422     def _supports_transactions(self):
     414    @cached_property
     415    def supports_transactions(self):
    423416        "Confirm support for transactions"
    424417        cursor = self.connection.cursor()
    425418        cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
    class BaseDatabaseFeatures(object):  
    432425        self.connection._commit()
    433426        return count == 0
    434427
    435     def _supports_stddev(self):
     428    @cached_property
     429    def supports_stddev(self):
    436430        "Confirm support for STDDEV and related stats functions"
    437431        class StdDevPop(object):
    438432            sql_function = 'STDDEV_POP'
    class BaseDatabaseFeatures(object):  
    440434        try:
    441435            self.connection.ops.check_aggregate_support(StdDevPop())
    442436        except NotImplementedError:
    443             self.supports_stddev = False
    444 
    445     def _can_introspect_foreign_keys(self):
    446         "Confirm support for introspected foreign keys"
    447         # Every database can do this reliably, except MySQL,
    448         # which can't do it for MyISAM tables
     437            return False
    449438        return True
    450439
    451440
  • django/db/backends/creation.py

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    index 2db0acc..a5ed311 100644
    a b class BaseDatabaseCreation(object):  
    258258        self.connection.close()
    259259        self.connection.settings_dict["NAME"] = test_database_name
    260260
    261         # Confirm the feature set of the test database
    262         self.connection.features.confirm()
    263 
    264261        # Report syncdb messages at one level lower than that requested.
    265262        # This ensures we don't get flooded with messages during testing
    266263        # (unless you really ask to be flooded)
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index 830808b..92e04f0 100644
    a b from django.db.backends.mysql.client import DatabaseClient  
    3232from django.db.backends.mysql.creation import DatabaseCreation
    3333from django.db.backends.mysql.introspection import DatabaseIntrospection
    3434from django.db.backends.mysql.validation import DatabaseValidation
     35from django.utils.functional import cached_property
    3536from django.utils.safestring import SafeString, SafeUnicode
    3637from django.utils.timezone import is_aware, is_naive, utc
    3738
    class DatabaseFeatures(BaseDatabaseFeatures):  
    152153
    153154    def __init__(self, connection):
    154155        super(DatabaseFeatures, self).__init__(connection)
    155         self._storage_engine = None
    156156
     157    @cached_property
    157158    def _mysql_storage_engine(self):
    158159        "Internal method used in Django tests. Don't rely on this from your code"
    159         if self._storage_engine is None:
    160             cursor = self.connection.cursor()
    161             cursor.execute('CREATE TABLE INTROSPECT_TEST (X INT)')
    162             # This command is MySQL specific; the second column
    163             # will tell you the default table type of the created
    164             # table. Since all Django's test tables will have the same
    165             # table type, that's enough to evaluate the feature.
    166             cursor.execute("SHOW TABLE STATUS WHERE Name='INTROSPECT_TEST'")
    167             result = cursor.fetchone()
    168             cursor.execute('DROP TABLE INTROSPECT_TEST')
    169             self._storage_engine = result[1]
    170         return self._storage_engine
    171 
    172     def _can_introspect_foreign_keys(self):
     160        cursor = self.connection.cursor()
     161        cursor.execute('CREATE TABLE INTROSPECT_TEST (X INT)')
     162        # This command is MySQL specific; the second column
     163        # will tell you the default table type of the created
     164        # table. Since all Django's test tables will have the same
     165        # table type, that's enough to evaluate the feature.
     166        cursor.execute("SHOW TABLE STATUS WHERE Name='INTROSPECT_TEST'")
     167        result = cursor.fetchone()
     168        cursor.execute('DROP TABLE INTROSPECT_TEST')
     169        return result[1]
     170
     171    @cached_property
     172    def can_introspect_foreign_keys(self):
    173173        "Confirm support for introspected foreign keys"
    174174        return self._mysql_storage_engine() != 'MyISAM'
    175175
  • tests/regressiontests/transactions_regress/tests.py

    diff --git a/tests/regressiontests/transactions_regress/tests.py b/tests/regressiontests/transactions_regress/tests.py
    index 5972263..abd7a4c 100644
    a b class SavepointTest(TransactionTestCase):  
    208208        work()
    209209
    210210    @skipIf(connection.vendor == 'mysql' and \
    211             connection.features._mysql_storage_engine() == 'MyISAM',
     211            connection.features._mysql_storage_engine == 'MyISAM',
    212212            "MyISAM MySQL storage engine doesn't support savepoints")
    213213    @skipUnlessDBFeature('uses_savepoints')
    214214    def test_savepoint_rollback(self):
Back to Top