From 624b5310b456e8bbf9cc729fe1d9a18aeb068ae1 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Thu, 1 Feb 2018 15:25:53 -0800 Subject: [PATCH] py3: port common/wsgi.py Note that we're punting on configuring socket buffer sizes (for now) Change-Id: I285a9b521fd0af381a227e0e824bc391817547f4 --- swift/common/wsgi.py | 2 ++ swift/obj/diskfile.py | 4 ++-- swift/obj/server.py | 5 ++++- swift/proxy/controllers/obj.py | 4 ++-- swift/proxy/server.py | 5 ++++- test/unit/common/test_wsgi.py | 23 ++++++++++++----------- tox.ini | 3 ++- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py index 3057762c85..752e8767aa 100644 --- a/swift/common/wsgi.py +++ b/swift/common/wsgi.py @@ -119,6 +119,8 @@ class ConfigString(NamedConfigLoader): } self.parser = loadwsgi.NicerConfigParser("string", defaults=defaults) self.parser.optionxform = str # Don't lower-case keys + # Defaults don't need interpolation (crazy PasteDeploy...) + self.parser.defaults = lambda: dict(self.parser._defaults, **defaults) self.parser.readfp(self.contents) diff --git a/swift/obj/diskfile.py b/swift/obj/diskfile.py index fc542a9f0a..9a2e9ff2a4 100644 --- a/swift/obj/diskfile.py +++ b/swift/obj/diskfile.py @@ -608,10 +608,10 @@ class DiskFileRouter(object): self.policy_to_manager = {} for policy in POLICIES: manager_cls = self.policy_type_to_manager_cls[policy.policy_type] - self.policy_to_manager[policy] = manager_cls(*args, **kwargs) + self.policy_to_manager[int(policy)] = manager_cls(*args, **kwargs) def __getitem__(self, policy): - return self.policy_to_manager[policy] + return self.policy_to_manager[int(policy)] class BaseDiskFileManager(object): diff --git a/swift/obj/server.py b/swift/obj/server.py index 788480d7b1..53851d7feb 100644 --- a/swift/obj/server.py +++ b/swift/obj/server.py @@ -15,6 +15,7 @@ """ Object Server for Swift """ +import six import six.moves.cPickle as pickle import json import os @@ -170,7 +171,9 @@ class ObjectController(BaseStorageServer): # disk_chunk_size parameter. However, it affects all created sockets # using this class so we have chosen to tie it to the # network_chunk_size parameter value instead. - socket._fileobject.default_bufsize = self.network_chunk_size + if six.PY2: + socket._fileobject.default_bufsize = self.network_chunk_size + # TODO: find a way to enable similar functionality in py3 # Provide further setup specific to an object server implementation. self.setup(conf) diff --git a/swift/proxy/controllers/obj.py b/swift/proxy/controllers/obj.py index 6bec505f32..d8aadf7935 100644 --- a/swift/proxy/controllers/obj.py +++ b/swift/proxy/controllers/obj.py @@ -150,11 +150,11 @@ class ObjectControllerRouter(object): def __init__(self): self.policy_to_controller_cls = {} for policy in POLICIES: - self.policy_to_controller_cls[policy] = \ + self.policy_to_controller_cls[int(policy)] = \ self.policy_type_to_controller_map[policy.policy_type] def __getitem__(self, policy): - return self.policy_to_controller_cls[policy] + return self.policy_to_controller_cls[int(policy)] class BaseObjectController(Controller): diff --git a/swift/proxy/server.py b/swift/proxy/server.py index 77863fb74a..acc8bc7c36 100644 --- a/swift/proxy/server.py +++ b/swift/proxy/server.py @@ -276,7 +276,10 @@ class Application(object): # # ** Because it affects the client as well, currently, we use the # client chunk size as the govenor and not the object chunk size. - socket._fileobject.default_bufsize = self.client_chunk_size + if sys.version_info < (3,): + socket._fileobject.default_bufsize = self.client_chunk_size + # TODO: find a way to enable similar functionality in py3 + self.expose_info = config_true_value( conf.get('expose_info', 'yes')) self.disallowed_sections = list_from_csv( diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index 44b7e3977e..774fcf84e8 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -203,7 +203,7 @@ class TestWSGI(unittest.TestCase): conf_file = os.path.join(tempdir, 'file.conf') def _write_and_load_conf_file(conf): - with open(conf_file, 'wb') as fd: + with open(conf_file, 'wt') as fd: fd.write(dedent(conf)) return wsgi.load_app_config(conf_file) @@ -659,12 +659,12 @@ class TestWSGI(unittest.TestCase): oldenv = {} newenv = wsgi.make_pre_authed_env(oldenv) self.assertTrue('wsgi.input' in newenv) - self.assertEqual(newenv['wsgi.input'].read(), '') + self.assertEqual(newenv['wsgi.input'].read(), b'') oldenv = {'wsgi.input': BytesIO(b'original wsgi.input')} newenv = wsgi.make_pre_authed_env(oldenv) self.assertTrue('wsgi.input' in newenv) - self.assertEqual(newenv['wsgi.input'].read(), '') + self.assertEqual(newenv['wsgi.input'].read(), b'') oldenv = {'swift.source': 'UT'} newenv = wsgi.make_pre_authed_env(oldenv) @@ -677,7 +677,7 @@ class TestWSGI(unittest.TestCase): def test_pre_auth_req(self): class FakeReq(object): @classmethod - def fake_blank(cls, path, environ=None, body='', headers=None): + def fake_blank(cls, path, environ=None, body=b'', headers=None): if environ is None: environ = {} if headers is None: @@ -687,7 +687,7 @@ class TestWSGI(unittest.TestCase): was_blank = Request.blank Request.blank = FakeReq.fake_blank wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'}, - 'PUT', '/', body='tester', headers={}) + 'PUT', '/', body=b'tester', headers={}) wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'}, 'PUT', '/', headers={}) Request.blank = was_blank @@ -695,7 +695,7 @@ class TestWSGI(unittest.TestCase): def test_pre_auth_req_with_quoted_path(self): r = wsgi.make_pre_authed_request( {'HTTP_X_TRANS_ID': '1234'}, 'PUT', path=quote('/a space'), - body='tester', headers={}) + body=b'tester', headers={}) self.assertEqual(r.path, quote('/a space')) def test_pre_auth_req_drops_query(self): @@ -711,8 +711,8 @@ class TestWSGI(unittest.TestCase): def test_pre_auth_req_with_body(self): r = wsgi.make_pre_authed_request( - {'QUERY_STRING': 'original'}, 'GET', 'path', 'the body') - self.assertEqual(r.body, 'the body') + {'QUERY_STRING': 'original'}, 'GET', 'path', b'the body') + self.assertEqual(r.body, b'the body') def test_pre_auth_creates_script_name(self): e = wsgi.make_pre_authed_env({}) @@ -730,9 +730,9 @@ class TestWSGI(unittest.TestCase): def test_pre_auth_req_swift_source(self): r = wsgi.make_pre_authed_request( - {'QUERY_STRING': 'original'}, 'GET', 'path', 'the body', + {'QUERY_STRING': 'original'}, 'GET', 'path', b'the body', swift_source='UT') - self.assertEqual(r.body, 'the body') + self.assertEqual(r.body, b'the body') self.assertEqual(r.environ['swift.source'], 'UT') def test_run_server_global_conf_callback(self): @@ -1363,7 +1363,8 @@ class TestWSGIContext(unittest.TestCase): self.assertEqual('aaaaa', next(iterator)) self.assertEqual('bbbbb', next(iterator)) iterable.close() - self.assertRaises(StopIteration, iterator.next) + with self.assertRaises(StopIteration): + next(iterator) def test_update_content_length(self): statuses = ['200 Ok'] diff --git a/tox.ini b/tox.ini index 00483a6e6d..0984488217 100644 --- a/tox.ini +++ b/tox.ini @@ -40,7 +40,8 @@ commands = test/unit/common/test_manager.py \ test/unit/common/test_splice.py \ test/unit/common/test_storage_policy.py \ - test/unit/common/test_utils.py + test/unit/common/test_utils.py \ + test/unit/common/test_wsgi.py [testenv:py35] commands = {[testenv:py34]commands}