From 33e0d0b14a44cfe0f321e8ffd4934c850a51c5a6 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Tue, 30 Jul 2024 14:49:50 -0700 Subject: [PATCH] common.memcache_key: encode Unicode chars as UTF-8 --- common.py | 2 +- tests/test_common.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/common.py b/common.py index e48f8f15..b498128e 100644 --- a/common.py +++ b/common.py @@ -412,4 +412,4 @@ def memcache_key(key): TODO: truncate to 250 *UTF-8* chars, to handle Unicode chars in URLs. Related: pymemcache Client's allow_unicode_keys constructor kwarg. """ - return key[:MEMCACHE_KEY_MAX_LEN].replace(' ', '%20') + return key[:MEMCACHE_KEY_MAX_LEN].replace(' ', '%20').encode() diff --git a/tests/test_common.py b/tests/test_common.py index c52c7c9b..85ddecae 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -152,8 +152,9 @@ class CommonTest(TestCase): @patch('common.MEMCACHE_KEY_MAX_LEN', new=10) def test_memcache_key(self): for input, expected in ( - ('foo', 'foo'), - ('foo-bar-baz', 'foo-bar-ba'), - ('foo bar', 'foo%20bar'), + ('foo', b'foo'), + ('foo-bar-baz', b'foo-bar-ba'), + ('foo bar', b'foo%20bar'), + ('☃.net', b'\xe2\x98\x83.net'), ): self.assertEqual(expected, common.memcache_key(input))