Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support newbytes from future #187

Merged
merged 1 commit into from
Sep 7, 2018
Merged

Fix support newbytes from future #187

merged 1 commit into from
Sep 7, 2018

Conversation

jogo
Copy link
Contributor

@jogo jogo commented Sep 7, 2018

Previously python2 code using python-future to backport the py3 bytes
behavior would trigger the following exception:

code:

from builtins import bytes as newbytes
from pymemcache.client.base import Client
client = Client(('localhost', 11211))
client.set(newbytes('key'), 'value')

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 297, in set
    return self._store_cmd(b'set', key, expire, noreply, value)
  File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 770, in _store_cmd
    key = self.check_key(key)
  File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 251, in check_key
    key_prefix=self.key_prefix)
  File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 91, in _check_key
    key = key.encode('ascii')
  File "/usr/local/lib/python2.7/site-packages/future/types/newbytes.py", line 381, in __getattribute__
    raise AttributeError("encode method has been disabled in newbytes")
AttributeError: encode method has been disabled in newbytes

Add a test case for this and fix.

Previously python2 code using python-future to backport the py3 bytes
behavior would trigger the following exception:

code:

  from builtins import bytes as newbytes
  from pymemcache.client.base import Client
  client = Client(('localhost', 11211))
  client.set(newbytes('key'), 'value')

  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 297, in set
      return self._store_cmd(b'set', key, expire, noreply, value)
    File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 770, in _store_cmd
      key = self.check_key(key)
    File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 251, in check_key
      key_prefix=self.key_prefix)
    File "/usr/local/lib/python2.7/site-packages/pymemcache/client/base.py", line 91, in _check_key
      key = key.encode('ascii')
    File "/usr/local/lib/python2.7/site-packages/future/types/newbytes.py", line 381, in __getattribute__
      raise AttributeError("encode method has been disabled in newbytes")
  AttributeError: encode method has been disabled in newbytes

Add a test case for this and fix.
Copy link
Collaborator

@nichochar nichochar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty for the tests

@@ -48,7 +48,10 @@ def get(self, key, default=None):
raise MemcacheIllegalInputError(key)
if isinstance(key, six.string_types):
try:
key = key.encode('ascii')
if isinstance(key, bytes):
key = key.decode().encode('ascii')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, do we think there is a performance impact to the decode and rencoding? Intuitively it seems negligeable

@jogo jogo merged commit 57e3e95 into pinterest:master Sep 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants