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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pymemcache/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def _check_key(key, allow_unicode_keys, key_prefix=b''):
key = key.encode('utf8')
elif isinstance(key, VALID_STRING_TYPES):
try:
key = key.encode('ascii')
if isinstance(key, bytes):
key = key.decode().encode('ascii')
else:
key = key.encode('ascii')
except (UnicodeEncodeError, UnicodeDecodeError):
raise MemcacheIllegalInputError("Non-ASCII key: '%r'" % key)

Expand Down
7 changes: 7 additions & 0 deletions pymemcache/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from builtins import bytes as newbytes

import collections
import errno
import functools
Expand Down Expand Up @@ -100,6 +102,11 @@ def test_set_success(self):
result = client.set(b'key', b'value', noreply=False)
assert result is True

def test_set_future(self):
client = self.make_client([b'STORED\r\n'])
result = client.set(newbytes(b'key'), newbytes(b'value'), noreply=False)
assert result is True

def test_set_unicode_key(self):
client = self.make_client([b''])

Expand Down
14 changes: 14 additions & 0 deletions pymemcache/test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from builtins import bytes as newbytes

from collections import defaultdict
import json
import pytest
Expand Down Expand Up @@ -60,6 +62,18 @@ def test_get_set(client_class, host, port, socket_module):
get_set_helper(client, key, value, key2, value2)


@pytest.mark.integration()
def test_get_set_newbytes(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

key = newbytes(b'key3')
value = b'value3'
key2 = newbytes(b'key4')
value2 = b'value4'
get_set_helper(client, key, value, key2, value2)


@pytest.mark.integration()
def test_get_set_unicode_key(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module,
Expand Down
10 changes: 8 additions & 2 deletions pymemcache/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

else:
key = key.encode('ascii')
except (UnicodeEncodeError, UnicodeDecodeError):
raise MemcacheIllegalInputError

Expand Down Expand Up @@ -80,7 +83,10 @@ def set(self, key, value, expire=0, noreply=True):
raise MemcacheIllegalInputError(key)
if isinstance(key, six.string_types):
try:
key = key.encode('ascii')
if isinstance(key, bytes):
key = key.decode().encode('ascii')
else:
key = key.encode('ascii')
except (UnicodeEncodeError, UnicodeDecodeError):
raise MemcacheIllegalInputError
if isinstance(value, six.text_type):
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pytest-cov
gevent==1.3.6; "PyPy" not in platform_python_implementation
pylibmc
python-memcached
future