annotate MoinMoin/support/werkzeug/_compat.py @ 6111:1fdd537e9d83

SubProcess: reimplement exec_cmd subclassing Popen and overriding some methods isn't pretty. the code we have was written for py 2.4 or so and the py 2.7 Popen looked quite different. this way with the timer should be less problematic.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 06 Sep 2016 04:39:28 +0200
parents 9f12f41504fc
children
rev   line source
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # flake8: noqa
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 # This whole file is full of lint errors
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 import codecs
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4 import sys
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 import operator
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 import functools
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7 import warnings
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 try:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10 import builtins
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 except ImportError:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 import __builtin__ as builtins
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 PY2 = sys.version_info[0] == 2
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 WIN = sys.platform.startswith('win')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 _identity = lambda x: x
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 if PY2:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 unichr = unichr
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 text_type = unicode
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 string_types = (str, unicode)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 integer_types = (int, long)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 iterkeys = lambda d, *args, **kwargs: d.iterkeys(*args, **kwargs)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 itervalues = lambda d, *args, **kwargs: d.itervalues(*args, **kwargs)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 iteritems = lambda d, *args, **kwargs: d.iteritems(*args, **kwargs)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 iterlists = lambda d, *args, **kwargs: d.iterlists(*args, **kwargs)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 iterlistvalues = lambda d, *args, **kwargs: d.iterlistvalues(*args, **kwargs)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 int_to_byte = chr
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 iter_bytes = iter
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38 def fix_tuple_repr(obj):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 def __repr__(self):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40 cls = self.__class__
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41 return '%s(%s)' % (cls.__name__, ', '.join(
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 '%s=%r' % (field, self[index])
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43 for index, field in enumerate(cls._fields)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44 ))
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45 obj.__repr__ = __repr__
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46 return obj
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
47
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
48 def implements_iterator(cls):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49 cls.next = cls.__next__
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
50 del cls.__next__
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51 return cls
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 def implements_to_string(cls):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 cls.__unicode__ = cls.__str__
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56 return cls
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 def native_string_result(func):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 def wrapper(*args, **kwargs):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 return func(*args, **kwargs).encode('utf-8')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 return functools.update_wrapper(wrapper, func)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 def implements_bool(cls):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 cls.__nonzero__ = cls.__bool__
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 del cls.__bool__
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 return cls
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 from itertools import imap, izip, ifilter
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 range_type = xrange
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 from StringIO import StringIO
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 from cStringIO import StringIO as BytesIO
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 NativeStringIO = BytesIO
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 def make_literal_wrapper(reference):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 return _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 def normalize_string_tuple(tup):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 """Normalizes a string tuple to a common type. Following Python 2
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 rules, upgrades to unicode are implicit.
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81 """
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 if any(isinstance(x, text_type) for x in tup):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 return tuple(to_unicode(x) for x in tup)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84 return tup
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86 def try_coerce_native(s):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87 """Try to coerce a unicode string to native if possible. Otherwise,
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 leave it as unicode.
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 """
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 try:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91 return to_native(s)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92 except UnicodeError:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93 return s
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 wsgi_get_bytes = _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98 return s.decode(charset, errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
100 def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101 if isinstance(s, bytes):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102 return s
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 return s.encode(charset, errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
105 def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
106 if x is None:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
107 return None
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108 if isinstance(x, (bytes, bytearray, buffer)):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 return bytes(x)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 if isinstance(x, unicode):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
111 return x.encode(charset, errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 raise TypeError('Expected bytes')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114 def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115 if x is None or isinstance(x, str):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
116 return x
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
117 return x.encode(charset, errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
118
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
119 else:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120 unichr = chr
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
121 text_type = str
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
122 string_types = (str, )
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
123 integer_types = (int, )
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
124
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
125 iterkeys = lambda d, *args, **kwargs: iter(d.keys(*args, **kwargs))
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
126 itervalues = lambda d, *args, **kwargs: iter(d.values(*args, **kwargs))
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
127 iteritems = lambda d, *args, **kwargs: iter(d.items(*args, **kwargs))
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
128
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
129 iterlists = lambda d, *args, **kwargs: iter(d.lists(*args, **kwargs))
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
130 iterlistvalues = lambda d, *args, **kwargs: iter(d.listvalues(*args, **kwargs))
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
131
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
132 int_to_byte = operator.methodcaller('to_bytes', 1, 'big')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
133 iter_bytes = functools.partial(map, int_to_byte)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
134
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
135 def reraise(tp, value, tb=None):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
136 if value.__traceback__ is not tb:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
137 raise value.with_traceback(tb)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
138 raise value
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
139
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
140 fix_tuple_repr = _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
141 implements_iterator = _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
142 implements_to_string = _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
143 implements_bool = _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144 native_string_result = _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
145 imap = map
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
146 izip = zip
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 ifilter = filter
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
148 range_type = range
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
149
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
150 from io import StringIO, BytesIO
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
151 NativeStringIO = StringIO
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
152
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
153 _latin1_encode = operator.methodcaller('encode', 'latin1')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
154
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
155 def make_literal_wrapper(reference):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
156 if isinstance(reference, text_type):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
157 return _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
158 return _latin1_encode
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160 def normalize_string_tuple(tup):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161 """Ensures that all types in the tuple are either strings
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
162 or bytes.
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
163 """
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
164 tupiter = iter(tup)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
165 is_text = isinstance(next(tupiter, None), text_type)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
166 for arg in tupiter:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
167 if isinstance(arg, text_type) != is_text:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
168 raise TypeError('Cannot mix str and bytes arguments (got %s)'
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
169 % repr(tup))
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
170 return tup
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
171
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
172 try_coerce_native = _identity
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
173 wsgi_get_bytes = _latin1_encode
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
174
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
175 def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
176 return s.encode('latin1').decode(charset, errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
177
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
178 def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
179 if isinstance(s, text_type):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
180 s = s.encode(charset)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
181 return s.decode('latin1', errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
182
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
183 def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
184 if x is None:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
185 return None
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
186 if isinstance(x, (bytes, bytearray, memoryview)): # noqa
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
187 return bytes(x)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
188 if isinstance(x, str):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
189 return x.encode(charset, errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
190 raise TypeError('Expected bytes')
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
191
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
192 def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
193 if x is None or isinstance(x, str):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
194 return x
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
195 return x.decode(charset, errors)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
196
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
197
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
198 def to_unicode(x, charset=sys.getdefaultencoding(), errors='strict',
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
199 allow_none_charset=False):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
200 if x is None:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
201 return None
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
202 if not isinstance(x, bytes):
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
203 return text_type(x)
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
204 if charset is None and allow_none_charset:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
205 return x
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
206 return x.decode(charset, errors)