Package coro :: Module tb
[hide private]
[frames] | no frames]

Source Code for Module coro.tb

  1  # Copyright (c) 2002-2011 IronPort Systems and Cisco Systems 
  2  # 
  3  # Permission is hereby granted, free of charge, to any person obtaining a copy   
  4  # of this software and associated documentation files (the "Software"), to deal 
  5  # in the Software without restriction, including without limitation the rights   
  6  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  7  # copies of the Software, and to permit persons to whom the Software is  
  8  # furnished to do so, subject to the following conditions: 
  9  # 
 10  # The above copyright notice and this permission notice shall be included in  
 11  # all copies or substantial portions of the Software. 
 12  # 
 13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 16  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 18  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 19  # SOFTWARE. 
 20   
 21  """Compact stack trace functions. 
 22   
 23  This module provides some functions to get a stack trace string. 
 24   
 25  These stack traces are much more compact than the ones generated by the 
 26  traceback module. 
 27  """ 
 28   
 29  import os 
 30  import sys 
 31   
32 -def _get_module_name(n):
33 try: 34 path, filename = os.path.split(n) 35 path, directory = os.path.split(path) 36 #name, ext = os.path.splitext(filename) 37 if directory: 38 return '/'.join((directory, filename)) 39 else: 40 return filename 41 except: 42 return '???'
43
44 -def stack_string(f=None):
45 """Return a compact string representing the current call stack. 46 47 :Parameters: 48 - `f`: Frame object. If not specified, will use the current call 49 position. 50 51 :Return: 52 Returns a string of the current stack trace. 53 """ 54 if f is None: 55 try: 56 raise ZeroDivisionError 57 except ZeroDivisionError: 58 f = sys.exc_info()[2].tb_frame.f_back 59 stack = [] 60 while f is not None: 61 stack.append( 62 _get_module_name(f.f_code.co_filename) + ' ' + 63 f.f_code.co_name + '|' + 64 str(f.f_lineno) 65 ) 66 f = f.f_back 67 return '[' + ('] ['.join(stack))+ ']'
68
69 -def traceback_string(t=None, v=None, tb=None):
70 """Returns a compact string representing the current exception. 71 72 If an exception is not provided as an argument, then it will get the 73 current exception from `sys.exc_info`. 74 75 :Parameters: 76 - `t`: Exception type. 77 - `v`: Exception value. 78 - `tb`: Traceback object. 79 80 :Return: 81 Returns a string of the current exception and stack trace. 82 """ 83 if t is None: 84 t,v,tb = sys.exc_info() 85 tbinfo = [] 86 if tb is None: 87 # this should never happen, but then again, lots of things 88 # should never happen but do 89 return 'traceback is None!!!' 90 while tb is not None: 91 tbinfo.append ( 92 _get_module_name (tb.tb_frame.f_code.co_filename) + ' ' + 93 tb.tb_frame.f_code.co_name + '|' + 94 str(tb.tb_lineno) 95 ) 96 tb = tb.tb_next 97 98 # just to be safe 99 del tb 100 first = tbinfo[-1] 101 info = '[' + ('] ['.join (tbinfo)) + ']' 102 return repr(((first), str(t), str(v), info))
103