From 16e14c92192f22269dcbedb73a48be8e7c1204ad Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Sat, 11 Apr 2015 01:57:13 +0200 Subject: [PATCH] Drops Django 1.6 & Python 2.6 support. --- benchmark.py | 3 +- cachalot/__init__.py | 2 ++ cachalot/apps.py | 13 +++++++++ cachalot/cache.py | 5 ++-- cachalot/models.py | 5 ---- cachalot/monkey_patch.py | 28 ++---------------- cachalot/tests/api.py | 5 +--- cachalot/tests/multi_db.py | 5 +--- cachalot/tests/settings.py | 5 +--- cachalot/tests/write.py | 11 ++----- cachalot/utils.py | 12 ++------ docs/quickstart.rst | 8 ++--- requirements.txt | 2 +- runtests.py | 3 +- runtests_requirements_py2.txt | 3 -- runtests_requirements_py3.txt | 1 - settings.py | 55 +++++++++++++++-------------------- setup.py | 1 - 18 files changed, 58 insertions(+), 109 deletions(-) create mode 100644 cachalot/apps.py diff --git a/benchmark.py b/benchmark.py index ef32d96..af121b6 100755 --- a/benchmark.py +++ b/benchmark.py @@ -261,8 +261,7 @@ def create_data(using): if __name__ == '__main__': - if django.VERSION[:2] >= (1, 7): - django.setup() + django.setup() write_conditions() diff --git a/cachalot/__init__.py b/cachalot/__init__.py index 119f8df..edcab8b 100644 --- a/cachalot/__init__.py +++ b/cachalot/__init__.py @@ -1,2 +1,4 @@ __version__ = (1, 0, 0) version_string = '.'.join(str(n) for n in __version__) + +default_app_config = 'cachalot.apps.CachalotConfig' diff --git a/cachalot/apps.py b/cachalot/apps.py new file mode 100644 index 0000000..8efc520 --- /dev/null +++ b/cachalot/apps.py @@ -0,0 +1,13 @@ +from django.apps import AppConfig + +from .monkey_patch import patch + + +class CachalotConfig(AppConfig): + name = 'cachalot' + patched = False + + def ready(self): + if not self.patched: + patch() + self.patched = True diff --git a/cachalot/cache.py b/cachalot/cache.py index 3b098f1..2cbc0c3 100644 --- a/cachalot/cache.py +++ b/cachalot/cache.py @@ -3,8 +3,7 @@ from __future__ import unicode_literals from threading import local -# TODO: Replace with caches[CACHALOT_CACHE] when we drop Django 1.6 support. -from django.core.cache import get_cache as get_django_cache +from django.core.cache import caches from django.db import connections from .settings import cachalot_settings @@ -31,7 +30,7 @@ class CacheHandler(local): min_level = -len(self.atomic_caches) if atomic_level < min_level: - return get_django_cache(cache_alias) + return caches[cache_alias] return self.get_atomic_cache(cache_alias, atomic_level) def enter_atomic(self): diff --git a/cachalot/models.py b/cachalot/models.py index 543e8b2..e69de29 100644 --- a/cachalot/models.py +++ b/cachalot/models.py @@ -1,5 +0,0 @@ -from .monkey_patch import is_patched, patch - - -if not is_patched(): - patch() diff --git a/cachalot/monkey_patch.py b/cachalot/monkey_patch.py index 1013617..31d6d38 100644 --- a/cachalot/monkey_patch.py +++ b/cachalot/monkey_patch.py @@ -5,17 +5,9 @@ from collections import Iterable from functools import wraps from time import time -from django import VERSION as django_version -from django.conf import settings -if django_version >= (1, 7): - from django.db.backends.utils import CursorWrapper -else: - from django.db.backends.util import CursorWrapper +from django.db.backends.utils import CursorWrapper from django.db.models.query import EmptyResultSet -if django_version >= (1, 7): - from django.db.models.signals import post_migrate -else: - from django.db.models.signals import post_syncdb as post_migrate +from django.db.models.signals import post_migrate from django.db.models.sql.compiler import ( SQLCompiler, SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler) from django.db.transaction import Atomic, get_connection @@ -32,13 +24,6 @@ from .utils import ( WRITE_COMPILERS = (SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler) -PATCHED = False - - -def is_patched(): - return PATCHED - - def _unset_raw_connection(original): def inner(compiler, *args, **kwargs): compiler.connection.raw = False @@ -178,21 +163,14 @@ def _patch_tests(): def _invalidate_on_migration(sender, **kwargs): - db_alias = kwargs['using'] if django_version >= (1, 7) else kwargs['db'] + db_alias = kwargs['using'] invalidate_all(db_alias=db_alias) def patch(): - global PATCHED - post_migrate.connect(_invalidate_on_migration) - if 'south' in settings.INSTALLED_APPS: - from south.signals import post_migrate as south_post_migrate - south_post_migrate.connect(_invalidate_on_migration) _patch_cursor() _patch_tests() _patch_atomic() _patch_orm() - - PATCHED = True diff --git a/cachalot/tests/api.py b/cachalot/tests/api.py index f31a04e..dfce28b 100644 --- a/cachalot/tests/api.py +++ b/cachalot/tests/api.py @@ -1,10 +1,7 @@ # coding: utf-8 from __future__ import unicode_literals -try: - from unittest import skipIf -except ImportError: # For Python 2.6 - from unittest2 import skipIf +from unittest import skipIf from django.conf import settings from django.contrib.auth.models import User diff --git a/cachalot/tests/multi_db.py b/cachalot/tests/multi_db.py index 2fa7eb6..b7dd3af 100644 --- a/cachalot/tests/multi_db.py +++ b/cachalot/tests/multi_db.py @@ -1,10 +1,7 @@ # coding: utf-8 from __future__ import unicode_literals -try: - from unittest import skipIf -except ImportError: # For Python 2.6 - from unittest2 import skipIf +from unittest import skipIf from django.conf import settings from django.db import DEFAULT_DB_ALIAS, connections diff --git a/cachalot/tests/settings.py b/cachalot/tests/settings.py index 064566f..f640ffb 100644 --- a/cachalot/tests/settings.py +++ b/cachalot/tests/settings.py @@ -1,10 +1,7 @@ # coding: utf-8 from __future__ import unicode_literals -try: - from unittest import skipIf -except ImportError: # For Python 2.6 - from unittest2 import skipIf +from unittest import skipIf from django.conf import settings from django.core.cache import DEFAULT_CACHE_ALIAS diff --git a/cachalot/tests/write.py b/cachalot/tests/write.py index 04be171..4b4990f 100644 --- a/cachalot/tests/write.py +++ b/cachalot/tests/write.py @@ -1,12 +1,7 @@ # coding: utf-8 from __future__ import unicode_literals -try: - from unittest import skipIf -except ImportError: # For Python 2.6 - from unittest2 import skipIf -from django import VERSION as django_version from django.contrib.auth.models import User, Permission, Group from django.core.exceptions import MultipleObjectsReturned from django.core.management import call_command @@ -79,8 +74,6 @@ class WriteTestCase(TransactionTestCase): data2 = list(Test.objects.all()) self.assertListEqual(data2, [t]) - @skipIf(django_version < (1, 7), - 'QuerySet.update_or_create is not implemented in Django < 1.7') def test_update_or_create(self): with self.assertNumQueries(1): self.assertListEqual(list(Test.objects.all()), []) @@ -821,7 +814,7 @@ class DatabaseCommandTestCase(TransactionTestCase): call_command('flush', verbosity=0, interactive=False) - if django_version >= (1, 7) and connection.vendor == 'mysql': + if connection.vendor == 'mysql': # We need to reopen the connection or Django # will execute an extra SQL request below. connection.cursor() @@ -836,7 +829,7 @@ class DatabaseCommandTestCase(TransactionTestCase): call_command('loaddata', 'cachalot/tests/loaddata_fixture.json', verbosity=0, interactive=False) - if django_version >= (1, 7) and connection.vendor == 'mysql': + if connection.vendor == 'mysql': # We need to reopen the connection or Django # will execute an extra SQL request below. connection.cursor() diff --git a/cachalot/utils.py b/cachalot/utils.py index 151b3b8..02948c3 100644 --- a/cachalot/utils.py +++ b/cachalot/utils.py @@ -4,14 +4,9 @@ from __future__ import unicode_literals from hashlib import sha1 from time import time -import django from django.db import connections from django.db.models.sql.where import ExtraWhere, SubqueryConstraint -DJANGO_GTE_1_7 = django.VERSION[:2] >= (1, 7) -if DJANGO_GTE_1_7: - from django.utils.module_loading import import_string -else: - from django.utils.module_loading import import_by_path as import_string +from django.utils.module_loading import import_string from django.utils.six import PY3 from .settings import cachalot_settings @@ -87,9 +82,8 @@ def _find_subqueries(children): yield child.query_object.query else: rhs = None - if DJANGO_GTE_1_7: - if hasattr(child, 'rhs'): - rhs = child.rhs + if hasattr(child, 'rhs'): + rhs = child.rhs elif isinstance(child, tuple): rhs = child[-1] if hasattr(rhs, 'query'): diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 29f17ba..fd082d4 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -4,16 +4,14 @@ Quick start Requirements ............ -- Django 1.6 or 1.7 -- Python 2.6, 2.7, 3.2, 3.3, or 3.4 +- Django 1.7 or 1.8 +- Python 2.7, 3.2, 3.3, or 3.4 - a cache configured as ``'default'`` with one of these backends: - `django-redis `_ - `memcached `_ - (using either python-memcached or pylibmc (but pylibmc is only supported - with Django >= 1.7)) + (using either python-memcached or pylibmc) - `filebased `_ - (only with Django >= 1.7 as it was not thread-safe before) - `locmem `_ (but it’s not shared between processes, see :ref:`Limits`) diff --git a/requirements.txt b/requirements.txt index d08919c..e38f742 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -Django>=1.6 +Django>=1.7 diff --git a/runtests.py b/runtests.py index 02256b6..08718be 100755 --- a/runtests.py +++ b/runtests.py @@ -9,8 +9,7 @@ import django if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') - if django.VERSION[:2] >= (1, 7): - django.setup() + django.setup() from django.test.runner import DiscoverRunner test_runner = DiscoverRunner(verbosity=2) failures = test_runner.run_tests(['cachalot.tests']) diff --git a/runtests_requirements_py2.txt b/runtests_requirements_py2.txt index 41e21e0..6f526e1 100644 --- a/runtests_requirements_py2.txt +++ b/runtests_requirements_py2.txt @@ -5,6 +5,3 @@ MySQL-python django-redis python-memcached pylibmc -South -# For Python 2.6 -unittest2 diff --git a/runtests_requirements_py3.txt b/runtests_requirements_py3.txt index 5934344..eaa485d 100644 --- a/runtests_requirements_py3.txt +++ b/runtests_requirements_py3.txt @@ -4,4 +4,3 @@ psycopg2 https://github.com/clelland/MySQL-for-Python-3/tarball/master django-redis python3-memcached -South diff --git a/settings.py b/settings.py index c5f836b..fa4b803 100644 --- a/settings.py +++ b/settings.py @@ -3,8 +3,6 @@ from __future__ import unicode_literals import os -import django - DATABASES = { 'sqlite3': { @@ -26,26 +24,12 @@ DATABASES = { } for alias in DATABASES: test_db_name = 'test_' + DATABASES[alias]['NAME'] - if django.VERSION < (1, 7): - DATABASES[alias]['TEST_NAME'] = test_db_name - else: - DATABASES[alias]['TEST'] = {'NAME': test_db_name} + DATABASES[alias]['TEST'] = {'NAME': test_db_name} DATABASES['default'] = DATABASES.pop(os.environ.get('DB_ENGINE', 'sqlite3')) CACHES = { - 'locmem': { - 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', - 'OPTIONS': { - # We want that limit to be infinite, otherwise we can’t - # reliably count the number of SQL queries executed in tests. - - # In this context, 10e9 is enough to be considered - # infinite. - 'MAX_ENTRIES': 10e9, - } - }, 'redis': { 'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'redis://127.0.0.1:6379/0', @@ -60,24 +44,35 @@ CACHES = { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', }, -} -if django.VERSION >= (1, 7): - CACHES['filebased'] = { + 'locmem': { + 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', + 'OPTIONS': { + # We want that limit to be infinite, otherwise we can’t + # reliably count the number of SQL queries executed in tests. + + # In this context, 10e9 is enough to be considered + # infinite. + 'MAX_ENTRIES': 10e9, + } + }, + 'filebased': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/tmp/django_cache', 'OPTIONS': { 'MAX_ENTRIES': 10e9, # (See locmem) } } - try: - import pylibmc - except ImportError: - pass - else: - CACHES['pylibmc'] = { - 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', - 'LOCATION': '127.0.0.1:11211', - } +} + +try: + import pylibmc +except ImportError: + pass +else: + CACHES['pylibmc'] = { + 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', + 'LOCATION': '127.0.0.1:11211', + } DEFAULT_CACHE_ALIAS = os.environ.get('CACHE_BACKEND', 'locmem') CACHES['default'] = CACHES.pop(DEFAULT_CACHE_ALIAS) @@ -92,8 +87,6 @@ INSTALLED_APPS = [ 'django.contrib.auth', 'django.contrib.contenttypes', ] -if django.VERSION < (1, 7): - INSTALLED_APPS.append('south') MIGRATION_MODULES = { diff --git a/setup.py b/setup.py index e34ff17..13cfb8a 100755 --- a/setup.py +++ b/setup.py @@ -27,7 +27,6 @@ setup( 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.2',