Show More
@@ -1,868 +1,868 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Python advanced pretty printer. This pretty printer is intended to |
|
4 | 4 | replace the old `pprint` python module which does not allow developers |
|
5 | 5 | to provide their own pretty print callbacks. |
|
6 | 6 | |
|
7 | 7 | This module is based on ruby's `prettyprint.rb` library by `Tanaka Akira`. |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | Example Usage |
|
11 | 11 | ------------- |
|
12 | 12 | |
|
13 | 13 | To directly print the representation of an object use `pprint`:: |
|
14 | 14 | |
|
15 | 15 | from pretty import pprint |
|
16 | 16 | pprint(complex_object) |
|
17 | 17 | |
|
18 | 18 | To get a string of the output use `pretty`:: |
|
19 | 19 | |
|
20 | 20 | from pretty import pretty |
|
21 | 21 | string = pretty(complex_object) |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | Extending |
|
25 | 25 | --------- |
|
26 | 26 | |
|
27 | 27 | The pretty library allows developers to add pretty printing rules for their |
|
28 | 28 | own objects. This process is straightforward. All you have to do is to |
|
29 | 29 | add a `_repr_pretty_` method to your object and call the methods on the |
|
30 | 30 | pretty printer passed:: |
|
31 | 31 | |
|
32 | 32 | class MyObject(object): |
|
33 | 33 | |
|
34 | 34 | def _repr_pretty_(self, p, cycle): |
|
35 | 35 | ... |
|
36 | 36 | |
|
37 | 37 | Here is an example implementation of a `_repr_pretty_` method for a list |
|
38 | 38 | subclass:: |
|
39 | 39 | |
|
40 | 40 | class MyList(list): |
|
41 | 41 | |
|
42 | 42 | def _repr_pretty_(self, p, cycle): |
|
43 | 43 | if cycle: |
|
44 | 44 | p.text('MyList(...)') |
|
45 | 45 | else: |
|
46 | 46 | with p.group(8, 'MyList([', '])'): |
|
47 | 47 | for idx, item in enumerate(self): |
|
48 | 48 | if idx: |
|
49 | 49 | p.text(',') |
|
50 | 50 | p.breakable() |
|
51 | 51 | p.pretty(item) |
|
52 | 52 | |
|
53 | 53 | The `cycle` parameter is `True` if pretty detected a cycle. You *have* to |
|
54 | 54 | react to that or the result is an infinite loop. `p.text()` just adds |
|
55 | 55 | non breaking text to the output, `p.breakable()` either adds a whitespace |
|
56 | 56 | or breaks here. If you pass it an argument it's used instead of the |
|
57 | 57 | default space. `p.pretty` prettyprints another object using the pretty print |
|
58 | 58 | method. |
|
59 | 59 | |
|
60 | 60 | The first parameter to the `group` function specifies the extra indentation |
|
61 | 61 | of the next line. In this example the next item will either be on the same |
|
62 | 62 | line (if the items are short enough) or aligned with the right edge of the |
|
63 | 63 | opening bracket of `MyList`. |
|
64 | 64 | |
|
65 | 65 | If you just want to indent something you can use the group function |
|
66 | 66 | without open / close parameters. You can also use this code:: |
|
67 | 67 | |
|
68 | 68 | with p.indent(2): |
|
69 | 69 | ... |
|
70 | 70 | |
|
71 | 71 | Inheritance diagram: |
|
72 | 72 | |
|
73 | 73 | .. inheritance-diagram:: IPython.lib.pretty |
|
74 | 74 | :parts: 3 |
|
75 | 75 | |
|
76 | 76 | :copyright: 2007 by Armin Ronacher. |
|
77 | 77 | Portions (c) 2009 by Robert Kern. |
|
78 | 78 | :license: BSD License. |
|
79 | 79 | """ |
|
80 | 80 | from __future__ import print_function |
|
81 | 81 | from contextlib import contextmanager |
|
82 | 82 | import sys |
|
83 | 83 | import types |
|
84 | 84 | import re |
|
85 | 85 | import datetime |
|
86 | 86 | from collections import deque |
|
87 | 87 | |
|
88 | 88 | from IPython.utils.py3compat import PY3, PYPY, cast_unicode, string_types |
|
89 | 89 | from IPython.utils.encoding import get_stream_enc |
|
90 | 90 | |
|
91 | 91 | from io import StringIO |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', |
|
95 | 95 | 'for_type', 'for_type_by_name'] |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | MAX_SEQ_LENGTH = 1000 |
|
99 | 99 | _re_pattern_type = type(re.compile('')) |
|
100 | 100 | |
|
101 | 101 | def _safe_getattr(obj, attr, default=None): |
|
102 | 102 | """Safe version of getattr. |
|
103 | 103 | |
|
104 | 104 | Same as getattr, but will return ``default`` on any Exception, |
|
105 | 105 | rather than raising. |
|
106 | 106 | """ |
|
107 | 107 | try: |
|
108 | 108 | return getattr(obj, attr, default) |
|
109 | 109 | except Exception: |
|
110 | 110 | return default |
|
111 | 111 | |
|
112 | 112 | if PY3: |
|
113 | 113 | CUnicodeIO = StringIO |
|
114 | 114 | else: |
|
115 | 115 | class CUnicodeIO(StringIO): |
|
116 | 116 | """StringIO that casts str to unicode on Python 2""" |
|
117 | 117 | def write(self, text): |
|
118 | 118 | return super(CUnicodeIO, self).write( |
|
119 | 119 | cast_unicode(text, encoding=get_stream_enc(sys.stdout))) |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH): |
|
123 | 123 | """ |
|
124 | 124 | Pretty print the object's representation. |
|
125 | 125 | """ |
|
126 | 126 | stream = CUnicodeIO() |
|
127 | 127 | printer = RepresentationPrinter(stream, verbose, max_width, newline, max_seq_length=max_seq_length) |
|
128 | 128 | printer.pretty(obj) |
|
129 | 129 | printer.flush() |
|
130 | 130 | return stream.getvalue() |
|
131 | 131 | |
|
132 | 132 | |
|
133 | 133 | def pprint(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH): |
|
134 | 134 | """ |
|
135 | 135 | Like `pretty` but print to stdout. |
|
136 | 136 | """ |
|
137 | 137 | printer = RepresentationPrinter(sys.stdout, verbose, max_width, newline, max_seq_length=max_seq_length) |
|
138 | 138 | printer.pretty(obj) |
|
139 | 139 | printer.flush() |
|
140 | 140 | sys.stdout.write(newline) |
|
141 | 141 | sys.stdout.flush() |
|
142 | 142 | |
|
143 | 143 | class _PrettyPrinterBase(object): |
|
144 | 144 | |
|
145 | 145 | @contextmanager |
|
146 | 146 | def indent(self, indent): |
|
147 | 147 | """with statement support for indenting/dedenting.""" |
|
148 | 148 | self.indentation += indent |
|
149 | 149 | try: |
|
150 | 150 | yield |
|
151 | 151 | finally: |
|
152 | 152 | self.indentation -= indent |
|
153 | 153 | |
|
154 | 154 | @contextmanager |
|
155 | 155 | def group(self, indent=0, open='', close=''): |
|
156 | 156 | """like begin_group / end_group but for the with statement.""" |
|
157 | 157 | self.begin_group(indent, open) |
|
158 | 158 | try: |
|
159 | 159 | yield |
|
160 | 160 | finally: |
|
161 | 161 | self.end_group(indent, close) |
|
162 | 162 | |
|
163 | 163 | class PrettyPrinter(_PrettyPrinterBase): |
|
164 | 164 | """ |
|
165 | 165 | Baseclass for the `RepresentationPrinter` prettyprinter that is used to |
|
166 | 166 | generate pretty reprs of objects. Contrary to the `RepresentationPrinter` |
|
167 | 167 | this printer knows nothing about the default pprinters or the `_repr_pretty_` |
|
168 | 168 | callback method. |
|
169 | 169 | """ |
|
170 | 170 | |
|
171 | 171 | def __init__(self, output, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH): |
|
172 | 172 | self.output = output |
|
173 | 173 | self.max_width = max_width |
|
174 | 174 | self.newline = newline |
|
175 | 175 | self.max_seq_length = max_seq_length |
|
176 | 176 | self.output_width = 0 |
|
177 | 177 | self.buffer_width = 0 |
|
178 | 178 | self.buffer = deque() |
|
179 | 179 | |
|
180 | 180 | root_group = Group(0) |
|
181 | 181 | self.group_stack = [root_group] |
|
182 | 182 | self.group_queue = GroupQueue(root_group) |
|
183 | 183 | self.indentation = 0 |
|
184 | 184 | |
|
185 | 185 | def _break_outer_groups(self): |
|
186 | 186 | while self.max_width < self.output_width + self.buffer_width: |
|
187 | 187 | group = self.group_queue.deq() |
|
188 | 188 | if not group: |
|
189 | 189 | return |
|
190 | 190 | while group.breakables: |
|
191 | 191 | x = self.buffer.popleft() |
|
192 | 192 | self.output_width = x.output(self.output, self.output_width) |
|
193 | 193 | self.buffer_width -= x.width |
|
194 | 194 | while self.buffer and isinstance(self.buffer[0], Text): |
|
195 | 195 | x = self.buffer.popleft() |
|
196 | 196 | self.output_width = x.output(self.output, self.output_width) |
|
197 | 197 | self.buffer_width -= x.width |
|
198 | 198 | |
|
199 | 199 | def text(self, obj): |
|
200 | 200 | """Add literal text to the output.""" |
|
201 | 201 | width = len(obj) |
|
202 | 202 | if self.buffer: |
|
203 | 203 | text = self.buffer[-1] |
|
204 | 204 | if not isinstance(text, Text): |
|
205 | 205 | text = Text() |
|
206 | 206 | self.buffer.append(text) |
|
207 | 207 | text.add(obj, width) |
|
208 | 208 | self.buffer_width += width |
|
209 | 209 | self._break_outer_groups() |
|
210 | 210 | else: |
|
211 | 211 | self.output.write(obj) |
|
212 | 212 | self.output_width += width |
|
213 | 213 | |
|
214 | 214 | def breakable(self, sep=' '): |
|
215 | 215 | """ |
|
216 | 216 | Add a breakable separator to the output. This does not mean that it |
|
217 | 217 | will automatically break here. If no breaking on this position takes |
|
218 | 218 | place the `sep` is inserted which default to one space. |
|
219 | 219 | """ |
|
220 | 220 | width = len(sep) |
|
221 | 221 | group = self.group_stack[-1] |
|
222 | 222 | if group.want_break: |
|
223 | 223 | self.flush() |
|
224 | 224 | self.output.write(self.newline) |
|
225 | 225 | self.output.write(' ' * self.indentation) |
|
226 | 226 | self.output_width = self.indentation |
|
227 | 227 | self.buffer_width = 0 |
|
228 | 228 | else: |
|
229 | 229 | self.buffer.append(Breakable(sep, width, self)) |
|
230 | 230 | self.buffer_width += width |
|
231 | 231 | self._break_outer_groups() |
|
232 | 232 | |
|
233 | 233 | def break_(self): |
|
234 | 234 | """ |
|
235 | 235 | Explicitly insert a newline into the output, maintaining correct indentation. |
|
236 | 236 | """ |
|
237 | 237 | self.flush() |
|
238 | 238 | self.output.write(self.newline) |
|
239 | 239 | self.output.write(' ' * self.indentation) |
|
240 | 240 | self.output_width = self.indentation |
|
241 | 241 | self.buffer_width = 0 |
|
242 | 242 | |
|
243 | 243 | |
|
244 | 244 | def begin_group(self, indent=0, open=''): |
|
245 | 245 | """ |
|
246 | 246 | Begin a group. If you want support for python < 2.5 which doesn't has |
|
247 | 247 | the with statement this is the preferred way: |
|
248 | 248 | |
|
249 | 249 | p.begin_group(1, '{') |
|
250 | 250 | ... |
|
251 | 251 | p.end_group(1, '}') |
|
252 | 252 | |
|
253 | 253 | The python 2.5 expression would be this: |
|
254 | 254 | |
|
255 | 255 | with p.group(1, '{', '}'): |
|
256 | 256 | ... |
|
257 | 257 | |
|
258 | 258 | The first parameter specifies the indentation for the next line (usually |
|
259 | 259 | the width of the opening text), the second the opening text. All |
|
260 | 260 | parameters are optional. |
|
261 | 261 | """ |
|
262 | 262 | if open: |
|
263 | 263 | self.text(open) |
|
264 | 264 | group = Group(self.group_stack[-1].depth + 1) |
|
265 | 265 | self.group_stack.append(group) |
|
266 | 266 | self.group_queue.enq(group) |
|
267 | 267 | self.indentation += indent |
|
268 | 268 | |
|
269 | 269 | def _enumerate(self, seq): |
|
270 | 270 | """like enumerate, but with an upper limit on the number of items""" |
|
271 | 271 | for idx, x in enumerate(seq): |
|
272 | 272 | if self.max_seq_length and idx >= self.max_seq_length: |
|
273 | 273 | self.text(',') |
|
274 | 274 | self.breakable() |
|
275 | 275 | self.text('...') |
|
276 | 276 | return |
|
277 | 277 | yield idx, x |
|
278 | 278 | |
|
279 | 279 | def end_group(self, dedent=0, close=''): |
|
280 | 280 | """End a group. See `begin_group` for more details.""" |
|
281 | 281 | self.indentation -= dedent |
|
282 | 282 | group = self.group_stack.pop() |
|
283 | 283 | if not group.breakables: |
|
284 | 284 | self.group_queue.remove(group) |
|
285 | 285 | if close: |
|
286 | 286 | self.text(close) |
|
287 | 287 | |
|
288 | 288 | def flush(self): |
|
289 | 289 | """Flush data that is left in the buffer.""" |
|
290 | 290 | for data in self.buffer: |
|
291 | 291 | self.output_width += data.output(self.output, self.output_width) |
|
292 | 292 | self.buffer.clear() |
|
293 | 293 | self.buffer_width = 0 |
|
294 | 294 | |
|
295 | 295 | |
|
296 | 296 | def _get_mro(obj_class): |
|
297 | 297 | """ Get a reasonable method resolution order of a class and its superclasses |
|
298 | 298 | for both old-style and new-style classes. |
|
299 | 299 | """ |
|
300 | 300 | if not hasattr(obj_class, '__mro__'): |
|
301 | 301 | # Old-style class. Mix in object to make a fake new-style class. |
|
302 | 302 | try: |
|
303 | 303 | obj_class = type(obj_class.__name__, (obj_class, object), {}) |
|
304 | 304 | except TypeError: |
|
305 | 305 | # Old-style extension type that does not descend from object. |
|
306 | 306 | # FIXME: try to construct a more thorough MRO. |
|
307 | 307 | mro = [obj_class] |
|
308 | 308 | else: |
|
309 | 309 | mro = obj_class.__mro__[1:-1] |
|
310 | 310 | else: |
|
311 | 311 | mro = obj_class.__mro__ |
|
312 | 312 | return mro |
|
313 | 313 | |
|
314 | 314 | |
|
315 | 315 | class RepresentationPrinter(PrettyPrinter): |
|
316 | 316 | """ |
|
317 | 317 | Special pretty printer that has a `pretty` method that calls the pretty |
|
318 | 318 | printer for a python object. |
|
319 | 319 | |
|
320 | 320 | This class stores processing data on `self` so you must *never* use |
|
321 | 321 | this class in a threaded environment. Always lock it or reinstanciate |
|
322 | 322 | it. |
|
323 | 323 | |
|
324 | 324 | Instances also have a verbose flag callbacks can access to control their |
|
325 | 325 | output. For example the default instance repr prints all attributes and |
|
326 | 326 | methods that are not prefixed by an underscore if the printer is in |
|
327 | 327 | verbose mode. |
|
328 | 328 | """ |
|
329 | 329 | |
|
330 | 330 | def __init__(self, output, verbose=False, max_width=79, newline='\n', |
|
331 | 331 | singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None, |
|
332 | 332 | max_seq_length=MAX_SEQ_LENGTH): |
|
333 | 333 | |
|
334 | 334 | PrettyPrinter.__init__(self, output, max_width, newline, max_seq_length=max_seq_length) |
|
335 | 335 | self.verbose = verbose |
|
336 | 336 | self.stack = [] |
|
337 | 337 | if singleton_pprinters is None: |
|
338 | 338 | singleton_pprinters = _singleton_pprinters.copy() |
|
339 | 339 | self.singleton_pprinters = singleton_pprinters |
|
340 | 340 | if type_pprinters is None: |
|
341 | 341 | type_pprinters = _type_pprinters.copy() |
|
342 | 342 | self.type_pprinters = type_pprinters |
|
343 | 343 | if deferred_pprinters is None: |
|
344 | 344 | deferred_pprinters = _deferred_type_pprinters.copy() |
|
345 | 345 | self.deferred_pprinters = deferred_pprinters |
|
346 | 346 | |
|
347 | 347 | def pretty(self, obj): |
|
348 | 348 | """Pretty print the given object.""" |
|
349 | 349 | obj_id = id(obj) |
|
350 | 350 | cycle = obj_id in self.stack |
|
351 | 351 | self.stack.append(obj_id) |
|
352 | 352 | self.begin_group() |
|
353 | 353 | try: |
|
354 | 354 | obj_class = _safe_getattr(obj, '__class__', None) or type(obj) |
|
355 | 355 | # First try to find registered singleton printers for the type. |
|
356 | 356 | try: |
|
357 | 357 | printer = self.singleton_pprinters[obj_id] |
|
358 | 358 | except (TypeError, KeyError): |
|
359 | 359 | pass |
|
360 | 360 | else: |
|
361 | 361 | return printer(obj, self, cycle) |
|
362 | 362 | # Next walk the mro and check for either: |
|
363 | 363 | # 1) a registered printer |
|
364 | 364 | # 2) a _repr_pretty_ method |
|
365 | 365 | for cls in _get_mro(obj_class): |
|
366 | 366 | if cls in self.type_pprinters: |
|
367 | 367 | # printer registered in self.type_pprinters |
|
368 | 368 | return self.type_pprinters[cls](obj, self, cycle) |
|
369 | 369 | else: |
|
370 | 370 | # deferred printer |
|
371 | 371 | printer = self._in_deferred_types(cls) |
|
372 | 372 | if printer is not None: |
|
373 | 373 | return printer(obj, self, cycle) |
|
374 | 374 | else: |
|
375 | 375 | # Finally look for special method names. |
|
376 | 376 | # Some objects automatically create any requested |
|
377 | 377 | # attribute. Try to ignore most of them by checking for |
|
378 | 378 | # callability. |
|
379 | 379 | if '_repr_pretty_' in cls.__dict__: |
|
380 | 380 | meth = cls._repr_pretty_ |
|
381 | 381 | if callable(meth): |
|
382 | 382 | return meth(obj, self, cycle) |
|
383 | 383 | return _default_pprint(obj, self, cycle) |
|
384 | 384 | finally: |
|
385 | 385 | self.end_group() |
|
386 | 386 | self.stack.pop() |
|
387 | 387 | |
|
388 | 388 | def _in_deferred_types(self, cls): |
|
389 | 389 | """ |
|
390 | 390 | Check if the given class is specified in the deferred type registry. |
|
391 | 391 | |
|
392 | 392 | Returns the printer from the registry if it exists, and None if the |
|
393 | 393 | class is not in the registry. Successful matches will be moved to the |
|
394 | 394 | regular type registry for future use. |
|
395 | 395 | """ |
|
396 | 396 | mod = _safe_getattr(cls, '__module__', None) |
|
397 | 397 | name = _safe_getattr(cls, '__name__', None) |
|
398 | 398 | key = (mod, name) |
|
399 | 399 | printer = None |
|
400 | 400 | if key in self.deferred_pprinters: |
|
401 | 401 | # Move the printer over to the regular registry. |
|
402 | 402 | printer = self.deferred_pprinters.pop(key) |
|
403 | 403 | self.type_pprinters[cls] = printer |
|
404 | 404 | return printer |
|
405 | 405 | |
|
406 | 406 | |
|
407 | 407 | class Printable(object): |
|
408 | 408 | |
|
409 | 409 | def output(self, stream, output_width): |
|
410 | 410 | return output_width |
|
411 | 411 | |
|
412 | 412 | |
|
413 | 413 | class Text(Printable): |
|
414 | 414 | |
|
415 | 415 | def __init__(self): |
|
416 | 416 | self.objs = [] |
|
417 | 417 | self.width = 0 |
|
418 | 418 | |
|
419 | 419 | def output(self, stream, output_width): |
|
420 | 420 | for obj in self.objs: |
|
421 | 421 | stream.write(obj) |
|
422 | 422 | return output_width + self.width |
|
423 | 423 | |
|
424 | 424 | def add(self, obj, width): |
|
425 | 425 | self.objs.append(obj) |
|
426 | 426 | self.width += width |
|
427 | 427 | |
|
428 | 428 | |
|
429 | 429 | class Breakable(Printable): |
|
430 | 430 | |
|
431 | 431 | def __init__(self, seq, width, pretty): |
|
432 | 432 | self.obj = seq |
|
433 | 433 | self.width = width |
|
434 | 434 | self.pretty = pretty |
|
435 | 435 | self.indentation = pretty.indentation |
|
436 | 436 | self.group = pretty.group_stack[-1] |
|
437 | 437 | self.group.breakables.append(self) |
|
438 | 438 | |
|
439 | 439 | def output(self, stream, output_width): |
|
440 | 440 | self.group.breakables.popleft() |
|
441 | 441 | if self.group.want_break: |
|
442 | 442 | stream.write(self.pretty.newline) |
|
443 | 443 | stream.write(' ' * self.indentation) |
|
444 | 444 | return self.indentation |
|
445 | 445 | if not self.group.breakables: |
|
446 | 446 | self.pretty.group_queue.remove(self.group) |
|
447 | 447 | stream.write(self.obj) |
|
448 | 448 | return output_width + self.width |
|
449 | 449 | |
|
450 | 450 | |
|
451 | 451 | class Group(Printable): |
|
452 | 452 | |
|
453 | 453 | def __init__(self, depth): |
|
454 | 454 | self.depth = depth |
|
455 | 455 | self.breakables = deque() |
|
456 | 456 | self.want_break = False |
|
457 | 457 | |
|
458 | 458 | |
|
459 | 459 | class GroupQueue(object): |
|
460 | 460 | |
|
461 | 461 | def __init__(self, *groups): |
|
462 | 462 | self.queue = [] |
|
463 | 463 | for group in groups: |
|
464 | 464 | self.enq(group) |
|
465 | 465 | |
|
466 | 466 | def enq(self, group): |
|
467 | 467 | depth = group.depth |
|
468 | 468 | while depth > len(self.queue) - 1: |
|
469 | 469 | self.queue.append([]) |
|
470 | 470 | self.queue[depth].append(group) |
|
471 | 471 | |
|
472 | 472 | def deq(self): |
|
473 | 473 | for stack in self.queue: |
|
474 | 474 | for idx, group in enumerate(reversed(stack)): |
|
475 | 475 | if group.breakables: |
|
476 | 476 | del stack[idx] |
|
477 | 477 | group.want_break = True |
|
478 | 478 | return group |
|
479 | 479 | for group in stack: |
|
480 | 480 | group.want_break = True |
|
481 | 481 | del stack[:] |
|
482 | 482 | |
|
483 | 483 | def remove(self, group): |
|
484 | 484 | try: |
|
485 | 485 | self.queue[group.depth].remove(group) |
|
486 | 486 | except ValueError: |
|
487 | 487 | pass |
|
488 | 488 | |
|
489 | 489 | try: |
|
490 | 490 | _baseclass_reprs = (object.__repr__, types.InstanceType.__repr__) |
|
491 | 491 | except AttributeError: # Python 3 |
|
492 | 492 | _baseclass_reprs = (object.__repr__,) |
|
493 | 493 | |
|
494 | 494 | |
|
495 | 495 | def _default_pprint(obj, p, cycle): |
|
496 | 496 | """ |
|
497 | 497 | The default print function. Used if an object does not provide one and |
|
498 | 498 | it's none of the builtin objects. |
|
499 | 499 | """ |
|
500 | 500 | klass = _safe_getattr(obj, '__class__', None) or type(obj) |
|
501 | 501 | if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs: |
|
502 | 502 | # A user-provided repr. Find newlines and replace them with p.break_() |
|
503 | 503 | _repr_pprint(obj, p, cycle) |
|
504 | 504 | return |
|
505 | 505 | p.begin_group(1, '<') |
|
506 | 506 | p.pretty(klass) |
|
507 | 507 | p.text(' at 0x%x' % id(obj)) |
|
508 | 508 | if cycle: |
|
509 | 509 | p.text(' ...') |
|
510 | 510 | elif p.verbose: |
|
511 | 511 | first = True |
|
512 | 512 | for key in dir(obj): |
|
513 | 513 | if not key.startswith('_'): |
|
514 | 514 | try: |
|
515 | 515 | value = getattr(obj, key) |
|
516 | 516 | except AttributeError: |
|
517 | 517 | continue |
|
518 | 518 | if isinstance(value, types.MethodType): |
|
519 | 519 | continue |
|
520 | 520 | if not first: |
|
521 | 521 | p.text(',') |
|
522 | 522 | p.breakable() |
|
523 | 523 | p.text(key) |
|
524 | 524 | p.text('=') |
|
525 | 525 | step = len(key) + 1 |
|
526 | 526 | p.indentation += step |
|
527 | 527 | p.pretty(value) |
|
528 | 528 | p.indentation -= step |
|
529 | 529 | first = False |
|
530 | 530 | p.end_group(1, '>') |
|
531 | 531 | |
|
532 | 532 | |
|
533 | 533 | def _seq_pprinter_factory(start, end, basetype): |
|
534 | 534 | """ |
|
535 | 535 | Factory that returns a pprint function useful for sequences. Used by |
|
536 | 536 | the default pprint for tuples, dicts, and lists. |
|
537 | 537 | """ |
|
538 | 538 | def inner(obj, p, cycle): |
|
539 | 539 | typ = type(obj) |
|
540 | 540 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: |
|
541 | 541 | # If the subclass provides its own repr, use it instead. |
|
542 | 542 | return p.text(typ.__repr__(obj)) |
|
543 | 543 | |
|
544 | 544 | if cycle: |
|
545 | 545 | return p.text(start + '...' + end) |
|
546 | 546 | step = len(start) |
|
547 | 547 | p.begin_group(step, start) |
|
548 | 548 | for idx, x in p._enumerate(obj): |
|
549 | 549 | if idx: |
|
550 | 550 | p.text(',') |
|
551 | 551 | p.breakable() |
|
552 | 552 | p.pretty(x) |
|
553 | 553 | if len(obj) == 1 and type(obj) is tuple: |
|
554 | 554 | # Special case for 1-item tuples. |
|
555 | 555 | p.text(',') |
|
556 | 556 | p.end_group(step, end) |
|
557 | 557 | return inner |
|
558 | 558 | |
|
559 | 559 | |
|
560 | 560 | def _set_pprinter_factory(start, end, basetype): |
|
561 | 561 | """ |
|
562 | 562 | Factory that returns a pprint function useful for sets and frozensets. |
|
563 | 563 | """ |
|
564 | 564 | def inner(obj, p, cycle): |
|
565 | 565 | typ = type(obj) |
|
566 | 566 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: |
|
567 | 567 | # If the subclass provides its own repr, use it instead. |
|
568 | 568 | return p.text(typ.__repr__(obj)) |
|
569 | 569 | |
|
570 | 570 | if cycle: |
|
571 | 571 | return p.text(start + '...' + end) |
|
572 | 572 | if len(obj) == 0: |
|
573 | 573 | # Special case. |
|
574 | 574 | p.text(basetype.__name__ + '()') |
|
575 | 575 | else: |
|
576 | 576 | step = len(start) |
|
577 | 577 | p.begin_group(step, start) |
|
578 | 578 | # Like dictionary keys, we will try to sort the items if there aren't too many |
|
579 | 579 | items = obj |
|
580 | 580 | if not (p.max_seq_length and len(obj) >= p.max_seq_length): |
|
581 | 581 | try: |
|
582 | 582 | items = sorted(obj) |
|
583 | 583 | except Exception: |
|
584 | 584 | # Sometimes the items don't sort. |
|
585 | 585 | pass |
|
586 | 586 | for idx, x in p._enumerate(items): |
|
587 | 587 | if idx: |
|
588 | 588 | p.text(',') |
|
589 | 589 | p.breakable() |
|
590 | 590 | p.pretty(x) |
|
591 | 591 | p.end_group(step, end) |
|
592 | 592 | return inner |
|
593 | 593 | |
|
594 | 594 | |
|
595 | 595 | def _dict_pprinter_factory(start, end, basetype=None): |
|
596 | 596 | """ |
|
597 | 597 | Factory that returns a pprint function used by the default pprint of |
|
598 | 598 | dicts and dict proxies. |
|
599 | 599 | """ |
|
600 | 600 | def inner(obj, p, cycle): |
|
601 | 601 | typ = type(obj) |
|
602 | 602 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: |
|
603 | 603 | # If the subclass provides its own repr, use it instead. |
|
604 | 604 | return p.text(typ.__repr__(obj)) |
|
605 | 605 | |
|
606 | 606 | if cycle: |
|
607 | 607 | return p.text('{...}') |
|
608 | 608 | step = len(start) |
|
609 | 609 | p.begin_group(step, start) |
|
610 | 610 | keys = obj.keys() |
|
611 | 611 | # if dict isn't large enough to be truncated, sort keys before displaying |
|
612 | 612 | if not (p.max_seq_length and len(obj) >= p.max_seq_length): |
|
613 | 613 | try: |
|
614 | 614 | keys = sorted(keys) |
|
615 | 615 | except Exception: |
|
616 | 616 | # Sometimes the keys don't sort. |
|
617 | 617 | pass |
|
618 | 618 | for idx, key in p._enumerate(keys): |
|
619 | 619 | if idx: |
|
620 | 620 | p.text(',') |
|
621 | 621 | p.breakable() |
|
622 | 622 | p.pretty(key) |
|
623 | 623 | p.text(': ') |
|
624 | 624 | p.pretty(obj[key]) |
|
625 | 625 | p.end_group(step, end) |
|
626 | 626 | return inner |
|
627 | 627 | |
|
628 | 628 | |
|
629 | 629 | def _super_pprint(obj, p, cycle): |
|
630 | 630 | """The pprint for the super type.""" |
|
631 | 631 | p.begin_group(8, '<super: ') |
|
632 | 632 | p.pretty(obj.__thisclass__) |
|
633 | 633 | p.text(',') |
|
634 | 634 | p.breakable() |
|
635 |
if PYPY: # In PyPy, super() objects do |
|
|
635 | if PYPY: # In PyPy, super() objects don't have __self__ attributes | |
|
636 | 636 | dself = obj.__repr__.__self__ |
|
637 | 637 | p.pretty(None if dself is obj else dself) |
|
638 | 638 | else: |
|
639 | 639 | p.pretty(obj.__self__) |
|
640 | 640 | p.end_group(8, '>') |
|
641 | 641 | |
|
642 | 642 | |
|
643 | 643 | def _re_pattern_pprint(obj, p, cycle): |
|
644 | 644 | """The pprint function for regular expression patterns.""" |
|
645 | 645 | p.text('re.compile(') |
|
646 | 646 | pattern = repr(obj.pattern) |
|
647 | 647 | if pattern[:1] in 'uU': |
|
648 | 648 | pattern = pattern[1:] |
|
649 | 649 | prefix = 'ur' |
|
650 | 650 | else: |
|
651 | 651 | prefix = 'r' |
|
652 | 652 | pattern = prefix + pattern.replace('\\\\', '\\') |
|
653 | 653 | p.text(pattern) |
|
654 | 654 | if obj.flags: |
|
655 | 655 | p.text(',') |
|
656 | 656 | p.breakable() |
|
657 | 657 | done_one = False |
|
658 | 658 | for flag in ('TEMPLATE', 'IGNORECASE', 'LOCALE', 'MULTILINE', 'DOTALL', |
|
659 | 659 | 'UNICODE', 'VERBOSE', 'DEBUG'): |
|
660 | 660 | if obj.flags & getattr(re, flag): |
|
661 | 661 | if done_one: |
|
662 | 662 | p.text('|') |
|
663 | 663 | p.text('re.' + flag) |
|
664 | 664 | done_one = True |
|
665 | 665 | p.text(')') |
|
666 | 666 | |
|
667 | 667 | |
|
668 | 668 | def _type_pprint(obj, p, cycle): |
|
669 | 669 | """The pprint for classes and types.""" |
|
670 | 670 | # Heap allocated types might not have the module attribute, |
|
671 | 671 | # and others may set it to None. |
|
672 | 672 | |
|
673 | 673 | # Checks for a __repr__ override in the metaclass. Can't compare the |
|
674 | 674 | # type(obj).__repr__ directly because in PyPy the representation function |
|
675 | 675 | # inherited from type isn't the same type.__repr__ |
|
676 | 676 | if [m for m in _get_mro(type(obj)) if "__repr__" in vars(m)][:1] != [type]: |
|
677 | 677 | _repr_pprint(obj, p, cycle) |
|
678 | 678 | return |
|
679 | 679 | |
|
680 | 680 | mod = _safe_getattr(obj, '__module__', None) |
|
681 | 681 | try: |
|
682 | 682 | name = obj.__qualname__ |
|
683 | 683 | if not isinstance(name, string_types): |
|
684 | 684 | # This can happen if the type implements __qualname__ as a property |
|
685 | 685 | # or other descriptor in Python 2. |
|
686 | 686 | raise Exception("Try __name__") |
|
687 | 687 | except Exception: |
|
688 | 688 | name = obj.__name__ |
|
689 | 689 | if not isinstance(name, string_types): |
|
690 | 690 | name = '<unknown type>' |
|
691 | 691 | |
|
692 | 692 | if mod in (None, '__builtin__', 'builtins', 'exceptions'): |
|
693 | 693 | p.text(name) |
|
694 | 694 | else: |
|
695 | 695 | p.text(mod + '.' + name) |
|
696 | 696 | |
|
697 | 697 | |
|
698 | 698 | def _repr_pprint(obj, p, cycle): |
|
699 | 699 | """A pprint that just redirects to the normal repr function.""" |
|
700 | 700 | # Find newlines and replace them with p.break_() |
|
701 | 701 | output = repr(obj) |
|
702 | 702 | for idx,output_line in enumerate(output.splitlines()): |
|
703 | 703 | if idx: |
|
704 | 704 | p.break_() |
|
705 | 705 | p.text(output_line) |
|
706 | 706 | |
|
707 | 707 | |
|
708 | 708 | def _function_pprint(obj, p, cycle): |
|
709 | 709 | """Base pprint for all functions and builtin functions.""" |
|
710 | 710 | name = _safe_getattr(obj, '__qualname__', obj.__name__) |
|
711 | 711 | mod = obj.__module__ |
|
712 | 712 | if mod and mod not in ('__builtin__', 'builtins', 'exceptions'): |
|
713 | 713 | name = mod + '.' + name |
|
714 | 714 | p.text('<function %s>' % name) |
|
715 | 715 | |
|
716 | 716 | |
|
717 | 717 | def _exception_pprint(obj, p, cycle): |
|
718 | 718 | """Base pprint for all exceptions.""" |
|
719 | 719 | name = getattr(obj.__class__, '__qualname__', obj.__class__.__name__) |
|
720 | 720 | if obj.__class__.__module__ not in ('exceptions', 'builtins'): |
|
721 | 721 | name = '%s.%s' % (obj.__class__.__module__, name) |
|
722 | 722 | step = len(name) + 1 |
|
723 | 723 | p.begin_group(step, name + '(') |
|
724 | 724 | for idx, arg in enumerate(getattr(obj, 'args', ())): |
|
725 | 725 | if idx: |
|
726 | 726 | p.text(',') |
|
727 | 727 | p.breakable() |
|
728 | 728 | p.pretty(arg) |
|
729 | 729 | p.end_group(step, ')') |
|
730 | 730 | |
|
731 | 731 | |
|
732 | 732 | #: the exception base |
|
733 | 733 | try: |
|
734 | 734 | _exception_base = BaseException |
|
735 | 735 | except NameError: |
|
736 | 736 | _exception_base = Exception |
|
737 | 737 | |
|
738 | 738 | |
|
739 | 739 | #: printers for builtin types |
|
740 | 740 | _type_pprinters = { |
|
741 | 741 | int: _repr_pprint, |
|
742 | 742 | float: _repr_pprint, |
|
743 | 743 | str: _repr_pprint, |
|
744 | 744 | tuple: _seq_pprinter_factory('(', ')', tuple), |
|
745 | 745 | list: _seq_pprinter_factory('[', ']', list), |
|
746 | 746 | dict: _dict_pprinter_factory('{', '}', dict), |
|
747 | 747 | |
|
748 | 748 | set: _set_pprinter_factory('{', '}', set), |
|
749 | 749 | frozenset: _set_pprinter_factory('frozenset({', '})', frozenset), |
|
750 | 750 | super: _super_pprint, |
|
751 | 751 | _re_pattern_type: _re_pattern_pprint, |
|
752 | 752 | type: _type_pprint, |
|
753 | 753 | types.FunctionType: _function_pprint, |
|
754 | 754 | types.BuiltinFunctionType: _function_pprint, |
|
755 | 755 | types.MethodType: _repr_pprint, |
|
756 | 756 | |
|
757 | 757 | datetime.datetime: _repr_pprint, |
|
758 | 758 | datetime.timedelta: _repr_pprint, |
|
759 | 759 | _exception_base: _exception_pprint |
|
760 | 760 | } |
|
761 | 761 | |
|
762 | 762 | try: |
|
763 | 763 | # In PyPy, types.DictProxyType is dict, setting the dictproxy printer |
|
764 | 764 | # using dict.setdefault avoids overwritting the dict printer |
|
765 | 765 | _type_pprinters.setdefault(types.DictProxyType, |
|
766 | 766 | _dict_pprinter_factory('dict_proxy({', '})')) |
|
767 | 767 | _type_pprinters[types.ClassType] = _type_pprint |
|
768 | 768 | _type_pprinters[types.SliceType] = _repr_pprint |
|
769 | 769 | except AttributeError: # Python 3 |
|
770 | 770 | _type_pprinters[types.MappingProxyType] = \ |
|
771 | 771 | _dict_pprinter_factory('mappingproxy({', '})') |
|
772 | 772 | _type_pprinters[slice] = _repr_pprint |
|
773 | 773 | |
|
774 | 774 | try: |
|
775 | 775 | _type_pprinters[xrange] = _repr_pprint |
|
776 | 776 | _type_pprinters[long] = _repr_pprint |
|
777 | 777 | _type_pprinters[unicode] = _repr_pprint |
|
778 | 778 | except NameError: |
|
779 | 779 | _type_pprinters[range] = _repr_pprint |
|
780 | 780 | _type_pprinters[bytes] = _repr_pprint |
|
781 | 781 | |
|
782 | 782 | #: printers for types specified by name |
|
783 | 783 | _deferred_type_pprinters = { |
|
784 | 784 | } |
|
785 | 785 | |
|
786 | 786 | def for_type(typ, func): |
|
787 | 787 | """ |
|
788 | 788 | Add a pretty printer for a given type. |
|
789 | 789 | """ |
|
790 | 790 | oldfunc = _type_pprinters.get(typ, None) |
|
791 | 791 | if func is not None: |
|
792 | 792 | # To support easy restoration of old pprinters, we need to ignore Nones. |
|
793 | 793 | _type_pprinters[typ] = func |
|
794 | 794 | return oldfunc |
|
795 | 795 | |
|
796 | 796 | def for_type_by_name(type_module, type_name, func): |
|
797 | 797 | """ |
|
798 | 798 | Add a pretty printer for a type specified by the module and name of a type |
|
799 | 799 | rather than the type object itself. |
|
800 | 800 | """ |
|
801 | 801 | key = (type_module, type_name) |
|
802 | 802 | oldfunc = _deferred_type_pprinters.get(key, None) |
|
803 | 803 | if func is not None: |
|
804 | 804 | # To support easy restoration of old pprinters, we need to ignore Nones. |
|
805 | 805 | _deferred_type_pprinters[key] = func |
|
806 | 806 | return oldfunc |
|
807 | 807 | |
|
808 | 808 | |
|
809 | 809 | #: printers for the default singletons |
|
810 | 810 | _singleton_pprinters = dict.fromkeys(map(id, [None, True, False, Ellipsis, |
|
811 | 811 | NotImplemented]), _repr_pprint) |
|
812 | 812 | |
|
813 | 813 | |
|
814 | 814 | def _defaultdict_pprint(obj, p, cycle): |
|
815 | 815 | name = obj.__class__.__name__ |
|
816 | 816 | with p.group(len(name) + 1, name + '(', ')'): |
|
817 | 817 | if cycle: |
|
818 | 818 | p.text('...') |
|
819 | 819 | else: |
|
820 | 820 | p.pretty(obj.default_factory) |
|
821 | 821 | p.text(',') |
|
822 | 822 | p.breakable() |
|
823 | 823 | p.pretty(dict(obj)) |
|
824 | 824 | |
|
825 | 825 | def _ordereddict_pprint(obj, p, cycle): |
|
826 | 826 | name = obj.__class__.__name__ |
|
827 | 827 | with p.group(len(name) + 1, name + '(', ')'): |
|
828 | 828 | if cycle: |
|
829 | 829 | p.text('...') |
|
830 | 830 | elif len(obj): |
|
831 | 831 | p.pretty(list(obj.items())) |
|
832 | 832 | |
|
833 | 833 | def _deque_pprint(obj, p, cycle): |
|
834 | 834 | name = obj.__class__.__name__ |
|
835 | 835 | with p.group(len(name) + 1, name + '(', ')'): |
|
836 | 836 | if cycle: |
|
837 | 837 | p.text('...') |
|
838 | 838 | else: |
|
839 | 839 | p.pretty(list(obj)) |
|
840 | 840 | |
|
841 | 841 | |
|
842 | 842 | def _counter_pprint(obj, p, cycle): |
|
843 | 843 | name = obj.__class__.__name__ |
|
844 | 844 | with p.group(len(name) + 1, name + '(', ')'): |
|
845 | 845 | if cycle: |
|
846 | 846 | p.text('...') |
|
847 | 847 | elif len(obj): |
|
848 | 848 | p.pretty(dict(obj)) |
|
849 | 849 | |
|
850 | 850 | for_type_by_name('collections', 'defaultdict', _defaultdict_pprint) |
|
851 | 851 | for_type_by_name('collections', 'OrderedDict', _ordereddict_pprint) |
|
852 | 852 | for_type_by_name('collections', 'deque', _deque_pprint) |
|
853 | 853 | for_type_by_name('collections', 'Counter', _counter_pprint) |
|
854 | 854 | |
|
855 | 855 | if __name__ == '__main__': |
|
856 | 856 | from random import randrange |
|
857 | 857 | class Foo(object): |
|
858 | 858 | def __init__(self): |
|
859 | 859 | self.foo = 1 |
|
860 | 860 | self.bar = re.compile(r'\s+') |
|
861 | 861 | self.blub = dict.fromkeys(range(30), randrange(1, 40)) |
|
862 | 862 | self.hehe = 23424.234234 |
|
863 | 863 | self.list = ["blub", "blah", self] |
|
864 | 864 | |
|
865 | 865 | def get_foo(self): |
|
866 | 866 | print("foo") |
|
867 | 867 | |
|
868 | 868 | pprint(Foo(), verbose=True) |
@@ -1,335 +1,336 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 shutil |
|
8 | 8 | import types |
|
9 | import platform | |
|
9 | 10 | |
|
10 | 11 | from .encoding import DEFAULT_ENCODING |
|
11 | 12 | |
|
12 | 13 | def no_code(x, encoding=None): |
|
13 | 14 | return x |
|
14 | 15 | |
|
15 | 16 | def decode(s, encoding=None): |
|
16 | 17 | encoding = encoding or DEFAULT_ENCODING |
|
17 | 18 | return s.decode(encoding, "replace") |
|
18 | 19 | |
|
19 | 20 | def encode(u, encoding=None): |
|
20 | 21 | encoding = encoding or DEFAULT_ENCODING |
|
21 | 22 | return u.encode(encoding, "replace") |
|
22 | 23 | |
|
23 | 24 | |
|
24 | 25 | def cast_unicode(s, encoding=None): |
|
25 | 26 | if isinstance(s, bytes): |
|
26 | 27 | return decode(s, encoding) |
|
27 | 28 | return s |
|
28 | 29 | |
|
29 | 30 | def cast_bytes(s, encoding=None): |
|
30 | 31 | if not isinstance(s, bytes): |
|
31 | 32 | return encode(s, encoding) |
|
32 | 33 | return s |
|
33 | 34 | |
|
34 | 35 | def buffer_to_bytes(buf): |
|
35 | 36 | """Cast a buffer object to bytes""" |
|
36 | 37 | if not isinstance(buf, bytes): |
|
37 | 38 | buf = bytes(buf) |
|
38 | 39 | return buf |
|
39 | 40 | |
|
40 | 41 | def _modify_str_or_docstring(str_change_func): |
|
41 | 42 | @functools.wraps(str_change_func) |
|
42 | 43 | def wrapper(func_or_str): |
|
43 | 44 | if isinstance(func_or_str, string_types): |
|
44 | 45 | func = None |
|
45 | 46 | doc = func_or_str |
|
46 | 47 | else: |
|
47 | 48 | func = func_or_str |
|
48 | 49 | doc = func.__doc__ |
|
49 | 50 | |
|
50 | 51 | # PYTHONOPTIMIZE=2 strips docstrings, so they can disappear unexpectedly |
|
51 | 52 | if doc is not None: |
|
52 | 53 | doc = str_change_func(doc) |
|
53 | 54 | |
|
54 | 55 | if func: |
|
55 | 56 | func.__doc__ = doc |
|
56 | 57 | return func |
|
57 | 58 | return doc |
|
58 | 59 | return wrapper |
|
59 | 60 | |
|
60 | 61 | def safe_unicode(e): |
|
61 | 62 | """unicode(e) with various fallbacks. Used for exceptions, which may not be |
|
62 | 63 | safe to call unicode() on. |
|
63 | 64 | """ |
|
64 | 65 | try: |
|
65 | 66 | return unicode_type(e) |
|
66 | 67 | except UnicodeError: |
|
67 | 68 | pass |
|
68 | 69 | |
|
69 | 70 | try: |
|
70 | 71 | return str_to_unicode(str(e)) |
|
71 | 72 | except UnicodeError: |
|
72 | 73 | pass |
|
73 | 74 | |
|
74 | 75 | try: |
|
75 | 76 | return str_to_unicode(repr(e)) |
|
76 | 77 | except UnicodeError: |
|
77 | 78 | pass |
|
78 | 79 | |
|
79 | 80 | return u'Unrecoverably corrupt evalue' |
|
80 | 81 | |
|
81 | 82 | # shutil.which from Python 3.4 |
|
82 | 83 | def _shutil_which(cmd, mode=os.F_OK | os.X_OK, path=None): |
|
83 | 84 | """Given a command, mode, and a PATH string, return the path which |
|
84 | 85 | conforms to the given mode on the PATH, or None if there is no such |
|
85 | 86 | file. |
|
86 | 87 | |
|
87 | 88 | `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
|
88 | 89 | of os.environ.get("PATH"), or can be overridden with a custom search |
|
89 | 90 | path. |
|
90 | 91 | |
|
91 | 92 | This is a backport of shutil.which from Python 3.4 |
|
92 | 93 | """ |
|
93 | 94 | # Check that a given file can be accessed with the correct mode. |
|
94 | 95 | # Additionally check that `file` is not a directory, as on Windows |
|
95 | 96 | # directories pass the os.access check. |
|
96 | 97 | def _access_check(fn, mode): |
|
97 | 98 | return (os.path.exists(fn) and os.access(fn, mode) |
|
98 | 99 | and not os.path.isdir(fn)) |
|
99 | 100 | |
|
100 | 101 | # If we're given a path with a directory part, look it up directly rather |
|
101 | 102 | # than referring to PATH directories. This includes checking relative to the |
|
102 | 103 | # current directory, e.g. ./script |
|
103 | 104 | if os.path.dirname(cmd): |
|
104 | 105 | if _access_check(cmd, mode): |
|
105 | 106 | return cmd |
|
106 | 107 | return None |
|
107 | 108 | |
|
108 | 109 | if path is None: |
|
109 | 110 | path = os.environ.get("PATH", os.defpath) |
|
110 | 111 | if not path: |
|
111 | 112 | return None |
|
112 | 113 | path = path.split(os.pathsep) |
|
113 | 114 | |
|
114 | 115 | if sys.platform == "win32": |
|
115 | 116 | # The current directory takes precedence on Windows. |
|
116 | 117 | if not os.curdir in path: |
|
117 | 118 | path.insert(0, os.curdir) |
|
118 | 119 | |
|
119 | 120 | # PATHEXT is necessary to check on Windows. |
|
120 | 121 | pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
|
121 | 122 | # See if the given file matches any of the expected path extensions. |
|
122 | 123 | # This will allow us to short circuit when given "python.exe". |
|
123 | 124 | # If it does match, only test that one, otherwise we have to try |
|
124 | 125 | # others. |
|
125 | 126 | if any(cmd.lower().endswith(ext.lower()) for ext in pathext): |
|
126 | 127 | files = [cmd] |
|
127 | 128 | else: |
|
128 | 129 | files = [cmd + ext for ext in pathext] |
|
129 | 130 | else: |
|
130 | 131 | # On other platforms you don't have things like PATHEXT to tell you |
|
131 | 132 | # what file suffixes are executable, so just pass on cmd as-is. |
|
132 | 133 | files = [cmd] |
|
133 | 134 | |
|
134 | 135 | seen = set() |
|
135 | 136 | for dir in path: |
|
136 | 137 | normdir = os.path.normcase(dir) |
|
137 | 138 | if not normdir in seen: |
|
138 | 139 | seen.add(normdir) |
|
139 | 140 | for thefile in files: |
|
140 | 141 | name = os.path.join(dir, thefile) |
|
141 | 142 | if _access_check(name, mode): |
|
142 | 143 | return name |
|
143 | 144 | return None |
|
144 | 145 | |
|
145 | 146 | if sys.version_info[0] >= 3: |
|
146 | 147 | PY3 = True |
|
147 | 148 | |
|
148 | 149 | # keep reference to builtin_mod because the kernel overrides that value |
|
149 | 150 | # to forward requests to a frontend. |
|
150 | 151 | def input(prompt=''): |
|
151 | 152 | return builtin_mod.input(prompt) |
|
152 | 153 | |
|
153 | 154 | builtin_mod_name = "builtins" |
|
154 | 155 | import builtins as builtin_mod |
|
155 | 156 | |
|
156 | 157 | str_to_unicode = no_code |
|
157 | 158 | unicode_to_str = no_code |
|
158 | 159 | str_to_bytes = encode |
|
159 | 160 | bytes_to_str = decode |
|
160 | 161 | cast_bytes_py2 = no_code |
|
161 | 162 | cast_unicode_py2 = no_code |
|
162 | 163 | buffer_to_bytes_py2 = no_code |
|
163 | 164 | |
|
164 | 165 | string_types = (str,) |
|
165 | 166 | unicode_type = str |
|
166 | 167 | |
|
167 | 168 | which = shutil.which |
|
168 | 169 | |
|
169 | 170 | def isidentifier(s, dotted=False): |
|
170 | 171 | if dotted: |
|
171 | 172 | return all(isidentifier(a) for a in s.split(".")) |
|
172 | 173 | return s.isidentifier() |
|
173 | 174 | |
|
174 | 175 | xrange = range |
|
175 | 176 | def iteritems(d): return iter(d.items()) |
|
176 | 177 | def itervalues(d): return iter(d.values()) |
|
177 | 178 | getcwd = os.getcwd |
|
178 | 179 | |
|
179 | 180 | MethodType = types.MethodType |
|
180 | 181 | |
|
181 | 182 | def execfile(fname, glob, loc=None, compiler=None): |
|
182 | 183 | loc = loc if (loc is not None) else glob |
|
183 | 184 | with open(fname, 'rb') as f: |
|
184 | 185 | compiler = compiler or compile |
|
185 | 186 | exec(compiler(f.read(), fname, 'exec'), glob, loc) |
|
186 | 187 | |
|
187 | 188 | # Refactor print statements in doctests. |
|
188 | 189 | _print_statement_re = re.compile(r"\bprint (?P<expr>.*)$", re.MULTILINE) |
|
189 | 190 | def _print_statement_sub(match): |
|
190 | 191 | expr = match.groups('expr') |
|
191 | 192 | return "print(%s)" % expr |
|
192 | 193 | |
|
193 | 194 | @_modify_str_or_docstring |
|
194 | 195 | def doctest_refactor_print(doc): |
|
195 | 196 | """Refactor 'print x' statements in a doctest to print(x) style. 2to3 |
|
196 | 197 | unfortunately doesn't pick up on our doctests. |
|
197 | 198 | |
|
198 | 199 | Can accept a string or a function, so it can be used as a decorator.""" |
|
199 | 200 | return _print_statement_re.sub(_print_statement_sub, doc) |
|
200 | 201 | |
|
201 | 202 | # Abstract u'abc' syntax: |
|
202 | 203 | @_modify_str_or_docstring |
|
203 | 204 | def u_format(s): |
|
204 | 205 | """"{u}'abc'" --> "'abc'" (Python 3) |
|
205 | 206 | |
|
206 | 207 | Accepts a string or a function, so it can be used as a decorator.""" |
|
207 | 208 | return s.format(u='') |
|
208 | 209 | |
|
209 | 210 | def get_closure(f): |
|
210 | 211 | """Get a function's closure attribute""" |
|
211 | 212 | return f.__closure__ |
|
212 | 213 | |
|
213 | 214 | else: |
|
214 | 215 | PY3 = False |
|
215 | 216 | |
|
216 | 217 | # keep reference to builtin_mod because the kernel overrides that value |
|
217 | 218 | # to forward requests to a frontend. |
|
218 | 219 | def input(prompt=''): |
|
219 | 220 | return builtin_mod.raw_input(prompt) |
|
220 | 221 | |
|
221 | 222 | builtin_mod_name = "__builtin__" |
|
222 | 223 | import __builtin__ as builtin_mod |
|
223 | 224 | |
|
224 | 225 | str_to_unicode = decode |
|
225 | 226 | unicode_to_str = encode |
|
226 | 227 | str_to_bytes = no_code |
|
227 | 228 | bytes_to_str = no_code |
|
228 | 229 | cast_bytes_py2 = cast_bytes |
|
229 | 230 | cast_unicode_py2 = cast_unicode |
|
230 | 231 | buffer_to_bytes_py2 = buffer_to_bytes |
|
231 | 232 | |
|
232 | 233 | string_types = (str, unicode) |
|
233 | 234 | unicode_type = unicode |
|
234 | 235 | |
|
235 | 236 | import re |
|
236 | 237 | _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$") |
|
237 | 238 | def isidentifier(s, dotted=False): |
|
238 | 239 | if dotted: |
|
239 | 240 | return all(isidentifier(a) for a in s.split(".")) |
|
240 | 241 | return bool(_name_re.match(s)) |
|
241 | 242 | |
|
242 | 243 | xrange = xrange |
|
243 | 244 | def iteritems(d): return d.iteritems() |
|
244 | 245 | def itervalues(d): return d.itervalues() |
|
245 | 246 | getcwd = os.getcwdu |
|
246 | 247 | |
|
247 | 248 | def MethodType(func, instance): |
|
248 | 249 | return types.MethodType(func, instance, type(instance)) |
|
249 | 250 | |
|
250 | 251 | def doctest_refactor_print(func_or_str): |
|
251 | 252 | return func_or_str |
|
252 | 253 | |
|
253 | 254 | def get_closure(f): |
|
254 | 255 | """Get a function's closure attribute""" |
|
255 | 256 | return f.func_closure |
|
256 | 257 | |
|
257 | 258 | which = _shutil_which |
|
258 | 259 | |
|
259 | 260 | # Abstract u'abc' syntax: |
|
260 | 261 | @_modify_str_or_docstring |
|
261 | 262 | def u_format(s): |
|
262 | 263 | """"{u}'abc'" --> "u'abc'" (Python 2) |
|
263 | 264 | |
|
264 | 265 | Accepts a string or a function, so it can be used as a decorator.""" |
|
265 | 266 | return s.format(u='u') |
|
266 | 267 | |
|
267 | 268 | if sys.platform == 'win32': |
|
268 | 269 | def execfile(fname, glob=None, loc=None, compiler=None): |
|
269 | 270 | loc = loc if (loc is not None) else glob |
|
270 | 271 | scripttext = builtin_mod.open(fname).read()+ '\n' |
|
271 | 272 | # compile converts unicode filename to str assuming |
|
272 | 273 | # ascii. Let's do the conversion before calling compile |
|
273 | 274 | if isinstance(fname, unicode): |
|
274 | 275 | filename = unicode_to_str(fname) |
|
275 | 276 | else: |
|
276 | 277 | filename = fname |
|
277 | 278 | compiler = compiler or compile |
|
278 | 279 | exec(compiler(scripttext, filename, 'exec'), glob, loc) |
|
279 | 280 | |
|
280 | 281 | else: |
|
281 | 282 | def execfile(fname, glob=None, loc=None, compiler=None): |
|
282 | 283 | if isinstance(fname, unicode): |
|
283 | 284 | filename = fname.encode(sys.getfilesystemencoding()) |
|
284 | 285 | else: |
|
285 | 286 | filename = fname |
|
286 | 287 | where = [ns for ns in [glob, loc] if ns is not None] |
|
287 | 288 | if compiler is None: |
|
288 | 289 | builtin_mod.execfile(filename, *where) |
|
289 | 290 | else: |
|
290 | 291 | scripttext = builtin_mod.open(fname).read().rstrip() + '\n' |
|
291 | 292 | exec(compiler(scripttext, filename, 'exec'), glob, loc) |
|
292 | 293 | |
|
293 | 294 | |
|
294 | 295 | PY2 = not PY3 |
|
295 | PYPY = any(k.startswith("pypy") for k in dir(sys)) | |
|
296 | PYPY = platform.python_implementation() == "PyPy" | |
|
296 | 297 | |
|
297 | 298 | |
|
298 | 299 | def annotate(**kwargs): |
|
299 | 300 | """Python 3 compatible function annotation for Python 2.""" |
|
300 | 301 | if not kwargs: |
|
301 | 302 | raise ValueError('annotations must be provided as keyword arguments') |
|
302 | 303 | def dec(f): |
|
303 | 304 | if hasattr(f, '__annotations__'): |
|
304 | 305 | for k, v in kwargs.items(): |
|
305 | 306 | f.__annotations__[k] = v |
|
306 | 307 | else: |
|
307 | 308 | f.__annotations__ = kwargs |
|
308 | 309 | return f |
|
309 | 310 | return dec |
|
310 | 311 | |
|
311 | 312 | |
|
312 | 313 | # Parts below taken from six: |
|
313 | 314 | # Copyright (c) 2010-2013 Benjamin Peterson |
|
314 | 315 | # |
|
315 | 316 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
|
316 | 317 | # of this software and associated documentation files (the "Software"), to deal |
|
317 | 318 | # in the Software without restriction, including without limitation the rights |
|
318 | 319 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
319 | 320 | # copies of the Software, and to permit persons to whom the Software is |
|
320 | 321 | # furnished to do so, subject to the following conditions: |
|
321 | 322 | # |
|
322 | 323 | # The above copyright notice and this permission notice shall be included in all |
|
323 | 324 | # copies or substantial portions of the Software. |
|
324 | 325 | # |
|
325 | 326 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
326 | 327 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
327 | 328 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
328 | 329 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
329 | 330 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
330 | 331 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
331 | 332 | # SOFTWARE. |
|
332 | 333 | |
|
333 | 334 | def with_metaclass(meta, *bases): |
|
334 | 335 | """Create a base class with a metaclass.""" |
|
335 | 336 | return meta("_NewBase", bases, {}) |
@@ -1,28 +1,28 b'' | |||
|
1 | 1 | ; Tox (http://tox.testrun.org/) is a virtualenv manager for running tests in |
|
2 | 2 | ; multiple environments. This configuration file gets the requirements from |
|
3 | 3 | ; setup.py like a "pip install ipython[test]". To create the environments, it |
|
4 | 4 | ; requires every interpreter available/installed. |
|
5 | 5 | ; -- Commands -- |
|
6 | 6 | ; pip install tox # Installs tox |
|
7 | 7 | ; tox # Runs the tests (call from the directory with tox.ini) |
|
8 |
; tox -r # |
|
|
8 | ; tox -r # Ditto, but forcing the virtual environments to be rebuilt | |
|
9 | 9 | ; tox -e py35,pypy # Runs only in the selected environments |
|
10 | 10 | ; tox -- --all -j # Runs "iptest --all -j" in every environment |
|
11 | 11 | |
|
12 | 12 | [tox] |
|
13 | 13 | envlist = py{36,35,34,33,27,py} |
|
14 | 14 | skip_missing_interpreters = True |
|
15 | 15 | toxworkdir = /tmp/tox_ipython |
|
16 | 16 | |
|
17 | 17 | [testenv] |
|
18 | 18 | ; PyPy requires its Numpy fork instead of "pip install numpy" |
|
19 | 19 | ; Other IPython/testing dependencies should be in setup.py, not here |
|
20 | 20 | deps = |
|
21 | 21 | pypy: https://bitbucket.org/pypy/numpy/get/master.zip |
|
22 | 22 | py{36,35,34,33,27}: matplotlib |
|
23 | 23 | .[test] |
|
24 | 24 | |
|
25 |
; It's just to avoid loading IPython |
|
|
25 | ; It's just to avoid loading the IPython package in the current directory | |
|
26 | 26 | changedir = {envtmpdir} |
|
27 | 27 | |
|
28 | 28 | commands = iptest {posargs} |
General Comments 0
You need to be logged in to leave comments.
Login now