[Checkins] SVN: zope.schema/branches/tseaver-test_cleanup/ Merge jinty-native_string branch:

Tres Seaver cvs-admin at zope.org
Fri May 4 23:45:04 UTC 2012


Log message for revision 125653:
  Merge jinty-native_string branch:
  
  - Introduce NativeString and NativeStringLine which are equal to Bytes and
    BytesLine on Python 2 and Text and TextLine on Python 3.
  
  - Change IURI from a Bytes string to a "native" string. This is a backwards
    incompatibility which only affects Python 3.
  

Changed:
  U   zope.schema/branches/tseaver-test_cleanup/CHANGES.txt
  U   zope.schema/branches/tseaver-test_cleanup/docs/narr.rst
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/__init__.py
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/interfaces.py
  U   zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py

-=-
Modified: zope.schema/branches/tseaver-test_cleanup/CHANGES.txt
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/CHANGES.txt	2012-05-04 23:14:55 UTC (rev 125652)
+++ zope.schema/branches/tseaver-test_cleanup/CHANGES.txt	2012-05-04 23:45:00 UTC (rev 125653)
@@ -5,6 +5,12 @@
 4.2.0 (unreleased)
 ------------------
 
+- Introduce NativeString and NativeStringLine which are equal to Bytes and
+  BytesLine on Python 2 and Text and TextLine on Python 3.
+
+- Change IURI from a Bytes string to a "native" string. This is a backwards
+  incompatibility which only affects Python 3.
+
 - 100% unit test coverage.
 
 - Doctests moved from the package and wired up as normal Sphinx documentation.

Modified: zope.schema/branches/tseaver-test_cleanup/docs/narr.rst
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/docs/narr.rst	2012-05-04 23:14:55 UTC (rev 125652)
+++ zope.schema/branches/tseaver-test_cleanup/docs/narr.rst	2012-05-04 23:45:00 UTC (rev 125653)
@@ -100,12 +100,13 @@
 
 .. doctest::
 
-   >>> url_bound.validate(u('http://zope.org/foo'))
+   >>> from zope.schema._compat import non_native_string
+   >>> url_bound.validate(non_native_string('http://zope.org/foo'))
    Traceback (most recent call last):
    ...
-   WrongType: (u'http://zope.org/foo', <type 'str'>, 'url')
+   WrongType: ...
 
-   >>> url_bound.validate(b('foo.bar'))
+   >>> url_bound.validate('foo.bar'))
    Traceback (most recent call last):
    ...
    InvalidURI: foo.bar

Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/__init__.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/__init__.py	2012-05-04 23:14:55 UTC (rev 125652)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/__init__.py	2012-05-04 23:45:00 UTC (rev 125653)
@@ -35,6 +35,8 @@
 from zope.schema._field import Iterable
 from zope.schema._field import List
 from zope.schema._field import MinMaxLen
+from zope.schema._field import NativeString
+from zope.schema._field import NativeStringLine
 from zope.schema._field import Object
 from zope.schema._field import Orderable
 from zope.schema._field import Password

Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py	2012-05-04 23:14:55 UTC (rev 125652)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/_field.py	2012-05-04 23:45:00 UTC (rev 125653)
@@ -150,12 +150,12 @@
 
 # for things which are of the str type on both Python 2 and 3
 if PY3: #pragma NO COVER
-    _Str = Text
+    NativeString = Text
 else: #pragma NO COVER
-    _Str = Bytes
+    NativeString = Bytes
 
 @implementer(IASCII)
-class ASCII(_Str):
+class ASCII(NativeString):
     __doc__ = IASCII.__doc__
 
     def _validate(self, value):
@@ -195,9 +195,9 @@
 
 # for things which are of the str type on both Python 2 and 3
 if PY3: #pragma NO COVER
-    _StrLine = TextLine
+    NativeStringLine = TextLine
 else: #pragma NO COVER
-    _StrLine = BytesLine
+    NativeStringLine = BytesLine
 
 @implementer(IASCIILine)
 class ASCIILine(ASCII):
@@ -651,12 +651,10 @@
 
 _isuri = r"[a-zA-z0-9+.-]+:" # scheme
 _isuri += r"\S*$" # non space (should be pickier)
-
-_isuri_bytes = re.compile(_isuri.encode('ascii')).match
 _isuri = re.compile(_isuri).match
 
 @implementer(IURI, IFromUnicode)
-class URI(BytesLine):
+class URI(NativeStringLine):
     """URI schema field
     """
 
@@ -672,7 +670,7 @@
         """
 
         super(URI, self)._validate(value)
-        if _isuri_bytes(value):
+        if _isuri(value):
             return
 
         raise InvalidURI(value)
@@ -691,7 +689,7 @@
         ...
         InvalidURI: http://www.python.org/ foo/bar
         """
-        v = value.strip().encode('ascii')
+        v = str(value.strip())
         self.validate(v)
         return v
 
@@ -704,7 +702,7 @@
 
 
 @implementer(IId, IFromUnicode)
-class Id(_StrLine):
+class Id(NativeStringLine):
     """Id field
 
     Values of id fields must be either uris or dotted names.
@@ -755,7 +753,7 @@
 
 
 @implementer(IDottedName)
-class DottedName(_StrLine):
+class DottedName(NativeStringLine):
     """Dotted name field.
 
     Values of DottedName fields must be Python-style dotted names.

Modified: zope.schema/branches/tseaver-test_cleanup/src/zope/schema/interfaces.py
===================================================================
--- zope.schema/branches/tseaver-test_cleanup/src/zope/schema/interfaces.py	2012-05-04 23:14:55 UTC (rev 125652)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/interfaces.py	2012-05-04 23:45:00 UTC (rev 125653)
@@ -293,11 +293,11 @@
 
 # for things which are of the str type on both Python 2 and 3
 if PY3: #pragma NO COVER
-    _IStr = IText
+    INativeString = IText
 else: #pragma NO COVER
-    _IStr = IBytes
+    INativeString = IBytes
 
-class IASCII(_IStr):
+class IASCII(INativeString):
     """Field containing a 7-bit ASCII string. No characters > DEL
     (chr(127)) are allowed
 
@@ -317,9 +317,9 @@
     """Field containing a unicode string without newlines."""
 
 if PY3: #pragma NO COVER
-    _IStrLine = ITextLine
+    INativeStringLine = ITextLine
 else: #pragma NO COVER
-    _IStrLine = IBytesLine
+    INativeStringLine = IBytesLine
 
 class IPassword(ITextLine):
     "Field containing a unicode string without newlines that is a password."
@@ -375,18 +375,18 @@
     return True
 
 
-class IURI(IBytesLine):
+class IURI(INativeStringLine):
     """A field containing an absolute URI
     """
 
-class IId(_IStrLine):
+class IId(INativeStringLine):
     """A field containing a unique identifier
 
     A unique identifier is either an absolute URI or a dotted name.
     If it's a dotted name, it should have a module/package name as a prefix.
     """
 
-class IDottedName(_IStrLine):
+class IDottedName(INativeStringLine):
     """Dotted name field.
 
     Values of DottedName fields must be Python-style dotted names.

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-05-04 23:14:55 UTC (rev 125652)
+++ zope.schema/branches/tseaver-test_cleanup/src/zope/schema/tests/test__field.py	2012-05-04 23:45:00 UTC (rev 125653)
@@ -1785,9 +1785,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, ())
@@ -1798,35 +1798,31 @@
         self.assertRaises(WrongType, field.validate, object())
 
     def test_validate_not_required(self):
-        from zope.schema._compat import b
         field = self._makeOne(required=False)
-        field.validate(b('http://example.com/'))
+        field.validate('http://example.com/')
         field.validate(None)
 
     def test_validate_required(self):
         from zope.schema.interfaces import RequiredMissing
-        from zope.schema._compat import b
         field = self._makeOne()
-        field.validate(b('http://example.com/'))
+        field.validate('http://example.com/')
         self.assertRaises(RequiredMissing, field.validate, None)
 
     def test_validate_not_a_uri(self):
         from zope.schema.interfaces import ConstraintNotSatisfied
         from zope.schema.interfaces import InvalidURI
-        from zope.schema._compat import b
         field = self._makeOne()
-        self.assertRaises(InvalidURI, field.validate, b(''))
-        self.assertRaises(InvalidURI, field.validate, b('abc'))
-        self.assertRaises(InvalidURI, field.validate, b('\xab\xde'))
+        self.assertRaises(InvalidURI, field.validate, '')
+        self.assertRaises(InvalidURI, field.validate, 'abc')
+        self.assertRaises(InvalidURI, field.validate, '\xab\xde')
         self.assertRaises(ConstraintNotSatisfied,
-                          field.validate, b('http://example.com/\nDAV:'))
+                          field.validate, 'http://example.com/\nDAV:')
 
     def test_fromUnicode_ok(self):
-        from zope.schema._compat import b
         from zope.schema._compat import u
         field = self._makeOne()
         self.assertEqual(field.fromUnicode(u('http://example.com/')),
-                         b('http://example.com/'))
+                         'http://example.com/')
 
     def test_fromUnicode_invalid(self):
         from zope.schema.interfaces import ConstraintNotSatisfied



More information about the checkins mailing list