Skip to content

Commit 16a3606

Browse files
Delete the first-defined (and thus "duplicate") Script class (#3333)
The second definition was copied over the first definition, with the following changes: * The type annotations were copied to the second definition * The mutable default arguments to the `keys` and `args` parameters were replaced with `None`, as is best-practice. Closes #3332 Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com>
1 parent ce7a9a7 commit 16a3606

File tree

2 files changed

+20
-61
lines changed

2 files changed

+20
-61
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* Prevent async ClusterPipeline instances from becoming "false-y" in case of empty command stack (#3061)
6767
* Close Unix sockets if the connection attempt fails. This prevents `ResourceWarning`s. (#3314)
6868
* Close SSL sockets if the connection attempt fails, or if validations fail. (#3317)
69+
* Eliminate mutable default arguments in the `redis.commands.core.Script` class. (#3332)
6970

7071
* 4.1.3 (Feb 8, 2022)
7172
* Fix flushdb and flushall (#1926)

redis/commands/core.py

+19-61
Original file line numberDiff line numberDiff line change
@@ -5475,11 +5475,7 @@ def __init__(self, registered_client: "redis.client.Redis", script: ScriptTextT)
54755475
if isinstance(script, str):
54765476
# We need the encoding from the client in order to generate an
54775477
# accurate byte representation of the script
5478-
try:
5479-
encoder = registered_client.connection_pool.get_encoder()
5480-
except AttributeError:
5481-
# Cluster
5482-
encoder = registered_client.get_encoder()
5478+
encoder = self.get_encoder()
54835479
script = encoder.encode(script)
54845480
self.sha = hashlib.sha1(script).hexdigest()
54855481

@@ -5510,6 +5506,24 @@ def __call__(
55105506
self.sha = client.script_load(self.script)
55115507
return client.evalsha(self.sha, len(keys), *args)
55125508

5509+
def get_encoder(self):
5510+
"""Get the encoder to encode string scripts into bytes."""
5511+
try:
5512+
return self.registered_client.get_encoder()
5513+
except AttributeError:
5514+
# DEPRECATED
5515+
# In version <=4.1.2, this was the code we used to get the encoder.
5516+
# However, after 4.1.2 we added support for scripting in clustered
5517+
# redis. ClusteredRedis doesn't have a `.connection_pool` attribute
5518+
# so we changed the Script class to use
5519+
# `self.registered_client.get_encoder` (see above).
5520+
# However, that is technically a breaking change, as consumers who
5521+
# use Scripts directly might inject a `registered_client` that
5522+
# doesn't have a `.get_encoder` field. This try/except prevents us
5523+
# from breaking backward-compatibility. Ideally, it would be
5524+
# removed in the next major release.
5525+
return self.registered_client.connection_pool.get_encoder()
5526+
55135527

55145528
class AsyncScript:
55155529
"""
@@ -6293,62 +6307,6 @@ def command(self) -> ResponseT:
62936307
return self.execute_command("COMMAND")
62946308

62956309

6296-
class Script:
6297-
"""
6298-
An executable Lua script object returned by ``register_script``
6299-
"""
6300-
6301-
def __init__(self, registered_client, script):
6302-
self.registered_client = registered_client
6303-
self.script = script
6304-
# Precalculate and store the SHA1 hex digest of the script.
6305-
6306-
if isinstance(script, str):
6307-
# We need the encoding from the client in order to generate an
6308-
# accurate byte representation of the script
6309-
encoder = self.get_encoder()
6310-
script = encoder.encode(script)
6311-
self.sha = hashlib.sha1(script).hexdigest()
6312-
6313-
def __call__(self, keys=[], args=[], client=None):
6314-
"Execute the script, passing any required ``args``"
6315-
if client is None:
6316-
client = self.registered_client
6317-
args = tuple(keys) + tuple(args)
6318-
# make sure the Redis server knows about the script
6319-
from redis.client import Pipeline
6320-
6321-
if isinstance(client, Pipeline):
6322-
# Make sure the pipeline can register the script before executing.
6323-
client.scripts.add(self)
6324-
try:
6325-
return client.evalsha(self.sha, len(keys), *args)
6326-
except NoScriptError:
6327-
# Maybe the client is pointed to a different server than the client
6328-
# that created this instance?
6329-
# Overwrite the sha just in case there was a discrepancy.
6330-
self.sha = client.script_load(self.script)
6331-
return client.evalsha(self.sha, len(keys), *args)
6332-
6333-
def get_encoder(self):
6334-
"""Get the encoder to encode string scripts into bytes."""
6335-
try:
6336-
return self.registered_client.get_encoder()
6337-
except AttributeError:
6338-
# DEPRECATED
6339-
# In version <=4.1.2, this was the code we used to get the encoder.
6340-
# However, after 4.1.2 we added support for scripting in clustered
6341-
# redis. ClusteredRedis doesn't have a `.connection_pool` attribute
6342-
# so we changed the Script class to use
6343-
# `self.registered_client.get_encoder` (see above).
6344-
# However, that is technically a breaking change, as consumers who
6345-
# use Scripts directly might inject a `registered_client` that
6346-
# doesn't have a `.get_encoder` field. This try/except prevents us
6347-
# from breaking backward-compatibility. Ideally, it would be
6348-
# removed in the next major release.
6349-
return self.registered_client.connection_pool.get_encoder()
6350-
6351-
63526310
class AsyncModuleCommands(ModuleCommands):
63536311
async def command_info(self) -> None:
63546312
return super().command_info()

0 commit comments

Comments
 (0)