Skip to content

Commit 58b28e4

Browse files
authored
Add support for EVAL_RO (#1862)
* add sort_ro * mark test as onlynon cluster * delete mark test as onlynoncluster * add eval_ro * fix linters * delete sort_ro * fix pr comment * add type hints * add type hints * linters
1 parent e37ebd5 commit 58b28e4

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

redis/commands/core.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,7 +3917,12 @@ class ScriptCommands:
39173917
https://redis.com/ebook/part-3-next-steps/chapter-11-scripting-redis-with-lua/
39183918
"""
39193919

3920-
def eval(self, script, numkeys, *keys_and_args):
3920+
def _eval(
3921+
self, command: str, script: str, numkeys: int, *keys_and_args: list
3922+
) -> str:
3923+
return self.execute_command(command, script, numkeys, *keys_and_args)
3924+
3925+
def eval(self, script: str, numkeys: int, *keys_and_args: list) -> str:
39213926
"""
39223927
Execute the Lua ``script``, specifying the ``numkeys`` the script
39233928
will touch and the key names and argument values in ``keys_and_args``.
@@ -3928,7 +3933,19 @@ def eval(self, script, numkeys, *keys_and_args):
39283933
39293934
For more information check https://redis.io/commands/eval
39303935
"""
3931-
return self.execute_command("EVAL", script, numkeys, *keys_and_args)
3936+
return self._eval("EVAL", script, numkeys, *keys_and_args)
3937+
3938+
def eval_ro(self, script: str, numkeys: int, *keys_and_args: list) -> str:
3939+
"""
3940+
The read-only variant of the EVAL command
3941+
3942+
Execute the read-only Lue ``script`` specifying the ``numkeys`` the script
3943+
will touch and the key names and argument values in ``keys_and_args``.
3944+
Returns the result of the script.
3945+
3946+
For more information check https://redis.io/commands/eval_ro
3947+
"""
3948+
return self._eval("EVAL_RO", script, numkeys, *keys_and_args)
39323949

39333950
def evalsha(self, sha, numkeys, *keys_and_args):
39343951
"""

tests/test_scripting.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
import redis
34
from redis import exceptions
45
from tests.conftest import skip_if_server_version_lt
56

@@ -31,6 +32,13 @@ def test_eval(self, r):
3132
# 2 * 3 == 6
3233
assert r.eval(multiply_script, 1, "a", 3) == 6
3334

35+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
36+
def test_eval_ro(self, unstable_r):
37+
unstable_r.set("a", "b")
38+
assert unstable_r.eval_ro("return redis.call('GET', KEYS[1])", 1, "a") == b"b"
39+
with pytest.raises(redis.ResponseError):
40+
unstable_r.eval_ro("return redis.call('DEL', KEYS[1])", 1, "a")
41+
3442
@skip_if_server_version_lt("6.2.0")
3543
def test_script_flush_620(self, r):
3644
r.set("a", 2)

0 commit comments

Comments
 (0)