@@ -36,8 +36,8 @@ then the fifth test fails too::
3636 AssertionError
3737
3838
39- After importing the builtins from ``future ``, the tests pass on Python 2 as
40- on Python 3::
39+ After importing the builtins from ``future ``, all these tests pass on
40+ Python 2 as on Python 3::
4141
4242 >>> from __future__ import unicode_literals
4343 >>> from future.builtins import *
@@ -50,6 +50,11 @@ on Python 3::
5050
5151Note that the last test requires that ``unicode_literals `` be imported to succeed.
5252
53+ This works because the backported types ``int ``, ``bytes `` and ``str ``
54+ have metaclasses that override ``__instancecheck__ ``. See `PEP 3119
55+ <http://www.python.org/dev/peps/pep-3119/#overloading-isinstance-and-issubclass> `_
56+ for details.
57+
5358
5459Passing data to/from Python 2 libraries
5560---------------------------------------
@@ -110,96 +115,3 @@ The objects ``native_str`` and ``native_bytes`` are available in
110115The functions ``native_str_to_bytes `` and ``bytes_to_native_str `` are also
111116available for more explicit conversions.
112117
113-
114- .. ``isinstance`` checks are sometimes fragile and generally discouraged in
115- .. Python code (in favour of duck typing). When passing ``future``'s backported
116- .. ``int``, ``str``, or ``bytes`` types from Python 3 to standard library code
117- .. or 3rd-party modules on Python 2 that contain checks with ``isinstance``, some
118- .. special handling may be required to achieve portability.
119- ..
120- .. This section explains the issues involved and describes some utility functions
121- .. in :mod:`future.utils` that assist with writing clean code.
122- ..
123- .. Distinguishing bytes from unicode text
124- .. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125- ..
126- .. On Python 2, (unicode) string literals ``'...'`` and byte-string literals
127- .. ``b'...'`` create instances of the superclasses of the backported
128- .. :class:`str` and :class:`bytes` types from :mod:`future.builtins` (i.e.
129- .. the native Py2 unicode and 8-bit string types). Therefore ``isinstance`` checks
130- .. in standard library code or 3rd-party modules should succeed. Just keep in mind
131- .. that with ``future``, ``str`` and ``bytes`` are like Python 3's types of the
132- .. same names.
133-
134- .. Old
135- .. ~~~
136- .. If type-checking is necessary to distinguish unicode text from bytes
137- .. portably across Py3 and Py2, utility functions called :func:`istext` and
138- .. :func:`isbytes` are available in :mod:`future.utils`. You can use them
139- .. as follows::
140- ..
141- .. >>> from __future__ import unicode_literals
142- .. >>> from future.builtins import *
143- .. >>> from future.utils import istext, isbytes
144- ..
145- .. >>> assert istext('My (unicode) string')
146- .. >>> assert istext(str('My (unicode) string'))
147- ..
148- .. >>> assert isbytes(b'My byte-string')
149- .. >>> assert isbytes(bytes(b'My byte-string'))
150- ..
151- .. ``istext(s)`` tests whether the object ``s`` is (or inherits from) a
152- .. unicode string. It is equivalent to the following expression::
153- ..
154- .. isinstance(s, type(u''))
155- ..
156- .. which is ``True`` if ``s`` is a native Py3 string, Py2 unicode object, or
157- .. :class:`future.builtins.str` object on Py2.
158- ..
159- .. Likewise, ``isbytes(b)`` tests whether ``b`` is (or inherits from) an
160- .. 8-bit byte-string. It is equivalent to::
161- ..
162- .. isinstance(b, type(b''))
163- ..
164- .. which is ``True`` if ``b`` is a native Py3 bytes object, Py2 8-bit str,
165- .. or :class:`future.builtins.bytes` object on Py2.
166-
167-
168- .. Integers and long integers
169- .. ~~~~~~~~~~~~~~~~~~~~~~~~~~
170- ..
171- .. Python 3 unifies Python 2's concepts of integers (``int``) and long
172- .. integers (``long``) into a single ``int`` type.
173- ..
174- .. On Python 2, checks such as ``isinstance(x, int)`` are fragile because
175- .. ``long`` does not inherit from ``int``. So when an integer gets too
176- .. large, the check starts to fail. For example::
177- ..
178- .. >>> x = 2**62
179- .. >>> assert isinstance(x, int)
180- .. >>> x *= 2
181- .. >>> assert isinstance(x, int)
182- .. Traceback (most recent call last):
183- .. File "<stdin>", line 1, in <module>
184- .. AssertionError
185- ..
186- .. ``future``'s backported ``int`` object doesn't help with these checks;
187- .. both of them fail. To test if a variable is an integer on Py3 or either an
188- .. ``int`` or ``long`` on Py2, you can use the ``future.utils.isint``
189- .. function ::
190- ..
191- .. >>> from future.utils import isint
192- ..
193- .. >>> assert isint(10)
194- .. >>> assert isint(10**1000)
195- ..
196- .. An alternative is to use the abstract base class :class:`Integral`
197- .. from the :mod:`numbers` module as follows::
198- ..
199- .. >>> from numbers import Integral
200- ..
201- .. >>> assert isinstance(10, Integral)
202- .. >>> assert isinstance(10**1000, Integral)
203-
204-
205-
0 commit comments