2
2
compat
3
3
======
4
4
5
- Cross-compatible functions for Python 2 and 3 .
5
+ Cross-compatible functions for different versions of Python .
6
6
7
- Key items to import for 2/3 compatible code:
7
+ Key items to import for compatible code:
8
8
* lists: lrange(), lmap(), lzip(), lfilter()
9
9
* iterable method compatibility: iteritems, iterkeys, itervalues
10
10
* Uses the original method if available, otherwise uses items, keys, values.
23
23
import platform
24
24
import struct
25
25
26
- PY2 = sys .version_info [0 ] == 2
27
- PY3 = sys .version_info [0 ] >= 3
28
26
PY36 = sys .version_info >= (3 , 6 )
29
27
PY37 = sys .version_info >= (3 , 7 )
30
28
PYPY = platform .python_implementation () == 'PyPy'
@@ -47,25 +45,16 @@ def lfilter(*args, **kwargs):
47
45
return list (filter (* args , ** kwargs ))
48
46
49
47
50
- if PY2 :
51
- def iteritems (obj , ** kw ):
52
- return obj .iteritems (** kw )
48
+ def iteritems (obj , ** kw ):
49
+ return iter (obj .items (** kw ))
53
50
54
- def iterkeys (obj , ** kw ):
55
- return obj .iterkeys (** kw )
56
51
57
- def itervalues (obj , ** kw ):
58
- return obj .itervalues (** kw )
52
+ def iterkeys (obj , ** kw ):
53
+ return iter ( obj .keys (** kw ) )
59
54
60
- else :
61
- def iteritems (obj , ** kw ):
62
- return iter (obj .items (** kw ))
63
-
64
- def iterkeys (obj , ** kw ):
65
- return iter (obj .keys (** kw ))
66
55
67
- def itervalues (obj , ** kw ):
68
- return iter (obj .values (** kw ))
56
+ def itervalues (obj , ** kw ):
57
+ return iter (obj .values (** kw ))
69
58
70
59
# ----------------------------------------------------------------------------
71
60
# functions largely based / taken from the six module
@@ -75,42 +64,33 @@ def itervalues(obj, **kw):
75
64
# found at https://bitbucket.org/gutworth/six
76
65
77
66
78
- if PY3 :
79
- def to_str (s ):
80
- """
81
- Convert bytes and non-string into Python 3 str
82
- """
83
- if isinstance (s , bytes ):
84
- s = s .decode ('utf-8' )
85
- elif not isinstance (s , str ):
86
- s = str (s )
87
- return s
88
-
89
- def set_function_name (f , name , cls ):
90
- """ Bind the name/qualname attributes of the function """
91
- f .__name__ = name
92
- f .__qualname__ = '{klass}.{name}' .format (
93
- klass = cls .__name__ ,
94
- name = name )
95
- f .__module__ = cls .__module__
96
- return f
97
- else :
98
- def to_str (s ):
99
- """
100
- Convert unicode and non-string into Python 2 str
101
- """
102
- if not isinstance (s , basestring ):
103
- s = str (s )
104
- return s
67
+ def to_str (s ):
68
+ """
69
+ Convert bytes and non-string into Python 3 str
70
+ """
71
+ if isinstance (s , bytes ):
72
+ s = s .decode ('utf-8' )
73
+ elif not isinstance (s , str ):
74
+ s = str (s )
75
+ return s
105
76
106
- def set_function_name (f , name , cls ):
107
- """ Bind the name attributes of the function """
108
- f .__name__ = name
109
- return f
77
+
78
+ def set_function_name (f , name , cls ):
79
+ """
80
+ Bind the name/qualname attributes of the function
81
+ """
82
+ f .__name__ = name
83
+ f .__qualname__ = '{klass}.{name}' .format (
84
+ klass = cls .__name__ ,
85
+ name = name )
86
+ f .__module__ = cls .__module__
87
+ return f
110
88
111
89
112
90
def add_metaclass (metaclass ):
113
- """Class decorator for creating a class with a metaclass."""
91
+ """
92
+ Class decorator for creating a class with a metaclass.
93
+ """
114
94
def wrapper (cls ):
115
95
orig_vars = cls .__dict__ .copy ()
116
96
orig_vars .pop ('__dict__' , None )
@@ -121,22 +101,14 @@ def wrapper(cls):
121
101
return wrapper
122
102
123
103
124
- if PY3 :
125
- def raise_with_traceback (exc , traceback = Ellipsis ):
126
- if traceback == Ellipsis :
127
- _ , _ , traceback = sys .exc_info ()
128
- raise exc .with_traceback (traceback )
129
- else :
130
- # this version of raise is a syntax error in Python 3
131
- exec ("""
132
104
def raise_with_traceback (exc , traceback = Ellipsis ):
105
+ """
106
+ Raise exception with existing traceback.
107
+ If traceback is not passed, uses sys.exc_info() to get traceback.
108
+ """
133
109
if traceback == Ellipsis :
134
110
_ , _ , traceback = sys .exc_info ()
135
- raise exc, None, traceback
136
- """ )
137
-
138
- raise_with_traceback .__doc__ = """Raise exception with existing traceback.
139
- If traceback is not passed, uses sys.exc_info() to get traceback."""
111
+ raise exc .with_traceback (traceback )
140
112
141
113
# In Python 3.7, the private re._pattern_type is removed.
142
114
# Python 3.5+ have typing.re.Pattern
0 commit comments