0%

Fixing SyntaxWarning in python-memcached on Python 3.8

We had been using the python-memcached library for memcached access for a long time, and recently noticed warnings like the following under Python 3.8:

1
2
3
4
memcache.py:1303: SyntaxWarning: "is" with a literal. Did you mean "=="?
if key is '':
memcache.py:1304: SyntaxWarning: "is" with a literal. Did you mean "=="?
if key_extra_len is 0:

After checking the python-memcached source code, which is basically a single file, the problematic lines were easy to find:

1
2
if key is '':
if key_extra_len is 0:

That pattern is no longer acceptable in Python 3.8.

Even though it is “just” a warning, it is still noisy and unpleasant in logs, so I wanted to fix it.

I then checked the official GitHub repository and found that the project has not really been maintained for quite a while. Even though there is already a PR for this issue, it has not been merged:

https://github.com/linsomniac/python-memcached/issues/176

So there are basically two practical options:

  1. Patch memcache.py directly in your environment and replace is with == in those lines.
  2. Since we distribute a requirements.txt, I published a fixed package to PyPI, which you can install directly if needed:
1
pip install python-memcached-py38fix

At this point, moving to pymemcache is probably the more sensible long-term direction anyway, especially since Django has already dropped support for python-memcached.

如果我的文字帮到了您,那么可不可以请我喝罐可乐?