|
| 1 | +# -*- coding: utf8 -*- |
| 2 | +import sys |
| 3 | + |
| 4 | +PY2 = sys.version_info[0] == 2 |
| 5 | + |
| 6 | +_identity = lambda x: x |
| 7 | + |
| 8 | + |
| 9 | +if PY2: |
| 10 | + unichr = unichr |
| 11 | + text_type = unicode |
| 12 | + string_types = (str, unicode) |
| 13 | + integer_types = (int, long) |
| 14 | + from urllib import urlretrieve |
| 15 | + |
| 16 | + text_to_native = lambda s, enc: s.encode(enc) |
| 17 | + |
| 18 | + iterkeys = lambda d: d.iterkeys() |
| 19 | + itervalues = lambda d: d.itervalues() |
| 20 | + iteritems = lambda d: d.iteritems() |
| 21 | + |
| 22 | + from cStringIO import StringIO as BytesIO |
| 23 | + from StringIO import StringIO |
| 24 | + import cPickle as pickle |
| 25 | + import ConfigParser as configparser |
| 26 | + |
| 27 | + from itertools import izip, imap |
| 28 | + range_type = xrange |
| 29 | + |
| 30 | + cmp = cmp |
| 31 | + |
| 32 | + input = raw_input |
| 33 | + from string import lower as ascii_lowercase |
| 34 | + import urlparse |
| 35 | + |
| 36 | + exec('def reraise(tp, value, tb=None):\n raise tp, value, tb') |
| 37 | + |
| 38 | + def implements_to_string(cls): |
| 39 | + cls.__unicode__ = cls.__str__ |
| 40 | + cls.__str__ = lambda x: x.__unicode__().encode('utf-8') |
| 41 | + return cls |
| 42 | + |
| 43 | + def console_to_str(s): |
| 44 | + return s.decode('utf_8') |
| 45 | + |
| 46 | +else: |
| 47 | + unichr = chr |
| 48 | + text_type = str |
| 49 | + string_types = (str,) |
| 50 | + integer_types = (int, ) |
| 51 | + |
| 52 | + text_to_native = lambda s, enc: s |
| 53 | + |
| 54 | + iterkeys = lambda d: iter(d.keys()) |
| 55 | + itervalues = lambda d: iter(d.values()) |
| 56 | + iteritems = lambda d: iter(d.items()) |
| 57 | + |
| 58 | + from io import StringIO, BytesIO |
| 59 | + import pickle |
| 60 | + import configparser |
| 61 | + |
| 62 | + izip = zip |
| 63 | + imap = map |
| 64 | + range_type = range |
| 65 | + |
| 66 | + cmp = lambda a, b: (a > b) - (a < b) |
| 67 | + |
| 68 | + input = input |
| 69 | + from string import ascii_lowercase |
| 70 | + import urllib.parse as urllib |
| 71 | + import urllib.parse as urlparse |
| 72 | + from urllib.request import urlretrieve |
| 73 | + |
| 74 | + console_encoding = sys.__stdout__.encoding |
| 75 | + |
| 76 | + implements_to_string = _identity |
| 77 | + |
| 78 | + def console_to_str(s): |
| 79 | + """ From pypa/pip project, pip.backwardwardcompat. License MIT. """ |
| 80 | + try: |
| 81 | + return s.decode(console_encoding) |
| 82 | + except UnicodeDecodeError: |
| 83 | + return s.decode('utf_8') |
| 84 | + |
| 85 | + def reraise(tp, value, tb=None): |
| 86 | + if value.__traceback__ is not tb: |
| 87 | + raise(value.with_traceback(tb)) |
| 88 | + raise value |
| 89 | + |
| 90 | + |
| 91 | +number_types = integer_types + (float,) |
0 commit comments