|
| 1 | +import functools |
| 2 | +import hashlib |
| 3 | +import inspect |
| 4 | +import itertools |
1 | 5 | import logging |
2 | | -import time |
3 | | -import threading |
4 | 6 | import math |
| 7 | +import pickle |
| 8 | +import queue |
5 | 9 | import random |
6 | 10 | import re |
| 11 | +import threading |
| 12 | +import time |
7 | 13 | import warnings |
8 | | -import functools |
9 | | -import itertools |
10 | | -import hashlib |
11 | 14 | import weakref |
12 | | -import queue |
13 | | -import pickle |
14 | 15 | from collections import defaultdict |
15 | 16 | from collections.abc import MutableMapping |
16 | 17 |
|
17 | | -import six |
18 | 18 | import redis |
| 19 | +import six |
19 | 20 |
|
20 | 21 | from ._zset import ZSet |
21 | 22 |
|
22 | | - |
23 | 23 | LOGGER = logging.getLogger('fakeredis') |
24 | 24 | REDIS_LOG_LEVELS = { |
25 | 25 | b'LOG_DEBUG': 0, |
@@ -2493,7 +2493,7 @@ def _lua_redis_log(self, lua_runtime, expected_globals, lvl, *args): |
2493 | 2493 |
|
2494 | 2494 | @command((bytes, Int), (bytes,), flags='s') |
2495 | 2495 | def eval(self, script, numkeys, *keys_and_args): |
2496 | | - from lupa import LuaRuntime, LuaError, as_attrgetter |
| 2496 | + from lupa import LuaError, LuaRuntime, as_attrgetter |
2497 | 2497 |
|
2498 | 2498 | if numkeys > len(keys_and_args): |
2499 | 2499 | raise SimpleError(TOO_MANY_KEYS_MSG) |
@@ -2741,65 +2741,57 @@ def repr_pieces(self): |
2741 | 2741 |
|
2742 | 2742 |
|
2743 | 2743 | 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'] |
2757 | 2753 | # Adapted from redis-py |
2758 | 2754 | if charset is not None: |
2759 | 2755 | warnings.warn(DeprecationWarning( |
2760 | 2756 | '"charset" is deprecated. Use "encoding" instead')) |
2761 | | - encoding = charset |
| 2757 | + bound.arguments['encoding'] = charset |
2762 | 2758 | if errors is not None: |
2763 | 2759 | warnings.warn(DeprecationWarning( |
2764 | 2760 | '"errors" is deprecated. Use "encoding_errors" instead')) |
2765 | | - encoding_errors = errors |
| 2761 | + bound.arguments['encoding_errors'] = errors |
2766 | 2762 |
|
2767 | 2763 | if server is None: |
2768 | 2764 | server = FakeServer() |
2769 | 2765 | server.connected = connected |
2770 | 2766 | 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, |
2777 | 2767 | 'connection_class': FakeConnection, |
2778 | 2768 | 'server': server |
2779 | 2769 | } |
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) |
2796 | 2788 |
|
2797 | 2789 | @classmethod |
2798 | | - def from_url(cls, url, db=None, **kwargs): |
| 2790 | + def from_url(cls, *args, **kwargs): |
2799 | 2791 | server = kwargs.pop('server', None) |
2800 | 2792 | if server is None: |
2801 | 2793 | server = FakeServer() |
2802 | | - self = super().from_url(url, db, **kwargs) |
| 2794 | + self = super().from_url(*args, **kwargs) |
2803 | 2795 | # Now override how it creates connections |
2804 | 2796 | pool = self.connection_pool |
2805 | 2797 | pool.connection_class = FakeConnection |
|
0 commit comments