Skip to content

Commit 3b9c211

Browse files
committed
future.utils: add string_types etc. and clean up docs (issue #126)
1 parent c865899 commit 3b9c211

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

docs/whatsnew.rst

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Bug fixes:
4040
- ``futurize`` no longer breaks working Py2 code by changing ``basestring`` to
4141
``str``. Instead it imports the ``basestring`` forward-port from
4242
``past.builtins`` (issues #127 and #156)
43+
- ``future.utils``: add ``string_types`` etc. and update docs (issue #126)
4344

4445

4546
What's new in version 0.14.3 (2014-12-15)

src/future/utils/__init__.py

+53-37
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
"""
22
A selection of cross-compatible functions for Python 2 and 3.
33
4-
This exports useful functions for 2/3 compatible code that are not
5-
builtins on Python 3:
4+
This module exports useful functions for 2/3 compatible code:
65
76
* bind_method: binds functions to classes
87
* ``native_str_to_bytes`` and ``bytes_to_native_str``
98
* ``native_str``: always equal to the native platform string object (because
109
this may be shadowed by imports from future.builtins)
1110
* lists: lrange(), lmap(), lzip(), lfilter()
12-
* iterable method compatibility: iteritems, iterkeys, itervalues
11+
* iterable method compatibility:
12+
- iteritems, iterkeys, itervalues
13+
- viewitems, viewkeys, viewvalues
1314
14-
* Uses the original method if available, otherwise uses items, keys, values.
15+
These use the original method if available, otherwise they use items,
16+
keys, values.
1517
1618
* types:
1719
@@ -26,39 +28,15 @@
2628
* tobytes(s)
2729
Take a text string, a byte string, or a sequence of characters taken
2830
from a byte string, and make a byte string.
29-
30-
This module also defines a simple decorator called
31-
``python_2_unicode_compatible`` (from django.utils.encoding) which
32-
defines ``__unicode__`` and ``__str__`` methods consistently under Python
33-
3 and 2. To support Python 3 and 2 with a single code base, simply define
34-
a ``__str__`` method returning unicode text and apply the
35-
python_2_unicode_compatible decorator to the class like this::
36-
37-
>>> from future.utils import python_2_unicode_compatible
38-
39-
>>> @python_2_unicode_compatible
40-
... class MyClass(object):
41-
... def __str__(self):
42-
... return u'Unicode string: \u5b54\u5b50'
4331
44-
>>> a = MyClass()
32+
* raise_from()
33+
* raise_with_traceback()
4534
46-
Then, after this import:
35+
This module also defines these decorators:
4736
48-
>>> from future.builtins import str
49-
50-
the following is ``True`` on both Python 3 and 2::
51-
52-
>>> str(a) == a.encode('utf-8').decode('utf-8')
53-
True
54-
55-
and, on a Unicode-enabled terminal with the right fonts, these both print the
56-
Chinese characters for Confucius::
57-
58-
print(a)
59-
print(str(a))
60-
61-
On Python 3, this decorator is a no-op.
37+
* ``python_2_unicode_compatible``
38+
* ``with_metaclass``
39+
* ``implements_iterator``
6240
6341
Some of the functions in this module come from the following sources:
6442
@@ -87,10 +65,35 @@
8765
def python_2_unicode_compatible(cls):
8866
"""
8967
A decorator that defines __unicode__ and __str__ methods under Python
90-
2. Under Python 3 it does nothing.
68+
2. Under Python 3, this decorator is a no-op.
9169
9270
To support Python 2 and 3 with a single code base, define a __str__
93-
method returning unicode text and apply this decorator to the class.
71+
method returning unicode text and apply this decorator to the class, like
72+
this::
73+
74+
>>> from future.utils import python_2_unicode_compatible
75+
76+
>>> @python_2_unicode_compatible
77+
... class MyClass(object):
78+
... def __str__(self):
79+
... return u'Unicode string: \u5b54\u5b50'
80+
81+
>>> a = MyClass()
82+
83+
Then, after this import:
84+
85+
>>> from future.builtins import str
86+
87+
the following is ``True`` on both Python 3 and 2::
88+
89+
>>> str(a) == a.encode('utf-8').decode('utf-8')
90+
True
91+
92+
and, on a Unicode-enabled terminal with the right fonts, these both print the
93+
Chinese characters for Confucius::
94+
95+
>>> print(a)
96+
>>> print(str(a))
9497
9598
The implementation comes from django.utils.encoding.
9699
"""
@@ -135,7 +138,7 @@ def __new__(cls, name, this_bases, d):
135138
return metaclass('temporary_class', None, {})
136139

137140

138-
# Definitions from pandas.compat follow:
141+
# Definitions from pandas.compat and six.py follow:
139142
if PY3:
140143
def bchr(s):
141144
return bytes([s])
@@ -146,6 +149,13 @@ def bstr(s):
146149
return bytes(s)
147150
def bord(s):
148151
return s
152+
153+
string_types = str,
154+
integer_types = int,
155+
class_types = type,
156+
text_type = str
157+
binary_type = bytes
158+
149159
else:
150160
# Python 2
151161
def bchr(s):
@@ -155,6 +165,12 @@ def bstr(s):
155165
def bord(s):
156166
return ord(s)
157167

168+
string_types = basestring,
169+
integer_types = (int, long)
170+
class_types = (type, types.ClassType)
171+
text_type = unicode
172+
binary_type = str
173+
158174
###
159175

160176
if PY3:

0 commit comments

Comments
 (0)