[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/zope/ testing that assert statements are triggered does not work when running with

Fred L. Drake, Jr. fdrake at gmail.com
Fri Jul 30 16:39:29 EDT 2004


Log message for revision 26859:
  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
  


Changed:
  U   Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/utilities.py
  U   Zope3/branches/ZopeX3-3.0/src/zope/schema/tests/test_vocabulary.py
  U   Zope3/branches/ZopeX3-3.0/src/zope/schema/vocabulary.py


-=-
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/utilities.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/utilities.py	2004-07-30 18:54:48 UTC (rev 26858)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/utilities.py	2004-07-30 20:39:29 UTC (rev 26859)
@@ -316,11 +316,12 @@
       
       >>> 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 @@
 
     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 @@
       >>> 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))

Modified: Zope3/branches/ZopeX3-3.0/src/zope/schema/tests/test_vocabulary.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/schema/tests/test_vocabulary.py	2004-07-30 18:54:48 UTC (rev 26858)
+++ Zope3/branches/ZopeX3-3.0/src/zope/schema/tests/test_vocabulary.py	2004-07-30 20:39:29 UTC (rev 26859)
@@ -145,10 +145,10 @@
 
     def test_nonunique_tokens(self):
         self.assertRaises(
-            AssertionError, vocabulary.SimpleVocabulary.fromValues,
+            ValueError, vocabulary.SimpleVocabulary.fromValues,
             [2, '2'])
         self.assertRaises(
-            AssertionError, vocabulary.SimpleVocabulary.fromItems, 
+            ValueError, vocabulary.SimpleVocabulary.fromItems, 
             [(1, 'one'), ('1', 'another one')])
 
     def test_overriding_createTerm(self):

Modified: Zope3/branches/ZopeX3-3.0/src/zope/schema/vocabulary.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/schema/vocabulary.py	2004-07-30 18:54:48 UTC (rev 26858)
+++ Zope3/branches/ZopeX3-3.0/src/zope/schema/vocabulary.py	2004-07-30 20:39:29 UTC (rev 26859)
@@ -63,8 +63,9 @@
         for term in self._terms:
             self.by_value[term.value] = term
             self.by_token[term.token] = term
-        assert len(self.by_value) == len(self.by_token) == len(terms), \
-               'Supplied vocabulary values resulted in duplicate term tokens'
+        if not (len(self.by_value) == len(self.by_token) == len(terms)):
+            raise ValueError(
+                'Supplied vocabulary values resulted in duplicate term tokens')
         if interfaces:
             directlyProvides(self, *interfaces)
 



More information about the Zope3-Checkins mailing list