home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / util / primitives / error_handling.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  3.1 KB  |  154 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from contextlib import contextmanager
  5. import sys
  6. import traceback
  7.  
  8. try:
  9.     sentinel
  10. except NameError:
  11.     sentinel = object()
  12.  
  13.  
  14. def try_this(func, default = sentinel, allow = (NameError,)):
  15.     if not callable(func):
  16.         raise TypeError('try_this takes a callable as its first argument')
  17.     
  18.     
  19.     try:
  20.         return func()
  21.     except allow:
  22.         raise 
  23.     except Exception:
  24.         if default is sentinel:
  25.             raise 
  26.         else:
  27.             return default
  28.     except:
  29.         default is sentinel
  30.  
  31.  
  32.  
  33. def syck_error_message(e, fpath):
  34.     (msg, line, column) = e
  35.     msg = '%s in %s:\n\n' % (msg, fpath)
  36.     path = path
  37.     import path
  38.     
  39.     try:
  40.         error_line = path(fpath).lines()[line].strip()
  41.     except:
  42.         error_line = '(could not load line)'
  43.  
  44.     msg += 'line %d, column %d: "%s"' % (line, column, error_line)
  45.     return msg
  46.  
  47.  
  48. def repr_exception(*args, **kwargs):
  49.     
  50.     try:
  51.         yield None
  52.     except:
  53.         for item in args:
  54.             found = False
  55.             for k, v in sys._getframe(2).f_locals.iteritems():
  56.                 if v is item:
  57.                     print >>sys.stderr, k, '-->', repr(item)
  58.                     found = True
  59.                     break
  60.                     continue
  61.             
  62.             if not found:
  63.                 print >>sys.stderr, repr(item)
  64.                 continue
  65.         
  66.         for k, v in kwargs.iteritems():
  67.             print >>sys.stderr, repr(k), repr(v)
  68.         
  69.         raise 
  70.  
  71.  
  72. repr_exception = contextmanager(repr_exception)
  73.  
  74. def with_traceback(func, *args, **kwargs):
  75.     
  76.     try:
  77.         return func(*args, **kwargs)
  78.     except Exception:
  79.         traceback.print_exc()
  80.  
  81.  
  82.  
  83. class traceguard:
  84.     
  85.     def __enter__(cls):
  86.         pass
  87.  
  88.     __enter__ = classmethod(__enter__)
  89.     
  90.     def __exit__(cls, *a):
  91.         
  92.         try:
  93.             if filter(None, a):
  94.                 
  95.                 try:
  96.                     print >>sys.stderr, 'The following exception has been squashed!'
  97.                 except Exception:
  98.                     pass
  99.  
  100.                 
  101.                 try:
  102.                     exc_string = traceback.format_exc(a)
  103.                 except Exception:
  104.                     exc_string = '(Could not format exception.)'
  105.                     if hasattr(traceback, '_old_format_exc'):
  106.                         
  107.                         try:
  108.                             exc_string = traceback._old_format_exc(a)
  109.                         except Exception:
  110.                             pass
  111.                         except:
  112.                             None<EXCEPTION MATCH>Exception
  113.                         
  114.  
  115.                     None<EXCEPTION MATCH>Exception
  116.  
  117.                 
  118.                 try:
  119.                     if isinstance(exc_string, unicode):
  120.                         
  121.                         try:
  122.                             exc_string = exc_string.encode('utf-8')
  123.                         except Exception:
  124.                             exc_string = 'Error encoding this as utf8: %r' % (exc_string,)
  125.                         except:
  126.                             None<EXCEPTION MATCH>Exception
  127.                         
  128.  
  129.                     None<EXCEPTION MATCH>Exception
  130.                     print >>sys.stderr, exc_string
  131.                 except Exception:
  132.                     
  133.                     try:
  134.                         print >>sys.stderr, 'could not print exception'
  135.                     except Exception:
  136.                         pass
  137.                     except:
  138.                         None<EXCEPTION MATCH>Exception
  139.                     
  140.  
  141.                     None<EXCEPTION MATCH>Exception
  142.                 
  143.  
  144.             None<EXCEPTION MATCH>Exception
  145.         except Exception:
  146.             pass
  147.         finally:
  148.             del a
  149.  
  150.         return True
  151.  
  152.     __exit__ = classmethod(__exit__)
  153.  
  154.