|
| 1 | +""" Compatibility. |
| 2 | +
|
| 3 | + Some py2/py3 compatibility support based on a stripped down |
| 4 | + version of six so we don't have to depend on a specific version |
| 5 | + of it. |
| 6 | +
|
| 7 | + :copyright: (c) 2014 by Armin Ronacher. |
| 8 | + :license: BSD |
| 9 | +""" |
| 10 | +import sys |
| 11 | + |
| 12 | +PY2 = sys.version_info[0] == 2 |
| 13 | +_identity = lambda x: x |
| 14 | + |
| 15 | + |
| 16 | +if not PY2: |
| 17 | + text_type = str |
| 18 | + string_types = (str,) |
| 19 | + integer_types = (int, ) |
| 20 | + |
| 21 | + iterkeys = lambda d: iter(d.keys()) |
| 22 | + itervalues = lambda d: iter(d.values()) |
| 23 | + iteritems = lambda d: iter(d.items()) |
| 24 | + |
| 25 | + from io import StringIO |
| 26 | + from queue import Queue # noqa |
| 27 | + |
| 28 | + def reraise(tp, value, tb=None): |
| 29 | + if value.__traceback__ is not tb: |
| 30 | + raise value.with_traceback(tb) |
| 31 | + raise value |
| 32 | + |
| 33 | + implements_to_string = _identity |
| 34 | + |
| 35 | +else: |
| 36 | + text_type = unicode |
| 37 | + string_types = (str, unicode) |
| 38 | + integer_types = (int, long) |
| 39 | + |
| 40 | + iterkeys = lambda d: d.iterkeys() |
| 41 | + itervalues = lambda d: d.itervalues() |
| 42 | + iteritems = lambda d: d.iteritems() |
| 43 | + |
| 44 | + from cStringIO import StringIO |
| 45 | + from Queue import Queue |
| 46 | + |
| 47 | + exec('def reraise(tp, value, tb=None):\n raise tp, value, tb') |
| 48 | + |
| 49 | + def implements_to_string(cls): |
| 50 | + cls.__unicode__ = cls.__str__ |
| 51 | + cls.__str__ = lambda x: x.__unicode__().encode('utf-8') |
| 52 | + return cls |
| 53 | + |
| 54 | + |
| 55 | +def with_metaclass(meta, *bases): |
| 56 | + # This requires a bit of explanation: the basic idea is to make a |
| 57 | + # dummy metaclass for one level of class instantiation that replaces |
| 58 | + # itself with the actual metaclass. Because of internal type checks |
| 59 | + # we also need to make sure that we downgrade the custom metaclass |
| 60 | + # for one level to something closer to type (that's why __call__ and |
| 61 | + # __init__ comes back from type etc.). |
| 62 | + # |
| 63 | + # This has the advantage over six.with_metaclass in that it does not |
| 64 | + # introduce dummy classes into the final MRO. |
| 65 | + class metaclass(meta): |
| 66 | + __call__ = type.__call__ |
| 67 | + __init__ = type.__init__ |
| 68 | + def __new__(cls, name, this_bases, d): |
| 69 | + if this_bases is None: |
| 70 | + return type.__new__(cls, name, (), d) |
| 71 | + return meta(name, bases, d) |
| 72 | + return metaclass('temporary_class', None, {}) |
| 73 | + |
| 74 | + |
| 75 | +# Certain versions of pypy have a bug where clearing the exception stack |
| 76 | +# breaks the __exit__ function in a very peculiar way. This is currently |
| 77 | +# true for pypy 2.2.1 for instance. The second level of exception blocks |
| 78 | +# is necessary because pypy seems to forget to check if an exception |
| 79 | +# happend until the next bytecode instruction? |
| 80 | +BROKEN_PYPY_CTXMGR_EXIT = False |
| 81 | +if hasattr(sys, 'pypy_version_info'): |
| 82 | + class _Mgr(object): |
| 83 | + def __enter__(self): |
| 84 | + return self |
| 85 | + def __exit__(self, *args): |
| 86 | + sys.exc_clear() |
| 87 | + try: |
| 88 | + try: |
| 89 | + with _Mgr(): |
| 90 | + raise AssertionError() |
| 91 | + except: |
| 92 | + raise |
| 93 | + except TypeError: |
| 94 | + BROKEN_PYPY_CTXMGR_EXIT = True |
| 95 | + except AssertionError: |
| 96 | + pass |
| 97 | + |
| 98 | +# pylama:skip=1 |
0 commit comments