Skip to content

Commit

Permalink
testing that assert statements are triggered does not work when runni…
Browse files Browse the repository at this point in the history
…ng 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
  • Loading branch information
freddrake committed Jul 30, 2004
1 parent 8a66ae0 commit 3b6cde3
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions utilities.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 3b6cde3

Please sign in to comment.