[Checkins] SVN: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py Don't rely on assert to enforce runtime constraints.

Tres Seaver cvs-admin at zope.org
Mon Apr 23 21:59:54 UTC 2012


Log message for revision 125261:
  Don't rely on assert to enforce runtime constraints.

Changed:
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py

-=-
Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py	2012-04-23 21:59:47 UTC (rev 125260)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py	2012-04-23 21:59:51 UTC (rev 125261)
@@ -300,16 +300,21 @@
     def __init__(self, values=None, vocabulary=None, source=None, **kw):
         """Initialize object."""
         if vocabulary is not None:
-            assert (isinstance(vocabulary, string_types)
-                    or IBaseVocabulary.providedBy(vocabulary))
-            assert source is None, (
-                "You cannot specify both source and vocabulary.")
+            if (not isinstance(vocabulary, string_types)
+                    and not IBaseVocabulary.providedBy(vocabulary)):
+                raise ValueError('vocabulary must be a string or implement '
+                                 'IBaseVocabulary')
+            if source is not None:
+                raise ValueError(
+                    "You cannot specify both source and vocabulary.")
         elif source is not None:
             vocabulary = source
 
-        assert not (values is None and vocabulary is None), (
+        if (values is None and vocabulary is None):
+            raise ValueError(
                "You must specify either values or vocabulary.")
-        assert values is None or vocabulary is None, (
+        if values is not None and vocabulary is not None:
+            raise ValueError(
                "You cannot specify both values and vocabulary.")
 
         self.vocabulary = None
@@ -319,8 +324,9 @@
         elif isinstance(vocabulary, string_types):
             self.vocabularyName = vocabulary
         else:
-            assert (ISource.providedBy(vocabulary) or
-                    IContextSourceBinder.providedBy(vocabulary))
+            if (not ISource.providedBy(vocabulary) and
+                not IContextSourceBinder.providedBy(vocabulary)):
+                raise ValueError('Invalid vocabulary')
             self.vocabulary = vocabulary
         # Before a default value is checked, it is validated. However, a
         # named vocabulary is usually not complete when these fields are
@@ -340,11 +346,13 @@
         # get registered vocabulary if needed:
         if IContextSourceBinder.providedBy(self.vocabulary):
             clone.vocabulary = self.vocabulary(object)
-            assert ISource.providedBy(clone.vocabulary)
+            if not ISource.providedBy(clone.vocabulary):
+                raise ValueError('Invalid clone vocabulary')
         elif clone.vocabulary is None and self.vocabularyName is not None:
             vr = getVocabularyRegistry()
             clone.vocabulary = vr.get(object, self.vocabularyName)
-            assert ISource.providedBy(clone.vocabulary)
+            if not ISource.providedBy(clone.vocabulary):
+                raise ValueError('Invalid clone vocabulary')
 
         return clone
 



More information about the checkins mailing list