PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pydbgr/processor/command/info_subcmd/line.py

http://pydbgr.googlecode.com/
Python | 113 lines | 90 code | 3 blank | 20 comment | 4 complexity | 2c5384fc9b0655672f36c62c546a50b1 MD5 | raw file
Possible License(s): GPL-3.0
  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2008, 2009 Rocky Bernstein <rocky@gnu.org>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import inspect, os
  17. from import_relative import *
  18. # Our local modules
  19. base_subcmd = import_relative('base_subcmd', '..', 'pydbgr')
  20. Mclifns = import_relative('clifns', '....', 'pydbgr')
  21. Mmisc = import_relative('misc', '....', 'pydbgr')
  22. class InfoLine(base_subcmd.DebuggerSubcommand):
  23. '''Show information about the current line'''
  24. min_abbrev = 2
  25. need_stack = True
  26. short_help = 'Show current-line information'
  27. def lineinfo(self, identifier):
  28. failed = (None, None, None)
  29. # Input is identifier, may be in single quotes
  30. idstring = identifier.split("'")
  31. if len(idstring) == 1:
  32. # not in single quotes
  33. ident = idstring[0].strip()
  34. elif len(idstring) == 3:
  35. # quoted
  36. ident = idstring[1].strip()
  37. else:
  38. return failed
  39. if ident == '': return failed
  40. parts = ident.split('.')
  41. # Protection for derived debuggers
  42. if parts[0] == 'self':
  43. del parts[0]
  44. if len(parts) == 0:
  45. return failed
  46. # Best first guess at file to look at
  47. fname = self.proc.defaultFile()
  48. if len(parts) == 1:
  49. item = parts[0]
  50. else:
  51. # More than one part.
  52. # First is module, second is method/class
  53. m, f = lookupmodule('.'.join(parts[1:]), self.debugger.mainpyfile,
  54. self.core.canonic)
  55. if f:
  56. fname = f
  57. item = parts[-1]
  58. answer = find_function(item, fname)
  59. return answer or failed
  60. def run(self, args):
  61. """Current line number in source file"""
  62. #info line identifier
  63. if not self.proc.curframe:
  64. self.errmsg("No line number information available.")
  65. return
  66. if len(args) == 3:
  67. # lineinfo returns (item, file, lineno) or (None,)
  68. answer = self.lineinfo(args[2])
  69. if answer[0]:
  70. item, filename, lineno = answer
  71. if not os.path.isfile(filename):
  72. filename = Mclifns.search_file(filename,
  73. self.core.search_path,
  74. self.main_dirname)
  75. self.msg('Line %s of "%s" <%s>' %
  76. (lineno, filename, item))
  77. return
  78. filename=self.core.canonic_filename(self.proc.curframe)
  79. if not os.path.isfile(filename):
  80. filename = Mclifns.search_file(filename, self.core.search_path,
  81. self.main_dirname)
  82. pass
  83. filename = self.core.canonic_filename(self.proc.curframe)
  84. msg1 = 'Line %d of \"%s\"' % (inspect.getlineno(self.proc.curframe),
  85. self.core.filename(filename))
  86. msg2 = ('at instruction %d' % self.proc.curframe.f_lasti)
  87. if self.proc.event:
  88. msg2 += ', %s event' % self.proc.event
  89. pass
  90. self.msg(Mmisc.wrapped_lines(msg1, msg2, self.settings['width']))
  91. return False
  92. pass
  93. if __name__ == '__main__':
  94. mock = import_relative('mock', '..')
  95. Minfo = import_relative('info', '..')
  96. Mdebugger = import_relative('debugger', '....')
  97. d = Mdebugger.Debugger()
  98. d, cp = mock.dbg_setup(d)
  99. i = Minfo.InfoCommand(cp)
  100. sub = InfoLine(i)
  101. sub.run([])
  102. cp.curframe = inspect.currentframe()
  103. for width in (80, 200):
  104. sub.settings['width'] = width
  105. sub.run(['file.py', 'lines'])
  106. pass
  107. pass