From 3944d820387f08372c1a29444f4af7d8e6090ae9 Mon Sep 17 00:00:00 2001 From: Samuel Merritt Date: Tue, 14 Jun 2016 16:10:16 -0700 Subject: [PATCH] Catch AttributeError less often I always get tripped up when I'm editing code that catches AttributeError and does something with it. I'll type "req.emthod" or something, and next thing I know I'm getting 405s in all my unit tests. This diff removes some places where we catch AttributeError (sometimes, having deliberately thrown it only one line before) so that typos can crash the way Guido intended. Change-Id: I2f7586f96b41a97e6ae254efc83218b3b5c6cc9e --- swift/account/server.py | 8 ++------ swift/common/manager.py | 5 ++--- swift/container/server.py | 8 ++------ swift/obj/server.py | 5 +---- swift/proxy/server.py | 7 +++---- 5 files changed, 10 insertions(+), 23 deletions(-) diff --git a/swift/account/server.py b/swift/account/server.py index 8795844afc..3bfe3fbf49 100644 --- a/swift/account/server.py +++ b/swift/account/server.py @@ -262,14 +262,10 @@ class AccountController(BaseStorageServer): else: try: # disallow methods which are not publicly accessible - try: - if req.method not in self.allowed_methods: - raise AttributeError('Not allowed method.') - except AttributeError: + if req.method not in self.allowed_methods: res = HTTPMethodNotAllowed() else: - method = getattr(self, req.method) - res = method(req) + res = getattr(self, req.method)(req) except HTTPException as error_response: res = error_response except (Exception, Timeout): diff --git a/swift/common/manager.py b/swift/common/manager.py index 123f27d10f..2cc764493c 100644 --- a/swift/common/manager.py +++ b/swift/common/manager.py @@ -378,9 +378,8 @@ class Manager(object): """ cmd = cmd.lower().replace('-', '_') - try: - f = getattr(self, cmd) - except AttributeError: + f = getattr(self, cmd, None) + if f is None: raise UnknownCommandError(cmd) if not hasattr(f, 'publicly_accessible'): raise UnknownCommandError(cmd) diff --git a/swift/container/server.py b/swift/container/server.py index a77dadcd22..898ef36ea1 100644 --- a/swift/container/server.py +++ b/swift/container/server.py @@ -594,14 +594,10 @@ class ContainerController(BaseStorageServer): else: try: # disallow methods which have not been marked 'public' - try: - if req.method not in self.allowed_methods: - raise AttributeError('Not allowed method.') - except AttributeError: + if req.method not in self.allowed_methods: res = HTTPMethodNotAllowed() else: - method = getattr(self, req.method) - res = method(req) + res = getattr(self, req.method)(req) except HTTPException as error_response: res = error_response except (Exception, Timeout): diff --git a/swift/obj/server.py b/swift/obj/server.py index c3fde72525..b9c8616124 100644 --- a/swift/obj/server.py +++ b/swift/obj/server.py @@ -1031,10 +1031,7 @@ class ObjectController(BaseStorageServer): else: try: # disallow methods which have not been marked 'public' - try: - if req.method not in self.allowed_methods: - raise AttributeError('Not allowed method.') - except AttributeError: + if req.method not in self.allowed_methods: res = HTTPMethodNotAllowed() else: method = getattr(self, req.method) diff --git a/swift/proxy/server.py b/swift/proxy/server.py index 4993c90735..99b99afd54 100644 --- a/swift/proxy/server.py +++ b/swift/proxy/server.py @@ -382,10 +382,9 @@ class Application(object): req.headers['x-trans-id'] = req.environ['swift.trans_id'] controller.trans_id = req.environ['swift.trans_id'] self.logger.client_ip = get_remote_client(req) - try: - handler = getattr(controller, req.method) - getattr(handler, 'publicly_accessible') - except AttributeError: + + handler = getattr(controller, req.method, None) + if not getattr(handler, 'publicly_accessible', False): allowed_methods = getattr(controller, 'allowed_methods', set()) return HTTPMethodNotAllowed( request=req, headers={'Allow': ', '.join(allowed_methods)})