From 0e0c1400b57d5de8aa8c18dd4897527f905a4b42 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Sat, 1 Apr 2017 02:36:46 -0400 Subject: [PATCH] 11308: Fix bytes vs. str problems. --- sdk/python/arvados/keep.py | 20 ++++++++++++-------- sdk/python/tests/test_arv_put.py | 5 ++++- sdk/python/tests/test_arvfile.py | 2 +- sdk/python/tests/test_cache.py | 4 ++-- sdk/python/tests/test_keep_client.py | 4 ++-- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/sdk/python/arvados/keep.py b/sdk/python/arvados/keep.py index f680d6ce10..ee91491efa 100644 --- a/sdk/python/arvados/keep.py +++ b/sdk/python/arvados/keep.py @@ -22,6 +22,11 @@ import threading from . import timer import urllib.parse +if sys.version_info >= (3, 0): + from io import BytesIO +else: + from cStringIO import StringIO as BytesIO + import arvados import arvados.config as config import arvados.errors @@ -335,7 +340,7 @@ class KeepClient(object): try: with timer.Timer() as t: self._headers = {} - response_body = io.StringIO() + response_body = BytesIO() curl.setopt(pycurl.NOSIGNAL, 1) curl.setopt(pycurl.OPENSOCKETFUNCTION, self._socket_open) curl.setopt(pycurl.URL, url.encode('utf-8')) @@ -412,8 +417,8 @@ class KeepClient(object): try: with timer.Timer() as t: self._headers = {} - body_reader = io.StringIO(body) - response_body = io.StringIO() + body_reader = BytesIO(body) + response_body = BytesIO() curl.setopt(pycurl.NOSIGNAL, 1) curl.setopt(pycurl.OPENSOCKETFUNCTION, self._socket_open) curl.setopt(pycurl.URL, url.encode('utf-8')) @@ -486,7 +491,8 @@ class KeepClient(object): curl.setopt(pycurl.LOW_SPEED_LIMIT, int(math.ceil(bandwidth_bps))) def _headerfunction(self, header_line): - header_line = header_line.decode('iso-8859-1') + if isinstance(header_line, bytes): + header_line = header_line.decode('iso-8859-1') if ':' in header_line: name, value = header_line.split(':', 1) name = name.strip().lower() @@ -1066,10 +1072,8 @@ class KeepClient(object): KeepClient is initialized. """ - if isinstance(data, str): - data = data.encode("ascii") - elif not isinstance(data, str): - raise arvados.errors.ArgumentError("Argument 'data' to KeepClient.put is not type 'str'") + if not isinstance(data, bytes): + data = data.encode() self.put_counter.add(1) diff --git a/sdk/python/tests/test_arv_put.py b/sdk/python/tests/test_arv_put.py index 667abbe63e..05e5e06b65 100644 --- a/sdk/python/tests/test_arv_put.py +++ b/sdk/python/tests/test_arv_put.py @@ -24,7 +24,10 @@ import threading import hashlib import random -from io import StringIO +if sys.version_info >= (3, 0): + from io import StringIO +else: + from cStringIO import StringIO import arvados import arvados.commands.put as arv_put diff --git a/sdk/python/tests/test_arvfile.py b/sdk/python/tests/test_arvfile.py index f4bcd8a497..0b2b7412a5 100644 --- a/sdk/python/tests/test_arvfile.py +++ b/sdk/python/tests/test_arvfile.py @@ -36,7 +36,7 @@ class ArvadosFileWriterTestCase(unittest.TestCase): return self.blocks.get(locator) def put(self, data, num_retries=None, copies=None): pdh = tutil.str_keep_locator(data) - self.blocks[pdh] = str(data) + self.blocks[pdh] = bytes(data) return pdh class MockApi(object): diff --git a/sdk/python/tests/test_cache.py b/sdk/python/tests/test_cache.py index ea34c8051d..a1fbc27e35 100644 --- a/sdk/python/tests/test_cache.py +++ b/sdk/python/tests/test_cache.py @@ -34,10 +34,10 @@ class CacheTestThread(threading.Thread): for x in range(16): try: data_in = _random(128) - data_in = md5.new(data_in).hexdigest() + "\n" + str(data_in) + data_in = md5.new(data_in).hexdigest() + bytes("\n") + bytes(data_in) c.set(url, data_in) data_out = c.get(url) - digest, content = data_out.split("\n", 1) + digest, _, content = data_out.partition("\n") if digest != md5.new(content).hexdigest(): self.ok = False except Exception as err: diff --git a/sdk/python/tests/test_keep_client.py b/sdk/python/tests/test_keep_client.py index 8c2a67130a..1b0b627a65 100644 --- a/sdk/python/tests/test_keep_client.py +++ b/sdk/python/tests/test_keep_client.py @@ -107,8 +107,8 @@ class KeepTestCase(run_test_server.TestCaseWithServers): # Error if it is not ASCII self.keep_client.put(u'\xe2') - with self.assertRaises(arvados.errors.ArgumentError): - # Must be a string type + with self.assertRaises(AttributeError): + # Must be bytes or have an encode() method self.keep_client.put({}) def test_KeepHeadTest(self): -- 2.30.2