Skip to content

Commit

Permalink
use importlib.util.find_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
wongcht authored and sergeyklay committed Aug 27, 2023
1 parent 5fe1c7f commit 638720c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions environ/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

"""This module handles import compatibility issues."""

from pkgutil import find_loader
from importlib.util import find_spec

if find_loader('simplejson'):
if find_spec('simplejson'):
import simplejson as json
else:
import json

if find_loader('django'):
if find_spec('django'):
from django import VERSION as DJANGO_VERSION
from django.core.exceptions import ImproperlyConfigured
else:
Expand All @@ -29,7 +29,7 @@ def choose_rediscache_driver():
"""Backward compatibility for RedisCache driver."""

# django-redis library takes precedence
if find_loader('django_redis'):
if find_spec('django_redis'):
return 'django_redis.cache.RedisCache'

# use built-in support if Django 4+
Expand All @@ -51,7 +51,7 @@ def choose_postgres_driver():
def choose_pymemcache_driver():
"""Backward compatibility for pymemcache."""
old_django = DJANGO_VERSION is not None and DJANGO_VERSION < (3, 2)
if old_django or not find_loader('pymemcache'):
if old_django or not find_spec('pymemcache'):
# The original backend choice for the 'pymemcache' scheme is
# unfortunately 'pylibmc'.
return 'django.core.cache.backends.memcached.PyLibMCCache'
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_pymemcache_compat(django_version, pymemcache_installed):
old = 'django.core.cache.backends.memcached.PyLibMCCache'
new = 'django.core.cache.backends.memcached.PyMemcacheCache'
with mock.patch.object(environ.compat, 'DJANGO_VERSION', django_version):
with mock.patch('environ.compat.find_loader') as mock_find_loader:
mock_find_loader.return_value = pymemcache_installed
with mock.patch('environ.compat.find_spec') as mock_find_spec:
mock_find_spec.return_value = pymemcache_installed
driver = environ.compat.choose_pymemcache_driver()
if django_version and django_version < (3, 2):
assert driver == old
Expand All @@ -124,8 +124,8 @@ def test_rediscache_compat(django_version, django_redis_installed):
django_redis = 'django_redis.cache.RedisCache'

with mock.patch.object(environ.compat, 'DJANGO_VERSION', django_version):
with mock.patch('environ.compat.find_loader') as mock_find_loader:
mock_find_loader.return_value = django_redis_installed
with mock.patch('environ.compat.find_spec') as mock_find_spec:
mock_find_spec.return_value = django_redis_installed
driver = environ.compat.choose_rediscache_driver()
if django_redis_installed:
assert driver == django_redis
Expand Down

0 comments on commit 638720c

Please sign in to comment.