##// END OF EJS Templates
Merge pull request #1845 from fperez/magic_inspect...
Fernando Perez -
r7433:44b81218 merge
parent child Browse files
Show More
@@ -1,700 +1,700 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
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.path import get_py_filename
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Magic implementation classes
30 30 #-----------------------------------------------------------------------------
31 31
32 32 @magics_class
33 33 class NamespaceMagics(Magics):
34 34 """Magics to manage various aspects of the user's namespace.
35 35
36 36 These include listing variables, introspecting into them, etc.
37 37 """
38 38
39 39 @line_magic
40 40 def pinfo(self, parameter_s='', namespaces=None):
41 41 """Provide detailed information about an object.
42 42
43 43 '%pinfo object' is just a synonym for object? or ?object."""
44 44
45 45 #print 'pinfo par: <%s>' % parameter_s # dbg
46 46 # detail_level: 0 -> obj? , 1 -> obj??
47 47 detail_level = 0
48 48 # We need to detect if we got called as 'pinfo pinfo foo', which can
49 49 # happen if the user types 'pinfo foo?' at the cmd line.
50 50 pinfo,qmark1,oname,qmark2 = \
51 51 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
52 52 if pinfo or qmark1 or qmark2:
53 53 detail_level = 1
54 54 if "*" in oname:
55 55 self.psearch(oname)
56 56 else:
57 57 self.shell._inspect('pinfo', oname, detail_level=detail_level,
58 58 namespaces=namespaces)
59 59
60 60 @line_magic
61 61 def pinfo2(self, parameter_s='', namespaces=None):
62 62 """Provide extra detailed information about an object.
63 63
64 64 '%pinfo2 object' is just a synonym for object?? or ??object."""
65 65 self.shell._inspect('pinfo', parameter_s, detail_level=1,
66 66 namespaces=namespaces)
67 67
68 68 @skip_doctest
69 69 @line_magic
70 70 def pdef(self, parameter_s='', namespaces=None):
71 71 """Print the definition header for any callable object.
72 72
73 73 If the object is a class, print the constructor information.
74 74
75 75 Examples
76 76 --------
77 77 ::
78 78
79 79 In [3]: %pdef urllib.urlopen
80 80 urllib.urlopen(url, data=None, proxies=None)
81 81 """
82 self._inspect('pdef',parameter_s, namespaces)
82 self.shell._inspect('pdef',parameter_s, namespaces)
83 83
84 84 @line_magic
85 85 def pdoc(self, parameter_s='', namespaces=None):
86 86 """Print the docstring for an object.
87 87
88 88 If the given object is a class, it will print both the class and the
89 89 constructor docstrings."""
90 self._inspect('pdoc',parameter_s, namespaces)
90 self.shell._inspect('pdoc',parameter_s, namespaces)
91 91
92 92 @line_magic
93 93 def psource(self, parameter_s='', namespaces=None):
94 94 """Print (or run through pager) the source code for an object."""
95 self._inspect('psource',parameter_s, namespaces)
95 self.shell._inspect('psource',parameter_s, namespaces)
96 96
97 97 @line_magic
98 98 def pfile(self, parameter_s=''):
99 99 """Print (or run through pager) the file where an object is defined.
100 100
101 101 The file opens at the line where the object definition begins. IPython
102 102 will honor the environment variable PAGER if set, and otherwise will
103 103 do its best to print the file in a convenient form.
104 104
105 105 If the given argument is not an object currently defined, IPython will
106 106 try to interpret it as a filename (automatically adding a .py extension
107 107 if needed). You can thus use %pfile as a syntax highlighting code
108 108 viewer."""
109 109
110 110 # first interpret argument as an object name
111 out = self._inspect('pfile',parameter_s)
111 out = self.shell._inspect('pfile',parameter_s)
112 112 # if not, try the input as a filename
113 113 if out == 'not found':
114 114 try:
115 115 filename = get_py_filename(parameter_s)
116 116 except IOError,msg:
117 117 print msg
118 118 return
119 119 page.page(self.shell.inspector.format(open(filename).read()))
120 120
121 121 @line_magic
122 122 def psearch(self, parameter_s=''):
123 123 """Search for object in namespaces by wildcard.
124 124
125 125 %psearch [options] PATTERN [OBJECT TYPE]
126 126
127 127 Note: ? can be used as a synonym for %psearch, at the beginning or at
128 128 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
129 129 rest of the command line must be unchanged (options come first), so
130 130 for example the following forms are equivalent
131 131
132 132 %psearch -i a* function
133 133 -i a* function?
134 134 ?-i a* function
135 135
136 136 Arguments:
137 137
138 138 PATTERN
139 139
140 140 where PATTERN is a string containing * as a wildcard similar to its
141 141 use in a shell. The pattern is matched in all namespaces on the
142 142 search path. By default objects starting with a single _ are not
143 143 matched, many IPython generated objects have a single
144 144 underscore. The default is case insensitive matching. Matching is
145 145 also done on the attributes of objects and not only on the objects
146 146 in a module.
147 147
148 148 [OBJECT TYPE]
149 149
150 150 Is the name of a python type from the types module. The name is
151 151 given in lowercase without the ending type, ex. StringType is
152 152 written string. By adding a type here only objects matching the
153 153 given type are matched. Using all here makes the pattern match all
154 154 types (this is the default).
155 155
156 156 Options:
157 157
158 158 -a: makes the pattern match even objects whose names start with a
159 159 single underscore. These names are normally omitted from the
160 160 search.
161 161
162 162 -i/-c: make the pattern case insensitive/sensitive. If neither of
163 163 these options are given, the default is read from your configuration
164 164 file, with the option ``InteractiveShell.wildcards_case_sensitive``.
165 165 If this option is not specified in your configuration file, IPython's
166 166 internal default is to do a case sensitive search.
167 167
168 168 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
169 169 specify can be searched in any of the following namespaces:
170 170 'builtin', 'user', 'user_global','internal', 'alias', where
171 171 'builtin' and 'user' are the search defaults. Note that you should
172 172 not use quotes when specifying namespaces.
173 173
174 174 'Builtin' contains the python module builtin, 'user' contains all
175 175 user data, 'alias' only contain the shell aliases and no python
176 176 objects, 'internal' contains objects used by IPython. The
177 177 'user_global' namespace is only used by embedded IPython instances,
178 178 and it contains module-level globals. You can add namespaces to the
179 179 search with -s or exclude them with -e (these options can be given
180 180 more than once).
181 181
182 182 Examples
183 183 --------
184 184 ::
185 185
186 186 %psearch a* -> objects beginning with an a
187 187 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
188 188 %psearch a* function -> all functions beginning with an a
189 189 %psearch re.e* -> objects beginning with an e in module re
190 190 %psearch r*.e* -> objects that start with e in modules starting in r
191 191 %psearch r*.* string -> all strings in modules beginning with r
192 192
193 193 Case sensitive search::
194 194
195 195 %psearch -c a* list all object beginning with lower case a
196 196
197 197 Show objects beginning with a single _::
198 198
199 199 %psearch -a _* list objects beginning with a single underscore
200 200 """
201 201 try:
202 202 parameter_s.encode('ascii')
203 203 except UnicodeEncodeError:
204 204 print 'Python identifiers can only contain ascii characters.'
205 205 return
206 206
207 207 # default namespaces to be searched
208 208 def_search = ['user_local', 'user_global', 'builtin']
209 209
210 210 # Process options/args
211 211 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
212 212 opt = opts.get
213 213 shell = self.shell
214 214 psearch = shell.inspector.psearch
215 215
216 216 # select case options
217 217 if opts.has_key('i'):
218 218 ignore_case = True
219 219 elif opts.has_key('c'):
220 220 ignore_case = False
221 221 else:
222 222 ignore_case = not shell.wildcards_case_sensitive
223 223
224 224 # Build list of namespaces to search from user options
225 225 def_search.extend(opt('s',[]))
226 226 ns_exclude = ns_exclude=opt('e',[])
227 227 ns_search = [nm for nm in def_search if nm not in ns_exclude]
228 228
229 229 # Call the actual search
230 230 try:
231 231 psearch(args,shell.ns_table,ns_search,
232 232 show_all=opt('a'),ignore_case=ignore_case)
233 233 except:
234 234 shell.showtraceback()
235 235
236 236 @skip_doctest
237 237 @line_magic
238 238 def who_ls(self, parameter_s=''):
239 239 """Return a sorted list of all interactive variables.
240 240
241 241 If arguments are given, only variables of types matching these
242 242 arguments are returned.
243 243
244 244 Examples
245 245 --------
246 246
247 247 Define two variables and list them with who_ls::
248 248
249 249 In [1]: alpha = 123
250 250
251 251 In [2]: beta = 'test'
252 252
253 253 In [3]: %who_ls
254 254 Out[3]: ['alpha', 'beta']
255 255
256 256 In [4]: %who_ls int
257 257 Out[4]: ['alpha']
258 258
259 259 In [5]: %who_ls str
260 260 Out[5]: ['beta']
261 261 """
262 262
263 263 user_ns = self.shell.user_ns
264 264 user_ns_hidden = self.shell.user_ns_hidden
265 265 out = [ i for i in user_ns
266 266 if not i.startswith('_') \
267 267 and not i in user_ns_hidden ]
268 268
269 269 typelist = parameter_s.split()
270 270 if typelist:
271 271 typeset = set(typelist)
272 272 out = [i for i in out if type(user_ns[i]).__name__ in typeset]
273 273
274 274 out.sort()
275 275 return out
276 276
277 277 @skip_doctest
278 278 @line_magic
279 279 def who(self, parameter_s=''):
280 280 """Print all interactive variables, with some minimal formatting.
281 281
282 282 If any arguments are given, only variables whose type matches one of
283 283 these are printed. For example::
284 284
285 285 %who function str
286 286
287 287 will only list functions and strings, excluding all other types of
288 288 variables. To find the proper type names, simply use type(var) at a
289 289 command line to see how python prints type names. For example:
290 290
291 291 ::
292 292
293 293 In [1]: type('hello')\\
294 294 Out[1]: <type 'str'>
295 295
296 296 indicates that the type name for strings is 'str'.
297 297
298 298 ``%who`` always excludes executed names loaded through your configuration
299 299 file and things which are internal to IPython.
300 300
301 301 This is deliberate, as typically you may load many modules and the
302 302 purpose of %who is to show you only what you've manually defined.
303 303
304 304 Examples
305 305 --------
306 306
307 307 Define two variables and list them with who::
308 308
309 309 In [1]: alpha = 123
310 310
311 311 In [2]: beta = 'test'
312 312
313 313 In [3]: %who
314 314 alpha beta
315 315
316 316 In [4]: %who int
317 317 alpha
318 318
319 319 In [5]: %who str
320 320 beta
321 321 """
322 322
323 323 varlist = self.who_ls(parameter_s)
324 324 if not varlist:
325 325 if parameter_s:
326 326 print 'No variables match your requested type.'
327 327 else:
328 328 print 'Interactive namespace is empty.'
329 329 return
330 330
331 331 # if we have variables, move on...
332 332 count = 0
333 333 for i in varlist:
334 334 print i+'\t',
335 335 count += 1
336 336 if count > 8:
337 337 count = 0
338 338 print
339 339 print
340 340
341 341 @skip_doctest
342 342 @line_magic
343 343 def whos(self, parameter_s=''):
344 344 """Like %who, but gives some extra information about each variable.
345 345
346 346 The same type filtering of %who can be applied here.
347 347
348 348 For all variables, the type is printed. Additionally it prints:
349 349
350 350 - For {},[],(): their length.
351 351
352 352 - For numpy arrays, a summary with shape, number of
353 353 elements, typecode and size in memory.
354 354
355 355 - Everything else: a string representation, snipping their middle if
356 356 too long.
357 357
358 358 Examples
359 359 --------
360 360
361 361 Define two variables and list them with whos::
362 362
363 363 In [1]: alpha = 123
364 364
365 365 In [2]: beta = 'test'
366 366
367 367 In [3]: %whos
368 368 Variable Type Data/Info
369 369 --------------------------------
370 370 alpha int 123
371 371 beta str test
372 372 """
373 373
374 374 varnames = self.who_ls(parameter_s)
375 375 if not varnames:
376 376 if parameter_s:
377 377 print 'No variables match your requested type.'
378 378 else:
379 379 print 'Interactive namespace is empty.'
380 380 return
381 381
382 382 # if we have variables, move on...
383 383
384 384 # for these types, show len() instead of data:
385 385 seq_types = ['dict', 'list', 'tuple']
386 386
387 387 # for numpy arrays, display summary info
388 388 ndarray_type = None
389 389 if 'numpy' in sys.modules:
390 390 try:
391 391 from numpy import ndarray
392 392 except ImportError:
393 393 pass
394 394 else:
395 395 ndarray_type = ndarray.__name__
396 396
397 397 # Find all variable names and types so we can figure out column sizes
398 398 def get_vars(i):
399 399 return self.shell.user_ns[i]
400 400
401 401 # some types are well known and can be shorter
402 402 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
403 403 def type_name(v):
404 404 tn = type(v).__name__
405 405 return abbrevs.get(tn,tn)
406 406
407 407 varlist = map(get_vars,varnames)
408 408
409 409 typelist = []
410 410 for vv in varlist:
411 411 tt = type_name(vv)
412 412
413 413 if tt=='instance':
414 414 typelist.append( abbrevs.get(str(vv.__class__),
415 415 str(vv.__class__)))
416 416 else:
417 417 typelist.append(tt)
418 418
419 419 # column labels and # of spaces as separator
420 420 varlabel = 'Variable'
421 421 typelabel = 'Type'
422 422 datalabel = 'Data/Info'
423 423 colsep = 3
424 424 # variable format strings
425 425 vformat = "{0:<{varwidth}}{1:<{typewidth}}"
426 426 aformat = "%s: %s elems, type `%s`, %s bytes"
427 427 # find the size of the columns to format the output nicely
428 428 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
429 429 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
430 430 # table header
431 431 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
432 432 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
433 433 # and the table itself
434 434 kb = 1024
435 435 Mb = 1048576 # kb**2
436 436 for vname,var,vtype in zip(varnames,varlist,typelist):
437 437 print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth),
438 438 if vtype in seq_types:
439 439 print "n="+str(len(var))
440 440 elif vtype == ndarray_type:
441 441 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
442 442 if vtype==ndarray_type:
443 443 # numpy
444 444 vsize = var.size
445 445 vbytes = vsize*var.itemsize
446 446 vdtype = var.dtype
447 447
448 448 if vbytes < 100000:
449 449 print aformat % (vshape, vsize, vdtype, vbytes)
450 450 else:
451 451 print aformat % (vshape, vsize, vdtype, vbytes),
452 452 if vbytes < Mb:
453 453 print '(%s kb)' % (vbytes/kb,)
454 454 else:
455 455 print '(%s Mb)' % (vbytes/Mb,)
456 456 else:
457 457 try:
458 458 vstr = str(var)
459 459 except UnicodeEncodeError:
460 460 vstr = unicode(var).encode(DEFAULT_ENCODING,
461 461 'backslashreplace')
462 462 except:
463 463 vstr = "<object with id %d (str() failed)>" % id(var)
464 464 vstr = vstr.replace('\n', '\\n')
465 465 if len(vstr) < 50:
466 466 print vstr
467 467 else:
468 468 print vstr[:25] + "<...>" + vstr[-25:]
469 469
470 470 @line_magic
471 471 def reset(self, parameter_s=''):
472 472 """Resets the namespace by removing all names defined by the user, if
473 473 called without arguments, or by removing some types of objects, such
474 474 as everything currently in IPython's In[] and Out[] containers (see
475 475 the parameters for details).
476 476
477 477 Parameters
478 478 ----------
479 479 -f : force reset without asking for confirmation.
480 480
481 481 -s : 'Soft' reset: Only clears your namespace, leaving history intact.
482 482 References to objects may be kept. By default (without this option),
483 483 we do a 'hard' reset, giving you a new session and removing all
484 484 references to objects from the current session.
485 485
486 486 in : reset input history
487 487
488 488 out : reset output history
489 489
490 490 dhist : reset directory history
491 491
492 492 array : reset only variables that are NumPy arrays
493 493
494 494 See Also
495 495 --------
496 496 magic_reset_selective : invoked as ``%reset_selective``
497 497
498 498 Examples
499 499 --------
500 500 ::
501 501
502 502 In [6]: a = 1
503 503
504 504 In [7]: a
505 505 Out[7]: 1
506 506
507 507 In [8]: 'a' in _ip.user_ns
508 508 Out[8]: True
509 509
510 510 In [9]: %reset -f
511 511
512 512 In [1]: 'a' in _ip.user_ns
513 513 Out[1]: False
514 514
515 515 In [2]: %reset -f in
516 516 Flushing input history
517 517
518 518 In [3]: %reset -f dhist in
519 519 Flushing directory history
520 520 Flushing input history
521 521
522 522 Notes
523 523 -----
524 524 Calling this magic from clients that do not implement standard input,
525 525 such as the ipython notebook interface, will reset the namespace
526 526 without confirmation.
527 527 """
528 528 opts, args = self.parse_options(parameter_s,'sf', mode='list')
529 529 if 'f' in opts:
530 530 ans = True
531 531 else:
532 532 try:
533 533 ans = self.shell.ask_yes_no(
534 534 "Once deleted, variables cannot be recovered. Proceed (y/[n])?",
535 535 default='n')
536 536 except StdinNotImplementedError:
537 537 ans = True
538 538 if not ans:
539 539 print 'Nothing done.'
540 540 return
541 541
542 542 if 's' in opts: # Soft reset
543 543 user_ns = self.shell.user_ns
544 544 for i in self.who_ls():
545 545 del(user_ns[i])
546 546 elif len(args) == 0: # Hard reset
547 547 self.shell.reset(new_session = False)
548 548
549 549 # reset in/out/dhist/array: previously extensinions/clearcmd.py
550 550 ip = self.shell
551 551 user_ns = self.shell.user_ns # local lookup, heavily used
552 552
553 553 for target in args:
554 554 target = target.lower() # make matches case insensitive
555 555 if target == 'out':
556 556 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
557 557 self.shell.displayhook.flush()
558 558
559 559 elif target == 'in':
560 560 print "Flushing input history"
561 561 pc = self.shell.displayhook.prompt_count + 1
562 562 for n in range(1, pc):
563 563 key = '_i'+repr(n)
564 564 user_ns.pop(key,None)
565 565 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
566 566 hm = ip.history_manager
567 567 # don't delete these, as %save and %macro depending on the
568 568 # length of these lists to be preserved
569 569 hm.input_hist_parsed[:] = [''] * pc
570 570 hm.input_hist_raw[:] = [''] * pc
571 571 # hm has internal machinery for _i,_ii,_iii, clear it out
572 572 hm._i = hm._ii = hm._iii = hm._i00 = u''
573 573
574 574 elif target == 'array':
575 575 # Support cleaning up numpy arrays
576 576 try:
577 577 from numpy import ndarray
578 578 # This must be done with items and not iteritems because
579 579 # we're going to modify the dict in-place.
580 580 for x,val in user_ns.items():
581 581 if isinstance(val,ndarray):
582 582 del user_ns[x]
583 583 except ImportError:
584 584 print "reset array only works if Numpy is available."
585 585
586 586 elif target == 'dhist':
587 587 print "Flushing directory history"
588 588 del user_ns['_dh'][:]
589 589
590 590 else:
591 591 print "Don't know how to reset ",
592 592 print target + ", please run `%reset?` for details"
593 593
594 594 gc.collect()
595 595
596 596 @line_magic
597 597 def reset_selective(self, parameter_s=''):
598 598 """Resets the namespace by removing names defined by the user.
599 599
600 600 Input/Output history are left around in case you need them.
601 601
602 602 %reset_selective [-f] regex
603 603
604 604 No action is taken if regex is not included
605 605
606 606 Options
607 607 -f : force reset without asking for confirmation.
608 608
609 609 See Also
610 610 --------
611 611 magic_reset : invoked as ``%reset``
612 612
613 613 Examples
614 614 --------
615 615
616 616 We first fully reset the namespace so your output looks identical to
617 617 this example for pedagogical reasons; in practice you do not need a
618 618 full reset::
619 619
620 620 In [1]: %reset -f
621 621
622 622 Now, with a clean namespace we can make a few variables and use
623 623 ``%reset_selective`` to only delete names that match our regexp::
624 624
625 625 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
626 626
627 627 In [3]: who_ls
628 628 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
629 629
630 630 In [4]: %reset_selective -f b[2-3]m
631 631
632 632 In [5]: who_ls
633 633 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
634 634
635 635 In [6]: %reset_selective -f d
636 636
637 637 In [7]: who_ls
638 638 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
639 639
640 640 In [8]: %reset_selective -f c
641 641
642 642 In [9]: who_ls
643 643 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
644 644
645 645 In [10]: %reset_selective -f b
646 646
647 647 In [11]: who_ls
648 648 Out[11]: ['a']
649 649
650 650 Notes
651 651 -----
652 652 Calling this magic from clients that do not implement standard input,
653 653 such as the ipython notebook interface, will reset the namespace
654 654 without confirmation.
655 655 """
656 656
657 657 opts, regex = self.parse_options(parameter_s,'f')
658 658
659 659 if opts.has_key('f'):
660 660 ans = True
661 661 else:
662 662 try:
663 663 ans = self.shell.ask_yes_no(
664 664 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ",
665 665 default='n')
666 666 except StdinNotImplementedError:
667 667 ans = True
668 668 if not ans:
669 669 print 'Nothing done.'
670 670 return
671 671 user_ns = self.shell.user_ns
672 672 if not regex:
673 673 print 'No regex pattern specified. Nothing done.'
674 674 return
675 675 else:
676 676 try:
677 677 m = re.compile(regex)
678 678 except TypeError:
679 679 raise TypeError('regex must be a string or compiled pattern')
680 680 for i in self.who_ls():
681 681 if m.search(i):
682 682 del(user_ns[i])
683 683
684 684 @line_magic
685 685 def xdel(self, parameter_s=''):
686 686 """Delete a variable, trying to clear it from anywhere that
687 687 IPython's machinery has references to it. By default, this uses
688 688 the identity of the named object in the user namespace to remove
689 689 references held under other names. The object is also removed
690 690 from the output history.
691 691
692 692 Options
693 693 -n : Delete the specified name from all namespaces, without
694 694 checking their identity.
695 695 """
696 696 opts, varname = self.parse_options(parameter_s,'n')
697 697 try:
698 698 self.shell.del_var(varname, ('n' in opts))
699 699 except (NameError, ValueError) as e:
700 700 print type(e).__name__ +": "+ str(e)
@@ -1,772 +1,825 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 #*****************************************************************************
11 11 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
12 12 #
13 13 # Distributed under the terms of the BSD License. The full license is in
14 14 # the file COPYING, distributed as part of this software.
15 15 #*****************************************************************************
16 16
17 17 __all__ = ['Inspector','InspectColors']
18 18
19 19 # stdlib modules
20 20 import __builtin__
21 21 import inspect
22 22 import linecache
23 23 import os
24 24 import sys
25 25 import types
26 26 from collections import namedtuple
27 27 try:
28 28 from itertools import izip_longest
29 29 except ImportError:
30 30 from itertools import zip_longest as izip_longest
31 31
32 32 # IPython's own
33 33 from IPython.core import page
34 34 from IPython.testing.skipdoctest import skip_doctest_py3
35 35 from IPython.utils import PyColorize
36 36 from IPython.utils import io
37 37 from IPython.utils import py3compat
38 38 from IPython.utils.text import indent
39 39 from IPython.utils.wildcard import list_namespace
40 40 from IPython.utils.coloransi import *
41 41
42 42 #****************************************************************************
43 43 # Builtin color schemes
44 44
45 45 Colors = TermColors # just a shorthand
46 46
47 47 # Build a few color schemes
48 48 NoColor = ColorScheme(
49 49 'NoColor',{
50 50 'header' : Colors.NoColor,
51 51 'normal' : Colors.NoColor # color off (usu. Colors.Normal)
52 52 } )
53 53
54 54 LinuxColors = ColorScheme(
55 55 'Linux',{
56 56 'header' : Colors.LightRed,
57 57 'normal' : Colors.Normal # color off (usu. Colors.Normal)
58 58 } )
59 59
60 60 LightBGColors = ColorScheme(
61 61 'LightBG',{
62 62 'header' : Colors.Red,
63 63 'normal' : Colors.Normal # color off (usu. Colors.Normal)
64 64 } )
65 65
66 66 # Build table of color schemes (needed by the parser)
67 67 InspectColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors],
68 68 'Linux')
69 69
70 70 #****************************************************************************
71 71 # Auxiliary functions and objects
72 72
73 73 # See the messaging spec for the definition of all these fields. This list
74 74 # effectively defines the order of display
75 75 info_fields = ['type_name', 'base_class', 'string_form', 'namespace',
76 76 'length', 'file', 'definition', 'docstring', 'source',
77 77 'init_definition', 'class_docstring', 'init_docstring',
78 78 'call_def', 'call_docstring',
79 79 # These won't be printed but will be used to determine how to
80 80 # format the object
81 81 'ismagic', 'isalias', 'isclass', 'argspec', 'found', 'name'
82 82 ]
83 83
84 84
85 85 def object_info(**kw):
86 86 """Make an object info dict with all fields present."""
87 87 infodict = dict(izip_longest(info_fields, [None]))
88 88 infodict.update(kw)
89 89 return infodict
90 90
91 91
92 92 def getdoc(obj):
93 93 """Stable wrapper around inspect.getdoc.
94 94
95 95 This can't crash because of attribute problems.
96 96
97 97 It also attempts to call a getdoc() method on the given object. This
98 98 allows objects which provide their docstrings via non-standard mechanisms
99 99 (like Pyro proxies) to still be inspected by ipython's ? system."""
100 100 # Allow objects to offer customized documentation via a getdoc method:
101 101 try:
102 102 ds = obj.getdoc()
103 103 except Exception:
104 104 pass
105 105 else:
106 106 # if we get extra info, we add it to the normal docstring.
107 107 if isinstance(ds, basestring):
108 108 return inspect.cleandoc(ds)
109 109
110 110 try:
111 111 return inspect.getdoc(obj)
112 112 except Exception:
113 113 # Harden against an inspect failure, which can occur with
114 114 # SWIG-wrapped extensions.
115 115 return None
116 116
117 117
118 118 def getsource(obj,is_binary=False):
119 119 """Wrapper around inspect.getsource.
120 120
121 121 This can be modified by other projects to provide customized source
122 122 extraction.
123 123
124 124 Inputs:
125 125
126 126 - obj: an object whose source code we will attempt to extract.
127 127
128 128 Optional inputs:
129 129
130 130 - is_binary: whether the object is known to come from a binary source.
131 131 This implementation will skip returning any output for binary objects, but
132 132 custom extractors may know how to meaningfully process them."""
133 133
134 134 if is_binary:
135 135 return None
136 136 else:
137 137 # get source if obj was decorated with @decorator
138 138 if hasattr(obj,"__wrapped__"):
139 139 obj = obj.__wrapped__
140 140 try:
141 141 src = inspect.getsource(obj)
142 142 except TypeError:
143 143 if hasattr(obj,'__class__'):
144 144 src = inspect.getsource(obj.__class__)
145 145 return src
146 146
147 147 def getargspec(obj):
148 148 """Get the names and default values of a function's arguments.
149 149
150 150 A tuple of four things is returned: (args, varargs, varkw, defaults).
151 151 'args' is a list of the argument names (it may contain nested lists).
152 152 'varargs' and 'varkw' are the names of the * and ** arguments or None.
153 153 'defaults' is an n-tuple of the default values of the last n arguments.
154 154
155 155 Modified version of inspect.getargspec from the Python Standard
156 156 Library."""
157 157
158 158 if inspect.isfunction(obj):
159 159 func_obj = obj
160 160 elif inspect.ismethod(obj):
161 161 func_obj = obj.im_func
162 162 elif hasattr(obj, '__call__'):
163 163 func_obj = obj.__call__
164 164 else:
165 165 raise TypeError('arg is not a Python function')
166 166 args, varargs, varkw = inspect.getargs(func_obj.func_code)
167 167 return args, varargs, varkw, func_obj.func_defaults
168 168
169 169
170 170 def format_argspec(argspec):
171 171 """Format argspect, convenience wrapper around inspect's.
172 172
173 173 This takes a dict instead of ordered arguments and calls
174 174 inspect.format_argspec with the arguments in the necessary order.
175 175 """
176 176 return inspect.formatargspec(argspec['args'], argspec['varargs'],
177 177 argspec['varkw'], argspec['defaults'])
178 178
179 179
180 180 def call_tip(oinfo, format_call=True):
181 181 """Extract call tip data from an oinfo dict.
182 182
183 183 Parameters
184 184 ----------
185 185 oinfo : dict
186 186
187 187 format_call : bool, optional
188 188 If True, the call line is formatted and returned as a string. If not, a
189 189 tuple of (name, argspec) is returned.
190 190
191 191 Returns
192 192 -------
193 193 call_info : None, str or (str, dict) tuple.
194 194 When format_call is True, the whole call information is formattted as a
195 195 single string. Otherwise, the object's name and its argspec dict are
196 196 returned. If no call information is available, None is returned.
197 197
198 198 docstring : str or None
199 199 The most relevant docstring for calling purposes is returned, if
200 200 available. The priority is: call docstring for callable instances, then
201 201 constructor docstring for classes, then main object's docstring otherwise
202 202 (regular functions).
203 203 """
204 204 # Get call definition
205 205 argspec = oinfo.get('argspec')
206 206 if argspec is None:
207 207 call_line = None
208 208 else:
209 209 # Callable objects will have 'self' as their first argument, prune
210 210 # it out if it's there for clarity (since users do *not* pass an
211 211 # extra first argument explicitly).
212 212 try:
213 213 has_self = argspec['args'][0] == 'self'
214 214 except (KeyError, IndexError):
215 215 pass
216 216 else:
217 217 if has_self:
218 218 argspec['args'] = argspec['args'][1:]
219 219
220 220 call_line = oinfo['name']+format_argspec(argspec)
221 221
222 222 # Now get docstring.
223 223 # The priority is: call docstring, constructor docstring, main one.
224 224 doc = oinfo.get('call_docstring')
225 225 if doc is None:
226 226 doc = oinfo.get('init_docstring')
227 227 if doc is None:
228 228 doc = oinfo.get('docstring','')
229 229
230 230 return call_line, doc
231 231
232 232
233 def find_file(obj):
234 """Find the absolute path to the file where an object was defined.
235
236 This is essentially a robust wrapper around `inspect.getabsfile`.
237
238 Returns None if no file can be found.
239
240 Parameters
241 ----------
242 obj : any Python object
243
244 Returns
245 -------
246 fname : str
247 The absolute path to the file where the object was defined.
248 """
249 # get source if obj was decorated with @decorator
250 if hasattr(obj, '__wrapped__'):
251 obj = obj.__wrapped__
252
253 try:
254 fname = inspect.getabsfile(obj)
255 except TypeError:
256 # For an instance, the file that matters is where its class was
257 # declared.
258 if hasattr(obj, '__class__'):
259 try:
260 fname = inspect.getabsfile(obj.__class__)
261 except TypeError:
262 # Can happen for builtins
263 fname = None
264 except:
265 fname = None
266 return fname
267
268
269 def find_source_lines(obj):
270 """Find the line number in a file where an object was defined.
271
272 This is essentially a robust wrapper around `inspect.getsourcelines`.
273
274 Returns None if no file can be found.
275
276 Parameters
277 ----------
278 obj : any Python object
279
280 Returns
281 -------
282 lineno : int
283 The line number where the object definition starts.
284 """
285 # get source if obj was decorated with @decorator
286 if hasattr(obj, '__wrapped__'):
287 obj = obj.__wrapped__
288
289 try:
290 try:
291 lineno = inspect.getsourcelines(obj)[1]
292 except TypeError:
293 # For instances, try the class object like getsource() does
294 if hasattr(obj, '__class__'):
295 lineno = inspect.getsourcelines(obj.__class__)[1]
296 except:
297 return None
298
299 return lineno
300
301
233 302 class Inspector:
234 303 def __init__(self, color_table=InspectColors,
235 304 code_color_table=PyColorize.ANSICodeColors,
236 305 scheme='NoColor',
237 306 str_detail_level=0):
238 307 self.color_table = color_table
239 308 self.parser = PyColorize.Parser(code_color_table,out='str')
240 309 self.format = self.parser.format
241 310 self.str_detail_level = str_detail_level
242 311 self.set_active_scheme(scheme)
243 312
244 313 def _getdef(self,obj,oname=''):
245 314 """Return the definition header for any callable object.
246 315
247 316 If any exception is generated, None is returned instead and the
248 317 exception is suppressed."""
249 318
250 319 try:
251 320 # We need a plain string here, NOT unicode!
252 321 hdef = oname + inspect.formatargspec(*getargspec(obj))
253 322 return py3compat.unicode_to_str(hdef, 'ascii')
254 323 except:
255 324 return None
256 325
257 326 def __head(self,h):
258 327 """Return a header string with proper colors."""
259 328 return '%s%s%s' % (self.color_table.active_colors.header,h,
260 329 self.color_table.active_colors.normal)
261 330
262 def set_active_scheme(self,scheme):
331 def set_active_scheme(self, scheme):
263 332 self.color_table.set_active_scheme(scheme)
264 333 self.parser.color_table.set_active_scheme(scheme)
265 334
266 def noinfo(self,msg,oname):
335 def noinfo(self, msg, oname):
267 336 """Generic message when no information is found."""
268 337 print 'No %s found' % msg,
269 338 if oname:
270 339 print 'for %s' % oname
271 340 else:
272 341 print
273 342
274 def pdef(self,obj,oname=''):
343 def pdef(self, obj, oname=''):
275 344 """Print the definition header for any callable object.
276 345
277 346 If the object is a class, print the constructor information."""
278 347
279 348 if not callable(obj):
280 349 print 'Object is not callable.'
281 350 return
282 351
283 352 header = ''
284 353
285 354 if inspect.isclass(obj):
286 355 header = self.__head('Class constructor information:\n')
287 356 obj = obj.__init__
288 357 elif type(obj) is types.InstanceType:
289 358 obj = obj.__call__
290 359
291 360 output = self._getdef(obj,oname)
292 361 if output is None:
293 362 self.noinfo('definition header',oname)
294 363 else:
295 364 print >>io.stdout, header,self.format(output),
296 365
297 366 # In Python 3, all classes are new-style, so they all have __init__.
298 367 @skip_doctest_py3
299 368 def pdoc(self,obj,oname='',formatter = None):
300 369 """Print the docstring for any object.
301 370
302 371 Optional:
303 372 -formatter: a function to run the docstring through for specially
304 373 formatted docstrings.
305 374
306 375 Examples
307 376 --------
308 377
309 378 In [1]: class NoInit:
310 379 ...: pass
311 380
312 381 In [2]: class NoDoc:
313 382 ...: def __init__(self):
314 383 ...: pass
315 384
316 385 In [3]: %pdoc NoDoc
317 386 No documentation found for NoDoc
318 387
319 388 In [4]: %pdoc NoInit
320 389 No documentation found for NoInit
321 390
322 391 In [5]: obj = NoInit()
323 392
324 393 In [6]: %pdoc obj
325 394 No documentation found for obj
326 395
327 396 In [5]: obj2 = NoDoc()
328 397
329 398 In [6]: %pdoc obj2
330 399 No documentation found for obj2
331 400 """
332 401
333 402 head = self.__head # For convenience
334 403 lines = []
335 404 ds = getdoc(obj)
336 405 if formatter:
337 406 ds = formatter(ds)
338 407 if ds:
339 408 lines.append(head("Class Docstring:"))
340 409 lines.append(indent(ds))
341 410 if inspect.isclass(obj) and hasattr(obj, '__init__'):
342 411 init_ds = getdoc(obj.__init__)
343 412 if init_ds is not None:
344 413 lines.append(head("Constructor Docstring:"))
345 414 lines.append(indent(init_ds))
346 415 elif hasattr(obj,'__call__'):
347 416 call_ds = getdoc(obj.__call__)
348 417 if call_ds:
349 418 lines.append(head("Calling Docstring:"))
350 419 lines.append(indent(call_ds))
351 420
352 421 if not lines:
353 422 self.noinfo('documentation',oname)
354 423 else:
355 424 page.page('\n'.join(lines))
356 425
357 426 def psource(self,obj,oname=''):
358 427 """Print the source code for an object."""
359 428
360 429 # Flush the source cache because inspect can return out-of-date source
361 430 linecache.checkcache()
362 431 try:
363 432 src = getsource(obj)
364 433 except:
365 434 self.noinfo('source',oname)
366 435 else:
367 436 page.page(self.format(py3compat.unicode_to_str(src)))
368 437
369 def pfile(self,obj,oname=''):
438 def pfile(self, obj, oname=''):
370 439 """Show the whole file where an object was defined."""
371
372 try:
373 try:
374 lineno = inspect.getsourcelines(obj)[1]
375 except TypeError:
376 # For instances, try the class object like getsource() does
377 if hasattr(obj,'__class__'):
378 lineno = inspect.getsourcelines(obj.__class__)[1]
379 # Adjust the inspected object so getabsfile() below works
380 obj = obj.__class__
381 except:
382 self.noinfo('file',oname)
440
441 lineno = find_source_lines(obj)
442 if lineno is None:
443 self.noinfo('file', oname)
383 444 return
384 445
385 # We only reach this point if object was successfully queried
386
387 # run contents of file through pager starting at line
388 # where the object is defined
389 ofile = inspect.getabsfile(obj)
390
446 ofile = find_file(obj)
447 # run contents of file through pager starting at line where the object
448 # is defined, as long as the file isn't binary and is actually on the
449 # filesystem.
391 450 if ofile.endswith(('.so', '.dll', '.pyd')):
392 451 print 'File %r is binary, not printing.' % ofile
393 452 elif not os.path.isfile(ofile):
394 453 print 'File %r does not exist, not printing.' % ofile
395 454 else:
396 455 # Print only text files, not extension binaries. Note that
397 456 # getsourcelines returns lineno with 1-offset and page() uses
398 457 # 0-offset, so we must adjust.
399 page.page(self.format(open(ofile).read()),lineno-1)
458 page.page(self.format(open(ofile).read()), lineno-1)
400 459
401 460 def _format_fields(self, fields, title_width=12):
402 461 """Formats a list of fields for display.
403 462
404 463 Parameters
405 464 ----------
406 465 fields : list
407 466 A list of 2-tuples: (field_title, field_content)
408 467 title_width : int
409 468 How many characters to pad titles to. Default 12.
410 469 """
411 470 out = []
412 471 header = self.__head
413 472 for title, content in fields:
414 473 if len(content.splitlines()) > 1:
415 474 title = header(title + ":") + "\n"
416 475 else:
417 476 title = header((title+":").ljust(title_width))
418 477 out.append(title + content)
419 478 return "\n".join(out)
420 479
421 480 # The fields to be displayed by pinfo: (fancy_name, key_in_info_dict)
422 481 pinfo_fields1 = [("Type", "type_name"),
423 482 ("Base Class", "base_class"),
424 483 ("String Form", "string_form"),
425 484 ("Namespace", "namespace"),
426 485 ("Length", "length"),
427 486 ("File", "file"),
428 487 ("Definition", "definition")]
429 488
430 489 pinfo_fields_obj = [("Class Docstring", "class_docstring"),
431 490 ("Constructor Docstring","init_docstring"),
432 491 ("Call def", "call_def"),
433 492 ("Call docstring", "call_docstring")]
434 493
435 494 def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0):
436 495 """Show detailed information about an object.
437 496
438 497 Optional arguments:
439 498
440 499 - oname: name of the variable pointing to the object.
441 500
442 501 - formatter: special formatter for docstrings (see pdoc)
443 502
444 503 - info: a structure with some information fields which may have been
445 504 precomputed already.
446 505
447 506 - detail_level: if set to 1, more information is given.
448 507 """
449 508 info = self.info(obj, oname=oname, formatter=formatter,
450 509 info=info, detail_level=detail_level)
451 510 displayfields = []
452 511 for title, key in self.pinfo_fields1:
453 512 field = info[key]
454 513 if field is not None:
455 514 displayfields.append((title, field.rstrip()))
456 515
457 516 # Source or docstring, depending on detail level and whether
458 517 # source found.
459 518 if detail_level > 0 and info['source'] is not None:
460 519 displayfields.append(("Source", self.format(py3compat.cast_bytes_py2(info['source']))))
461 520 elif info['docstring'] is not None:
462 521 displayfields.append(("Docstring", info["docstring"]))
463 522
464 523 # Constructor info for classes
465 524 if info['isclass']:
466 525 if info['init_definition'] or info['init_docstring']:
467 526 displayfields.append(("Constructor information", ""))
468 527 if info['init_definition'] is not None:
469 528 displayfields.append((" Definition",
470 529 info['init_definition'].rstrip()))
471 530 if info['init_docstring'] is not None:
472 531 displayfields.append((" Docstring",
473 532 indent(info['init_docstring'])))
474 533
475 534 # Info for objects:
476 535 else:
477 536 for title, key in self.pinfo_fields_obj:
478 537 field = info[key]
479 538 if field is not None:
480 539 displayfields.append((title, field.rstrip()))
481 540
482 541 # Finally send to printer/pager:
483 542 if displayfields:
484 543 page.page(self._format_fields(displayfields))
485 544
486 545 def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
487 546 """Compute a dict with detailed information about an object.
488 547
489 548 Optional arguments:
490 549
491 550 - oname: name of the variable pointing to the object.
492 551
493 552 - formatter: special formatter for docstrings (see pdoc)
494 553
495 554 - info: a structure with some information fields which may have been
496 555 precomputed already.
497 556
498 557 - detail_level: if set to 1, more information is given.
499 558 """
500 559
501 560 obj_type = type(obj)
502 561
503 562 header = self.__head
504 563 if info is None:
505 564 ismagic = 0
506 565 isalias = 0
507 566 ospace = ''
508 567 else:
509 568 ismagic = info.ismagic
510 569 isalias = info.isalias
511 570 ospace = info.namespace
512 571
513 572 # Get docstring, special-casing aliases:
514 573 if isalias:
515 574 if not callable(obj):
516 575 try:
517 576 ds = "Alias to the system command:\n %s" % obj[1]
518 577 except:
519 578 ds = "Alias: " + str(obj)
520 579 else:
521 580 ds = "Alias to " + str(obj)
522 581 if obj.__doc__:
523 582 ds += "\nDocstring:\n" + obj.__doc__
524 583 else:
525 584 ds = getdoc(obj)
526 585 if ds is None:
527 586 ds = '<no docstring>'
528 587 if formatter is not None:
529 588 ds = formatter(ds)
530 589
531 590 # store output in a dict, we initialize it here and fill it as we go
532 591 out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic)
533 592
534 593 string_max = 200 # max size of strings to show (snipped if longer)
535 594 shalf = int((string_max -5)/2)
536 595
537 596 if ismagic:
538 597 obj_type_name = 'Magic function'
539 598 elif isalias:
540 599 obj_type_name = 'System alias'
541 600 else:
542 601 obj_type_name = obj_type.__name__
543 602 out['type_name'] = obj_type_name
544 603
545 604 try:
546 605 bclass = obj.__class__
547 606 out['base_class'] = str(bclass)
548 607 except: pass
549 608
550 609 # String form, but snip if too long in ? form (full in ??)
551 610 if detail_level >= self.str_detail_level:
552 611 try:
553 612 ostr = str(obj)
554 613 str_head = 'string_form'
555 614 if not detail_level and len(ostr)>string_max:
556 615 ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
557 616 ostr = ("\n" + " " * len(str_head.expandtabs())).\
558 617 join(q.strip() for q in ostr.split("\n"))
559 618 out[str_head] = ostr
560 619 except:
561 620 pass
562 621
563 622 if ospace:
564 623 out['namespace'] = ospace
565 624
566 625 # Length (for strings and lists)
567 626 try:
568 627 out['length'] = str(len(obj))
569 628 except: pass
570 629
571 630 # Filename where object was defined
572 631 binary_file = False
573 try:
574 try:
575 fname = inspect.getabsfile(obj)
576 except TypeError:
577 # For an instance, the file that matters is where its class was
578 # declared.
579 if hasattr(obj,'__class__'):
580 fname = inspect.getabsfile(obj.__class__)
581 if fname.endswith('<string>'):
582 fname = 'Dynamically generated function. No source code available.'
583 if fname.endswith(('.so', '.dll', '.pyd')):
584 binary_file = True
585 out['file'] = fname
586 except:
632 fname = find_file(obj)
633 if fname is None:
587 634 # if anything goes wrong, we don't want to show source, so it's as
588 635 # if the file was binary
589 636 binary_file = True
590
637 else:
638 if fname.endswith(('.so', '.dll', '.pyd')):
639 binary_file = True
640 elif fname.endswith('<string>'):
641 fname = 'Dynamically generated function. No source code available.'
642 out['file'] = fname
643
591 644 # reconstruct the function definition and print it:
592 645 defln = self._getdef(obj, oname)
593 646 if defln:
594 647 out['definition'] = self.format(defln)
595 648
596 649 # Docstrings only in detail 0 mode, since source contains them (we
597 650 # avoid repetitions). If source fails, we add them back, see below.
598 651 if ds and detail_level == 0:
599 652 out['docstring'] = ds
600 653
601 654 # Original source code for any callable
602 655 if detail_level:
603 656 # Flush the source cache because inspect can return out-of-date
604 657 # source
605 658 linecache.checkcache()
606 659 source = None
607 660 try:
608 661 try:
609 source = getsource(obj,binary_file)
662 source = getsource(obj, binary_file)
610 663 except TypeError:
611 if hasattr(obj,'__class__'):
612 source = getsource(obj.__class__,binary_file)
664 if hasattr(obj, '__class__'):
665 source = getsource(obj.__class__, binary_file)
613 666 if source is not None:
614 667 out['source'] = source.rstrip()
615 668 except Exception:
616 669 pass
617 670
618 671 if ds and source is None:
619 672 out['docstring'] = ds
620 673
621 674
622 675 # Constructor docstring for classes
623 676 if inspect.isclass(obj):
624 677 out['isclass'] = True
625 678 # reconstruct the function definition and print it:
626 679 try:
627 680 obj_init = obj.__init__
628 681 except AttributeError:
629 682 init_def = init_ds = None
630 683 else:
631 684 init_def = self._getdef(obj_init,oname)
632 685 init_ds = getdoc(obj_init)
633 686 # Skip Python's auto-generated docstrings
634 687 if init_ds and \
635 688 init_ds.startswith('x.__init__(...) initializes'):
636 689 init_ds = None
637 690
638 691 if init_def or init_ds:
639 692 if init_def:
640 693 out['init_definition'] = self.format(init_def)
641 694 if init_ds:
642 695 out['init_docstring'] = init_ds
643 696
644 697 # and class docstring for instances:
645 698 else:
646 699 # First, check whether the instance docstring is identical to the
647 700 # class one, and print it separately if they don't coincide. In
648 701 # most cases they will, but it's nice to print all the info for
649 702 # objects which use instance-customized docstrings.
650 703 if ds:
651 704 try:
652 705 cls = getattr(obj,'__class__')
653 706 except:
654 707 class_ds = None
655 708 else:
656 709 class_ds = getdoc(cls)
657 710 # Skip Python's auto-generated docstrings
658 711 if class_ds and \
659 712 (class_ds.startswith('function(code, globals[,') or \
660 713 class_ds.startswith('instancemethod(function, instance,') or \
661 714 class_ds.startswith('module(name[,') ):
662 715 class_ds = None
663 716 if class_ds and ds != class_ds:
664 717 out['class_docstring'] = class_ds
665 718
666 719 # Next, try to show constructor docstrings
667 720 try:
668 721 init_ds = getdoc(obj.__init__)
669 722 # Skip Python's auto-generated docstrings
670 723 if init_ds and \
671 724 init_ds.startswith('x.__init__(...) initializes'):
672 725 init_ds = None
673 726 except AttributeError:
674 727 init_ds = None
675 728 if init_ds:
676 729 out['init_docstring'] = init_ds
677 730
678 731 # Call form docstring for callable instances
679 732 if hasattr(obj, '__call__'):
680 733 call_def = self._getdef(obj.__call__, oname)
681 734 if call_def is not None:
682 735 out['call_def'] = self.format(call_def)
683 736 call_ds = getdoc(obj.__call__)
684 737 # Skip Python's auto-generated docstrings
685 738 if call_ds and call_ds.startswith('x.__call__(...) <==> x(...)'):
686 739 call_ds = None
687 740 if call_ds:
688 741 out['call_docstring'] = call_ds
689 742
690 743 # Compute the object's argspec as a callable. The key is to decide
691 744 # whether to pull it from the object itself, from its __init__ or
692 745 # from its __call__ method.
693 746
694 747 if inspect.isclass(obj):
695 748 # Old-style classes need not have an __init__
696 749 callable_obj = getattr(obj, "__init__", None)
697 750 elif callable(obj):
698 751 callable_obj = obj
699 752 else:
700 753 callable_obj = None
701 754
702 755 if callable_obj:
703 756 try:
704 757 args, varargs, varkw, defaults = getargspec(callable_obj)
705 758 except (TypeError, AttributeError):
706 759 # For extensions/builtins we can't retrieve the argspec
707 760 pass
708 761 else:
709 762 out['argspec'] = dict(args=args, varargs=varargs,
710 763 varkw=varkw, defaults=defaults)
711 764
712 765 return object_info(**out)
713 766
714 767
715 768 def psearch(self,pattern,ns_table,ns_search=[],
716 769 ignore_case=False,show_all=False):
717 770 """Search namespaces with wildcards for objects.
718 771
719 772 Arguments:
720 773
721 774 - pattern: string containing shell-like wildcards to use in namespace
722 775 searches and optionally a type specification to narrow the search to
723 776 objects of that type.
724 777
725 778 - ns_table: dict of name->namespaces for search.
726 779
727 780 Optional arguments:
728 781
729 782 - ns_search: list of namespace names to include in search.
730 783
731 784 - ignore_case(False): make the search case-insensitive.
732 785
733 786 - show_all(False): show all names, including those starting with
734 787 underscores.
735 788 """
736 789 #print 'ps pattern:<%r>' % pattern # dbg
737 790
738 791 # defaults
739 792 type_pattern = 'all'
740 793 filter = ''
741 794
742 795 cmds = pattern.split()
743 796 len_cmds = len(cmds)
744 797 if len_cmds == 1:
745 798 # Only filter pattern given
746 799 filter = cmds[0]
747 800 elif len_cmds == 2:
748 801 # Both filter and type specified
749 802 filter,type_pattern = cmds
750 803 else:
751 804 raise ValueError('invalid argument string for psearch: <%s>' %
752 805 pattern)
753 806
754 807 # filter search namespaces
755 808 for name in ns_search:
756 809 if name not in ns_table:
757 810 raise ValueError('invalid namespace <%s>. Valid names: %s' %
758 811 (name,ns_table.keys()))
759 812
760 813 #print 'type_pattern:',type_pattern # dbg
761 814 search_result, namespaces_seen = set(), set()
762 815 for ns_name in ns_search:
763 816 ns = ns_table[ns_name]
764 817 # Normally, locals and globals are the same, so we just check one.
765 818 if id(ns) in namespaces_seen:
766 819 continue
767 820 namespaces_seen.add(id(ns))
768 821 tmp_res = list_namespace(ns, type_pattern, filter,
769 822 ignore_case=ignore_case, show_all=show_all)
770 823 search_result.update(tmp_res)
771 824
772 825 page.page('\n'.join(sorted(search_result)))
@@ -1,222 +1,277 b''
1 1 """Tests for the object inspection functionality.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (C) 2010-2011 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the 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 from __future__ import print_function
15 15
16 16 # Stdlib imports
17 import os
17 18
18 19 # Third-party imports
19 20 import nose.tools as nt
20 21
21 22 # Our own imports
22 23 from .. import oinspect
23 24 from IPython.core.magic import (Magics, magics_class, line_magic,
24 25 cell_magic, line_cell_magic,
25 26 register_line_magic, register_cell_magic,
26 27 register_line_cell_magic)
28 from IPython.external.decorator import decorator
27 29 from IPython.utils import py3compat
28 30
31
29 32 #-----------------------------------------------------------------------------
30 33 # Globals and constants
31 34 #-----------------------------------------------------------------------------
32 35
33 36 inspector = oinspect.Inspector()
34 37 ip = get_ipython()
35 38
36 39 #-----------------------------------------------------------------------------
37 40 # Local utilities
38 41 #-----------------------------------------------------------------------------
39 42
43 # WARNING: since this test checks the line number where a function is
44 # defined, if any code is inserted above, the following line will need to be
45 # updated. Do NOT insert any whitespace between the next line and the function
46 # definition below.
47 THIS_LINE_NUMBER = 47 # Put here the actual number of this line
48 def test_find_source_lines():
49 nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
50 THIS_LINE_NUMBER+1)
51
52
53 def test_find_file():
54 nt.assert_equal(oinspect.find_file(test_find_file),
55 os.path.abspath(__file__))
56
57
58 def test_find_file_decorated1():
59
60 @decorator
61 def noop1(f):
62 def wrapper():
63 return f(*a, **kw)
64 return wrapper
65
66 @noop1
67 def f(x):
68 "My docstring"
69
70 nt.assert_equal(oinspect.find_file(f),
71 os.path.abspath(__file__))
72 nt.assert_equal(f.__doc__, "My docstring")
73
74
75 def test_find_file_decorated2():
76
77 @decorator
78 def noop2(f, *a, **kw):
79 return f(*a, **kw)
80
81 @noop2
82 def f(x):
83 "My docstring 2"
84
85 nt.assert_equal(oinspect.find_file(f),
86 os.path.abspath(__file__))
87 nt.assert_equal(f.__doc__, "My docstring 2")
88
89
90 def test_find_file_magic():
91 run = ip.find_line_magic('run')
92 nt.assert_not_equal(oinspect.find_file(run), None)
93
94
40 95 # A few generic objects we can then inspect in the tests below
41 96
42 97 class Call(object):
43 98 """This is the class docstring."""
44 99
45 100 def __init__(self, x, y=1):
46 101 """This is the constructor docstring."""
47 102
48 103 def __call__(self, *a, **kw):
49 104 """This is the call docstring."""
50 105
51 106 def method(self, x, z=2):
52 107 """Some method's docstring"""
53 108
54 109
55 110 class OldStyle:
56 111 """An old-style class for testing."""
57 112 pass
58 113
59 114
60 115 def f(x, y=2, *a, **kw):
61 116 """A simple function."""
62 117
63 118
64 119 def g(y, z=3, *a, **kw):
65 120 pass # no docstring
66 121
67 122
68 123 @register_line_magic
69 124 def lmagic(line):
70 125 "A line magic"
71 126
72 127
73 128 @register_cell_magic
74 129 def cmagic(line, cell):
75 130 "A cell magic"
76 131
77 132
78 133 @register_line_cell_magic
79 134 def lcmagic(line, cell=None):
80 135 "A line/cell magic"
81 136
82 137
83 138 @magics_class
84 139 class SimpleMagics(Magics):
85 140 @line_magic
86 141 def Clmagic(self, cline):
87 142 "A class-based line magic"
88 143
89 144 @cell_magic
90 145 def Ccmagic(self, cline, ccell):
91 146 "A class-based cell magic"
92 147
93 148 @line_cell_magic
94 149 def Clcmagic(self, cline, ccell=None):
95 150 "A class-based line/cell magic"
96 151
97 152
98 153 def check_calltip(obj, name, call, docstring):
99 154 """Generic check pattern all calltip tests will use"""
100 155 info = inspector.info(obj, name)
101 156 call_line, ds = oinspect.call_tip(info)
102 157 nt.assert_equal(call_line, call)
103 158 nt.assert_equal(ds, docstring)
104 159
105 160 #-----------------------------------------------------------------------------
106 161 # Tests
107 162 #-----------------------------------------------------------------------------
108 163
109 164 def test_calltip_class():
110 165 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
111 166
112 167
113 168 def test_calltip_instance():
114 169 c = Call(1)
115 170 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
116 171
117 172
118 173 def test_calltip_method():
119 174 c = Call(1)
120 175 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
121 176
122 177
123 178 def test_calltip_function():
124 179 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
125 180
126 181
127 182 def test_calltip_function2():
128 183 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
129 184
130 185
131 186 def test_calltip_builtin():
132 187 check_calltip(sum, 'sum', None, sum.__doc__)
133 188
134 189
135 190 def test_calltip_line_magic():
136 191 check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")
137 192
138 193
139 194 def test_calltip_cell_magic():
140 195 check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")
141 196
142 197
143 198 def test_calltip_line_magic():
144 199 check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
145 200 "A line/cell magic")
146 201
147 202
148 203 def test_class_magics():
149 204 cm = SimpleMagics(ip)
150 205 ip.register_magics(cm)
151 206 check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
152 207 "A class-based line magic")
153 208 check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
154 209 "A class-based cell magic")
155 210 check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
156 211 "A class-based line/cell magic")
157 212
158 213
159 214 def test_info():
160 215 "Check that Inspector.info fills out various fields as expected."
161 216 i = inspector.info(Call, oname='Call')
162 217 nt.assert_equal(i['type_name'], 'type')
163 218 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
164 219 nt.assert_equal(i['base_class'], expted_class)
165 220 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
166 221 fname = __file__
167 222 if fname.endswith(".pyc"):
168 223 fname = fname[:-1]
169 224 # case-insensitive comparison needed on some filesystems
170 225 # e.g. Windows:
171 226 nt.assert_equal(i['file'].lower(), fname.lower())
172 227 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
173 228 nt.assert_equal(i['docstring'], Call.__doc__)
174 229 nt.assert_equal(i['source'], None)
175 230 nt.assert_true(i['isclass'])
176 231 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
177 232 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
178 233
179 234 i = inspector.info(Call, detail_level=1)
180 235 nt.assert_not_equal(i['source'], None)
181 236 nt.assert_equal(i['docstring'], None)
182 237
183 238 c = Call(1)
184 239 c.__doc__ = "Modified instance docstring"
185 240 i = inspector.info(c)
186 241 nt.assert_equal(i['type_name'], 'Call')
187 242 nt.assert_equal(i['docstring'], "Modified instance docstring")
188 243 nt.assert_equal(i['class_docstring'], Call.__doc__)
189 244 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
190 245 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
191 246
192 247 # Test old-style classes, which for example may not have an __init__ method.
193 248 if not py3compat.PY3:
194 249 i = inspector.info(OldStyle)
195 250 nt.assert_equal(i['type_name'], 'classobj')
196 251
197 252 i = inspector.info(OldStyle())
198 253 nt.assert_equal(i['type_name'], 'instance')
199 254 nt.assert_equal(i['docstring'], OldStyle.__doc__)
200 255
201 256 def test_getdoc():
202 257 class A(object):
203 258 """standard docstring"""
204 259 pass
205 260
206 261 class B(object):
207 262 """standard docstring"""
208 263 def getdoc(self):
209 264 return "custom docstring"
210 265
211 266 class C(object):
212 267 """standard docstring"""
213 268 def getdoc(self):
214 269 return None
215 270
216 271 a = A()
217 272 b = B()
218 273 c = C()
219 274
220 275 nt.assert_equal(oinspect.getdoc(a), "standard docstring")
221 276 nt.assert_equal(oinspect.getdoc(b), "custom docstring")
222 277 nt.assert_equal(oinspect.getdoc(c), "standard docstring")
General Comments 0
You need to be logged in to leave comments. Login now