[Zope3-checkins] CVS: Zope3/src/zope/schema - _bootstrapfields.py:1.25

Fred L. Drake, Jr. fred at zope.com
Mon Oct 20 12:11:47 EDT 2003


Update of /cvs-repository/Zope3/src/zope/schema
In directory cvs.zope.org:/tmp/cvs-serv17121

Modified Files:
	_bootstrapfields.py 
Log Message:
Small change in the Bool field type:
Using the Bool field with Python 2.2.3 caused it to check that values
are integers; using it with 2.3.x caused it to check that values are
instances of bool (the True and False singletons).  This change allows
integers to still be used, and converts them to bools for further
checks and insertion into object instances.  This makes perfectly
legitimate schemas written for Python 2.2.3 still work with Python
2.3.x.


=== Zope3/src/zope/schema/_bootstrapfields.py 1.24 => 1.25 ===
--- Zope3/src/zope/schema/_bootstrapfields.py:1.24	Wed Oct 15 16:28:22 2003
+++ Zope3/src/zope/schema/_bootstrapfields.py	Mon Oct 20 12:11:46 2003
@@ -382,6 +382,23 @@
     """A field representing a Bool."""
     _type = type(True)
 
+    if _type is not type(1):
+        # Python 2.2.1 and newer 2.2.x releases, True and False are
+        # integers, and bool() returns either 1 or 0.  We need to
+        # support using integers here so we don't invalidate schema
+        # that were perfectly valid with older versions of Python.
+        def _validate(self, value):
+            # Convert integers to bools to they don't get mis-flagged
+            # by the type check later.
+            if isinstance(value, int):
+                value = bool(value)
+            Field._validate(self, value)
+
+        def set(self, object, value):
+            if isinstance(value, int):
+                value = bool(value)
+            Field.set(self, object, value)
+
 class Int(Enumerated, Orderable, Field):
     """A field representing an Integer."""
     _type = int, long




More information about the Zope3-Checkins mailing list