Opened 4 years ago

Last modified 4 years ago

#31734 closed Bug

warnings.warn() fails when looping over memcache_key_warnings() — at Version 2

Reported by: Rich Rauenzahn Owned by: nobody
Component: Core (Cache system) Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Rich Rauenzahn)

I think I found a bug in cache backends -- while running unit tests I was getting:

TypeError: 'type' object cannot be interpreted as an integer

I'm not sure why that exact message, but it arises here:

> .../virtualenv-3.6.8/lib64/python3.6/site-packages/django/core/cache/backends/base.py(250)validate_key()
    248         """
    249         for warning in memcache_key_warnings(key):
--> 250             warnings.warn(warning, CacheKeyWarning)
    251 
    252     def incr_version(self, key, delta=1, version=None):

I think the problem is that for loop is receiving a list of tuples, but they are interpreted as a list of strings:

ipdb> key
":1:<redacted but probably invalid for memcache"

# The generator returns tuples of (message, warning type)

ipdb> for x in memcache_key_warnings(key): print(x)
('Cache key contains characters that will cause errors if used with memcached: ":1:BuildAPI._request:(\'http://buildapi.eng.vmware.com/sb/build/1895757\', expire=10)"', <class 'django.core.cache.backends.base.CacheKeyWarning'>)

# But "warning" passed to warnings.warn() is the tuple...  

ipdb> warning
('Cache key contains characters that will cause errors if used with memcached: ":1:BuildAPI._request:(\'http://buildapi.eng.vmware.com/sb/build/1895757\', expire=10)"', <class 'django.core.cache.backends.base.CacheKeyWarning'>)
ipdb>


I think it should be passing warnings.warn(warning[0], warning[1]) or warnings.warn(*warning)

Django 2.2.13 is what I am using.

Change History (2)

comment:1 by Rich Rauenzahn, 4 years ago

I made the change locally (i.e., warnings.warn(*warning)) and now properly get:

ipdb> 
virtualenv-3.6.8/lib64/python3.6/site-packages/django/core/cache/backends/base.py:250: CacheKeyWarning: Cache key contains characters that will cause errors if used with memcached: ":1:<redacted but invalid>"
  warnings.warn(*warning)

Last edited 4 years ago by Rich Rauenzahn (previous) (diff)

comment:2 by Rich Rauenzahn, 4 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top