28
28
* tobytes(s)
29
29
Take a text string, a byte string, or a sequence of characters taken
30
30
from a byte string, and make a byte string.
31
-
31
+
32
32
* raise_from()
33
33
* raise_with_traceback()
34
34
@@ -66,32 +66,32 @@ def python_2_unicode_compatible(cls):
66
66
"""
67
67
A decorator that defines __unicode__ and __str__ methods under Python
68
68
2. Under Python 3, this decorator is a no-op.
69
-
69
+
70
70
To support Python 2 and 3 with a single code base, define a __str__
71
71
method returning unicode text and apply this decorator to the class, like
72
72
this::
73
73
74
74
>>> from future.utils import python_2_unicode_compatible
75
-
75
+
76
76
>>> @python_2_unicode_compatible
77
77
... class MyClass(object):
78
78
... def __str__(self):
79
79
... return u'Unicode string: \u5b54 \u5b50 '
80
-
80
+
81
81
>>> a = MyClass()
82
82
83
83
Then, after this import:
84
84
85
85
>>> from future.builtins import str
86
-
86
+
87
87
the following is ``True`` on both Python 3 and 2::
88
-
88
+
89
89
>>> str(a) == a.encode('utf-8').decode('utf-8')
90
90
True
91
91
92
92
and, on a Unicode-enabled terminal with the right fonts, these both print the
93
93
Chinese characters for Confucius::
94
-
94
+
95
95
>>> print(a)
96
96
>>> print(str(a))
97
97
@@ -108,13 +108,13 @@ def with_metaclass(meta, *bases):
108
108
Function from jinja2/_compat.py. License: BSD.
109
109
110
110
Use it like this::
111
-
111
+
112
112
class BaseForm(object):
113
113
pass
114
-
114
+
115
115
class FormType(type):
116
116
pass
117
-
117
+
118
118
class Form(with_metaclass(FormType, BaseForm)):
119
119
pass
120
120
@@ -124,7 +124,7 @@ class Form(with_metaclass(FormType, BaseForm)):
124
124
we also need to make sure that we downgrade the custom metaclass
125
125
for one level to something closer to type (that's why __call__ and
126
126
__init__ comes back from type etc.).
127
-
127
+
128
128
This has the advantage over six.with_metaclass of not introducing
129
129
dummy classes into the final MRO.
130
130
"""
@@ -480,7 +480,7 @@ def implements_iterator(cls):
480
480
From jinja2/_compat.py. License: BSD.
481
481
482
482
Use as a decorator like this::
483
-
483
+
484
484
@implements_iterator
485
485
class UppercasingIterator(object):
486
486
def __init__(self, iterable):
@@ -489,7 +489,7 @@ def __iter__(self):
489
489
return self
490
490
def __next__(self):
491
491
return next(self._iter).upper()
492
-
492
+
493
493
'''
494
494
if PY3 :
495
495
return cls
@@ -520,7 +520,7 @@ def is_new_style(cls):
520
520
function to test for whether a class is new-style. (Python 3 only has
521
521
new-style classes.)
522
522
"""
523
- return hasattr (cls , '__class__' ) and ('__dict__' in dir (cls )
523
+ return hasattr (cls , '__class__' ) and ('__dict__' in dir (cls )
524
524
or hasattr (cls , '__slots__' ))
525
525
526
526
# The native platform string and bytes types. Useful because ``str`` and
@@ -587,7 +587,7 @@ def native(obj):
587
587
588
588
On Py2, returns the corresponding native Py2 types that are
589
589
superclasses for backported objects from Py3:
590
-
590
+
591
591
>>> from builtins import str, bytes, int
592
592
593
593
>>> native(str(u'ABC'))
@@ -656,7 +656,7 @@ def as_native_str(encoding='utf-8'):
656
656
unicode, into one that returns a native platform str.
657
657
658
658
Use it as a decorator like this::
659
-
659
+
660
660
from __future__ import unicode_literals
661
661
662
662
class MyClass(object):
@@ -717,7 +717,7 @@ def ensure_new_type(obj):
717
717
elif native_type == dict :
718
718
return newdict (obj )
719
719
else :
720
- return NotImplementedError ( 'type %s not supported' % type ( obj ))
720
+ return obj
721
721
else :
722
722
# Already a new type
723
723
assert type (obj ) in [newbytes , newstr ]
@@ -738,4 +738,3 @@ def ensure_new_type(obj):
738
738
'tobytes' , 'viewitems' , 'viewkeys' , 'viewvalues' ,
739
739
'with_metaclass'
740
740
]
741
-
0 commit comments