美文网首页
Python exception

Python exception

作者: 已不再更新_转移到qiita | 来源:发表于2017-10-25 09:55 被阅读17次
    import sys, os
    
    try:
        raise NotImplementedError("No error")
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)
    
        import inspect
    
        OKBLUE = '\033[94m'
        OKGREEN = '\033[92m'
        ENDC = '\033[0m'
        colo = OKGREEN + __file__+"#"+  inspect.stack()[0][3]  + ENDC
    
        print(colo)
    
    class bcolors:
        HEADER = '\033[95m'
        OKBLUE = '\033[94m'
        OKGREEN = '\033[92m'
        WARNING = '\033[93m'
        FAIL = '\033[91m'
        ENDC = '\033[0m'
        BOLD = '\033[1m'
        UNDERLINE = '\033[4m'
    
    import inspect
    
    def PrintFrame():
      callerframerecord = inspect.stack()[1]    # 0 represents this line
                                                # 1 represents line at caller
      frame = callerframerecord[0]
      info = inspect.getframeinfo(frame)
      print info.filename                       # __FILE__     -> Test.py
      print info.function                       # __FUNCTION__ -> Main
      print info.lineno                         # __LINE__     -> 13
    
    def Main():
      PrintFrame()                              # for this line
    
    Main()
    
    

    参考:

    https://stackoverflow.com/a/287944/6132577

    相关文章

      网友评论

          本文标题:Python exception

          本文链接:https://www.haomeiwen.com/subject/jbofpxtx.html