PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/2.7/pstats.py

https://bitbucket.org/varialus/jyjy
Python | 713 lines | 676 code | 6 blank | 31 comment | 2 complexity | 2066a735db5ff24eb8c0635b7919750f MD5 | raw file
  1. """Class for printing reports on profiled python code."""
  2. # Class for printing reports on profiled python code. rev 1.0 4/1/94
  3. #
  4. # Based on prior profile module by Sjoerd Mullender...
  5. # which was hacked somewhat by: Guido van Rossum
  6. #
  7. # see profile.py for more info.
  8. # Copyright 1994, by InfoSeek Corporation, all rights reserved.
  9. # Written by James Roskind
  10. #
  11. # Permission to use, copy, modify, and distribute this Python software
  12. # and its associated documentation for any purpose (subject to the
  13. # restriction in the following sentence) without fee is hereby granted,
  14. # provided that the above copyright notice appears in all copies, and
  15. # that both that copyright notice and this permission notice appear in
  16. # supporting documentation, and that the name of InfoSeek not be used in
  17. # advertising or publicity pertaining to distribution of the software
  18. # without specific, written prior permission. This permission is
  19. # explicitly restricted to the copying and modification of the software
  20. # to remain in Python, compiled Python, or other languages (such as C)
  21. # wherein the modified or derived code is exclusively imported into a
  22. # Python module.
  23. #
  24. # INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  25. # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26. # FITNESS. IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY
  27. # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  28. # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  29. # CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  30. # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31. import sys
  32. import os
  33. import time
  34. import marshal
  35. import re
  36. from functools import cmp_to_key
  37. __all__ = ["Stats"]
  38. class Stats:
  39. """This class is used for creating reports from data generated by the
  40. Profile class. It is a "friend" of that class, and imports data either
  41. by direct access to members of Profile class, or by reading in a dictionary
  42. that was emitted (via marshal) from the Profile class.
  43. The big change from the previous Profiler (in terms of raw functionality)
  44. is that an "add()" method has been provided to combine Stats from
  45. several distinct profile runs. Both the constructor and the add()
  46. method now take arbitrarily many file names as arguments.
  47. All the print methods now take an argument that indicates how many lines
  48. to print. If the arg is a floating point number between 0 and 1.0, then
  49. it is taken as a decimal percentage of the available lines to be printed
  50. (e.g., .1 means print 10% of all available lines). If it is an integer,
  51. it is taken to mean the number of lines of data that you wish to have
  52. printed.
  53. The sort_stats() method now processes some additional options (i.e., in
  54. addition to the old -1, 0, 1, or 2). It takes an arbitrary number of
  55. quoted strings to select the sort order. For example sort_stats('time',
  56. 'name') sorts on the major key of 'internal function time', and on the
  57. minor key of 'the name of the function'. Look at the two tables in
  58. sort_stats() and get_sort_arg_defs(self) for more examples.
  59. All methods return self, so you can string together commands like:
  60. Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
  61. print_stats(5).print_callers(5)
  62. """
  63. def __init__(self, *args, **kwds):
  64. # I can't figure out how to explictly specify a stream keyword arg
  65. # with *args:
  66. # def __init__(self, *args, stream=sys.stdout): ...
  67. # so I use **kwds and sqauwk if something unexpected is passed in.
  68. self.stream = sys.stdout
  69. if "stream" in kwds:
  70. self.stream = kwds["stream"]
  71. del kwds["stream"]
  72. if kwds:
  73. keys = kwds.keys()
  74. keys.sort()
  75. extras = ", ".join(["%s=%s" % (k, kwds[k]) for k in keys])
  76. raise ValueError, "unrecognized keyword args: %s" % extras
  77. if not len(args):
  78. arg = None
  79. else:
  80. arg = args[0]
  81. args = args[1:]
  82. self.init(arg)
  83. self.add(*args)
  84. def init(self, arg):
  85. self.all_callees = None # calc only if needed
  86. self.files = []
  87. self.fcn_list = None
  88. self.total_tt = 0
  89. self.total_calls = 0
  90. self.prim_calls = 0
  91. self.max_name_len = 0
  92. self.top_level = {}
  93. self.stats = {}
  94. self.sort_arg_dict = {}
  95. self.load_stats(arg)
  96. trouble = 1
  97. try:
  98. self.get_top_level_stats()
  99. trouble = 0
  100. finally:
  101. if trouble:
  102. print >> self.stream, "Invalid timing data",
  103. if self.files: print >> self.stream, self.files[-1],
  104. print >> self.stream
  105. def load_stats(self, arg):
  106. if not arg: self.stats = {}
  107. elif isinstance(arg, basestring):
  108. f = open(arg, 'rb')
  109. self.stats = marshal.load(f)
  110. f.close()
  111. try:
  112. file_stats = os.stat(arg)
  113. arg = time.ctime(file_stats.st_mtime) + " " + arg
  114. except: # in case this is not unix
  115. pass
  116. self.files = [ arg ]
  117. elif hasattr(arg, 'create_stats'):
  118. arg.create_stats()
  119. self.stats = arg.stats
  120. arg.stats = {}
  121. if not self.stats:
  122. raise TypeError, "Cannot create or construct a %r object from '%r''" % (
  123. self.__class__, arg)
  124. return
  125. def get_top_level_stats(self):
  126. for func, (cc, nc, tt, ct, callers) in self.stats.items():
  127. self.total_calls += nc
  128. self.prim_calls += cc
  129. self.total_tt += tt
  130. if ("jprofile", 0, "profiler") in callers:
  131. self.top_level[func] = None
  132. if len(func_std_string(func)) > self.max_name_len:
  133. self.max_name_len = len(func_std_string(func))
  134. def add(self, *arg_list):
  135. if not arg_list: return self
  136. if len(arg_list) > 1: self.add(*arg_list[1:])
  137. other = arg_list[0]
  138. if type(self) != type(other) or self.__class__ != other.__class__:
  139. other = Stats(other)
  140. self.files += other.files
  141. self.total_calls += other.total_calls
  142. self.prim_calls += other.prim_calls
  143. self.total_tt += other.total_tt
  144. for func in other.top_level:
  145. self.top_level[func] = None
  146. if self.max_name_len < other.max_name_len:
  147. self.max_name_len = other.max_name_len
  148. self.fcn_list = None
  149. for func, stat in other.stats.iteritems():
  150. if func in self.stats:
  151. old_func_stat = self.stats[func]
  152. else:
  153. old_func_stat = (0, 0, 0, 0, {},)
  154. self.stats[func] = add_func_stats(old_func_stat, stat)
  155. return self
  156. def dump_stats(self, filename):
  157. """Write the profile data to a file we know how to load back."""
  158. f = file(filename, 'wb')
  159. try:
  160. marshal.dump(self.stats, f)
  161. finally:
  162. f.close()
  163. # list the tuple indices and directions for sorting,
  164. # along with some printable description
  165. sort_arg_dict_default = {
  166. "calls" : (((1,-1), ), "call count"),
  167. "cumulative": (((3,-1), ), "cumulative time"),
  168. "file" : (((4, 1), ), "file name"),
  169. "line" : (((5, 1), ), "line number"),
  170. "module" : (((4, 1), ), "file name"),
  171. "name" : (((6, 1), ), "function name"),
  172. "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
  173. "pcalls" : (((0,-1), ), "call count"),
  174. "stdname" : (((7, 1), ), "standard name"),
  175. "time" : (((2,-1), ), "internal time"),
  176. }
  177. def get_sort_arg_defs(self):
  178. """Expand all abbreviations that are unique."""
  179. if not self.sort_arg_dict:
  180. self.sort_arg_dict = dict = {}
  181. bad_list = {}
  182. for word, tup in self.sort_arg_dict_default.iteritems():
  183. fragment = word
  184. while fragment:
  185. if not fragment:
  186. break
  187. if fragment in dict:
  188. bad_list[fragment] = 0
  189. break
  190. dict[fragment] = tup
  191. fragment = fragment[:-1]
  192. for word in bad_list:
  193. del dict[word]
  194. return self.sort_arg_dict
  195. def sort_stats(self, *field):
  196. if not field:
  197. self.fcn_list = 0
  198. return self
  199. if len(field) == 1 and isinstance(field[0], (int, long)):
  200. # Be compatible with old profiler
  201. field = [ {-1: "stdname",
  202. 0: "calls",
  203. 1: "time",
  204. 2: "cumulative"}[field[0]] ]
  205. sort_arg_defs = self.get_sort_arg_defs()
  206. sort_tuple = ()
  207. self.sort_type = ""
  208. connector = ""
  209. for word in field:
  210. sort_tuple = sort_tuple + sort_arg_defs[word][0]
  211. self.sort_type += connector + sort_arg_defs[word][1]
  212. connector = ", "
  213. stats_list = []
  214. for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
  215. stats_list.append((cc, nc, tt, ct) + func +
  216. (func_std_string(func), func))
  217. stats_list.sort(key=cmp_to_key(TupleComp(sort_tuple).compare))
  218. self.fcn_list = fcn_list = []
  219. for tuple in stats_list:
  220. fcn_list.append(tuple[-1])
  221. return self
  222. def reverse_order(self):
  223. if self.fcn_list:
  224. self.fcn_list.reverse()
  225. return self
  226. def strip_dirs(self):
  227. oldstats = self.stats
  228. self.stats = newstats = {}
  229. max_name_len = 0
  230. for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
  231. newfunc = func_strip_path(func)
  232. if len(func_std_string(newfunc)) > max_name_len:
  233. max_name_len = len(func_std_string(newfunc))
  234. newcallers = {}
  235. for func2, caller in callers.iteritems():
  236. newcallers[func_strip_path(func2)] = caller
  237. if newfunc in newstats:
  238. newstats[newfunc] = add_func_stats(
  239. newstats[newfunc],
  240. (cc, nc, tt, ct, newcallers))
  241. else:
  242. newstats[newfunc] = (cc, nc, tt, ct, newcallers)
  243. old_top = self.top_level
  244. self.top_level = new_top = {}
  245. for func in old_top:
  246. new_top[func_strip_path(func)] = None
  247. self.max_name_len = max_name_len
  248. self.fcn_list = None
  249. self.all_callees = None
  250. return self
  251. def calc_callees(self):
  252. if self.all_callees: return
  253. self.all_callees = all_callees = {}
  254. for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
  255. if not func in all_callees:
  256. all_callees[func] = {}
  257. for func2, caller in callers.iteritems():
  258. if not func2 in all_callees:
  259. all_callees[func2] = {}
  260. all_callees[func2][func] = caller
  261. return
  262. #******************************************************************
  263. # The following functions support actual printing of reports
  264. #******************************************************************
  265. # Optional "amount" is either a line count, or a percentage of lines.
  266. def eval_print_amount(self, sel, list, msg):
  267. new_list = list
  268. if isinstance(sel, basestring):
  269. try:
  270. rex = re.compile(sel)
  271. except re.error:
  272. msg += " <Invalid regular expression %r>\n" % sel
  273. return new_list, msg
  274. new_list = []
  275. for func in list:
  276. if rex.search(func_std_string(func)):
  277. new_list.append(func)
  278. else:
  279. count = len(list)
  280. if isinstance(sel, float) and 0.0 <= sel < 1.0:
  281. count = int(count * sel + .5)
  282. new_list = list[:count]
  283. elif isinstance(sel, (int, long)) and 0 <= sel < count:
  284. count = sel
  285. new_list = list[:count]
  286. if len(list) != len(new_list):
  287. msg += " List reduced from %r to %r due to restriction <%r>\n" % (
  288. len(list), len(new_list), sel)
  289. return new_list, msg
  290. def get_print_list(self, sel_list):
  291. width = self.max_name_len
  292. if self.fcn_list:
  293. stat_list = self.fcn_list[:]
  294. msg = " Ordered by: " + self.sort_type + '\n'
  295. else:
  296. stat_list = self.stats.keys()
  297. msg = " Random listing order was used\n"
  298. for selection in sel_list:
  299. stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
  300. count = len(stat_list)
  301. if not stat_list:
  302. return 0, stat_list
  303. print >> self.stream, msg
  304. if count < len(self.stats):
  305. width = 0
  306. for func in stat_list:
  307. if len(func_std_string(func)) > width:
  308. width = len(func_std_string(func))
  309. return width+2, stat_list
  310. def print_stats(self, *amount):
  311. for filename in self.files:
  312. print >> self.stream, filename
  313. if self.files: print >> self.stream
  314. indent = ' ' * 8
  315. for func in self.top_level:
  316. print >> self.stream, indent, func_get_function_name(func)
  317. print >> self.stream, indent, self.total_calls, "function calls",
  318. if self.total_calls != self.prim_calls:
  319. print >> self.stream, "(%d primitive calls)" % self.prim_calls,
  320. print >> self.stream, "in %.3f seconds" % self.total_tt
  321. print >> self.stream
  322. width, list = self.get_print_list(amount)
  323. if list:
  324. self.print_title()
  325. for func in list:
  326. self.print_line(func)
  327. print >> self.stream
  328. print >> self.stream
  329. return self
  330. def print_callees(self, *amount):
  331. width, list = self.get_print_list(amount)
  332. if list:
  333. self.calc_callees()
  334. self.print_call_heading(width, "called...")
  335. for func in list:
  336. if func in self.all_callees:
  337. self.print_call_line(width, func, self.all_callees[func])
  338. else:
  339. self.print_call_line(width, func, {})
  340. print >> self.stream
  341. print >> self.stream
  342. return self
  343. def print_callers(self, *amount):
  344. width, list = self.get_print_list(amount)
  345. if list:
  346. self.print_call_heading(width, "was called by...")
  347. for func in list:
  348. cc, nc, tt, ct, callers = self.stats[func]
  349. self.print_call_line(width, func, callers, "<-")
  350. print >> self.stream
  351. print >> self.stream
  352. return self
  353. def print_call_heading(self, name_size, column_title):
  354. print >> self.stream, "Function ".ljust(name_size) + column_title
  355. # print sub-header only if we have new-style callers
  356. subheader = False
  357. for cc, nc, tt, ct, callers in self.stats.itervalues():
  358. if callers:
  359. value = callers.itervalues().next()
  360. subheader = isinstance(value, tuple)
  361. break
  362. if subheader:
  363. print >> self.stream, " "*name_size + " ncalls tottime cumtime"
  364. def print_call_line(self, name_size, source, call_dict, arrow="->"):
  365. print >> self.stream, func_std_string(source).ljust(name_size) + arrow,
  366. if not call_dict:
  367. print >> self.stream
  368. return
  369. clist = call_dict.keys()
  370. clist.sort()
  371. indent = ""
  372. for func in clist:
  373. name = func_std_string(func)
  374. value = call_dict[func]
  375. if isinstance(value, tuple):
  376. nc, cc, tt, ct = value
  377. if nc != cc:
  378. substats = '%d/%d' % (nc, cc)
  379. else:
  380. substats = '%d' % (nc,)
  381. substats = '%s %s %s %s' % (substats.rjust(7+2*len(indent)),
  382. f8(tt), f8(ct), name)
  383. left_width = name_size + 1
  384. else:
  385. substats = '%s(%r) %s' % (name, value, f8(self.stats[func][3]))
  386. left_width = name_size + 3
  387. print >> self.stream, indent*left_width + substats
  388. indent = " "
  389. def print_title(self):
  390. print >> self.stream, ' ncalls tottime percall cumtime percall',
  391. print >> self.stream, 'filename:lineno(function)'
  392. def print_line(self, func): # hack : should print percentages
  393. cc, nc, tt, ct, callers = self.stats[func]
  394. c = str(nc)
  395. if nc != cc:
  396. c = c + '/' + str(cc)
  397. print >> self.stream, c.rjust(9),
  398. print >> self.stream, f8(tt),
  399. if nc == 0:
  400. print >> self.stream, ' '*8,
  401. else:
  402. print >> self.stream, f8(float(tt)/nc),
  403. print >> self.stream, f8(ct),
  404. if cc == 0:
  405. print >> self.stream, ' '*8,
  406. else:
  407. print >> self.stream, f8(float(ct)/cc),
  408. print >> self.stream, func_std_string(func)
  409. class TupleComp:
  410. """This class provides a generic function for comparing any two tuples.
  411. Each instance records a list of tuple-indices (from most significant
  412. to least significant), and sort direction (ascending or decending) for
  413. each tuple-index. The compare functions can then be used as the function
  414. argument to the system sort() function when a list of tuples need to be
  415. sorted in the instances order."""
  416. def __init__(self, comp_select_list):
  417. self.comp_select_list = comp_select_list
  418. def compare (self, left, right):
  419. for index, direction in self.comp_select_list:
  420. l = left[index]
  421. r = right[index]
  422. if l < r:
  423. return -direction
  424. if l > r:
  425. return direction
  426. return 0
  427. #**************************************************************************
  428. # func_name is a triple (file:string, line:int, name:string)
  429. def func_strip_path(func_name):
  430. filename, line, name = func_name
  431. return os.path.basename(filename), line, name
  432. def func_get_function_name(func):
  433. return func[2]
  434. def func_std_string(func_name): # match what old profile produced
  435. if func_name[:2] == ('~', 0):
  436. # special case for built-in functions
  437. name = func_name[2]
  438. if name.startswith('<') and name.endswith('>'):
  439. return '{%s}' % name[1:-1]
  440. else:
  441. return name
  442. else:
  443. return "%s:%d(%s)" % func_name
  444. #**************************************************************************
  445. # The following functions combine statists for pairs functions.
  446. # The bulk of the processing involves correctly handling "call" lists,
  447. # such as callers and callees.
  448. #**************************************************************************
  449. def add_func_stats(target, source):
  450. """Add together all the stats for two profile entries."""
  451. cc, nc, tt, ct, callers = source
  452. t_cc, t_nc, t_tt, t_ct, t_callers = target
  453. return (cc+t_cc, nc+t_nc, tt+t_tt, ct+t_ct,
  454. add_callers(t_callers, callers))
  455. def add_callers(target, source):
  456. """Combine two caller lists in a single list."""
  457. new_callers = {}
  458. for func, caller in target.iteritems():
  459. new_callers[func] = caller
  460. for func, caller in source.iteritems():
  461. if func in new_callers:
  462. if isinstance(caller, tuple):
  463. # format used by cProfile
  464. new_callers[func] = tuple([i[0] + i[1] for i in
  465. zip(caller, new_callers[func])])
  466. else:
  467. # format used by profile
  468. new_callers[func] += caller
  469. else:
  470. new_callers[func] = caller
  471. return new_callers
  472. def count_calls(callers):
  473. """Sum the caller statistics to get total number of calls received."""
  474. nc = 0
  475. for calls in callers.itervalues():
  476. nc += calls
  477. return nc
  478. #**************************************************************************
  479. # The following functions support printing of reports
  480. #**************************************************************************
  481. def f8(x):
  482. return "%8.3f" % x
  483. #**************************************************************************
  484. # Statistics browser added by ESR, April 2001
  485. #**************************************************************************
  486. if __name__ == '__main__':
  487. import cmd
  488. try:
  489. import readline
  490. except ImportError:
  491. pass
  492. class ProfileBrowser(cmd.Cmd):
  493. def __init__(self, profile=None):
  494. cmd.Cmd.__init__(self)
  495. self.prompt = "% "
  496. self.stats = None
  497. self.stream = sys.stdout
  498. if profile is not None:
  499. self.do_read(profile)
  500. def generic(self, fn, line):
  501. args = line.split()
  502. processed = []
  503. for term in args:
  504. try:
  505. processed.append(int(term))
  506. continue
  507. except ValueError:
  508. pass
  509. try:
  510. frac = float(term)
  511. if frac > 1 or frac < 0:
  512. print >> self.stream, "Fraction argument must be in [0, 1]"
  513. continue
  514. processed.append(frac)
  515. continue
  516. except ValueError:
  517. pass
  518. processed.append(term)
  519. if self.stats:
  520. getattr(self.stats, fn)(*processed)
  521. else:
  522. print >> self.stream, "No statistics object is loaded."
  523. return 0
  524. def generic_help(self):
  525. print >> self.stream, "Arguments may be:"
  526. print >> self.stream, "* An integer maximum number of entries to print."
  527. print >> self.stream, "* A decimal fractional number between 0 and 1, controlling"
  528. print >> self.stream, " what fraction of selected entries to print."
  529. print >> self.stream, "* A regular expression; only entries with function names"
  530. print >> self.stream, " that match it are printed."
  531. def do_add(self, line):
  532. if self.stats:
  533. self.stats.add(line)
  534. else:
  535. print >> self.stream, "No statistics object is loaded."
  536. return 0
  537. def help_add(self):
  538. print >> self.stream, "Add profile info from given file to current statistics object."
  539. def do_callees(self, line):
  540. return self.generic('print_callees', line)
  541. def help_callees(self):
  542. print >> self.stream, "Print callees statistics from the current stat object."
  543. self.generic_help()
  544. def do_callers(self, line):
  545. return self.generic('print_callers', line)
  546. def help_callers(self):
  547. print >> self.stream, "Print callers statistics from the current stat object."
  548. self.generic_help()
  549. def do_EOF(self, line):
  550. print >> self.stream, ""
  551. return 1
  552. def help_EOF(self):
  553. print >> self.stream, "Leave the profile brower."
  554. def do_quit(self, line):
  555. return 1
  556. def help_quit(self):
  557. print >> self.stream, "Leave the profile brower."
  558. def do_read(self, line):
  559. if line:
  560. try:
  561. self.stats = Stats(line)
  562. except IOError, args:
  563. print >> self.stream, args[1]
  564. return
  565. except Exception as err:
  566. print >> self.stream, err.__class__.__name__ + ':', err
  567. return
  568. self.prompt = line + "% "
  569. elif len(self.prompt) > 2:
  570. line = self.prompt[:-2]
  571. self.do_read(line)
  572. else:
  573. print >> self.stream, "No statistics object is current -- cannot reload."
  574. return 0
  575. def help_read(self):
  576. print >> self.stream, "Read in profile data from a specified file."
  577. print >> self.stream, "Without argument, reload the current file."
  578. def do_reverse(self, line):
  579. if self.stats:
  580. self.stats.reverse_order()
  581. else:
  582. print >> self.stream, "No statistics object is loaded."
  583. return 0
  584. def help_reverse(self):
  585. print >> self.stream, "Reverse the sort order of the profiling report."
  586. def do_sort(self, line):
  587. if not self.stats:
  588. print >> self.stream, "No statistics object is loaded."
  589. return
  590. abbrevs = self.stats.get_sort_arg_defs()
  591. if line and all((x in abbrevs) for x in line.split()):
  592. self.stats.sort_stats(*line.split())
  593. else:
  594. print >> self.stream, "Valid sort keys (unique prefixes are accepted):"
  595. for (key, value) in Stats.sort_arg_dict_default.iteritems():
  596. print >> self.stream, "%s -- %s" % (key, value[1])
  597. return 0
  598. def help_sort(self):
  599. print >> self.stream, "Sort profile data according to specified keys."
  600. print >> self.stream, "(Typing `sort' without arguments lists valid keys.)"
  601. def complete_sort(self, text, *args):
  602. return [a for a in Stats.sort_arg_dict_default if a.startswith(text)]
  603. def do_stats(self, line):
  604. return self.generic('print_stats', line)
  605. def help_stats(self):
  606. print >> self.stream, "Print statistics from the current stat object."
  607. self.generic_help()
  608. def do_strip(self, line):
  609. if self.stats:
  610. self.stats.strip_dirs()
  611. else:
  612. print >> self.stream, "No statistics object is loaded."
  613. def help_strip(self):
  614. print >> self.stream, "Strip leading path information from filenames in the report."
  615. def help_help(self):
  616. print >> self.stream, "Show help for a given command."
  617. def postcmd(self, stop, line):
  618. if stop:
  619. return stop
  620. return None
  621. import sys
  622. if len(sys.argv) > 1:
  623. initprofile = sys.argv[1]
  624. else:
  625. initprofile = None
  626. try:
  627. browser = ProfileBrowser(initprofile)
  628. print >> browser.stream, "Welcome to the profile statistics browser."
  629. browser.cmdloop()
  630. print >> browser.stream, "Goodbye."
  631. except KeyboardInterrupt:
  632. pass
  633. # That's all, folks.