Skip to content

Commit ccd62a9

Browse files
authored
Merge pull request jamesls#314 from jamesls/kwargs-init
More closely match what init arguments are accepted
2 parents f753f81 + ef52a50 commit ccd62a9

File tree

1 file changed

+41
-49
lines changed

1 file changed

+41
-49
lines changed

fakeredis/_server.py

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1+
import functools
2+
import hashlib
3+
import inspect
4+
import itertools
15
import logging
2-
import time
3-
import threading
46
import math
7+
import pickle
8+
import queue
59
import random
610
import re
11+
import threading
12+
import time
713
import warnings
8-
import functools
9-
import itertools
10-
import hashlib
1114
import weakref
12-
import queue
13-
import pickle
1415
from collections import defaultdict
1516
from collections.abc import MutableMapping
1617

17-
import six
1818
import redis
19+
import six
1920

2021
from ._zset import ZSet
2122

22-
2323
LOGGER = logging.getLogger('fakeredis')
2424
REDIS_LOG_LEVELS = {
2525
b'LOG_DEBUG': 0,
@@ -2493,7 +2493,7 @@ def _lua_redis_log(self, lua_runtime, expected_globals, lvl, *args):
24932493

24942494
@command((bytes, Int), (bytes,), flags='s')
24952495
def eval(self, script, numkeys, *keys_and_args):
2496-
from lupa import LuaRuntime, LuaError, as_attrgetter
2496+
from lupa import LuaError, LuaRuntime, as_attrgetter
24972497

24982498
if numkeys > len(keys_and_args):
24992499
raise SimpleError(TOO_MANY_KEYS_MSG)
@@ -2741,65 +2741,57 @@ def repr_pieces(self):
27412741

27422742

27432743
class FakeRedisMixin:
2744-
def __init__(self, host='localhost', port=6379,
2745-
db=0, password=None, socket_timeout=None,
2746-
socket_connect_timeout=None,
2747-
socket_keepalive=None, socket_keepalive_options=None,
2748-
connection_pool=None, unix_socket_path=None,
2749-
encoding='utf-8', encoding_errors='strict',
2750-
charset=None, errors=None,
2751-
decode_responses=False, retry_on_timeout=False,
2752-
ssl=False, ssl_keyfile=None, ssl_certfile=None,
2753-
ssl_cert_reqs=None, ssl_ca_certs=None,
2754-
max_connections=None, server=None,
2755-
connected=True):
2756-
if not connection_pool:
2744+
def __init__(self, *args, server=None, connected=True, **kwargs):
2745+
# Interpret the positional and keyword arguments according to the
2746+
# version of redis in use.
2747+
sig = inspect.signature(redis.Redis)
2748+
bound = sig.bind(*args, **kwargs)
2749+
bound.apply_defaults()
2750+
if not bound.arguments['connection_pool']:
2751+
charset = bound.arguments['charset']
2752+
errors = bound.arguments['errors']
27572753
# Adapted from redis-py
27582754
if charset is not None:
27592755
warnings.warn(DeprecationWarning(
27602756
'"charset" is deprecated. Use "encoding" instead'))
2761-
encoding = charset
2757+
bound.arguments['encoding'] = charset
27622758
if errors is not None:
27632759
warnings.warn(DeprecationWarning(
27642760
'"errors" is deprecated. Use "encoding_errors" instead'))
2765-
encoding_errors = errors
2761+
bound.arguments['encoding_errors'] = errors
27662762

27672763
if server is None:
27682764
server = FakeServer()
27692765
server.connected = connected
27702766
kwargs = {
2771-
'db': db,
2772-
'password': password,
2773-
'encoding': encoding,
2774-
'encoding_errors': encoding_errors,
2775-
'decode_responses': decode_responses,
2776-
'max_connections': max_connections,
27772767
'connection_class': FakeConnection,
27782768
'server': server
27792769
}
2780-
connection_pool = redis.connection.ConnectionPool(**kwargs)
2781-
# These need to be passed by name due to
2782-
# https://github.com/andymccurdy/redis-py/issues/1276
2783-
super().__init__(
2784-
host=host, port=port, db=db, password=password, socket_timeout=socket_timeout,
2785-
socket_connect_timeout=socket_connect_timeout,
2786-
socket_keepalive=socket_keepalive,
2787-
socket_keepalive_options=socket_keepalive_options,
2788-
connection_pool=connection_pool,
2789-
unix_socket_path=unix_socket_path,
2790-
encoding=encoding, encoding_errors=encoding_errors,
2791-
charset=charset, errors=errors,
2792-
decode_responses=decode_responses, retry_on_timeout=retry_on_timeout,
2793-
ssl=ssl, ssl_keyfile=ssl_keyfile, ssl_certfile=ssl_certfile,
2794-
ssl_cert_reqs=ssl_cert_reqs, ssl_ca_certs=ssl_ca_certs,
2795-
max_connections=max_connections)
2770+
conn_pool_args = [
2771+
'db',
2772+
'username',
2773+
'password',
2774+
'socket_timeout',
2775+
'encoding',
2776+
'encoding_errors',
2777+
'decode_responses',
2778+
'retry_on_timeout',
2779+
'max_connections',
2780+
'health_check_interval',
2781+
'client_name'
2782+
]
2783+
for arg in conn_pool_args:
2784+
if arg in bound.arguments:
2785+
kwargs[arg] = bound.arguments[arg]
2786+
bound.arguments['connection_pool'] = redis.connection.ConnectionPool(**kwargs)
2787+
super().__init__(*bound.args, **bound.kwargs)
27962788

27972789
@classmethod
2798-
def from_url(cls, url, db=None, **kwargs):
2790+
def from_url(cls, *args, **kwargs):
27992791
server = kwargs.pop('server', None)
28002792
if server is None:
28012793
server = FakeServer()
2802-
self = super().from_url(url, db, **kwargs)
2794+
self = super().from_url(*args, **kwargs)
28032795
# Now override how it creates connections
28042796
pool = self.connection_pool
28052797
pool.connection_class = FakeConnection

0 commit comments

Comments
 (0)