Skip to content

Commit

Permalink
Remove the u/b BWC functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Sep 15, 2016
1 parent 5baf992 commit 1d3f1c4
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 424 deletions.
27 changes: 12 additions & 15 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ Conversion from Unicode:

.. doctest::

>>> from zope.schema._compat import b
>>> from zope.schema._compat import u
>>> from zope.schema import Bytes
>>> obj = Bytes(constraint=lambda v: b('x') in v)
>>> obj.fromUnicode(u(" foo x.y.z bat"))
>>> obj = Bytes(constraint=lambda v: b'x' in v)
>>> obj.fromUnicode(u" foo x.y.z bat")
' foo x.y.z bat'
>>> obj.fromUnicode(u(" foo y.z bat"))
>>> obj.fromUnicode(u" foo y.z bat")
Traceback (most recent call last):
...
ConstraintNotSatisfied: foo y.z bat
Expand Down Expand Up @@ -162,14 +160,14 @@ Conversion from Unicode enforces the constraint:
>>> from zope.schema.vocabulary import SimpleVocabulary
>>> from zope.schema import Choice
>>> t = Choice(
... vocabulary=SimpleVocabulary.fromValues([u('foo'),u('bar')]))
... vocabulary=SimpleVocabulary.fromValues([u'foo',u'bar']))
>>> IFromUnicode.providedBy(t)
True
>>> t.fromUnicode(u("baz"))
>>> t.fromUnicode(u"baz")
Traceback (most recent call last):
...
ConstraintNotSatisfied: baz
>>> t.fromUnicode(u("foo"))
>>> t.fromUnicode(u"foo")
u'foo'

By default, ValueErrors are thrown if duplicate values or tokens
Expand All @@ -191,9 +189,9 @@ Validation ensures that the pattern is matched:

>>> from zope.schema import URI
>>> uri = URI(__name__='test')
>>> uri.validate(b("http://www.python.org/foo/bar"))
>>> uri.validate(b("DAV:"))
>>> uri.validate(b("www.python.org/foo/bar"))
>>> uri.validate(b"http://www.python.org/foo/bar")
>>> uri.validate(b"DAV:")
>>> uri.validate(b"www.python.org/foo/bar")
Traceback (most recent call last):
...
InvalidURI: www.python.org/foo/bar
Expand Down Expand Up @@ -334,7 +332,7 @@ Conversion from Unicode:
>>> id = Id(__name__='test')
>>> id.fromUnicode("http://www.python.org/foo/bar")
'http://www.python.org/foo/bar'
>>> id.fromUnicode(u(" http://www.python.org/foo/bar "))
>>> id.fromUnicode(u" http://www.python.org/foo/bar ")
'http://www.python.org/foo/bar'
>>> id.fromUnicode("http://www.python.org/ foo/bar")
Traceback (most recent call last):
Expand All @@ -359,12 +357,11 @@ uniqueness. In a schema, this might be written as follows:

>>> from zope.interface import Interface
>>> from zope.schema import List, Float
>>> from zope.schema._compat import u
>>> class IInventoryItem(Interface):
... pricePoints = List(
... title=u("Price Points"),
... title=u"Price Points",
... unique=True,
... value_type=Float(title=u("Price"), min=0.0)
... value_type=Float(title=u"Price", min=0.0)
... )

This indicates several things.
Expand Down
27 changes: 13 additions & 14 deletions docs/narr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ instances, we now use schema fields:

>>> import zope.interface
>>> import zope.schema
>>> from zope.schema._compat import u, b

>>> class IBookmark(zope.interface.Interface):
... title = zope.schema.TextLine(
... title=u('Title'),
... description=u('The title of the bookmark'),
... title=u'Title',
... description=u'The title of the bookmark',
... required=True)
...
... url = zope.schema.URI(
... title=u('Bookmark URL'),
... description=u('URL of the Bookmark'),
... title=u'Bookmark URL',
... description=u'URL of the Bookmark',
... required=True)
...

Expand All @@ -69,8 +68,8 @@ is to define some data:

.. doctest::

>>> title = u('Zope 3 Website')
>>> url = b('http://dev.zope.org/Zope3')
>>> title = u'Zope 3 Website'
>>> url = b'http://dev.zope.org/Zope3'

Now we, get the fields from the interface:

Expand Down Expand Up @@ -147,16 +146,16 @@ down example from the programmer's tutorial:
>>> class IContact(zope.interface.Interface):
... """Provides access to basic contact information."""
...
... first = zope.schema.TextLine(title=u("First name"))
... first = zope.schema.TextLine(title=u"First name")
...
... last = zope.schema.TextLine(title=u("Last name"))
... last = zope.schema.TextLine(title=u"Last name")
...
... email = zope.schema.TextLine(title=u("Electronic mail address"))
... email = zope.schema.TextLine(title=u"Electronic mail address")
...
... address = zope.schema.Text(title=u("Postal address"))
... address = zope.schema.Text(title=u"Postal address")
...
... postalCode = zope.schema.TextLine(
... title=u("Postal code"),
... title=u"Postal code",
... constraint=re.compile("\d{5,5}(-\d{4,4})?$").match)

``TextLine`` is a field and expresses that an attribute is a single line
Expand Down Expand Up @@ -184,8 +183,8 @@ schema:

.. doctest::

>>> someone = Contact(u('Tim'), u('Roberts'), u('tim@roberts'), u(''),
... u('12032-3492'))
>>> someone = Contact(u'Tim', u'Roberts', u'tim@roberts', u'',
... u'12032-3492')

>>> for field in zope.schema.getFields(IContact).values():
... bound = field.bind(someone)
Expand Down
4 changes: 2 additions & 2 deletions src/zope/schema/_bootstrapfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from zope.schema._bootstrapinterfaces import TooShort
from zope.schema._bootstrapinterfaces import TooSmall
from zope.schema._bootstrapinterfaces import WrongType
from zope.schema._compat import u

from zope.schema._compat import text_type
from zope.schema._compat import integer_types

Expand Down Expand Up @@ -110,7 +110,7 @@ class Field(Attribute):
interface = None
_Element__tagged_values = None

def __init__(self, title=u(''), description=u(''), __name__='',
def __init__(self, title=u'', description=u'', __name__='',
required=True, readonly=False, constraint=None, default=None,
defaultFactory=None, missing_value=__missing_value_marker):
"""Pass in field values as keyword parameters.
Expand Down
17 changes: 1 addition & 16 deletions src/zope/schema/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@

PY3 = sys.version_info[0] >= 3

try:
from collections import OrderedDict
except ImportError: # pragma: no cover
from ordereddict import OrderedDict
from collections import OrderedDict

# pep 8 friendlyness
OrderedDict


if PY3: # pragma: no cover

def b(s):
return s.encode("latin-1")

def u(s):
return s

string_types = str,
text_type = str
binary_type = bytes
Expand All @@ -36,12 +27,6 @@ def make_binary(x):

else: # pragma: no cover

def b(s):
return s

def u(s):
return unicode(s, "unicode_escape")

string_types = basestring,
text_type = unicode
binary_type = str
Expand Down
10 changes: 4 additions & 6 deletions src/zope/schema/_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
from zope.schema.vocabulary import VocabularyRegistryError
from zope.schema.vocabulary import SimpleVocabulary

from zope.schema._compat import b

from zope.schema._compat import text_type
from zope.schema._compat import string_types
from zope.schema._compat import binary_type
Expand Down Expand Up @@ -165,7 +165,7 @@ class BytesLine(Bytes):

def constraint(self, value):
# TODO: we should probably use a more general definition of newlines
return b('\n') not in value
return b'\n' not in value

# for things which are of the str type on both Python 2 and 3
if PY3: # pragma: no cover
Expand Down Expand Up @@ -459,13 +459,11 @@ def _validate_sequence(value_type, value, errors=None):
To illustrate, we'll use a text value type. All values must be unicode.
>>> from zope.schema._compat import u
>>> from zope.schema._compat import b
>>> field = TextLine(required=True)
To validate a sequence of various values:
>>> errors = _validate_sequence(field, (b('foo'), u('bar'), 1))
>>> errors = _validate_sequence(field, (b'foo', u'bar', 1))
>>> errors # XXX assumes Python2 reprs
[WrongType('foo', <type 'unicode'>, ''), WrongType(1, <type 'unicode'>, '')]
Expand All @@ -475,7 +473,7 @@ def _validate_sequence(value_type, value, errors=None):
We can use the optional errors argument to collect additional errors
for a new sequence:
>>> errors = _validate_sequence(field, (2, u('baz')), errors)
>>> errors = _validate_sequence(field, (2, u'baz'), errors)
>>> errors # XXX assumes Python2 reprs
[WrongType('foo', <type 'unicode'>, ''), WrongType(1, <type 'unicode'>, ''), WrongType(2, <type 'unicode'>, '')]
Expand Down
6 changes: 3 additions & 3 deletions src/zope/schema/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from zope.schema._bootstrapinterfaces import IContextAwareDefaultFactory

from zope.schema._compat import PY3
from zope.schema._compat import u

from zope.schema._messageid import _


Expand Down Expand Up @@ -125,14 +125,14 @@ def bind(object):
title = TextLine(
title=_("Title"),
description=_("A short summary or label"),
default=u(""),
default=u"",
required=False,
)

description = Text(
title=_("Description"),
description=_("A description of the field"),
default=u(""),
default=u"",
required=False,
)

Expand Down
122 changes: 61 additions & 61 deletions src/zope/schema/tests/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,74 @@
##############################################################################
"""Sample vocabulary supporting state abbreviations.
"""
from zope.schema._compat import u

from zope.interface import implementer
from zope.schema import interfaces
from zope.schema import Choice

# This table is based on information from the United States Postal Service:
# http://www.usps.com/ncsc/lookups/abbreviations.html#states
_states = {
'AL': u('Alabama'),
'AK': u('Alaska'),
'AS': u('American Samoa'),
'AZ': u('Arizona'),
'AR': u('Arkansas'),
'CA': u('California'),
'CO': u('Colorado'),
'CT': u('Connecticut'),
'DE': u('Delaware'),
'DC': u('District of Columbia'),
'FM': u('Federated States of Micronesia'),
'FL': u('Florida'),
'GA': u('Georgia'),
'GU': u('Guam'),
'HI': u('Hawaii'),
'ID': u('Idaho'),
'IL': u('Illinois'),
'IN': u('Indiana'),
'IA': u('Iowa'),
'KS': u('Kansas'),
'KY': u('Kentucky'),
'LA': u('Louisiana'),
'ME': u('Maine'),
'MH': u('Marshall Islands'),
'MD': u('Maryland'),
'MA': u('Massachusetts'),
'MI': u('Michigan'),
'MN': u('Minnesota'),
'MS': u('Mississippi'),
'MO': u('Missouri'),
'MT': u('Montana'),
'NE': u('Nebraska'),
'NV': u('Nevada'),
'NH': u('New Hampshire'),
'NJ': u('New Jersey'),
'NM': u('New Mexico'),
'NY': u('New York'),
'NC': u('North Carolina'),
'ND': u('North Dakota'),
'MP': u('Northern Mariana Islands'),
'OH': u('Ohio'),
'OK': u('Oklahoma'),
'OR': u('Oregon'),
'PW': u('Palau'),
'PA': u('Pennsylvania'),
'PR': u('Puerto Rico'),
'RI': u('Rhode Island'),
'SC': u('South Carolina'),
'SD': u('South Dakota'),
'TN': u('Tennessee'),
'TX': u('Texas'),
'UT': u('Utah'),
'VT': u('Vermont'),
'VI': u('Virgin Islands'),
'VA': u('Virginia'),
'WA': u('Washington'),
'WV': u('West Virginia'),
'WI': u('Wisconsin'),
'WY': u('Wyoming'),
}
'AL': u'Alabama',
'AK': u'Alaska',
'AS': u'American Samoa',
'AZ': u'Arizona',
'AR': u'Arkansas',
'CA': u'California',
'CO': u'Colorado',
'CT': u'Connecticut',
'DE': u'Delaware',
'DC': u'District of Columbia',
'FM': u'Federated States of Micronesia',
'FL': u'Florida',
'GA': u'Georgia',
'GU': u'Guam',
'HI': u'Hawaii',
'ID': u'Idaho',
'IL': u'Illinois',
'IN': u'Indiana',
'IA': u'Iowa',
'KS': u'Kansas',
'KY': u'Kentucky',
'LA': u'Louisiana',
'ME': u'Maine',
'MH': u'Marshall Islands',
'MD': u'Maryland',
'MA': u'Massachusetts',
'MI': u'Michigan',
'MN': u'Minnesota',
'MS': u'Mississippi',
'MO': u'Missouri',
'MT': u'Montana',
'NE': u'Nebraska',
'NV': u'Nevada',
'NH': u'New Hampshire',
'NJ': u'New Jersey',
'NM': u'New Mexico',
'NY': u'New York',
'NC': u'North Carolina',
'ND': u'North Dakota',
'MP': u'Northern Mariana Islands',
'OH': u'Ohio',
'OK': u'Oklahoma',
'OR': u'Oregon',
'PW': u'Palau',
'PA': u'Pennsylvania',
'PR': u'Puerto Rico',
'RI': u'Rhode Island',
'SC': u'South Carolina',
'SD': u'South Dakota',
'TN': u'Tennessee',
'TX': u'Texas',
'UT': u'Utah',
'VT': u'Vermont',
'VI': u'Virgin Islands',
'VA': u'Virginia',
'WA': u'Washington',
'WV': u'West Virginia',
'WI': u'Wisconsin',
'WY': u'Wyoming',
}


@implementer(interfaces.ITerm)
Expand Down
Loading

0 comments on commit 1d3f1c4

Please sign in to comment.