[Checkins] SVN: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/ Fix up test failures under Py3k.

Tres Seaver cvs-admin at zope.org
Wed Apr 25 18:57:55 UTC 2012


Log message for revision 125292:
  Fix up test failures under Py3k.

Changed:
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_compat.py
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__bootstrapinterfaces.py
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py

-=-
Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_compat.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_compat.py	2012-04-25 18:20:35 UTC (rev 125291)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_compat.py	2012-04-25 18:57:52 UTC (rev 125292)
@@ -12,7 +12,14 @@
     text_type = str
     binary_type = bytes
     integer_types = int,
-    non_native_string = bytes
+    def non_native_string(x):
+        if isinstance(x, bytes):
+            return x
+        return bytes(x, 'unicode_escape')
+    def make_binary(x):
+        if isinstance(x, bytes):
+            return x
+        return x.encode('ascii')
 else: #pragma NO COVER
     def b(s):
         return s
@@ -22,4 +29,11 @@
     text_type = unicode
     binary_type = str
     integer_types = (int, long)
-    non_native_string = unicode
+    def non_native_string(x):
+        if isinstance(x, unicode):
+            return x
+        return unicode(x, 'unicode_escape')
+    def make_binary(x):
+        if isinstance(x, str):
+            return x
+        return x.encode('ascii')

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-25 18:20:35 UTC (rev 125291)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py	2012-04-25 18:57:52 UTC (rev 125292)
@@ -98,6 +98,7 @@
 from zope.schema._compat import string_types
 from zope.schema._compat import binary_type
 from zope.schema._compat import PY3
+from zope.schema._compat import make_binary
 
 
 # Fix up bootstrap field types
@@ -143,7 +144,7 @@
         ConstraintNotSatisfied:  foo y.z bat
 
         """
-        v = binary_type(uc)
+        v = make_binary(uc)
         self.validate(v)
         return v
 
@@ -204,7 +205,7 @@
 
     def constraint(self, value):
         # TODO: we should probably use a more general definition of newlines
-        return b('\n') not in value
+        return '\n' not in value
 
 
 @implementer(IFloat, IFromUnicode)

Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__bootstrapinterfaces.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__bootstrapinterfaces.py	2012-04-25 18:20:35 UTC (rev 125291)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__bootstrapinterfaces.py	2012-04-25 18:57:52 UTC (rev 125292)
@@ -14,6 +14,11 @@
 import unittest
 
 
+def _skip_under_py3(testcase):
+    from zope.schema._compat import PY3
+    if not PY3:
+        return testcase
+
 class ValidationErrorTests(unittest.TestCase):
 
     def _getTargetClass(self):
@@ -29,11 +34,13 @@
         inst = Derived()
         self.assertEqual(inst.doc(), 'DERIVED')
 
+    @_skip_under_py3
     def test___cmp___no_args(self):
         # Py3k??
         ve = self._makeOne()
         self.assertEqual(cmp(ve, object()), -1)
 
+    @_skip_under_py3
     def test___cmp___hit(self):
         # Py3k??
         left = self._makeOne('abc')

Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py	2012-04-25 18:20:35 UTC (rev 125291)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py	2012-04-25 18:57:52 UTC (rev 125292)
@@ -81,9 +81,9 @@
 
     def test_validate_wrong_types(self):
         from zope.schema.interfaces import WrongType
-        from zope.schema._compat import u
+        from zope.schema._compat import non_native_string
         field = self._makeOne()
-        self.assertRaises(WrongType, field.validate, u(''))
+        self.assertRaises(WrongType, field.validate, non_native_string(''))
         self.assertRaises(WrongType, field.validate, 1)
         self.assertRaises(WrongType, field.validate, 1.0)
         self.assertRaises(WrongType, field.validate, ())
@@ -108,22 +108,8 @@
             asc._validate(chr(i)) #doesn't raise
 
 
-class _LineTestsBase(object):
+class BytesLineTests(unittest.TestCase):
 
-    def test_constraint_miss(self):
-        from zope.schema._compat import b
-        bl = self._makeOne()
-        self.assertEqual(bl.constraint(b('one line\nthen another')), False)
-
-    def test_constraint_hit(self):
-        from zope.schema._compat import b
-        bl = self._makeOne()
-        self.assertEqual(bl.constraint(b('')), True)
-        self.assertEqual(bl.constraint(b('one line')), True)
-
-
-class BytesLineTests(unittest.TestCase, _LineTestsBase):
-
     def _getTargetClass(self):
         from zope.schema._field import BytesLine
         return BytesLine
@@ -182,7 +168,7 @@
         self.assertEqual(field.constraint(b('abc\ndef')), False)
 
 
-class ASCIILineTests(unittest.TestCase, _LineTestsBase):
+class ASCIILineTests(unittest.TestCase):
 
     def _getTargetClass(self):
         from zope.schema._field import ASCIILine
@@ -203,9 +189,9 @@
 
     def test_validate_wrong_types(self):
         from zope.schema.interfaces import WrongType
-        from zope.schema._compat import u
+        from zope.schema._compat import non_native_string
         field = self._makeOne()
-        self.assertRaises(WrongType, field.validate, u(''))
+        self.assertRaises(WrongType, field.validate, non_native_string(''))
         self.assertRaises(WrongType, field.validate, 1)
         self.assertRaises(WrongType, field.validate, 1.0)
         self.assertRaises(WrongType, field.validate, ())
@@ -217,32 +203,29 @@
 
     def test_validate_not_required(self):
         from zope.schema.interfaces import InvalidValue
-        from zope.schema._compat import b
         field = self._makeOne(required=False)
         field.validate(None)
-        field.validate(b(''))
-        field.validate(b('abc'))
-        self.assertRaises(InvalidValue, field.validate, b('\xab\xde'))
+        field.validate('')
+        field.validate('abc')
+        self.assertRaises(InvalidValue, field.validate, '\xab\xde')
 
     def test_validate_required(self):
         from zope.schema.interfaces import InvalidValue
         from zope.schema.interfaces import RequiredMissing
-        from zope.schema._compat import b
         field = self._makeOne()
-        field.validate(b(''))
-        field.validate(b('abc'))
-        self.assertRaises(InvalidValue, field.validate, b('\xab\xde'))
+        field.validate('')
+        field.validate('abc')
+        self.assertRaises(InvalidValue, field.validate, '\xab\xde')
         self.assertRaises(RequiredMissing, field.validate, None)
 
     def test_constraint(self):
-        from zope.schema._compat import b
         field = self._makeOne()
-        self.assertEqual(field.constraint(b('')), True)
-        self.assertEqual(field.constraint(b('abc')), True)
-        self.assertEqual(field.constraint(b('abc')), True)
+        self.assertEqual(field.constraint(''), True)
+        self.assertEqual(field.constraint('abc'), True)
+        self.assertEqual(field.constraint('abc'), True)
         # Non-ASCII byltes get checked in '_validate'.
-        self.assertEqual(field.constraint(b('\xab\xde')), True)
-        self.assertEqual(field.constraint(b('abc\ndef')), False)
+        self.assertEqual(field.constraint('\xab\xde'), True)
+        self.assertEqual(field.constraint('abc\ndef'), False)
 
 
 class FloatTests(unittest.TestCase):



More information about the checkins mailing list