Skip to content

Commit eb9e3b4

Browse files
committed
Merge pull request #186 from posita/posita/185-fix-ensure_new_type
Fix #185. Return argument unmolested from ensure_new_type if conversion to new type does not exist.
2 parents d6cc41b + 2fcbb5a commit eb9e3b4

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/future/utils/__init__.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* tobytes(s)
2929
Take a text string, a byte string, or a sequence of characters taken
3030
from a byte string, and make a byte string.
31-
31+
3232
* raise_from()
3333
* raise_with_traceback()
3434
@@ -66,32 +66,32 @@ def python_2_unicode_compatible(cls):
6666
"""
6767
A decorator that defines __unicode__ and __str__ methods under Python
6868
2. Under Python 3, this decorator is a no-op.
69-
69+
7070
To support Python 2 and 3 with a single code base, define a __str__
7171
method returning unicode text and apply this decorator to the class, like
7272
this::
7373
7474
>>> from future.utils import python_2_unicode_compatible
75-
75+
7676
>>> @python_2_unicode_compatible
7777
... class MyClass(object):
7878
... def __str__(self):
7979
... return u'Unicode string: \u5b54\u5b50'
80-
80+
8181
>>> a = MyClass()
8282
8383
Then, after this import:
8484
8585
>>> from future.builtins import str
86-
86+
8787
the following is ``True`` on both Python 3 and 2::
88-
88+
8989
>>> str(a) == a.encode('utf-8').decode('utf-8')
9090
True
9191
9292
and, on a Unicode-enabled terminal with the right fonts, these both print the
9393
Chinese characters for Confucius::
94-
94+
9595
>>> print(a)
9696
>>> print(str(a))
9797
@@ -108,13 +108,13 @@ def with_metaclass(meta, *bases):
108108
Function from jinja2/_compat.py. License: BSD.
109109
110110
Use it like this::
111-
111+
112112
class BaseForm(object):
113113
pass
114-
114+
115115
class FormType(type):
116116
pass
117-
117+
118118
class Form(with_metaclass(FormType, BaseForm)):
119119
pass
120120
@@ -124,7 +124,7 @@ class Form(with_metaclass(FormType, BaseForm)):
124124
we also need to make sure that we downgrade the custom metaclass
125125
for one level to something closer to type (that's why __call__ and
126126
__init__ comes back from type etc.).
127-
127+
128128
This has the advantage over six.with_metaclass of not introducing
129129
dummy classes into the final MRO.
130130
"""
@@ -480,7 +480,7 @@ def implements_iterator(cls):
480480
From jinja2/_compat.py. License: BSD.
481481
482482
Use as a decorator like this::
483-
483+
484484
@implements_iterator
485485
class UppercasingIterator(object):
486486
def __init__(self, iterable):
@@ -489,7 +489,7 @@ def __iter__(self):
489489
return self
490490
def __next__(self):
491491
return next(self._iter).upper()
492-
492+
493493
'''
494494
if PY3:
495495
return cls
@@ -520,7 +520,7 @@ def is_new_style(cls):
520520
function to test for whether a class is new-style. (Python 3 only has
521521
new-style classes.)
522522
"""
523-
return hasattr(cls, '__class__') and ('__dict__' in dir(cls)
523+
return hasattr(cls, '__class__') and ('__dict__' in dir(cls)
524524
or hasattr(cls, '__slots__'))
525525

526526
# The native platform string and bytes types. Useful because ``str`` and
@@ -587,7 +587,7 @@ def native(obj):
587587
588588
On Py2, returns the corresponding native Py2 types that are
589589
superclasses for backported objects from Py3:
590-
590+
591591
>>> from builtins import str, bytes, int
592592
593593
>>> native(str(u'ABC'))
@@ -656,7 +656,7 @@ def as_native_str(encoding='utf-8'):
656656
unicode, into one that returns a native platform str.
657657
658658
Use it as a decorator like this::
659-
659+
660660
from __future__ import unicode_literals
661661
662662
class MyClass(object):
@@ -717,7 +717,7 @@ def ensure_new_type(obj):
717717
elif native_type == dict:
718718
return newdict(obj)
719719
else:
720-
return NotImplementedError('type %s not supported' % type(obj))
720+
return obj
721721
else:
722722
# Already a new type
723723
assert type(obj) in [newbytes, newstr]
@@ -738,4 +738,3 @@ def ensure_new_type(obj):
738738
'tobytes', 'viewitems', 'viewkeys', 'viewvalues',
739739
'with_metaclass'
740740
]
741-

tests/test_future/test_utils.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,24 @@ def test_native_str(self):
6262
for s in inputs:
6363
self.assertEqual(native_str(s), builtin_str(s))
6464
self.assertTrue(isinstance(native_str(s), builtin_str))
65-
66-
def test_native(self):
65+
66+
def test_native(self):
6767
a = int(10**20) # long int
6868
b = native(a)
6969
self.assertEqual(a, b)
7070
if PY2:
7171
self.assertEqual(type(b), long)
7272
else:
7373
self.assertEqual(type(b), int)
74-
74+
7575
c = bytes(b'ABC')
7676
d = native(c)
7777
self.assertEqual(c, d)
7878
if PY2:
7979
self.assertEqual(type(d), type(b'Py2 byte-string'))
8080
else:
8181
self.assertEqual(type(d), bytes)
82-
82+
8383
s = str(u'ABC')
8484
t = native(s)
8585
self.assertEqual(s, t)
@@ -151,7 +151,7 @@ def test_raise_from_None(self):
151151
except ValueError as e:
152152
self.assertTrue(isinstance(e.__context__, TypeError))
153153
self.assertIsNone(e.__cause__)
154-
154+
155155
@skip26
156156
def test_as_native_str(self):
157157
"""
@@ -161,9 +161,9 @@ class MyClass(object):
161161
@as_native_str()
162162
def __repr__(self):
163163
return u'abc'
164-
164+
165165
obj = MyClass()
166-
166+
167167
self.assertEqual(repr(obj), 'abc')
168168
if PY2:
169169
self.assertEqual(repr(obj), b'abc')
@@ -186,6 +186,9 @@ def test_ensure_new_type(self):
186186
self.assertEqual(ensure_new_type(i), i2)
187187
self.assertEqual(type(ensure_new_type(i)), int)
188188

189+
l = []
190+
self.assertIs(ensure_new_type(l), l)
191+
189192
def test_bytes_to_native_str(self):
190193
"""
191194
Test for issue #47
@@ -215,7 +218,7 @@ class DatabaseError(Exception):
215218

216219
# Python 2 and 3:
217220
from future.utils import raise_from
218-
221+
219222
class FileDatabase:
220223
def __init__(self, filename):
221224
try:
@@ -228,7 +231,7 @@ def __init__(self, filename):
228231
fd = FileDatabase('non_existent_file.txt')
229232
except Exception as e:
230233
assert isinstance(e.__cause__, IOError) # FileNotFoundError on
231-
# Py3.3+ inherits from IOError
234+
# Py3.3+ inherits from IOError
232235

233236
def testCauseSyntax(self):
234237
try:

0 commit comments

Comments
 (0)