Skip to content

Commit cb4d8a7

Browse files
committed
Delete the first-defined (and thus "duplicate") Script class
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
1 parent fb74aa2 commit cb4d8a7

File tree

2 files changed

+20
-61
lines changed

2 files changed

+20
-61
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
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

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5472,11 +5472,7 @@ def __init__(self, registered_client: "Redis", script: ScriptTextT):
54725472
if isinstance(script, str):
54735473
# We need the encoding from the client in order to generate an
54745474
# accurate byte representation of the script
5475-
try:
5476-
encoder = registered_client.connection_pool.get_encoder()
5477-
except AttributeError:
5478-
# Cluster
5479-
encoder = registered_client.get_encoder()
5475+
encoder = self.get_encoder()
54805476
script = encoder.encode(script)
54815477
self.sha = hashlib.sha1(script).hexdigest()
54825478

@@ -5507,6 +5503,24 @@ def __call__(
55075503
self.sha = client.script_load(self.script)
55085504
return client.evalsha(self.sha, len(keys), *args)
55095505

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

55115525
class AsyncScript:
55125526
"""
@@ -6283,62 +6297,6 @@ def command(self) -> ResponseT:
62836297
return self.execute_command("COMMAND")
62846298

62856299

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

0 commit comments

Comments
 (0)