##// END OF EJS Templates
Added support to list available object types, to use on object type matching in magic function %psearch.
Andreas -
Show More
@@ -1,702 +1,714 b''
1 1 """Implementation of namespace-related magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import gc
17 17 import re
18 18 import sys
19 19
20 20 # Our own packages
21 21 from IPython.core import page
22 22 from IPython.core.error import StdinNotImplementedError, UsageError
23 23 from IPython.core.magic import Magics, magics_class, line_magic
24 24 from IPython.testing.skipdoctest import skip_doctest
25 25 from IPython.utils.encoding import DEFAULT_ENCODING
26 26 from IPython.utils.openpy import read_py_file
27 27 from IPython.utils.path import get_py_filename
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Magic implementation classes
31 31 #-----------------------------------------------------------------------------
32 32
33 33 @magics_class
34 34 class NamespaceMagics(Magics):
35 35 """Magics to manage various aspects of the user's namespace.
36 36
37 37 These include listing variables, introspecting into them, etc.
38 38 """
39 39
40 40 @line_magic
41 41 def pinfo(self, parameter_s='', namespaces=None):
42 42 """Provide detailed information about an object.
43 43
44 44 '%pinfo object' is just a synonym for object? or ?object."""
45 45
46 46 #print 'pinfo par: <%s>' % parameter_s # dbg
47 47 # detail_level: 0 -> obj? , 1 -> obj??
48 48 detail_level = 0
49 49 # We need to detect if we got called as 'pinfo pinfo foo', which can
50 50 # happen if the user types 'pinfo foo?' at the cmd line.
51 51 pinfo,qmark1,oname,qmark2 = \
52 52 re.match(r'(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
53 53 if pinfo or qmark1 or qmark2:
54 54 detail_level = 1
55 55 if "*" in oname:
56 56 self.psearch(oname)
57 57 else:
58 58 self.shell._inspect('pinfo', oname, detail_level=detail_level,
59 59 namespaces=namespaces)
60 60
61 61 @line_magic
62 62 def pinfo2(self, parameter_s='', namespaces=None):
63 63 """Provide extra detailed information about an object.
64 64
65 65 '%pinfo2 object' is just a synonym for object?? or ??object."""
66 66 self.shell._inspect('pinfo', parameter_s, detail_level=1,
67 67 namespaces=namespaces)
68 68
69 69 @skip_doctest
70 70 @line_magic
71 71 def pdef(self, parameter_s='', namespaces=None):
72 72 """Print the call signature for any callable object.
73 73
74 74 If the object is a class, print the constructor information.
75 75
76 76 Examples
77 77 --------
78 78 ::
79 79
80 80 In [3]: %pdef urllib.urlopen
81 81 urllib.urlopen(url, data=None, proxies=None)
82 82 """
83 83 self.shell._inspect('pdef',parameter_s, namespaces)
84 84
85 85 @line_magic
86 86 def pdoc(self, parameter_s='', namespaces=None):
87 87 """Print the docstring for an object.
88 88
89 89 If the given object is a class, it will print both the class and the
90 90 constructor docstrings."""
91 91 self.shell._inspect('pdoc',parameter_s, namespaces)
92 92
93 93 @line_magic
94 94 def psource(self, parameter_s='', namespaces=None):
95 95 """Print (or run through pager) the source code for an object."""
96 96 if not parameter_s:
97 97 raise UsageError('Missing object name.')
98 98 self.shell._inspect('psource',parameter_s, namespaces)
99 99
100 100 @line_magic
101 101 def pfile(self, parameter_s='', namespaces=None):
102 102 """Print (or run through pager) the file where an object is defined.
103 103
104 104 The file opens at the line where the object definition begins. IPython
105 105 will honor the environment variable PAGER if set, and otherwise will
106 106 do its best to print the file in a convenient form.
107 107
108 108 If the given argument is not an object currently defined, IPython will
109 109 try to interpret it as a filename (automatically adding a .py extension
110 110 if needed). You can thus use %pfile as a syntax highlighting code
111 111 viewer."""
112 112
113 113 # first interpret argument as an object name
114 114 out = self.shell._inspect('pfile',parameter_s, namespaces)
115 115 # if not, try the input as a filename
116 116 if out == 'not found':
117 117 try:
118 118 filename = get_py_filename(parameter_s)
119 119 except IOError as msg:
120 120 print(msg)
121 121 return
122 122 page.page(self.shell.pycolorize(read_py_file(filename, skip_encoding_cookie=False)))
123 123
124 124 @line_magic
125 125 def psearch(self, parameter_s=''):
126 126 """Search for object in namespaces by wildcard.
127 127
128 128 %psearch [options] PATTERN [OBJECT TYPE]
129 129
130 130 Note: ? can be used as a synonym for %psearch, at the beginning or at
131 131 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
132 132 rest of the command line must be unchanged (options come first), so
133 133 for example the following forms are equivalent
134 134
135 135 %psearch -i a* function
136 136 -i a* function?
137 137 ?-i a* function
138 138
139 139 Arguments:
140 140
141 141 PATTERN
142 142
143 143 where PATTERN is a string containing * as a wildcard similar to its
144 144 use in a shell. The pattern is matched in all namespaces on the
145 145 search path. By default objects starting with a single _ are not
146 146 matched, many IPython generated objects have a single
147 147 underscore. The default is case insensitive matching. Matching is
148 148 also done on the attributes of objects and not only on the objects
149 149 in a module.
150 150
151 151 [OBJECT TYPE]
152 152
153 153 Is the name of a python type from the types module. The name is
154 154 given in lowercase without the ending type, ex. StringType is
155 155 written string. By adding a type here only objects matching the
156 156 given type are matched. Using all here makes the pattern match all
157 157 types (this is the default).
158 158
159 159 Options:
160 160
161 161 -a: makes the pattern match even objects whose names start with a
162 162 single underscore. These names are normally omitted from the
163 163 search.
164 164
165 165 -i/-c: make the pattern case insensitive/sensitive. If neither of
166 166 these options are given, the default is read from your configuration
167 167 file, with the option ``InteractiveShell.wildcards_case_sensitive``.
168 168 If this option is not specified in your configuration file, IPython's
169 169 internal default is to do a case sensitive search.
170 170
171 171 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
172 172 specify can be searched in any of the following namespaces:
173 173 'builtin', 'user', 'user_global','internal', 'alias', where
174 174 'builtin' and 'user' are the search defaults. Note that you should
175 175 not use quotes when specifying namespaces.
176
177 -l: List all available object types for object matching. This function
178 can be used without arguments.
176 179
177 180 'Builtin' contains the python module builtin, 'user' contains all
178 181 user data, 'alias' only contain the shell aliases and no python
179 182 objects, 'internal' contains objects used by IPython. The
180 183 'user_global' namespace is only used by embedded IPython instances,
181 184 and it contains module-level globals. You can add namespaces to the
182 185 search with -s or exclude them with -e (these options can be given
183 186 more than once).
184 187
185 188 Examples
186 189 --------
187 190 ::
188 191
189 192 %psearch a* -> objects beginning with an a
190 193 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
191 194 %psearch a* function -> all functions beginning with an a
192 195 %psearch re.e* -> objects beginning with an e in module re
193 196 %psearch r*.e* -> objects that start with e in modules starting in r
194 197 %psearch r*.* string -> all strings in modules beginning with r
195 198
196 199 Case sensitive search::
197 200
198 201 %psearch -c a* list all object beginning with lower case a
199 202
200 203 Show objects beginning with a single _::
201 204
202 205 %psearch -a _* list objects beginning with a single underscore
206
207 List available objects::
208
209 %psearch -l list all available object types
203 210 """
204 211 try:
205 212 parameter_s.encode('ascii')
206 213 except UnicodeEncodeError:
207 214 print('Python identifiers can only contain ascii characters.')
208 215 return
209 216
210 217 # default namespaces to be searched
211 218 def_search = ['user_local', 'user_global', 'builtin']
212 219
213 220 # Process options/args
214 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
221 opts,args = self.parse_options(parameter_s,'cias:e:l',list_all=True)
215 222 opt = opts.get
216 223 shell = self.shell
217 224 psearch = shell.inspector.psearch
225
226 # select list object types
227 list_types = False
228 if 'l' in opts:
229 list_types = True
218 230
219 231 # select case options
220 232 if 'i' in opts:
221 233 ignore_case = True
222 234 elif 'c' in opts:
223 235 ignore_case = False
224 236 else:
225 237 ignore_case = not shell.wildcards_case_sensitive
226 238
227 239 # Build list of namespaces to search from user options
228 240 def_search.extend(opt('s',[]))
229 241 ns_exclude = ns_exclude=opt('e',[])
230 242 ns_search = [nm for nm in def_search if nm not in ns_exclude]
231 243
232 244 # Call the actual search
233 245 try:
234 246 psearch(args,shell.ns_table,ns_search,
235 show_all=opt('a'),ignore_case=ignore_case)
247 show_all=opt('a'),ignore_case=ignore_case, list_types=list_types)
236 248 except:
237 249 shell.showtraceback()
238 250
239 251 @skip_doctest
240 252 @line_magic
241 253 def who_ls(self, parameter_s=''):
242 254 """Return a sorted list of all interactive variables.
243 255
244 256 If arguments are given, only variables of types matching these
245 257 arguments are returned.
246 258
247 259 Examples
248 260 --------
249 261
250 262 Define two variables and list them with who_ls::
251 263
252 264 In [1]: alpha = 123
253 265
254 266 In [2]: beta = 'test'
255 267
256 268 In [3]: %who_ls
257 269 Out[3]: ['alpha', 'beta']
258 270
259 271 In [4]: %who_ls int
260 272 Out[4]: ['alpha']
261 273
262 274 In [5]: %who_ls str
263 275 Out[5]: ['beta']
264 276 """
265 277
266 278 user_ns = self.shell.user_ns
267 279 user_ns_hidden = self.shell.user_ns_hidden
268 280 nonmatching = object() # This can never be in user_ns
269 281 out = [ i for i in user_ns
270 282 if not i.startswith('_') \
271 283 and (user_ns[i] is not user_ns_hidden.get(i, nonmatching)) ]
272 284
273 285 typelist = parameter_s.split()
274 286 if typelist:
275 287 typeset = set(typelist)
276 288 out = [i for i in out if type(user_ns[i]).__name__ in typeset]
277 289
278 290 out.sort()
279 291 return out
280 292
281 293 @skip_doctest
282 294 @line_magic
283 295 def who(self, parameter_s=''):
284 296 """Print all interactive variables, with some minimal formatting.
285 297
286 298 If any arguments are given, only variables whose type matches one of
287 299 these are printed. For example::
288 300
289 301 %who function str
290 302
291 303 will only list functions and strings, excluding all other types of
292 304 variables. To find the proper type names, simply use type(var) at a
293 305 command line to see how python prints type names. For example:
294 306
295 307 ::
296 308
297 309 In [1]: type('hello')\\
298 310 Out[1]: <type 'str'>
299 311
300 312 indicates that the type name for strings is 'str'.
301 313
302 314 ``%who`` always excludes executed names loaded through your configuration
303 315 file and things which are internal to IPython.
304 316
305 317 This is deliberate, as typically you may load many modules and the
306 318 purpose of %who is to show you only what you've manually defined.
307 319
308 320 Examples
309 321 --------
310 322
311 323 Define two variables and list them with who::
312 324
313 325 In [1]: alpha = 123
314 326
315 327 In [2]: beta = 'test'
316 328
317 329 In [3]: %who
318 330 alpha beta
319 331
320 332 In [4]: %who int
321 333 alpha
322 334
323 335 In [5]: %who str
324 336 beta
325 337 """
326 338
327 339 varlist = self.who_ls(parameter_s)
328 340 if not varlist:
329 341 if parameter_s:
330 342 print('No variables match your requested type.')
331 343 else:
332 344 print('Interactive namespace is empty.')
333 345 return
334 346
335 347 # if we have variables, move on...
336 348 count = 0
337 349 for i in varlist:
338 350 print(i+'\t', end=' ')
339 351 count += 1
340 352 if count > 8:
341 353 count = 0
342 354 print()
343 355 print()
344 356
345 357 @skip_doctest
346 358 @line_magic
347 359 def whos(self, parameter_s=''):
348 360 """Like %who, but gives some extra information about each variable.
349 361
350 362 The same type filtering of %who can be applied here.
351 363
352 364 For all variables, the type is printed. Additionally it prints:
353 365
354 366 - For {},[],(): their length.
355 367
356 368 - For numpy arrays, a summary with shape, number of
357 369 elements, typecode and size in memory.
358 370
359 371 - Everything else: a string representation, snipping their middle if
360 372 too long.
361 373
362 374 Examples
363 375 --------
364 376
365 377 Define two variables and list them with whos::
366 378
367 379 In [1]: alpha = 123
368 380
369 381 In [2]: beta = 'test'
370 382
371 383 In [3]: %whos
372 384 Variable Type Data/Info
373 385 --------------------------------
374 386 alpha int 123
375 387 beta str test
376 388 """
377 389
378 390 varnames = self.who_ls(parameter_s)
379 391 if not varnames:
380 392 if parameter_s:
381 393 print('No variables match your requested type.')
382 394 else:
383 395 print('Interactive namespace is empty.')
384 396 return
385 397
386 398 # if we have variables, move on...
387 399
388 400 # for these types, show len() instead of data:
389 401 seq_types = ['dict', 'list', 'tuple']
390 402
391 403 # for numpy arrays, display summary info
392 404 ndarray_type = None
393 405 if 'numpy' in sys.modules:
394 406 try:
395 407 from numpy import ndarray
396 408 except ImportError:
397 409 pass
398 410 else:
399 411 ndarray_type = ndarray.__name__
400 412
401 413 # Find all variable names and types so we can figure out column sizes
402 414
403 415 # some types are well known and can be shorter
404 416 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
405 417 def type_name(v):
406 418 tn = type(v).__name__
407 419 return abbrevs.get(tn,tn)
408 420
409 421 varlist = [self.shell.user_ns[n] for n in varnames]
410 422
411 423 typelist = []
412 424 for vv in varlist:
413 425 tt = type_name(vv)
414 426
415 427 if tt=='instance':
416 428 typelist.append( abbrevs.get(str(vv.__class__),
417 429 str(vv.__class__)))
418 430 else:
419 431 typelist.append(tt)
420 432
421 433 # column labels and # of spaces as separator
422 434 varlabel = 'Variable'
423 435 typelabel = 'Type'
424 436 datalabel = 'Data/Info'
425 437 colsep = 3
426 438 # variable format strings
427 439 vformat = "{0:<{varwidth}}{1:<{typewidth}}"
428 440 aformat = "%s: %s elems, type `%s`, %s bytes"
429 441 # find the size of the columns to format the output nicely
430 442 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
431 443 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
432 444 # table header
433 445 print(varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
434 446 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1))
435 447 # and the table itself
436 448 kb = 1024
437 449 Mb = 1048576 # kb**2
438 450 for vname,var,vtype in zip(varnames,varlist,typelist):
439 451 print(vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth), end=' ')
440 452 if vtype in seq_types:
441 453 print("n="+str(len(var)))
442 454 elif vtype == ndarray_type:
443 455 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
444 456 if vtype==ndarray_type:
445 457 # numpy
446 458 vsize = var.size
447 459 vbytes = vsize*var.itemsize
448 460 vdtype = var.dtype
449 461
450 462 if vbytes < 100000:
451 463 print(aformat % (vshape, vsize, vdtype, vbytes))
452 464 else:
453 465 print(aformat % (vshape, vsize, vdtype, vbytes), end=' ')
454 466 if vbytes < Mb:
455 467 print('(%s kb)' % (vbytes/kb,))
456 468 else:
457 469 print('(%s Mb)' % (vbytes/Mb,))
458 470 else:
459 471 try:
460 472 vstr = str(var)
461 473 except UnicodeEncodeError:
462 474 vstr = var.encode(DEFAULT_ENCODING,
463 475 'backslashreplace')
464 476 except:
465 477 vstr = "<object with id %d (str() failed)>" % id(var)
466 478 vstr = vstr.replace('\n', '\\n')
467 479 if len(vstr) < 50:
468 480 print(vstr)
469 481 else:
470 482 print(vstr[:25] + "<...>" + vstr[-25:])
471 483
472 484 @line_magic
473 485 def reset(self, parameter_s=''):
474 486 """Resets the namespace by removing all names defined by the user, if
475 487 called without arguments, or by removing some types of objects, such
476 488 as everything currently in IPython's In[] and Out[] containers (see
477 489 the parameters for details).
478 490
479 491 Parameters
480 492 ----------
481 493 -f : force reset without asking for confirmation.
482 494
483 495 -s : 'Soft' reset: Only clears your namespace, leaving history intact.
484 496 References to objects may be kept. By default (without this option),
485 497 we do a 'hard' reset, giving you a new session and removing all
486 498 references to objects from the current session.
487 499
488 500 in : reset input history
489 501
490 502 out : reset output history
491 503
492 504 dhist : reset directory history
493 505
494 506 array : reset only variables that are NumPy arrays
495 507
496 508 See Also
497 509 --------
498 510 reset_selective : invoked as ``%reset_selective``
499 511
500 512 Examples
501 513 --------
502 514 ::
503 515
504 516 In [6]: a = 1
505 517
506 518 In [7]: a
507 519 Out[7]: 1
508 520
509 521 In [8]: 'a' in _ip.user_ns
510 522 Out[8]: True
511 523
512 524 In [9]: %reset -f
513 525
514 526 In [1]: 'a' in _ip.user_ns
515 527 Out[1]: False
516 528
517 529 In [2]: %reset -f in
518 530 Flushing input history
519 531
520 532 In [3]: %reset -f dhist in
521 533 Flushing directory history
522 534 Flushing input history
523 535
524 536 Notes
525 537 -----
526 538 Calling this magic from clients that do not implement standard input,
527 539 such as the ipython notebook interface, will reset the namespace
528 540 without confirmation.
529 541 """
530 542 opts, args = self.parse_options(parameter_s,'sf', mode='list')
531 543 if 'f' in opts:
532 544 ans = True
533 545 else:
534 546 try:
535 547 ans = self.shell.ask_yes_no(
536 548 "Once deleted, variables cannot be recovered. Proceed (y/[n])?",
537 549 default='n')
538 550 except StdinNotImplementedError:
539 551 ans = True
540 552 if not ans:
541 553 print('Nothing done.')
542 554 return
543 555
544 556 if 's' in opts: # Soft reset
545 557 user_ns = self.shell.user_ns
546 558 for i in self.who_ls():
547 559 del(user_ns[i])
548 560 elif len(args) == 0: # Hard reset
549 561 self.shell.reset(new_session = False)
550 562
551 563 # reset in/out/dhist/array: previously extensinions/clearcmd.py
552 564 ip = self.shell
553 565 user_ns = self.shell.user_ns # local lookup, heavily used
554 566
555 567 for target in args:
556 568 target = target.lower() # make matches case insensitive
557 569 if target == 'out':
558 570 print("Flushing output cache (%d entries)" % len(user_ns['_oh']))
559 571 self.shell.displayhook.flush()
560 572
561 573 elif target == 'in':
562 574 print("Flushing input history")
563 575 pc = self.shell.displayhook.prompt_count + 1
564 576 for n in range(1, pc):
565 577 key = '_i'+repr(n)
566 578 user_ns.pop(key,None)
567 579 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
568 580 hm = ip.history_manager
569 581 # don't delete these, as %save and %macro depending on the
570 582 # length of these lists to be preserved
571 583 hm.input_hist_parsed[:] = [''] * pc
572 584 hm.input_hist_raw[:] = [''] * pc
573 585 # hm has internal machinery for _i,_ii,_iii, clear it out
574 586 hm._i = hm._ii = hm._iii = hm._i00 = u''
575 587
576 588 elif target == 'array':
577 589 # Support cleaning up numpy arrays
578 590 try:
579 591 from numpy import ndarray
580 592 # This must be done with items and not iteritems because
581 593 # we're going to modify the dict in-place.
582 594 for x,val in list(user_ns.items()):
583 595 if isinstance(val,ndarray):
584 596 del user_ns[x]
585 597 except ImportError:
586 598 print("reset array only works if Numpy is available.")
587 599
588 600 elif target == 'dhist':
589 601 print("Flushing directory history")
590 602 del user_ns['_dh'][:]
591 603
592 604 else:
593 605 print("Don't know how to reset ", end=' ')
594 606 print(target + ", please run `%reset?` for details")
595 607
596 608 gc.collect()
597 609
598 610 @line_magic
599 611 def reset_selective(self, parameter_s=''):
600 612 """Resets the namespace by removing names defined by the user.
601 613
602 614 Input/Output history are left around in case you need them.
603 615
604 616 %reset_selective [-f] regex
605 617
606 618 No action is taken if regex is not included
607 619
608 620 Options
609 621 -f : force reset without asking for confirmation.
610 622
611 623 See Also
612 624 --------
613 625 reset : invoked as ``%reset``
614 626
615 627 Examples
616 628 --------
617 629
618 630 We first fully reset the namespace so your output looks identical to
619 631 this example for pedagogical reasons; in practice you do not need a
620 632 full reset::
621 633
622 634 In [1]: %reset -f
623 635
624 636 Now, with a clean namespace we can make a few variables and use
625 637 ``%reset_selective`` to only delete names that match our regexp::
626 638
627 639 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
628 640
629 641 In [3]: who_ls
630 642 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
631 643
632 644 In [4]: %reset_selective -f b[2-3]m
633 645
634 646 In [5]: who_ls
635 647 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
636 648
637 649 In [6]: %reset_selective -f d
638 650
639 651 In [7]: who_ls
640 652 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
641 653
642 654 In [8]: %reset_selective -f c
643 655
644 656 In [9]: who_ls
645 657 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
646 658
647 659 In [10]: %reset_selective -f b
648 660
649 661 In [11]: who_ls
650 662 Out[11]: ['a']
651 663
652 664 Notes
653 665 -----
654 666 Calling this magic from clients that do not implement standard input,
655 667 such as the ipython notebook interface, will reset the namespace
656 668 without confirmation.
657 669 """
658 670
659 671 opts, regex = self.parse_options(parameter_s,'f')
660 672
661 673 if 'f' in opts:
662 674 ans = True
663 675 else:
664 676 try:
665 677 ans = self.shell.ask_yes_no(
666 678 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ",
667 679 default='n')
668 680 except StdinNotImplementedError:
669 681 ans = True
670 682 if not ans:
671 683 print('Nothing done.')
672 684 return
673 685 user_ns = self.shell.user_ns
674 686 if not regex:
675 687 print('No regex pattern specified. Nothing done.')
676 688 return
677 689 else:
678 690 try:
679 691 m = re.compile(regex)
680 692 except TypeError:
681 693 raise TypeError('regex must be a string or compiled pattern')
682 694 for i in self.who_ls():
683 695 if m.search(i):
684 696 del(user_ns[i])
685 697
686 698 @line_magic
687 699 def xdel(self, parameter_s=''):
688 700 """Delete a variable, trying to clear it from anywhere that
689 701 IPython's machinery has references to it. By default, this uses
690 702 the identity of the named object in the user namespace to remove
691 703 references held under other names. The object is also removed
692 704 from the output history.
693 705
694 706 Options
695 707 -n : Delete the specified name from all namespaces, without
696 708 checking their identity.
697 709 """
698 710 opts, varname = self.parse_options(parameter_s,'n')
699 711 try:
700 712 self.shell.del_var(varname, ('n' in opts))
701 713 except (NameError, ValueError) as e:
702 714 print(type(e).__name__ +": "+ str(e))
@@ -1,1064 +1,1072 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tools for inspecting Python objects.
3 3
4 4 Uses syntax highlighting for presenting the various information elements.
5 5
6 6 Similar in spirit to the inspect module, but all calls take a name argument to
7 7 reference the name under which an object is being read.
8 8 """
9 9
10 10 # Copyright (c) IPython Development Team.
11 11 # Distributed under the terms of the Modified BSD License.
12 12
13 13 __all__ = ['Inspector','InspectColors']
14 14
15 15 # stdlib modules
16 16 import ast
17 17 import inspect
18 18 from inspect import signature
19 19 import linecache
20 20 import warnings
21 21 import os
22 22 from textwrap import dedent
23 23 import types
24 24 import io as stdlib_io
25 25 from itertools import zip_longest
26 26
27 27 # IPython's own
28 28 from IPython.core import page
29 29 from IPython.lib.pretty import pretty
30 30 from IPython.testing.skipdoctest import skip_doctest
31 31 from IPython.utils import PyColorize
32 32 from IPython.utils import openpy
33 33 from IPython.utils import py3compat
34 34 from IPython.utils.dir2 import safe_hasattr
35 35 from IPython.utils.path import compress_user
36 36 from IPython.utils.text import indent
37 37 from IPython.utils.wildcard import list_namespace
38 from IPython.utils.wildcard import typestr2type
38 39 from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable
39 40 from IPython.utils.py3compat import cast_unicode
40 41 from IPython.utils.colorable import Colorable
41 42 from IPython.utils.decorators import undoc
42 43
43 44 from pygments import highlight
44 45 from pygments.lexers import PythonLexer
45 46 from pygments.formatters import HtmlFormatter
46 47
47 48 def pylight(code):
48 49 return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))
49 50
50 51 # builtin docstrings to ignore
51 52 _func_call_docstring = types.FunctionType.__call__.__doc__
52 53 _object_init_docstring = object.__init__.__doc__
53 54 _builtin_type_docstrings = {
54 55 inspect.getdoc(t) for t in (types.ModuleType, types.MethodType,
55 56 types.FunctionType, property)
56 57 }
57 58
58 59 _builtin_func_type = type(all)
59 60 _builtin_meth_type = type(str.upper) # Bound methods have the same type as builtin functions
60 61 #****************************************************************************
61 62 # Builtin color schemes
62 63
63 64 Colors = TermColors # just a shorthand
64 65
65 66 InspectColors = PyColorize.ANSICodeColors
66 67
67 68 #****************************************************************************
68 69 # Auxiliary functions and objects
69 70
70 71 # See the messaging spec for the definition of all these fields. This list
71 72 # effectively defines the order of display
72 73 info_fields = ['type_name', 'base_class', 'string_form', 'namespace',
73 74 'length', 'file', 'definition', 'docstring', 'source',
74 75 'init_definition', 'class_docstring', 'init_docstring',
75 76 'call_def', 'call_docstring',
76 77 # These won't be printed but will be used to determine how to
77 78 # format the object
78 79 'ismagic', 'isalias', 'isclass', 'argspec', 'found', 'name'
79 80 ]
80 81
81 82
82 83 def object_info(**kw):
83 84 """Make an object info dict with all fields present."""
84 85 infodict = dict(zip_longest(info_fields, [None]))
85 86 infodict.update(kw)
86 87 return infodict
87 88
88 89
89 90 def get_encoding(obj):
90 91 """Get encoding for python source file defining obj
91 92
92 93 Returns None if obj is not defined in a sourcefile.
93 94 """
94 95 ofile = find_file(obj)
95 96 # run contents of file through pager starting at line where the object
96 97 # is defined, as long as the file isn't binary and is actually on the
97 98 # filesystem.
98 99 if ofile is None:
99 100 return None
100 101 elif ofile.endswith(('.so', '.dll', '.pyd')):
101 102 return None
102 103 elif not os.path.isfile(ofile):
103 104 return None
104 105 else:
105 106 # Print only text files, not extension binaries. Note that
106 107 # getsourcelines returns lineno with 1-offset and page() uses
107 108 # 0-offset, so we must adjust.
108 109 with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2
109 110 encoding, lines = openpy.detect_encoding(buffer.readline)
110 111 return encoding
111 112
112 113 def getdoc(obj):
113 114 """Stable wrapper around inspect.getdoc.
114 115
115 116 This can't crash because of attribute problems.
116 117
117 118 It also attempts to call a getdoc() method on the given object. This
118 119 allows objects which provide their docstrings via non-standard mechanisms
119 120 (like Pyro proxies) to still be inspected by ipython's ? system.
120 121 """
121 122 # Allow objects to offer customized documentation via a getdoc method:
122 123 try:
123 124 ds = obj.getdoc()
124 125 except Exception:
125 126 pass
126 127 else:
127 128 if isinstance(ds, str):
128 129 return inspect.cleandoc(ds)
129 130 docstr = inspect.getdoc(obj)
130 131 encoding = get_encoding(obj)
131 132 return py3compat.cast_unicode(docstr, encoding=encoding)
132 133
133 134
134 135 def getsource(obj, oname=''):
135 136 """Wrapper around inspect.getsource.
136 137
137 138 This can be modified by other projects to provide customized source
138 139 extraction.
139 140
140 141 Parameters
141 142 ----------
142 143 obj : object
143 144 an object whose source code we will attempt to extract
144 145 oname : str
145 146 (optional) a name under which the object is known
146 147
147 148 Returns
148 149 -------
149 150 src : unicode or None
150 151
151 152 """
152 153
153 154 if isinstance(obj, property):
154 155 sources = []
155 156 for attrname in ['fget', 'fset', 'fdel']:
156 157 fn = getattr(obj, attrname)
157 158 if fn is not None:
158 159 encoding = get_encoding(fn)
159 160 oname_prefix = ('%s.' % oname) if oname else ''
160 161 sources.append(cast_unicode(
161 162 ''.join(('# ', oname_prefix, attrname)),
162 163 encoding=encoding))
163 164 if inspect.isfunction(fn):
164 165 sources.append(dedent(getsource(fn)))
165 166 else:
166 167 # Default str/repr only prints function name,
167 168 # pretty.pretty prints module name too.
168 169 sources.append(cast_unicode(
169 170 '%s%s = %s\n' % (
170 171 oname_prefix, attrname, pretty(fn)),
171 172 encoding=encoding))
172 173 if sources:
173 174 return '\n'.join(sources)
174 175 else:
175 176 return None
176 177
177 178 else:
178 179 # Get source for non-property objects.
179 180
180 181 obj = _get_wrapped(obj)
181 182
182 183 try:
183 184 src = inspect.getsource(obj)
184 185 except TypeError:
185 186 # The object itself provided no meaningful source, try looking for
186 187 # its class definition instead.
187 188 if hasattr(obj, '__class__'):
188 189 try:
189 190 src = inspect.getsource(obj.__class__)
190 191 except TypeError:
191 192 return None
192 193
193 194 encoding = get_encoding(obj)
194 195 return cast_unicode(src, encoding=encoding)
195 196
196 197
197 198 def is_simple_callable(obj):
198 199 """True if obj is a function ()"""
199 200 return (inspect.isfunction(obj) or inspect.ismethod(obj) or \
200 201 isinstance(obj, _builtin_func_type) or isinstance(obj, _builtin_meth_type))
201 202
202 203
203 204 def getargspec(obj):
204 205 """Wrapper around :func:`inspect.getfullargspec` on Python 3, and
205 206 :func:inspect.getargspec` on Python 2.
206 207
207 208 In addition to functions and methods, this can also handle objects with a
208 209 ``__call__`` attribute.
209 210 """
210 211 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
211 212 obj = obj.__call__
212 213
213 214 return inspect.getfullargspec(obj)
214 215
215 216
216 217 def format_argspec(argspec):
217 218 """Format argspect, convenience wrapper around inspect's.
218 219
219 220 This takes a dict instead of ordered arguments and calls
220 221 inspect.format_argspec with the arguments in the necessary order.
221 222 """
222 223 return inspect.formatargspec(argspec['args'], argspec['varargs'],
223 224 argspec['varkw'], argspec['defaults'])
224 225
225 226 @undoc
226 227 def call_tip(oinfo, format_call=True):
227 228 """DEPRECATED. Extract call tip data from an oinfo dict.
228 229 """
229 230 warnings.warn('`call_tip` function is deprecated as of IPython 6.0'
230 231 'and will be removed in future versions.', DeprecationWarning, stacklevel=2)
231 232 # Get call definition
232 233 argspec = oinfo.get('argspec')
233 234 if argspec is None:
234 235 call_line = None
235 236 else:
236 237 # Callable objects will have 'self' as their first argument, prune
237 238 # it out if it's there for clarity (since users do *not* pass an
238 239 # extra first argument explicitly).
239 240 try:
240 241 has_self = argspec['args'][0] == 'self'
241 242 except (KeyError, IndexError):
242 243 pass
243 244 else:
244 245 if has_self:
245 246 argspec['args'] = argspec['args'][1:]
246 247
247 248 call_line = oinfo['name']+format_argspec(argspec)
248 249
249 250 # Now get docstring.
250 251 # The priority is: call docstring, constructor docstring, main one.
251 252 doc = oinfo.get('call_docstring')
252 253 if doc is None:
253 254 doc = oinfo.get('init_docstring')
254 255 if doc is None:
255 256 doc = oinfo.get('docstring','')
256 257
257 258 return call_line, doc
258 259
259 260
260 261 def _get_wrapped(obj):
261 262 """Get the original object if wrapped in one or more @decorators
262 263
263 264 Some objects automatically construct similar objects on any unrecognised
264 265 attribute access (e.g. unittest.mock.call). To protect against infinite loops,
265 266 this will arbitrarily cut off after 100 levels of obj.__wrapped__
266 267 attribute access. --TK, Jan 2016
267 268 """
268 269 orig_obj = obj
269 270 i = 0
270 271 while safe_hasattr(obj, '__wrapped__'):
271 272 obj = obj.__wrapped__
272 273 i += 1
273 274 if i > 100:
274 275 # __wrapped__ is probably a lie, so return the thing we started with
275 276 return orig_obj
276 277 return obj
277 278
278 279 def find_file(obj):
279 280 """Find the absolute path to the file where an object was defined.
280 281
281 282 This is essentially a robust wrapper around `inspect.getabsfile`.
282 283
283 284 Returns None if no file can be found.
284 285
285 286 Parameters
286 287 ----------
287 288 obj : any Python object
288 289
289 290 Returns
290 291 -------
291 292 fname : str
292 293 The absolute path to the file where the object was defined.
293 294 """
294 295 obj = _get_wrapped(obj)
295 296
296 297 fname = None
297 298 try:
298 299 fname = inspect.getabsfile(obj)
299 300 except TypeError:
300 301 # For an instance, the file that matters is where its class was
301 302 # declared.
302 303 if hasattr(obj, '__class__'):
303 304 try:
304 305 fname = inspect.getabsfile(obj.__class__)
305 306 except TypeError:
306 307 # Can happen for builtins
307 308 pass
308 309 except:
309 310 pass
310 311 return cast_unicode(fname)
311 312
312 313
313 314 def find_source_lines(obj):
314 315 """Find the line number in a file where an object was defined.
315 316
316 317 This is essentially a robust wrapper around `inspect.getsourcelines`.
317 318
318 319 Returns None if no file can be found.
319 320
320 321 Parameters
321 322 ----------
322 323 obj : any Python object
323 324
324 325 Returns
325 326 -------
326 327 lineno : int
327 328 The line number where the object definition starts.
328 329 """
329 330 obj = _get_wrapped(obj)
330 331
331 332 try:
332 333 try:
333 334 lineno = inspect.getsourcelines(obj)[1]
334 335 except TypeError:
335 336 # For instances, try the class object like getsource() does
336 337 if hasattr(obj, '__class__'):
337 338 lineno = inspect.getsourcelines(obj.__class__)[1]
338 339 else:
339 340 lineno = None
340 341 except:
341 342 return None
342 343
343 344 return lineno
344 345
345 346 class Inspector(Colorable):
346 347
347 348 def __init__(self, color_table=InspectColors,
348 349 code_color_table=PyColorize.ANSICodeColors,
349 350 scheme=None,
350 351 str_detail_level=0,
351 352 parent=None, config=None):
352 353 super(Inspector, self).__init__(parent=parent, config=config)
353 354 self.color_table = color_table
354 355 self.parser = PyColorize.Parser(out='str', parent=self, style=scheme)
355 356 self.format = self.parser.format
356 357 self.str_detail_level = str_detail_level
357 358 self.set_active_scheme(scheme)
358 359
359 360 def _getdef(self,obj,oname=''):
360 361 """Return the call signature for any callable object.
361 362
362 363 If any exception is generated, None is returned instead and the
363 364 exception is suppressed."""
364 365 try:
365 366 hdef = _render_signature(signature(obj), oname)
366 367 return cast_unicode(hdef)
367 368 except:
368 369 return None
369 370
370 371 def __head(self,h):
371 372 """Return a header string with proper colors."""
372 373 return '%s%s%s' % (self.color_table.active_colors.header,h,
373 374 self.color_table.active_colors.normal)
374 375
375 376 def set_active_scheme(self, scheme):
376 377 if scheme is not None:
377 378 self.color_table.set_active_scheme(scheme)
378 379 self.parser.color_table.set_active_scheme(scheme)
379 380
380 381 def noinfo(self, msg, oname):
381 382 """Generic message when no information is found."""
382 383 print('No %s found' % msg, end=' ')
383 384 if oname:
384 385 print('for %s' % oname)
385 386 else:
386 387 print()
387 388
388 389 def pdef(self, obj, oname=''):
389 390 """Print the call signature for any callable object.
390 391
391 392 If the object is a class, print the constructor information."""
392 393
393 394 if not callable(obj):
394 395 print('Object is not callable.')
395 396 return
396 397
397 398 header = ''
398 399
399 400 if inspect.isclass(obj):
400 401 header = self.__head('Class constructor information:\n')
401 402
402 403
403 404 output = self._getdef(obj,oname)
404 405 if output is None:
405 406 self.noinfo('definition header',oname)
406 407 else:
407 408 print(header,self.format(output), end=' ')
408 409
409 410 # In Python 3, all classes are new-style, so they all have __init__.
410 411 @skip_doctest
411 412 def pdoc(self, obj, oname='', formatter=None):
412 413 """Print the docstring for any object.
413 414
414 415 Optional:
415 416 -formatter: a function to run the docstring through for specially
416 417 formatted docstrings.
417 418
418 419 Examples
419 420 --------
420 421
421 422 In [1]: class NoInit:
422 423 ...: pass
423 424
424 425 In [2]: class NoDoc:
425 426 ...: def __init__(self):
426 427 ...: pass
427 428
428 429 In [3]: %pdoc NoDoc
429 430 No documentation found for NoDoc
430 431
431 432 In [4]: %pdoc NoInit
432 433 No documentation found for NoInit
433 434
434 435 In [5]: obj = NoInit()
435 436
436 437 In [6]: %pdoc obj
437 438 No documentation found for obj
438 439
439 440 In [5]: obj2 = NoDoc()
440 441
441 442 In [6]: %pdoc obj2
442 443 No documentation found for obj2
443 444 """
444 445
445 446 head = self.__head # For convenience
446 447 lines = []
447 448 ds = getdoc(obj)
448 449 if formatter:
449 450 ds = formatter(ds).get('plain/text', ds)
450 451 if ds:
451 452 lines.append(head("Class docstring:"))
452 453 lines.append(indent(ds))
453 454 if inspect.isclass(obj) and hasattr(obj, '__init__'):
454 455 init_ds = getdoc(obj.__init__)
455 456 if init_ds is not None:
456 457 lines.append(head("Init docstring:"))
457 458 lines.append(indent(init_ds))
458 459 elif hasattr(obj,'__call__'):
459 460 call_ds = getdoc(obj.__call__)
460 461 if call_ds:
461 462 lines.append(head("Call docstring:"))
462 463 lines.append(indent(call_ds))
463 464
464 465 if not lines:
465 466 self.noinfo('documentation',oname)
466 467 else:
467 468 page.page('\n'.join(lines))
468 469
469 470 def psource(self, obj, oname=''):
470 471 """Print the source code for an object."""
471 472
472 473 # Flush the source cache because inspect can return out-of-date source
473 474 linecache.checkcache()
474 475 try:
475 476 src = getsource(obj, oname=oname)
476 477 except Exception:
477 478 src = None
478 479
479 480 if src is None:
480 481 self.noinfo('source', oname)
481 482 else:
482 483 page.page(self.format(src))
483 484
484 485 def pfile(self, obj, oname=''):
485 486 """Show the whole file where an object was defined."""
486 487
487 488 lineno = find_source_lines(obj)
488 489 if lineno is None:
489 490 self.noinfo('file', oname)
490 491 return
491 492
492 493 ofile = find_file(obj)
493 494 # run contents of file through pager starting at line where the object
494 495 # is defined, as long as the file isn't binary and is actually on the
495 496 # filesystem.
496 497 if ofile.endswith(('.so', '.dll', '.pyd')):
497 498 print('File %r is binary, not printing.' % ofile)
498 499 elif not os.path.isfile(ofile):
499 500 print('File %r does not exist, not printing.' % ofile)
500 501 else:
501 502 # Print only text files, not extension binaries. Note that
502 503 # getsourcelines returns lineno with 1-offset and page() uses
503 504 # 0-offset, so we must adjust.
504 505 page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1)
505 506
506 507 def _format_fields(self, fields, title_width=0):
507 508 """Formats a list of fields for display.
508 509
509 510 Parameters
510 511 ----------
511 512 fields : list
512 513 A list of 2-tuples: (field_title, field_content)
513 514 title_width : int
514 515 How many characters to pad titles to. Default to longest title.
515 516 """
516 517 out = []
517 518 header = self.__head
518 519 if title_width == 0:
519 520 title_width = max(len(title) + 2 for title, _ in fields)
520 521 for title, content in fields:
521 522 if len(content.splitlines()) > 1:
522 523 title = header(title + ':') + '\n'
523 524 else:
524 525 title = header((title + ':').ljust(title_width))
525 526 out.append(cast_unicode(title) + cast_unicode(content))
526 527 return "\n".join(out)
527 528
528 529 def _mime_format(self, text, formatter=None):
529 530 """Return a mime bundle representation of the input text.
530 531
531 532 - if `formatter` is None, the returned mime bundle has
532 533 a `text/plain` field, with the input text.
533 534 a `text/html` field with a `<pre>` tag containing the input text.
534 535
535 536 - if `formatter` is not None, it must be a callable transforming the
536 537 input text into a mime bundle. Default values for `text/plain` and
537 538 `text/html` representations are the ones described above.
538 539
539 540 Note:
540 541
541 542 Formatters returning strings are supported but this behavior is deprecated.
542 543
543 544 """
544 545 text = cast_unicode(text)
545 546 defaults = {
546 547 'text/plain': text,
547 548 'text/html': '<pre>' + text + '</pre>'
548 549 }
549 550
550 551 if formatter is None:
551 552 return defaults
552 553 else:
553 554 formatted = formatter(text)
554 555
555 556 if not isinstance(formatted, dict):
556 557 # Handle the deprecated behavior of a formatter returning
557 558 # a string instead of a mime bundle.
558 559 return {
559 560 'text/plain': formatted,
560 561 'text/html': '<pre>' + formatted + '</pre>'
561 562 }
562 563
563 564 else:
564 565 return dict(defaults, **formatted)
565 566
566 567
567 568 def format_mime(self, bundle):
568 569
569 570 text_plain = bundle['text/plain']
570 571
571 572 text = ''
572 573 heads, bodies = list(zip(*text_plain))
573 574 _len = max(len(h) for h in heads)
574 575
575 576 for head, body in zip(heads, bodies):
576 577 body = body.strip('\n')
577 578 delim = '\n' if '\n' in body else ' '
578 579 text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n'
579 580
580 581 bundle['text/plain'] = text
581 582 return bundle
582 583
583 584 def _get_info(self, obj, oname='', formatter=None, info=None, detail_level=0):
584 585 """Retrieve an info dict and format it.
585 586
586 587 Parameters
587 588 ==========
588 589
589 590 obj: any
590 591 Object to inspect and return info from
591 592 oname: str (default: ''):
592 593 Name of the variable pointing to `obj`.
593 594 formatter: callable
594 595 info:
595 596 already computed information
596 597 detail_level: integer
597 598 Granularity of detail level, if set to 1, give more information.
598 599 """
599 600
600 601 info = self._info(obj, oname=oname, info=info, detail_level=detail_level)
601 602
602 603 _mime = {
603 604 'text/plain': [],
604 605 'text/html': '',
605 606 }
606 607
607 608 def append_field(bundle, title, key, formatter=None):
608 609 field = info[key]
609 610 if field is not None:
610 611 formatted_field = self._mime_format(field, formatter)
611 612 bundle['text/plain'].append((title, formatted_field['text/plain']))
612 613 bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html'] + '\n'
613 614
614 615 def code_formatter(text):
615 616 return {
616 617 'text/plain': self.format(text),
617 618 'text/html': pylight(text)
618 619 }
619 620
620 621 if info['isalias']:
621 622 append_field(_mime, 'Repr', 'string_form')
622 623
623 624 elif info['ismagic']:
624 625 if detail_level > 0:
625 626 append_field(_mime, 'Source', 'source', code_formatter)
626 627 else:
627 628 append_field(_mime, 'Docstring', 'docstring', formatter)
628 629 append_field(_mime, 'File', 'file')
629 630
630 631 elif info['isclass'] or is_simple_callable(obj):
631 632 # Functions, methods, classes
632 633 append_field(_mime, 'Signature', 'definition', code_formatter)
633 634 append_field(_mime, 'Init signature', 'init_definition', code_formatter)
634 635 append_field(_mime, 'Docstring', 'docstring', formatter)
635 636 if detail_level > 0 and info['source']:
636 637 append_field(_mime, 'Source', 'source', code_formatter)
637 638 else:
638 639 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
639 640
640 641 append_field(_mime, 'File', 'file')
641 642 append_field(_mime, 'Type', 'type_name')
642 643 append_field(_mime, 'Subclasses', 'subclasses')
643 644
644 645 else:
645 646 # General Python objects
646 647 append_field(_mime, 'Signature', 'definition', code_formatter)
647 648 append_field(_mime, 'Call signature', 'call_def', code_formatter)
648 649 append_field(_mime, 'Type', 'type_name')
649 650 append_field(_mime, 'String form', 'string_form')
650 651
651 652 # Namespace
652 653 if info['namespace'] != 'Interactive':
653 654 append_field(_mime, 'Namespace', 'namespace')
654 655
655 656 append_field(_mime, 'Length', 'length')
656 657 append_field(_mime, 'File', 'file')
657 658
658 659 # Source or docstring, depending on detail level and whether
659 660 # source found.
660 661 if detail_level > 0 and info['source']:
661 662 append_field(_mime, 'Source', 'source', code_formatter)
662 663 else:
663 664 append_field(_mime, 'Docstring', 'docstring', formatter)
664 665
665 666 append_field(_mime, 'Class docstring', 'class_docstring', formatter)
666 667 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
667 668 append_field(_mime, 'Call docstring', 'call_docstring', formatter)
668 669
669 670
670 671 return self.format_mime(_mime)
671 672
672 673 def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0, enable_html_pager=True):
673 674 """Show detailed information about an object.
674 675
675 676 Optional arguments:
676 677
677 678 - oname: name of the variable pointing to the object.
678 679
679 680 - formatter: callable (optional)
680 681 A special formatter for docstrings.
681 682
682 683 The formatter is a callable that takes a string as an input
683 684 and returns either a formatted string or a mime type bundle
684 685 in the form of a dictionary.
685 686
686 687 Although the support of custom formatter returning a string
687 688 instead of a mime type bundle is deprecated.
688 689
689 690 - info: a structure with some information fields which may have been
690 691 precomputed already.
691 692
692 693 - detail_level: if set to 1, more information is given.
693 694 """
694 695 info = self._get_info(obj, oname, formatter, info, detail_level)
695 696 if not enable_html_pager:
696 697 del info['text/html']
697 698 page.page(info)
698 699
699 700 def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
700 701 """DEPRECATED. Compute a dict with detailed information about an object.
701 702 """
702 703 if formatter is not None:
703 704 warnings.warn('The `formatter` keyword argument to `Inspector.info`'
704 705 'is deprecated as of IPython 5.0 and will have no effects.',
705 706 DeprecationWarning, stacklevel=2)
706 707 return self._info(obj, oname=oname, info=info, detail_level=detail_level)
707 708
708 709 def _info(self, obj, oname='', info=None, detail_level=0) -> dict:
709 710 """Compute a dict with detailed information about an object.
710 711
711 712 Parameters
712 713 ==========
713 714
714 715 obj: any
715 716 An object to find information about
716 717 oname: str (default: ''):
717 718 Name of the variable pointing to `obj`.
718 719 info: (default: None)
719 720 A struct (dict like with attr access) with some information fields
720 721 which may have been precomputed already.
721 722 detail_level: int (default:0)
722 723 If set to 1, more information is given.
723 724
724 725 Returns
725 726 =======
726 727
727 728 An object info dict with known fields from `info_fields`.
728 729 """
729 730
730 731 if info is None:
731 732 ismagic = False
732 733 isalias = False
733 734 ospace = ''
734 735 else:
735 736 ismagic = info.ismagic
736 737 isalias = info.isalias
737 738 ospace = info.namespace
738 739
739 740 # Get docstring, special-casing aliases:
740 741 if isalias:
741 742 if not callable(obj):
742 743 try:
743 744 ds = "Alias to the system command:\n %s" % obj[1]
744 745 except:
745 746 ds = "Alias: " + str(obj)
746 747 else:
747 748 ds = "Alias to " + str(obj)
748 749 if obj.__doc__:
749 750 ds += "\nDocstring:\n" + obj.__doc__
750 751 else:
751 752 ds = getdoc(obj)
752 753 if ds is None:
753 754 ds = '<no docstring>'
754 755
755 756 # store output in a dict, we initialize it here and fill it as we go
756 757 out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic, subclasses=None)
757 758
758 759 string_max = 200 # max size of strings to show (snipped if longer)
759 760 shalf = int((string_max - 5) / 2)
760 761
761 762 if ismagic:
762 763 out['type_name'] = 'Magic function'
763 764 elif isalias:
764 765 out['type_name'] = 'System alias'
765 766 else:
766 767 out['type_name'] = type(obj).__name__
767 768
768 769 try:
769 770 bclass = obj.__class__
770 771 out['base_class'] = str(bclass)
771 772 except:
772 773 pass
773 774
774 775 # String form, but snip if too long in ? form (full in ??)
775 776 if detail_level >= self.str_detail_level:
776 777 try:
777 778 ostr = str(obj)
778 779 str_head = 'string_form'
779 780 if not detail_level and len(ostr)>string_max:
780 781 ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
781 782 ostr = ("\n" + " " * len(str_head.expandtabs())).\
782 783 join(q.strip() for q in ostr.split("\n"))
783 784 out[str_head] = ostr
784 785 except:
785 786 pass
786 787
787 788 if ospace:
788 789 out['namespace'] = ospace
789 790
790 791 # Length (for strings and lists)
791 792 try:
792 793 out['length'] = str(len(obj))
793 794 except Exception:
794 795 pass
795 796
796 797 # Filename where object was defined
797 798 binary_file = False
798 799 fname = find_file(obj)
799 800 if fname is None:
800 801 # if anything goes wrong, we don't want to show source, so it's as
801 802 # if the file was binary
802 803 binary_file = True
803 804 else:
804 805 if fname.endswith(('.so', '.dll', '.pyd')):
805 806 binary_file = True
806 807 elif fname.endswith('<string>'):
807 808 fname = 'Dynamically generated function. No source code available.'
808 809 out['file'] = compress_user(fname)
809 810
810 811 # Original source code for a callable, class or property.
811 812 if detail_level:
812 813 # Flush the source cache because inspect can return out-of-date
813 814 # source
814 815 linecache.checkcache()
815 816 try:
816 817 if isinstance(obj, property) or not binary_file:
817 818 src = getsource(obj, oname)
818 819 if src is not None:
819 820 src = src.rstrip()
820 821 out['source'] = src
821 822
822 823 except Exception:
823 824 pass
824 825
825 826 # Add docstring only if no source is to be shown (avoid repetitions).
826 827 if ds and not self._source_contains_docstring(out.get('source'), ds):
827 828 out['docstring'] = ds
828 829
829 830 # Constructor docstring for classes
830 831 if inspect.isclass(obj):
831 832 out['isclass'] = True
832 833
833 834 # get the init signature:
834 835 try:
835 836 init_def = self._getdef(obj, oname)
836 837 except AttributeError:
837 838 init_def = None
838 839
839 840 # get the __init__ docstring
840 841 try:
841 842 obj_init = obj.__init__
842 843 except AttributeError:
843 844 init_ds = None
844 845 else:
845 846 if init_def is None:
846 847 # Get signature from init if top-level sig failed.
847 848 # Can happen for built-in types (list, etc.).
848 849 try:
849 850 init_def = self._getdef(obj_init, oname)
850 851 except AttributeError:
851 852 pass
852 853 init_ds = getdoc(obj_init)
853 854 # Skip Python's auto-generated docstrings
854 855 if init_ds == _object_init_docstring:
855 856 init_ds = None
856 857
857 858 if init_def:
858 859 out['init_definition'] = init_def
859 860
860 861 if init_ds:
861 862 out['init_docstring'] = init_ds
862 863
863 864 names = [sub.__name__ for sub in type.__subclasses__(obj)]
864 865 if len(names) < 10:
865 866 all_names = ', '.join(names)
866 867 else:
867 868 all_names = ', '.join(names[:10]+['...'])
868 869 out['subclasses'] = all_names
869 870 # and class docstring for instances:
870 871 else:
871 872 # reconstruct the function definition and print it:
872 873 defln = self._getdef(obj, oname)
873 874 if defln:
874 875 out['definition'] = defln
875 876
876 877 # First, check whether the instance docstring is identical to the
877 878 # class one, and print it separately if they don't coincide. In
878 879 # most cases they will, but it's nice to print all the info for
879 880 # objects which use instance-customized docstrings.
880 881 if ds:
881 882 try:
882 883 cls = getattr(obj,'__class__')
883 884 except:
884 885 class_ds = None
885 886 else:
886 887 class_ds = getdoc(cls)
887 888 # Skip Python's auto-generated docstrings
888 889 if class_ds in _builtin_type_docstrings:
889 890 class_ds = None
890 891 if class_ds and ds != class_ds:
891 892 out['class_docstring'] = class_ds
892 893
893 894 # Next, try to show constructor docstrings
894 895 try:
895 896 init_ds = getdoc(obj.__init__)
896 897 # Skip Python's auto-generated docstrings
897 898 if init_ds == _object_init_docstring:
898 899 init_ds = None
899 900 except AttributeError:
900 901 init_ds = None
901 902 if init_ds:
902 903 out['init_docstring'] = init_ds
903 904
904 905 # Call form docstring for callable instances
905 906 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
906 907 call_def = self._getdef(obj.__call__, oname)
907 908 if call_def and (call_def != out.get('definition')):
908 909 # it may never be the case that call def and definition differ,
909 910 # but don't include the same signature twice
910 911 out['call_def'] = call_def
911 912 call_ds = getdoc(obj.__call__)
912 913 # Skip Python's auto-generated docstrings
913 914 if call_ds == _func_call_docstring:
914 915 call_ds = None
915 916 if call_ds:
916 917 out['call_docstring'] = call_ds
917 918
918 919 # Compute the object's argspec as a callable. The key is to decide
919 920 # whether to pull it from the object itself, from its __init__ or
920 921 # from its __call__ method.
921 922
922 923 if inspect.isclass(obj):
923 924 # Old-style classes need not have an __init__
924 925 callable_obj = getattr(obj, "__init__", None)
925 926 elif callable(obj):
926 927 callable_obj = obj
927 928 else:
928 929 callable_obj = None
929 930
930 931 if callable_obj is not None:
931 932 try:
932 933 argspec = getargspec(callable_obj)
933 934 except Exception:
934 935 # For extensions/builtins we can't retrieve the argspec
935 936 pass
936 937 else:
937 938 # named tuples' _asdict() method returns an OrderedDict, but we
938 939 # we want a normal
939 940 out['argspec'] = argspec_dict = dict(argspec._asdict())
940 941 # We called this varkw before argspec became a named tuple.
941 942 # With getfullargspec it's also called varkw.
942 943 if 'varkw' not in argspec_dict:
943 944 argspec_dict['varkw'] = argspec_dict.pop('keywords')
944 945
945 946 return object_info(**out)
946 947
947 948 @staticmethod
948 949 def _source_contains_docstring(src, doc):
949 950 """
950 951 Check whether the source *src* contains the docstring *doc*.
951 952
952 953 This is is helper function to skip displaying the docstring if the
953 954 source already contains it, avoiding repetition of information.
954 955 """
955 956 try:
956 957 def_node, = ast.parse(dedent(src)).body
957 958 return ast.get_docstring(def_node) == doc
958 959 except Exception:
959 960 # The source can become invalid or even non-existent (because it
960 961 # is re-fetched from the source file) so the above code fail in
961 962 # arbitrary ways.
962 963 return False
963 964
964 965 def psearch(self,pattern,ns_table,ns_search=[],
965 ignore_case=False,show_all=False):
966 ignore_case=False,show_all=False, list_types=False):
966 967 """Search namespaces with wildcards for objects.
967 968
968 969 Arguments:
969 970
970 971 - pattern: string containing shell-like wildcards to use in namespace
971 972 searches and optionally a type specification to narrow the search to
972 973 objects of that type.
973 974
974 975 - ns_table: dict of name->namespaces for search.
975 976
976 977 Optional arguments:
977 978
978 979 - ns_search: list of namespace names to include in search.
979 980
980 981 - ignore_case(False): make the search case-insensitive.
981 982
982 983 - show_all(False): show all names, including those starting with
983 984 underscores.
985
986 - list_types(False): list all available object types for object matching.
984 987 """
985 988 #print 'ps pattern:<%r>' % pattern # dbg
986 989
987 990 # defaults
988 991 type_pattern = 'all'
989 992 filter = ''
990 993
994 # list all object types
995 if list_types:
996 page.page('\n'.join(sorted(typestr2type)))
997 return
998
991 999 cmds = pattern.split()
992 1000 len_cmds = len(cmds)
993 1001 if len_cmds == 1:
994 1002 # Only filter pattern given
995 1003 filter = cmds[0]
996 1004 elif len_cmds == 2:
997 1005 # Both filter and type specified
998 1006 filter,type_pattern = cmds
999 1007 else:
1000 1008 raise ValueError('invalid argument string for psearch: <%s>' %
1001 1009 pattern)
1002 1010
1003 1011 # filter search namespaces
1004 1012 for name in ns_search:
1005 1013 if name not in ns_table:
1006 1014 raise ValueError('invalid namespace <%s>. Valid names: %s' %
1007 1015 (name,ns_table.keys()))
1008 1016
1009 1017 #print 'type_pattern:',type_pattern # dbg
1010 1018 search_result, namespaces_seen = set(), set()
1011 1019 for ns_name in ns_search:
1012 1020 ns = ns_table[ns_name]
1013 1021 # Normally, locals and globals are the same, so we just check one.
1014 1022 if id(ns) in namespaces_seen:
1015 1023 continue
1016 1024 namespaces_seen.add(id(ns))
1017 1025 tmp_res = list_namespace(ns, type_pattern, filter,
1018 1026 ignore_case=ignore_case, show_all=show_all)
1019 1027 search_result.update(tmp_res)
1020 1028
1021 1029 page.page('\n'.join(sorted(search_result)))
1022 1030
1023 1031
1024 1032 def _render_signature(obj_signature, obj_name):
1025 1033 """
1026 1034 This was mostly taken from inspect.Signature.__str__.
1027 1035 Look there for the comments.
1028 1036 The only change is to add linebreaks when this gets too long.
1029 1037 """
1030 1038 result = []
1031 1039 pos_only = False
1032 1040 kw_only = True
1033 1041 for param in obj_signature.parameters.values():
1034 1042 if param.kind == inspect._POSITIONAL_ONLY:
1035 1043 pos_only = True
1036 1044 elif pos_only:
1037 1045 result.append('/')
1038 1046 pos_only = False
1039 1047
1040 1048 if param.kind == inspect._VAR_POSITIONAL:
1041 1049 kw_only = False
1042 1050 elif param.kind == inspect._KEYWORD_ONLY and kw_only:
1043 1051 result.append('*')
1044 1052 kw_only = False
1045 1053
1046 1054 result.append(str(param))
1047 1055
1048 1056 if pos_only:
1049 1057 result.append('/')
1050 1058
1051 1059 # add up name, parameters, braces (2), and commas
1052 1060 if len(obj_name) + sum(len(r) + 2 for r in result) > 75:
1053 1061 # This doesn’t fit behind “Signature: ” in an inspect window.
1054 1062 rendered = '{}(\n{})'.format(obj_name, ''.join(
1055 1063 ' {},\n'.format(r) for r in result)
1056 1064 )
1057 1065 else:
1058 1066 rendered = '{}({})'.format(obj_name, ', '.join(result))
1059 1067
1060 1068 if obj_signature.return_annotation is not inspect._empty:
1061 1069 anno = inspect.formatannotation(obj_signature.return_annotation)
1062 1070 rendered += ' -> {}'.format(anno)
1063 1071
1064 1072 return rendered
General Comments 0
You need to be logged in to leave comments. Login now