Skip to content

Commit

Permalink
Merge pull request #471 from iurisilvio/store-string-as-string
Browse files Browse the repository at this point in the history
Check if value can be encoded but preserve original value.
  • Loading branch information
jogo committed Aug 3, 2023
2 parents 9dbd174 + 3edb4c1 commit b51350a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pymemcache/test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from pymemcache.serde import pickle_serde
from pymemcache.test.utils import MockMemcacheClient


Expand All @@ -12,6 +13,26 @@ def test_get_set():
assert client.get(b"hello") == 12


@pytest.mark.unit()
def test_get_set_string():
client = MockMemcacheClient()
assert client.get("hello") is None

value = "world"
client.set("hello", value)
assert client.get("hello") == b"world"


@pytest.mark.unit()
def test_get_set_string_with_serde():
client = MockMemcacheClient(serde=pickle_serde)
assert client.get("hello") is None

value = "world"
client.set("hello", value)
assert client.get("hello") == value


@pytest.mark.unit()
def test_get_set_unicide_key():
client = MockMemcacheClient()
Expand Down
10 changes: 9 additions & 1 deletion pymemcache/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def __init__(
):
self._contents = {}

def _serializer(key, value):
if isinstance(value, str):
value = value.encode()
return value, 0

if serializer is None:
serializer = _serializer

self.serde = serde or LegacyWrappingSerde(serializer, deserializer)
self.allow_unicode_keys = allow_unicode_keys

Expand Down Expand Up @@ -87,7 +95,7 @@ def set(self, key, value, expire=0, noreply=True, flags=None):
key = self.check_key(key)
if isinstance(value, str) and not isinstance(value, bytes):
try:
value = value.encode(self.encoding)
value.encode(self.encoding)
except (UnicodeEncodeError, UnicodeDecodeError):
raise MemcacheIllegalInputError

Expand Down

0 comments on commit b51350a

Please sign in to comment.