From 33f938e2be91bb0d18e5e979902ea3cfdda578b9 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Thu, 25 Sep 2014 17:05:39 -0400 Subject: Remove `assert` for flow control; it can be optimized away with `python -O`. Change-Id: Iaa3b9e5d6234db0c2a68992c8ef17dd90de59e40 Fixes-bug: 1373538 --- pecan/commands/base.py | 2 +- pecan/commands/create.py | 2 +- pecan/core.py | 3 ++- pecan/middleware/debug.py | 8 +++++--- pecan/tests/middleware/test_debug.py | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pecan/commands/base.py b/pecan/commands/base.py index 8f7d3dc..441d577 100644 --- a/pecan/commands/base.py +++ b/pecan/commands/base.py @@ -46,7 +46,7 @@ class CommandManager(object): continue try: cmd = ep.load() - assert hasattr(cmd, 'run') + cmd.run # ensure existance; catch AttributeError otherwise except Exception as e: # pragma: nocover warn("Unable to load plugin %s: %s" % (ep, e), RuntimeWarning) continue diff --git a/pecan/commands/create.py b/pecan/commands/create.py index b6eec2a..1187489 100644 --- a/pecan/commands/create.py +++ b/pecan/commands/create.py @@ -22,7 +22,7 @@ class ScaffoldManager(object): log.debug('%s loading scaffold %s', self.__class__.__name__, ep) try: cmd = ep.load() - assert hasattr(cmd, 'copy_to') + cmd.copy_to # ensure existance; catch AttributeError otherwise except Exception as e: # pragma: nocover warn( "Unable to load scaffold %s: %s" % (ep, e), RuntimeWarning diff --git a/pecan/core.py b/pecan/core.py index fc0468e..1e6d6cb 100644 --- a/pecan/core.py +++ b/pecan/core.py @@ -254,7 +254,8 @@ class PecanBase(object): module = __import__(name, fromlist=fromlist) kallable = getattr(module, parts[-1]) msg = "%s does not represent a callable class or function." - assert hasattr(kallable, '__call__'), msg % item + if not six.callable(kallable): + raise TypeError(msg % item) return kallable() raise ImportError('No item named %s' % item) diff --git a/pecan/middleware/debug.py b/pecan/middleware/debug.py index 0203245..764e8e9 100644 --- a/pecan/middleware/debug.py +++ b/pecan/middleware/debug.py @@ -269,9 +269,11 @@ class DebugMiddleware(object): self.debugger = debugger def __call__(self, environ, start_response): - assert not environ['wsgi.multiprocess'], ( - "The DebugMiddleware middleware is not usable in a " - "multi-process environment") + if environ['wsgi.multiprocess']: + raise RuntimeError( + "The DebugMiddleware middleware is not usable in a " + "multi-process environment" + ) if environ.get('paste.testing'): return self.app(environ, start_response) diff --git a/pecan/tests/middleware/test_debug.py b/pecan/tests/middleware/test_debug.py index de9d4cd..bc9d65f 100644 --- a/pecan/tests/middleware/test_debug.py +++ b/pecan/tests/middleware/test_debug.py @@ -58,7 +58,7 @@ class TestDebugMiddleware(PecanTestCase): app = TestApp(MultiProcessApp(DebugMiddleware(conditional_error_app))) self.assertRaises( - AssertionError, + RuntimeError, app.get, '/' ) -- cgit v1.2.1