Skip to content

Commit 2fcbb5a

Browse files
committed
Fix #185. Return argument unmolested from ensure_new_type if conversion to new type does not exist.
1 parent 3bf80a6 commit 2fcbb5a

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
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-

0 commit comments

Comments
 (0)