Kaydet (Commit) 90ce5d46 authored tarafından Przemysław Suliga's avatar Przemysław Suliga Kaydeden (comit) Tim Graham

Fixed #26462 -- Fixed Python 2 UnicodeEncodeError when warning about long cache keys.

üst 369fa471
...@@ -235,7 +235,7 @@ class BaseCache(object): ...@@ -235,7 +235,7 @@ class BaseCache(object):
""" """
if len(key) > MEMCACHE_MAX_KEY_LENGTH: if len(key) > MEMCACHE_MAX_KEY_LENGTH:
warnings.warn('Cache key will cause errors if used with memcached: ' warnings.warn('Cache key will cause errors if used with memcached: '
'%s (longer than %s)' % (key, MEMCACHE_MAX_KEY_LENGTH), '%r (longer than %s)' % (key, MEMCACHE_MAX_KEY_LENGTH),
CacheKeyWarning) CacheKeyWarning)
for char in key: for char in key:
if ord(char) < 33 or ord(char) == 127: if ord(char) < 33 or ord(char) == 127:
......
...@@ -585,15 +585,31 @@ class BaseCacheTests(object): ...@@ -585,15 +585,31 @@ class BaseCacheTests(object):
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always") warnings.simplefilter("always")
# memcached does not allow whitespace or control characters in keys # memcached does not allow whitespace or control characters in keys
cache.set('key with spaces', 'value') key = 'key with spaces and 清'
cache.set(key, 'value')
self.assertEqual(len(w), 1) self.assertEqual(len(w), 1)
self.assertIsInstance(w[0].message, CacheKeyWarning) self.assertIsInstance(w[0].message, CacheKeyWarning)
self.assertEqual(
# warnings.warn() crashes on Python 2 if message isn't
# coercible to str.
str(w[0].message.args[0]),
"Cache key contains characters that will cause errors if used "
"with memcached: %r" % key,
)
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always") warnings.simplefilter("always")
# memcached limits key length to 250 # memcached limits key length to 250
cache.set('a' * 251, 'value') key = ('a' * 250) + '清'
cache.set(key, 'value')
self.assertEqual(len(w), 1) self.assertEqual(len(w), 1)
self.assertIsInstance(w[0].message, CacheKeyWarning) self.assertIsInstance(w[0].message, CacheKeyWarning)
self.assertEqual(
# warnings.warn() crashes on Python 2 if message isn't
# coercible to str.
str(w[0].message.args[0]),
'Cache key will cause errors if used with memcached: '
'%r (longer than %s)' % (key, 250),
)
finally: finally:
cache.key_func = old_func cache.key_func = old_func
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment