##// END OF EJS Templates
Hide dynamically defined metaclass base from Sphinx....
Thomas Kluyver -
Show More
@@ -1,242 +1,242 b''
1 1 # coding: utf-8
2 2 """Compatibility tricks for Python 3. Mainly to do with unicode."""
3 3 import functools
4 4 import os
5 5 import sys
6 6 import re
7 7 import types
8 8
9 9 from .encoding import DEFAULT_ENCODING
10 10
11 11 orig_open = open
12 12
13 13 def no_code(x, encoding=None):
14 14 return x
15 15
16 16 def decode(s, encoding=None):
17 17 encoding = encoding or DEFAULT_ENCODING
18 18 return s.decode(encoding, "replace")
19 19
20 20 def encode(u, encoding=None):
21 21 encoding = encoding or DEFAULT_ENCODING
22 22 return u.encode(encoding, "replace")
23 23
24 24
25 25 def cast_unicode(s, encoding=None):
26 26 if isinstance(s, bytes):
27 27 return decode(s, encoding)
28 28 return s
29 29
30 30 def cast_bytes(s, encoding=None):
31 31 if not isinstance(s, bytes):
32 32 return encode(s, encoding)
33 33 return s
34 34
35 35 def _modify_str_or_docstring(str_change_func):
36 36 @functools.wraps(str_change_func)
37 37 def wrapper(func_or_str):
38 38 if isinstance(func_or_str, string_types):
39 39 func = None
40 40 doc = func_or_str
41 41 else:
42 42 func = func_or_str
43 43 doc = func.__doc__
44 44
45 45 doc = str_change_func(doc)
46 46
47 47 if func:
48 48 func.__doc__ = doc
49 49 return func
50 50 return doc
51 51 return wrapper
52 52
53 53 def safe_unicode(e):
54 54 """unicode(e) with various fallbacks. Used for exceptions, which may not be
55 55 safe to call unicode() on.
56 56 """
57 57 try:
58 58 return unicode_type(e)
59 59 except UnicodeError:
60 60 pass
61 61
62 62 try:
63 63 return str_to_unicode(str(e))
64 64 except UnicodeError:
65 65 pass
66 66
67 67 try:
68 68 return str_to_unicode(repr(e))
69 69 except UnicodeError:
70 70 pass
71 71
72 72 return u'Unrecoverably corrupt evalue'
73 73
74 74 if sys.version_info[0] >= 3:
75 75 PY3 = True
76 76
77 77 input = input
78 78 builtin_mod_name = "builtins"
79 79 import builtins as builtin_mod
80 80
81 81 str_to_unicode = no_code
82 82 unicode_to_str = no_code
83 83 str_to_bytes = encode
84 84 bytes_to_str = decode
85 85 cast_bytes_py2 = no_code
86 86
87 87 string_types = (str,)
88 88 unicode_type = str
89 89
90 90 def isidentifier(s, dotted=False):
91 91 if dotted:
92 92 return all(isidentifier(a) for a in s.split("."))
93 93 return s.isidentifier()
94 94
95 95 open = orig_open
96 96 xrange = range
97 97 def iteritems(d): return iter(d.items())
98 98 def itervalues(d): return iter(d.values())
99 99 getcwd = os.getcwd
100 100
101 101 MethodType = types.MethodType
102 102
103 103 def execfile(fname, glob, loc=None):
104 104 loc = loc if (loc is not None) else glob
105 105 with open(fname, 'rb') as f:
106 106 exec(compile(f.read(), fname, 'exec'), glob, loc)
107 107
108 108 # Refactor print statements in doctests.
109 109 _print_statement_re = re.compile(r"\bprint (?P<expr>.*)$", re.MULTILINE)
110 110 def _print_statement_sub(match):
111 111 expr = match.groups('expr')
112 112 return "print(%s)" % expr
113 113
114 114 @_modify_str_or_docstring
115 115 def doctest_refactor_print(doc):
116 116 """Refactor 'print x' statements in a doctest to print(x) style. 2to3
117 117 unfortunately doesn't pick up on our doctests.
118 118
119 119 Can accept a string or a function, so it can be used as a decorator."""
120 120 return _print_statement_re.sub(_print_statement_sub, doc)
121 121
122 122 # Abstract u'abc' syntax:
123 123 @_modify_str_or_docstring
124 124 def u_format(s):
125 125 """"{u}'abc'" --> "'abc'" (Python 3)
126 126
127 127 Accepts a string or a function, so it can be used as a decorator."""
128 128 return s.format(u='')
129 129
130 130 else:
131 131 PY3 = False
132 132
133 133 input = raw_input
134 134 builtin_mod_name = "__builtin__"
135 135 import __builtin__ as builtin_mod
136 136
137 137 str_to_unicode = decode
138 138 unicode_to_str = encode
139 139 str_to_bytes = no_code
140 140 bytes_to_str = no_code
141 141 cast_bytes_py2 = cast_bytes
142 142
143 143 string_types = (str, unicode)
144 144 unicode_type = unicode
145 145
146 146 import re
147 147 _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
148 148 def isidentifier(s, dotted=False):
149 149 if dotted:
150 150 return all(isidentifier(a) for a in s.split("."))
151 151 return bool(_name_re.match(s))
152 152
153 153 class open(object):
154 154 """Wrapper providing key part of Python 3 open() interface."""
155 155 def __init__(self, fname, mode="r", encoding="utf-8"):
156 156 self.f = orig_open(fname, mode)
157 157 self.enc = encoding
158 158
159 159 def write(self, s):
160 160 return self.f.write(s.encode(self.enc))
161 161
162 162 def read(self, size=-1):
163 163 return self.f.read(size).decode(self.enc)
164 164
165 165 def close(self):
166 166 return self.f.close()
167 167
168 168 def __enter__(self):
169 169 return self
170 170
171 171 def __exit__(self, etype, value, traceback):
172 172 self.f.close()
173 173
174 174 xrange = xrange
175 175 def iteritems(d): return d.iteritems()
176 176 def itervalues(d): return d.itervalues()
177 177 getcwd = os.getcwdu
178 178
179 179 def MethodType(func, instance):
180 180 return types.MethodType(func, instance, type(instance))
181 181
182 182 # don't override system execfile on 2.x:
183 183 execfile = execfile
184 184
185 185 def doctest_refactor_print(func_or_str):
186 186 return func_or_str
187 187
188 188
189 189 # Abstract u'abc' syntax:
190 190 @_modify_str_or_docstring
191 191 def u_format(s):
192 192 """"{u}'abc'" --> "u'abc'" (Python 2)
193 193
194 194 Accepts a string or a function, so it can be used as a decorator."""
195 195 return s.format(u='u')
196 196
197 197 if sys.platform == 'win32':
198 198 def execfile(fname, glob=None, loc=None):
199 199 loc = loc if (loc is not None) else glob
200 200 # The rstrip() is necessary b/c trailing whitespace in files will
201 201 # cause an IndentationError in Python 2.6 (this was fixed in 2.7,
202 202 # but we still support 2.6). See issue 1027.
203 203 scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
204 204 # compile converts unicode filename to str assuming
205 205 # ascii. Let's do the conversion before calling compile
206 206 if isinstance(fname, unicode):
207 207 filename = unicode_to_str(fname)
208 208 else:
209 209 filename = fname
210 210 exec(compile(scripttext, filename, 'exec'), glob, loc)
211 211 else:
212 212 def execfile(fname, *where):
213 213 if isinstance(fname, unicode):
214 214 filename = fname.encode(sys.getfilesystemencoding())
215 215 else:
216 216 filename = fname
217 217 builtin_mod.execfile(filename, *where)
218 218
219 219 # Parts below taken from six:
220 220 # Copyright (c) 2010-2013 Benjamin Peterson
221 221 #
222 222 # Permission is hereby granted, free of charge, to any person obtaining a copy
223 223 # of this software and associated documentation files (the "Software"), to deal
224 224 # in the Software without restriction, including without limitation the rights
225 225 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
226 226 # copies of the Software, and to permit persons to whom the Software is
227 227 # furnished to do so, subject to the following conditions:
228 228 #
229 229 # The above copyright notice and this permission notice shall be included in all
230 230 # copies or substantial portions of the Software.
231 231 #
232 232 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
233 233 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
234 234 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
235 235 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
236 236 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
237 237 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
238 238 # SOFTWARE.
239 239
240 240 def with_metaclass(meta, *bases):
241 241 """Create a base class with a metaclass."""
242 return meta("NewBase", bases, {})
242 return meta("_NewBase", bases, {})
General Comments 0
You need to be logged in to leave comments. Login now