From 3b6cde36c2399dd64b44e625c8bd28968b3035ad Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Fri, 30 Jul 2004 20:39:29 +0000 Subject: [PATCH] testing that assert statements are triggered does not work when running with Python's -O option, since that throws out the assertions use statements other than assert, and raise specific exceptions that make sense for the errors being detected --- utilities.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/utilities.py b/utilities.py index b6ba9640..7c2c1a71 100644 --- a/utilities.py +++ b/utilities.py @@ -316,11 +316,12 @@ def getFunctionSignature(func): >>> try: ... getFunctionSignature('func') - ... except AssertionError: + ... except TypeError: ... print 'Argument not a function or method.' Argument not a function or method. """ - assert type(func) in (types.FunctionType, types.MethodType) + if not isinstance(func, (types.FunctionType, types.MethodType)): + raise TypeError("func must be a function or method") args, varargs, varkw, default = inspect.getargspec(func) placeholder = object() @@ -413,7 +414,7 @@ def getInterfaceForAttribute(name, interfaces=_marker, klass=_marker, Either 'interfaces' or 'klass' must be specified. If 'interfaces' is not specified, the 'klass' is used to retrieve a list of - interfaces. 'interfaces' must be iteratable. + interfaces. 'interfaces' must be iterable. 'asPath' specifies whether the dotted name of the interface or the interface object is returned. @@ -449,14 +450,18 @@ def getInterfaceForAttribute(name, interfaces=_marker, klass=_marker, >>> getInterfaceForAttribute('attr2', klass=Sample) is None True - >>> try: - ... getInterfaceForAttribute('getAttr') - ... except AssertionError: - ... print 'need to specify the interfaces or a klass' - need to specify the interfaces or a klass + >>> getInterfaceForAttribute('getAttr') + Traceback (most recent call last): + ValueError: need to specify interfaces or klass + >>> getInterfaceForAttribute('getAttr', interfaces=(I1,I2), klass=Sample) + Traceback (most recent call last): + ValueError: must specify only one of interfaces and klass """ - assert (interfaces is _marker) != (klass is _marker) + if (interfaces is _marker) and (klass is _marker): + raise ValueError("need to specify interfaces or klass") + if (interfaces is not _marker) and (klass is not _marker): + raise ValueError("must specify only one of interfaces and klass") if interfaces is _marker: direct_interfaces = list(implementedBy(klass))