Skip to content

Commit f4472dc

Browse files
committed
Updates and tweaks for isinstance()
* Also remove the obsolete ``int`` declaration in future/builtins/misc.py
1 parent a45d318 commit f4472dc

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

future/builtins/backports/newbytes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
from collections import Iterable
2121
from numbers import Integral
2222

23-
from future.utils import istext, isbytes
23+
from future.utils import istext, isbytes, PY3
2424
from future.builtins.backports import no, issubset
2525

2626

2727
_builtin_bytes = bytes
2828

29+
if PY3:
30+
# We'll probably never use newstr on Py3 anyway...
31+
unicode = str
32+
2933

3034
class newbytes(_builtin_bytes):
3135
"""

future/builtins/backports/newstr.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
This module redefines ``str`` on Python 2.x to be a subclass of the Py2
33
``unicode`` type that behaves like the Python 3.x ``str``.
44
5+
The main differences between ``newstr`` and Python 2.x's ``unicode`` type are
6+
the stricter type-checking and absence of a `u''` prefix in the representation.
7+
58
It is designed to be used together with the ``unicode_literals`` import
69
as follows:
710
811
>>> from __future__ import unicode_literals
9-
>>> from future.builtins import str
12+
>>> from future.builtins import str, isinstance
1013
1114
On Python 3.x and normally on Python 2.x, these expressions hold
1215
@@ -26,10 +29,9 @@
2629
>>> isinstance('blah', str)
2730
False
2831
29-
This module is designed to be imported together with unicode_literals on
30-
Python 2 to bring the meaning of ``str`` back into alignment with
31-
unprefixed string literals (i.e. ``unicode`` if ``unicode_literals`` has
32-
been imported from ``__future__``).
32+
This module is designed to be imported together with ``unicode_literals`` on
33+
Python 2 to bring the meaning of ``str`` back into alignment with unprefixed
34+
string literals (i.e. ``unicode``).
3335
3436
Note that ``str()`` (and ``print()``) would then normally call the
3537
``__unicode__`` method on objects in Python 2. To define string
@@ -151,7 +153,6 @@ def replace(self, old, new, *args):
151153
def decode(self, *args):
152154
raise AttributeError("decode method has been disabled in newstr")
153155

154-
@no(bytes, 1)
155156
def encode(self, encoding='utf-8', errors='strict'):
156157
"""
157158
Returns bytes

future/builtins/misc.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
- ``input`` (equivalent to ``raw_input`` on Py2)
1212
- ``open`` (equivalent to io.open on Py2)
1313
14-
and:
15-
- ``int`` (currently unchanged)
14+
This function is also defined specially:
15+
16+
- ``isinstance``
17+
18+
to be compatible with the backported ``int``, ``str``, and ``bytes`` types of
19+
``future.builtins``.
1620
1721
1822
input()
@@ -41,12 +45,13 @@
4145
from io import open
4246
from future_builtins import ascii, oct, hex
4347
from __builtin__ import unichr as chr
44-
from __builtin__ import int
4548
import __builtin__
4649

47-
# Is it a good idea to define this? It'll mean many fewer lines of code
48-
# need to be changed. Example: xlwt package uses isinstance() a lot.
49-
# On the other hand, it may break some things and make them harder to debug.
50+
# Is it a good idea to define this? It'll mean many fewer lines of
51+
# type-checking code need to be changed to using custom
52+
# functions (like future.utils.isint). Example: the xlwt package uses
53+
# isinstance() a lot. On the other hand, it may break some things and make
54+
# them harder to debug.
5055
def isinstance(obj, class_or_type_or_tuple):
5156
"""
5257
isinstance(object, class-or-type-or-tuple) -> bool
@@ -80,15 +85,15 @@ def isinstance(obj, class_or_type_or_tuple):
8085
# In case some code wants to import 'callable' portably from Py3.0/3.1:
8186
callable = __builtin__.callable
8287

83-
__all__ = ['ascii', 'chr', 'hex', 'input', 'int', 'oct', 'open']
88+
__all__ = ['ascii', 'chr', 'hex', 'input', 'isinstance', 'oct', 'open']
8489

8590
else:
8691
import builtins
8792
ascii = builtins.ascii
8893
chr = builtins.chr
8994
hex = builtins.hex
9095
input = builtins.input
91-
int = builtins.int
96+
isinstance = builtins.isinstance
9297
oct = builtins.oct
9398
open = builtins.open
9499

future/tests/test_str.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def test_str_encode_utf8(self):
2727

2828
def test_str_encode_decode_with_py2_str_arg(self):
2929
# Try passing a standard Py2 string (as if unicode_literals weren't imported)
30-
b = str(TEST_UNICODE_STR).encode(utils.native_str(b'utf-8'))
30+
b = str(TEST_UNICODE_STR).encode(utils.bytes_to_native_str(b'utf-8'))
3131
self.assertTrue(isinstance(b, bytes))
3232
self.assertFalse(isinstance(b, str))
33-
s = b.decode(utils.native_str(b'utf-8'))
33+
s = b.decode(utils.bytes_to_native_str(b'utf-8'))
3434
self.assertTrue(isinstance(s, str))
3535
self.assertEqual(s, TEST_UNICODE_STR)
3636

0 commit comments

Comments
 (0)