Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
69 lines (50 loc) · 1.87 KB
/
Copy path__init__.py
File metadata and controls
69 lines (50 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
PY3 = sys.version_info[0] == 3
PY2 = sys.version_info[0] == 2
PYPY = hasattr(sys, 'pypy_translation_info')
def with_metaclass(meta, *bases):
"""
Function from jinja2/_compat.py. License: BSD.
Use it like this::
class BaseForm(object):
pass
class FormType(type):
pass
class Form(with_metaclass(FormType, BaseForm)):
pass
This requires a bit of explanation: the basic idea is to make a
dummy metaclass for one level of class instantiation that replaces
itself with the actual metaclass. Because of internal type checks
we also need to make sure that we downgrade the custom metaclass
for one level to something closer to type (that's why __call__ and
__init__ comes back from type etc.).
This has the advantage over six.with_metaclass of not introducing
dummy classes into the final MRO.
"""
class metaclass(meta):
__call__ = type.__call__
__init__ = type.__init__
def __new__(cls, name, this_bases, d):
if this_bases is None:
return type.__new__(cls, name, (), d)
return meta(name, bases, d)
return metaclass('temporary_class', None, {})
def native(obj):
"""
On Py2, this is a no-op: native(obj) -> obj
On Py3, returns the corresponding native Py3 types that are
superclasses for forward-ported objects from Py2:
>>> from past.builtins import str, dict
>>> native(str(b'ABC')) # Output on Py3 follows. On Py2, output is 'ABC'
b'ABC'
>>> type(native(str(b'ABC')))
bytes
Existing native types on Py3 will be returned unchanged:
>>> type(native(b'ABC'))
bytes
"""
if hasattr(obj, '__native__'):
return obj.__native__()
else:
return obj
__all__ = ['PY3', 'PY2', 'PYPY', 'with_metaclass', 'native']